Subversion Repositories wimsdev

Rev

Rev 725 | Rev 8067 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
7359 bpr 17
 /* line input / output / translation routines
18
 * and error routines */
10 reyssat 19
#include <stdarg.h>
20
 
7359 bpr 21
/* critical error situation */
10 reyssat 22
void error(char *s)
23
{
24
    /* fputs(s,stderr); */
25
    if(badpar!=NULL) {
7359 bpr 26
     char *p; int i,t,v;
27
     char buf[128];
28
     for(p=inpbuf,t=1;p<badpar;p++) if(*p=='\n') t++;
29
     for(p=badpar-1;p>=inpbuf && *p!='\n'; p--);
30
     p++; v=badpar-p+1;
31
     snprintf(buf,sizeof(buf),"%s",p);
32
     for(i=0,p=buf;i<3 && *p;p++) if(*p=='\n') i++;
33
     *p=0;
34
     printf("\nERROR %s '%c' at line %d col %d.\n",
35
           s,*badpar,t,v);
36
     for(i=1;i<v;i++) putchar(' ');
37
     printf("!\n%s\n",buf);
10 reyssat 38
    }
39
    else printf("\nERROR %s\n",s);
40
    exit(1);
41
}
42
 
43
char *substit(char *p)
44
{
45
    char *pp;
46
    while((pp=strchr(p,'$'))!=NULL) *pp=' ';
47
    return p;
48
}
49
 
7359 bpr 50
/* replace newline by tab */
10 reyssat 51
void replace_newline(char *p)
52
{
53
    for(;*p;p++) {
7359 bpr 54
     if(*p=='   ') *p=' ';
55
     if(*p=='\n') *p='  ';
10 reyssat 56
    }
57
}
58
 
7359 bpr 59
/* variable substitution. buffer p must have MAX_LINELEN */
10 reyssat 60
void subst(char *p)
61
{
62
    char *pp, *pe, nbuf[MAX_NAMELEN];
63
    int i;
64
 
65
    for(pp=p;pp-p<MAX_LINELEN && *pp; pp++) {
7359 bpr 66
     if(*pp=='$') {
67
         string_modify(p,pp,pp+1,"&#36;");
68
         pp+=3;
69
         continue;
70
     }
71
     if(*pp=='!' && isalnum(*(pp+1))) {
72
         string_modify(p,pp,pp+1,"&#33;");
73
         pp+=3; continue;
74
     }
75
     if(*pp!='\\') continue;
76
     if(*(pp+1)=='\\') {
77
         pp++; continue;
78
     }
79
     if(!isalpha(*(pp+1))) continue;
80
     for(pe=pp+1;isalnum(*pe) || *pe=='_'; pe++);
81
     if(pe-pp>=MAX_NAMELEN) continue;
82
     memmove(nbuf,pp+1,pe-pp-1);nbuf[pe-pp-1]=0;
83
     for(i=varcnt-1;i>=1 && strcmp(nbuf,param[i].name)!=0;i--);
84
     if(i>=1) {
85
         if(deftag) param[i].save=1;
86
         if(*pe=='[') {
87
          char *pt, tbuf[MAX_LINELEN+1], vbuf[MAX_LINELEN+1];
88
          int l;
89
          pt=find_matching(pe+1,']'); if(pt!=NULL && pt-pe<MAX_LINELEN) {
90
              pe++; memmove(tbuf,pe,pt-pe); tbuf[pt-pe]=0; pe=pt+1;
91
              subst(tbuf);
92
              for(pt=strchr(tbuf,'\\');pt;pt=strchr(pt,'\\'))
93
                string_modify(tbuf,pt,pt+1,"$m_");
94
              snprintf(vbuf,sizeof(vbuf),"$(val%d[%s])",i,tbuf);
95
              l=strlen(vbuf);
96
              string_modify(p,pp,pe,"%s",vbuf);
97
              pp+=l-1;continue;
98
          }
99
         }
100
         string_modify(p,pp,pe,"$val%d",i);
101
     }
102
     else if(wordchr(mdef,nbuf)!=NULL) string_modify(p,pp,pp+1,"$m_");
10 reyssat 103
    }
104
}
105
 
