Subversion Repositories wimsdev

Rev

Rev 10 | Rev 8084 | 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) 2002-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
 */
17
 
18
        /* This program makes comparison between two text strings,
19
         * according to the symtext syntax. */
20
 
21
/* Input data: via environment variables.
22
 * wims_exec_parm: line 1 = command (comp,expand,wordlist,random,1,2,3,...)
23
 * line 2 = text to examine (for comp).
24
 * line 3 and up = symtext syntax.
25
 * w_symtext: dictionary style.
26
 * w_symtext_option: option words.
27
 *
28
 * Output: two lines.
29
 * Line 1: ERROR or OK
30
 * Line 2: result depending on command.
31
 */
32
 
33
 
34
const char *codechar="_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
35
 
36
#include "symtext.h"
37
 
38
struct block blockbuf[MAX_BLOCKS];
39
int nextblock;
40
listtype listbuf[MAX_LISTS];
41
int nextlist;
42
listtype tagbuf[MAX_BLOCKS];
43
int nexttag;
44
 
45
struct poolstruct poolbuf[MAX_POOLS];
46
int nextpool;
47
 
48
int options;
49
#define op_nocase       (1<<0)
50
#define op_deaccent     (1<<1)
51
#define op_reaccent     (1<<2)
52
#define op_nopunct      (1<<3)
53
#define op_nomath       (1<<4)
54
#define op_noparenth    (1<<5)
55
#define op_nocs         (1<<6)
56
#define op_noquote      (1<<7)
57
#define op_matchall     (1<<8)
58
#define op_alphaonly    (1<<9)
59
#define op_alnumonly    (1<<10)
60
 
61
char cmdbuf[256], stbuf[MAX_LINELEN+1], textbuf[MAX_LINELEN+1];
62
char wbuf[MAX_LINELEN+1];
63
char cmdparm[1024];
64
char defbuf[MAX_LINELEN+1];
65
char style[MAX_NAMELEN+1];
66
char styledir[MAX_FNAME+1];
67
char optionbuf[1024];
68
char outbuf[4096];
69
char *outptr, *wptr;
70
int debug;
71
 
72
enum {
73
    cmd_none, cmd_comp, cmd_debug, cmd_random, cmd_1, cmd_wordlist
74
};
75
struct {
76
    char *name; int value;
77
} cmdlist[]={
78
    {"1",       cmd_1},
79
    {"comp",    cmd_comp},
80
    {"compare", cmd_comp},
81
    {"debug",   cmd_debug},
82
    {"match",   cmd_comp},
83
    {"rand",    cmd_random},
84
    {"random",  cmd_random},
85
    {"wordlist",cmd_wordlist},
86
    {"words",   cmd_wordlist}
87
};
88
#define cmdcnt (sizeof(cmdlist)/sizeof(cmdlist[0]))
89
int cmd;
90
 
91
void error(char *msg,...)
92
{
93
    va_list vp;
94
    char buf[1024];
95
 
96
    va_start(vp,msg);
97
    vsnprintf(buf,sizeof(buf),msg,vp);
98
    va_end(vp);
99
    printf("ERROR\n%s\n",buf);
100
    exit(1);
101
}
102
 
103
void _error(char *msg)
104
{
105
    error(msg);
106
}
107
 
108
        /* read-in a file into buffer. Use open() and read().
109
         * Return buffer address which will be malloc'ed if buf=NULL. */
110
char *readfile(char *fname, char buf[], long int buflen)
111
{
112
    int fd, t;
113
    struct stat st;
114
    long int l, lc;
115
    char *bf;
116
    t=0; if(buf) buf[0]=0;
117
    if(stat(fname,&st)) return NULL;
118
    l=st.st_size; if(l<0) return NULL;
119
    if(l>=buflen) {
120
        if(buflen<MAX_LINELEN) l=buflen-1;
121
        else error("file_too_long %s",fname);
122
    }
123
    fd=open(fname,O_RDONLY); if(fd==-1) return NULL;
124
    if(buf==NULL) bf=xmalloc(l+8); else {bf=buf;if(l==0) {t=1; l=buflen-1;}}
125
    lc=read(fd,bf,l); close(fd);
126
    if(lc<0 || lc>l || (lc!=l && t==0))
127
        {if(buf==NULL) free(bf); else buf[0]=0; return NULL;}
128
    bf[lc]=0; _tolinux(bf); return bf;
129
}
130
 
131
        /* get option word in a string */
132
void _getopt(char *name, char *p)
133
{
134
    char *p1, *p2, *p3, *p4;
135
    char buf[MAX_LINELEN+1];
136
 
137
    snprintf(buf,sizeof(buf),"%s",p);
138
    p1=find_word_start(name);
139
    for(p2=buf;*p2;p2++) {
140
        if(myisspace(*p2)) *p2=' ';
141
        if(*p2=='=') *p2='      ';
142
    }
143
    *p=0;
144
    p2=wordchr(buf,p1); if(p2==NULL) return;
145
    for(p3=find_word_end(p2);myisspace(*p3);p3++) {
146
        if(*p3=='       ') {
147
            p3=find_word_start(p3);
148
            switch(*p3) {
149
                case '"': {
150
                    p4=strchr(p3+1,'"');
151
                    goto tested;
152
                }
153
                case '(': {
154
                    p4=find_matching(p3+1,')');
155
                    goto tested;
156
                }
157
                case '[': {
158
                    p4=find_matching(p3+1,']');
159
                    goto tested;
160
                }
161
                case '{': {
162
                    p4=find_matching(p3+1,'}');
163
                    tested:
164
                    if(p4) {
165
                        p3++; *p4=0; break;
166
                    }
167
                    else goto nomatch;
168
                }
169
                default: {
170
                    nomatch:
171
                    *find_word_end(p3)=0;
172
                }
173
            }
174
            mystrncpy(p,p3,MAX_LINELEN);
175
            return;
176
        }
177
    }
178
    *find_word_end(p2)=0;
179
    memmove(p,p2,strlen(p2)+1);
180
}
181
 
182
void _getdef(char buf[], char *name, char value[])
183
{
184
    char *p1, *p2, *p3, *p4;
185
 
186
    if(*name==0) goto nothing;  /* this would create segfault. */
187
    for(p1=strstr(buf,name); p1!=NULL; p1=strstr(p1+1,name)) {
188
        p2=find_word_start(p1+strlen(name));
189
        if((p1>buf && !isspace(*(p1-1))) || *p2!='=') continue;
190
        p3=p1; while(p3>buf && *(p3-1)!='\n') p3--;
191
        p3=find_word_start(p3);
192
        if(p3<p1 && *p3!='!') continue;
193
        if(p3<p1) {
194
            p3++; p4=find_word_end(p3);
195
            if(find_word_start(p4)!=p1) continue;
196
            if(p4-p3!=3 || (strncmp(p3,"set",3)!=0 &&
197
               strncmp(p3,"let",3)!=0 &&
198
               strncmp(p3,"def",3)!=0)) {
199
                if(p4-p3!=6 || strncmp(p3,"define",6)!=0) continue;
200
            }
201
        }
202
        p2++;p3=strchr(p2,'\n'); if(p3==NULL) p3=p2+strlen(p2);
203
        p2=find_word_start(p2);
204
        if(p2>p3) goto nothing;
205
        if(p3-p2>=MAX_LINELEN) error("string_too_long def %s",name);
206
        memmove(value,p2,p3-p2); value[p3-p2]=0;
207
        strip_trailing_spaces(value); return;
208
    }
209
nothing:
210
    value[0]=0; return;
211
}
212
 
213
char fnbuf[MAX_FNAME+1];
214
 
215
        /* make a filename and check length */
216
char *mkfname(char buf[], char *s,...)
217
{
218
    va_list vp;
219
    char *p;
220
 
221
    if(buf==NULL) p=fnbuf; else p=buf;
222
    va_start(vp,s);
223
    vsnprintf(p,MAX_FNAME,s,vp);
224
    va_end(vp);
225
    if(strlen(p)>=MAX_FNAME-1) error("name_too_long %.20s",p);
226
    return p;
227
}
228
 
229
#include "lines.c"
230
#include "translate.c"
231
#include "match.c"
232
#include "compile.c"
233
 