106
void repsubst(char *p)
107
{
108
    char *p1;
109
    for(p1=strstr(p,"\\reply"); p1!=NULL; p1=strstr(p1+1,"\\reply")) {
7359 bpr 110
     if(p1>p && *(p1-1)=='\\') continue;
111
     string_modify(p,p1,p1+strlen("\\reply"),"$m_reply");
10 reyssat 112
    }
113
    for(p1=strstr(p,"\\result"); p1!=NULL; p1=strstr(p1+1,"\\result")) {
7359 bpr 114
     if(p1>p && *(p1-1)=='\\') continue;
115
     string_modify(p,p1,p1+strlen("\\result"),"$m_result");
10 reyssat 116
    }
117
    for(p1=strstr(p,"\\choice"); p1!=NULL; p1=strstr(p1+1,"\\choice")) {
7359 bpr 118
     if(p1>p && *(p1-1)=='\\') continue;
119
     string_modify(p,p1,p1+strlen("\\choice"),"$m_choice");
10 reyssat 120
    }
121
    for(p1=strstr(p,"\\step"); p1!=NULL; p1=strstr(p1+1,"\\step")) {
7359 bpr 122
     if(p1>p && *(p1-1)=='\\') continue;
123
     string_modify(p,p1,p1+strlen("\\step"),"$m_step");
10 reyssat 124
    }
213 bpr 125
    for(p1=strstr(p,"\\sc_reply"); p1!=NULL; p1=strstr(p1+1,"\\sc_reply")) {
7359 bpr 126
     if(p1>p && *(p1-1)=='\\') continue;
127
     string_modify(p,p1,p1+strlen("\\sc_reply"),"$m_sc_reply");
213 bpr 128
    }
725 guerimand 129
    for(p1=strstr(p,"\\sc_choice"); p1!=NULL; p1=strstr(p1+1,"\\sc_choice")) {
7359 bpr 130
     if(p1>p && *(p1-1)=='\\') continue;
131
     string_modify(p,p1,p1+strlen("\\sc_choice"),"$m_sc_choice");
725 guerimand 132
    }
7359 bpr 133
    for(p1=strstr(p,"\\reply_"); p1!=NULL; p1=strstr(p1+1,"\\reply_")) {
134
     if(p1>p && *(p1-1)=='\\') continue;
135
     string_modify(p,p1,p1+strlen("\\reply_"),"$m_reply_");
136
    }
10 reyssat 137
}
138
 
7359 bpr 139
 
140
 /* find matching parenthesis.
141
     * The entrance point should be after the opening
142
     * parenthesis.
143
     * Returns NULL if unmatched.
144
*/
10 reyssat 145
char *find_matching2(char *p, char c)
146
{
147
    char *pp, *pp2, *ppar,*pbrk, *pbrc;
148
    int parenth, brak, brace;
149
    if(c=='|') {
7359 bpr 150
     for(pp=p;*pp!=0;pp++) {
151
         pp2=pp;
152
         switch(*pp) {
153
          case '|': return pp;
154
          case '(': {
155
              pp=find_matching(pp+1,')');
156
              if(pp==NULL) {if(badpar==NULL) badpar=pp2; return NULL;}
157
              break;
158
          }
159
          case '[': {
160
              pp=find_matching(pp+1,']');
161
              if(pp==NULL) {if(badpar==NULL) badpar=pp2; return NULL;}
162
              break;
163
          }
164
          case '{': {
165
              pp=find_matching(pp+1,'}');
166
              if(pp==NULL) {if(badpar==NULL) badpar=pp2; return NULL;}
167
              break;
168
          }
169
          case ')':
170
          case ']':
171
          case '}': badpar=pp; return NULL;
172
 
173
          default: break;
174
         }
175
     }
176
     return NULL;
10 reyssat 177
    }
178
    parenth=brak=brace=0; ppar=pbrc=pbrk=NULL;
179
    for(pp=p; *pp!=0; pp++) {
7359 bpr 180
     switch(*pp) {
181
         case '[': if(brak==0) pbrk=pp; brak++; break;
182
         case ']': brak--; break;
183
         case '(': if(parenth==0) ppar=pp; parenth++; break;
184
         case ')': parenth--; break;
185
         case '{': if(brace==0) pbrc=pp; brace++; break;
186
         case '}': brace--; break;
187
         default: continue;
188
     }
189
     if(parenth<0 || brak<0 || brace<0) {
190
         if(badpar==NULL) {
191
          if(parenth>0) badpar=ppar;
192
          if(brace>0) badpar=pbrc;
193
          if(brak>0) badpar=pbrk;
194
         }
195
         if(parenth>0 || brak>0 || brace>0) return NULL;
196
         if(*pp!=c) {
197
          if(badpar==NULL) badpar=pp; return NULL;
198
         }
199
         else break;
200
     }
10 reyssat 201
    }
202
    if(*pp!=c) return NULL;
203
    return pp;
204
}
205
 
7359 bpr 206
/* Check whether parentheses are balanced in a given string.
207
  * Returns 0 if OK.
208
  * style=0: simple check. style<>0: strong check.
209
*/
10 reyssat 210
int checkparentheses(char *p, int style)
211
{
212
    int i,j,k;
213
    j=strlen(p);
214
    if(j>=MAX_FILELEN) return 65535;
215
    if(style!=0) {
7359 bpr 216
     char *pp, *pe, c;
217
     for(pp=p;pp<p+j;pp++) {
218
         switch (*pp) {
219
          case ')':
220
          case ']':
221
          case '}': {badpar=pp; return -1;}
222
          case '(': c=')'; goto find;
223
          case '[': c=']'; goto find;
224
          case '{': c='}';
225
          find: {
226
              pe=find_matching2(pp+1,c);
227
              if(pe==NULL) {
228
               if(badpar==NULL) badpar=pp;
229
               return 1;
230
              }
231
              else pp=pe;
232
          }
233
          default: break;
234
         }
235
     }
236
     return 0;
10 reyssat 237
    }
238
    for(i=k=0;i<j && k>=0;i++) {
7359 bpr 239
     if(*(p+i)=='(') k++;
240
    if(*(p+i)==')') k--;
10 reyssat 241
    }
242
    return k;
243
}
244