234
void getparms(void)
235
{
236
    char *p, *p2, *p3, lbuf[8];
237
    char buf[MAX_LINELEN+1], pbuf[MAX_LINELEN+1];
238
    struct stat st;
239
    int i;
240
 
241
    cmd=0;
242
    p=getenv("wims_exec_parm");
243
    if(p==NULL) return;
244
    snprintf(pbuf,sizeof(pbuf),"%s",p);
245
    rows2lines(pbuf);
246
    p2=strchr(pbuf,'\n'); if(p2==NULL) return; else *p2++=0;
247
    p=find_word_start(pbuf);
248
    p3=find_word_end(p); if(p3-p>=sizeof(cmdbuf)) return;
249
    if(*p==0) return; else *p3++=0;
250
    memmove(cmdbuf,p,p3-p); cmdbuf[p3-p]=0;
251
    p=p2; p2=strchr(p,'\n'); if(p2==NULL) p2=p+strlen(p); else *p2++=0;
252
    if(p2<=find_word_start(p)) return;
253
    if(p2-p<sizeof(textbuf)) {
254
        memmove(textbuf,p,p2-p); textbuf[p2-p]=0;
255
    }
256
    p=p2; p2=p+strlen(p);
257
    if(p2>p && p2-p<sizeof(stbuf)) {
258
        memmove(stbuf,p,p2-p); stbuf[p2-p]=0;
259
    }
260
    i=search_list(cmdlist,cmdcnt,sizeof(cmdlist[0]),cmdbuf);
261
    if(i>=0) cmd=cmdlist[i].value;
262
    else error("bad_command %.20s",cmdbuf);
263
    snprintf(cmdparm,sizeof(cmdparm),"%s",p2);
264
 
265
    options=0;
266
    p=getenv("w_module_language"); if(p==NULL) p="";
267
    snprintf(lbuf,sizeof(lbuf),"%2s",p);
268
    if(*p3) {
269
        snprintf(buf,sizeof(buf),"%s",p3);
270
        _getopt("style",buf);
271
        snprintf(style,sizeof(style),"%s",find_word_start(buf));
272
        *find_word_end(style)=0;
273
        snprintf(buf,sizeof(buf),"%s",p3);
274
        _getopt("language",buf);
275
        if(buf[0]) snprintf(lbuf,sizeof(lbuf),"%2s",buf);
276
    }
277
    lbuf[2]=0;
3718 reyssat 278
    if(!myisalpha(lbuf[0]) || !myisalpha(lbuf[1])) ovlstrcpy(lbuf,"en");
10 reyssat 279
    styledir[0]=defbuf[0]=optionbuf[0]=buf[0]=0;
280
    if(*style) {
281
        p=getenv("module_dir");
282
        if(p==NULL) {                   /* non-wims operation */
283
            snprintf(styledir,sizeof(styledir),"%s",style);
284
        }
285
        else {
286
            for(i=0;i<MAX_NAMELEN && myisalnum(style[i]);i++);
287
            style[i]=0;
288
            if(style[0]) {              /* style defined */
289
                if(*p!='/' && strstr(p,"..")==NULL) {   /* check module dir */
290
                    snprintf(styledir,sizeof(styledir),"%s/symtext/%s/%s/def",p,lbuf,style);
291
                    if(stat(styledir,&st)) styledir[0]=0;
292
                }
293
                if(styledir[0]==0) {    /* check default */
294
                    snprintf(styledir,sizeof(styledir),"%s/symtext/%s/%s/def",defaultdir,lbuf,style);
295
                    if(stat(styledir,&st)) error("style_not_found %s",style);
296
                }
297
            }
298
        }
299
        if(styledir[0]) {               /* get def */
300
            readfile(styledir,defbuf,sizeof(defbuf));
301
            styledir[strlen(styledir)-4]=0;
302
            suffix_dic(mkfname(NULL,"%s/suffix",styledir));
303
            transdic=diccnt;
304
            if(prepare_dic("trans")==NULL) transdic=-1;
305
            dic[transdic].unknown_type=unk_leave;
306
            macrodic=diccnt;
307
            if(prepare_dic("macros")==NULL) macrodic=-1;
308
            dic[macrodic].unknown_type=unk_delete;
309
        }
310
    }
311
    _getdef(defbuf,"option",buf);
312
    snprintf(optionbuf,sizeof(optionbuf),"%s %s",p3,buf);
313
    if(wordchr(optionbuf,"nocase")!=NULL) options|=op_nocase;
314
    if(wordchr(optionbuf,"deaccent")!=NULL) options|=op_deaccent;
315
    if(wordchr(optionbuf,"reaccent")!=NULL) options|=op_reaccent;
316
    if(wordchr(optionbuf,"nopunct")!=NULL) options|=op_nopunct;
317
    if(wordchr(optionbuf,"nomath")!=NULL) options|=op_nomath;
318
    if(wordchr(optionbuf,"noparenthesis")!=NULL) options|=op_noparenth;
319
    if(wordchr(optionbuf,"noparentheses")!=NULL) options|=op_noparenth;
320
    if(wordchr(optionbuf,"nocs")!=NULL) options|=op_nocs;
321
    if(wordchr(optionbuf,"noquote")!=NULL) options|=op_noquote;
322
    if(wordchr(optionbuf,"matchall")!=NULL) options|=op_matchall;
323
    if(wordchr(optionbuf,"abconly")!=NULL) options|=op_alphaonly;
324
    if(wordchr(optionbuf,"onlyabc")!=NULL) options|=op_alphaonly;
325
    if(wordchr(optionbuf,"alnumonly")!=NULL) options|=op_alnumonly;
326
    if(wordchr(optionbuf,"onlyalnum")!=NULL) options|=op_alnumonly;
327
 
328
    if(cmd==cmd_comp || cmd==cmd_debug) {
329
        _getopt("debug",optionbuf);
330
        if(optionbuf[0]) {
331
            i=atoi(optionbuf);
332
            if(i>0 || strcmp(optionbuf,"0")==0) debug=i; else debug=1;
333
            if(debug>0) cmd=cmd_debug;
334
        }
335
    }
336
    strip_enclosing_par(textbuf);
337
    strfold(textbuf);
338
}
339
 
340
int verify_tables(void)
341
{
342
    if(verify_order(builtin,builtincnt,sizeof(builtin[0]))) return -1;
343
    if(verify_order(cmdlist,cmdcnt,sizeof(cmdlist[0]))) return -1;
344
 
345
    return 0;
346
}
347
 
348
int main(int argc, char *argv[])
349
{
350
    int i, n, mat;
351
    char *p1, *p2;
352
    char lbuf[MAX_LINELEN+1];
353
 
354
    if(argc>1 && strcmp(argv[1],"-t")==0) {
355
        if(verify_tables()==0) {
356
            printf("Table orders OK.\n");
357
            return 0;
358
        }
359
        else return 1;
360
    }
361
    error1=error2=_error; debug=0;
362
    wptr=wbuf; wbuf[0]=0;
363
    getparms();
364
    Mnext=Mbuf; Mcnt=0;
365
    switch(cmd) {
366
        case cmd_comp: {
367
            comp:
368
            n=linenum(stbuf);
369
            for(mat=0,i=1,p1=stbuf;i<=n;i++,p1=p2) {
370
                p2=find_line_end(p1); if(*p2) *p2++=0;
371
                p1=find_word_start(p1);
372
                if(*p1==0) continue;
373
                snprintf(lbuf,sizeof(lbuf),"%s",p1);
374
                compile(lbuf);
375
                mat=match(textbuf);
376
                if(mat) {
377
                    printf("MATCH %d %s\n",i,outbuf);
378
                    if((options&op_matchall)==0) break;
379
                }
380
            }
381
            if(debug) fprintf(stderr,"word list: %s\n",wbuf);
382
            break;
383
        }
384
        case cmd_debug: {
385
            if(debug==0) debug=1;
386
            fprintf(stderr,"debug=%d.\n",debug);
387
            for(i=0;i<diccnt;i++)
388
              fprintf(stderr,"Dictionary %d: %s, %d entries.\n",
389
                      i+1,dic[i].name,dic[i].len);
390
            goto comp;
391
        }
392
        case cmd_random: {
393
 
394
            break;
395
        }
396
        case cmd_wordlist: {
397
 
398
            break;
399
        }
400
        case cmd_1: {
401
 
402
            break;
403
        }
404
 
405
        case cmd_none:
406
        default: return 1;
407
    }
408
    return 0;
409
}
410