Subversion Repositories wimsdev

Rev

Rev 6876 | Rev 8100 | 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
 */
17
 
7676 bpr 18
/* Versatile translation according to a dictionary */
10 reyssat 19
 
20
/*************** Customization: change values hereafter ****************/
21
 
7676 bpr 22
/* limit of dictionary entries */
10 reyssat 23
#define entrylim 32768
7676 bpr 24
/* limit of dictionary length */
25
#define diclim 2*1024*1024
26
/* limit of source length */
10 reyssat 27
#define sourcelim 16*1024*1024
28
 
29
/***************** Nothing should need change hereafter *****************/
30
 
31
#include "../wims.h"
3718 reyssat 32
#include "../Lib/basicstr.c"
10 reyssat 33
 
34
char *inpbuf, outbuf[2*(MAX_LINELEN+1)];
35
char *dicbuf;
36
struct entry {
37
    unsigned char *original, *replace;
38
    int olen,earlier;
39
} entry[entrylim];
40
int entrycount;
41
 
42
enum {
43
    unk_delete, unk_leave, unk_replace
44
};
45
 
46
int has_digits=0;
47
int unknown_type=unk_delete;
48
int nocase=0,leaveline=0,fromfile=0;
49
char *unknown, unkbuf[1024];
50
 
51
void *xmalloc(size_t n)
52
{
53
    void *p;
54
    p=malloc(n);
55
    if(p==NULL) exit(1);
56
    return p;
57
}
58
 
7676 bpr 59
/* Exit without translating anything */
10 reyssat 60
void escape(void)
61
{
62
    printf("%s",inpbuf); exit(0);
63
}
64
 
7676 bpr 65
/* Points to the end of the word */
10 reyssat 66
char *find_word_end(char *p)
67
{
68
    int i;
69
    for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
70
    return p;
71
}
72
 
7676 bpr 73
/* Strips leading spaces */
10 reyssat 74
char *find_word_start(char *p)
75
{
76
    int i;
77
    for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
78
    return p;
79
}
80
 
7676 bpr 81
/* strip trailing spaces; return string end. */
10 reyssat 82
char *strip_trailing_spaces(char *p)
83
{
84
    char *pp;
85
    if(*p==0) return p;
86
    for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
87
    return pp;
88
}
89
 
90
int compare(int i1, const char *s2)
91
{
92
    int k;
3247 bpr 93
    if(nocase) k=strncasecmp((char*)entry[i1].original,s2,entry[i1].olen);
94
    else k=strncmp((char*)entry[i1].original,s2,entry[i1].olen);
10 reyssat 95
    if(k==0 && (isalnum(*(s2+entry[i1].olen)) || (*(s2+entry[i1].olen)&128)!=0)) return -1;
96
    else return k;
97
}
98
 
7676 bpr 99
/* searches a list. Returns index if found, -1 if nomatch.
100
 * Uses binary search, list must be sorted. */
10 reyssat 101
int search_list(struct entry *list, int items, size_t item_size, const char *str)
102
{
103
    int i1,i2,j,k,t,t1;
104
    unsigned char c;
105
 
106
    if(items<=0) return -1;
107
    j=0; c=str[0];
108
    k=list[0].original[0]-c; if(k==0) k=compare(0,str);
109
    if(k==0) goto more; if(k>0) return -1;
110
    j=items-1; k=list[j].original[0]-c; if(k==0) k=compare(j,str);
111
    if(k==0) return j;
112
    if(k>0) for(i1=0,i2=j;i2>i1+1;) {
7676 bpr 113
      j=i1+(i2-i1)/2;
114
      k=list[j].original[0]-c; if(k==0) k=compare(j,str);
115
      if(k==0) goto more;
116
      if(k>0) {i2=j; continue;}
117
      if(k<0) {i1=j; continue;}
10 reyssat 118
    }
119
    if(k>0) {j--;k=compare(j,str);}
120
    more:
121
    if((t=list[j].earlier)<0) {
7676 bpr 122
      if(k==0) return j; else return -1;
10 reyssat 123
    }
124
    if(compare(t,str)!=0) return -1;
125
    for(j=t1=t,k=0;j<items && list[j].earlier==t1 && (k=compare(j,str))<=0; j++) {
7676 bpr 126
      if(k==0) t=j;
10 reyssat 127
    }
128
    return t;
129
}
130
 
7676 bpr 131
/* modify a string. Bufferlen must be ast least 2*MAX_LINELEN */
10 reyssat 132
void string_modify(char *start, char *bad_beg, char *bad_end, char *good,...)
133
{
134
    char buf[MAX_LINELEN+1];
135
    va_list vp;
7676 bpr 136
 
10 reyssat 137
    va_start(vp,good);
138
    vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
139
    if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=2*MAX_LINELEN)
7676 bpr 140
      return; /* this is an error situation. */
10 reyssat 141
    strcat(buf,bad_end);
3718 reyssat 142
    ovlstrcpy(bad_beg,buf);
10 reyssat 143
}
144
 
7676 bpr 145
/* change all spaces into ' ', and collapse multiple occurences */
10 reyssat 146
void singlespace(char *p)
147
{
148
    char *pp, *p2;
149
    for(pp=p;*pp;pp++) {
7676 bpr 150
      if(!isspace(*pp)) continue;
151
      if(leaveline) {
152
          if(*pp==13) ovlstrcpy(pp,pp+1);
153
          if(*pp=='\n') {
154
            pp++;
155
            gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++);
156
            if(p2>pp) ovlstrcpy(pp,p2); pp--;
157
          }
158
          else {
159
            pp++; if(!isspace(*pp) || *pp=='\n') continue;
160
            goto gopt;
161
          }
162
      }
163
      else {
164
          if(*pp!=' ') *pp=' ';
165
          pp++; if(!isspace(*pp)) continue;
166
          for(p2=pp;isspace(*p2);p2++);
167
          ovlstrcpy(pp,p2); pp--;
168
      }
10 reyssat 169
    }
170
}
171
 
172
#include "suffix.c"
173
 
7676 bpr 174
/* Prepare dictionary */
10 reyssat 175
void prepare_dic(void)
176
{
177
    int i,l;
178
    FILE *dicf;
179
    char *fname, *p1, *p2, *pp, buf[1024];
180
    long int flen;
181
    fname=getenv("w_dictionary");
182
    if(fname==NULL || *fname==0 || *fname=='/' || strstr(fname,"..")) {
7676 bpr 183
      p1=getenv("w_module"); if(p1 && strncmp(p1,"classes/",strlen("classes/"))==0) {
184
          p1=getenv("w_wims_class"); p2=getenv("w_wims_home");
185
          if(p1 && p2) {
186
            snprintf(buf,sizeof(buf),"%s/log/classes/%s/",p2,p1);
187
            if(strncmp(fname,buf,strlen(buf))!=0) escape();
188
          }
189
          else escape();
190
      }
191
      else {
192
          p1=getenv("untrust"); if(p1 && strstr(p1,"yes")) escape();
193
      }
10 reyssat 194
    }
6876 bpr 195
/* replace escape() by return if there is some suffix dictionary, */
196
 
197
    dicf=fopen(fname,"r"); if(dicf==NULL) return;
10 reyssat 198
    fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET);
199
    if(flen>diclim) escape();
200
    dicbuf=xmalloc(flen+16);flen=fread(dicbuf,1,flen,dicf);
201
    fclose(dicf);
202
    if(flen>0 && flen<diclim) dicbuf[flen]=0;
203
    else escape();
204
    for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) {
7676 bpr 205
      p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
206
      pp=strchr(p1,':'); if(pp==NULL) continue;
207
      *pp++=0;
208
      strip_trailing_spaces(p1); strip_trailing_spaces(pp);
209
      singlespace(p1);
210
      p1=find_word_start(p1); pp=find_word_start(pp);
211
      if(*p1==0) continue;
212
      if(has_digits==0) {
213
          char *p;
214
          for(p=p1;*p!=0 && p<pp && !isdigit(*p);p++);
215
          if(isdigit(*p)) has_digits=1;
216
      }
217
      entry[i].original=(unsigned char*)p1;
218
        entry[i].replace=(unsigned char*)pp;
219
      entry[i].olen=l=strlen(p1); entry[i].earlier=-1;
220
      if(i>0) {
221
          int l1,l2;
222
          l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen;
223
          else {l2=entry[i-1].olen;l1=i-1;}
224
          if(l>l2 && isspace(p1[l2])
225
             && strncmp((char*)entry[l1].original,p1,l2)==0)
226
            entry[i].earlier=entry[i-1].earlier=l1;
227
      }
228
      i++;
10 reyssat 229
    }
230
    entrycount=i; if(entrycount<=0) escape();
231
}
232
 
7676 bpr 233
/* now make the translation. */
10 reyssat 234
void translate(void)
235
{
236
    char *p1, *p2, *pp;
237
    int t;
238
 
239
    for(p1=find_word_start(outbuf);
7676 bpr 240
      p1!=NULL && p1-outbuf<MAX_LINELEN && *p1!=0;
241
      p1=p2) {
242
      p2=find_word_end(p1);
243
      for(pp=p1;pp<p2 &&
244
          ((!has_digits && isalpha(*pp)) ||
245
           (has_digits && isalnum(*pp)) || (*pp&128)!=0 ||
246
           strchr("_",*pp)!=NULL);pp++);
247
      p2=find_word_start(p2);
248
      if(pp==p1 ||
249
         (has_digits==0 && isdigit(*pp)) ||
250
         (*pp!=0 && !isspace(*pp) && strchr(",.?!/;",*pp)==NULL)) continue;
251
      t=search_list(entry,entrycount,sizeof(entry[0]),p1);
252
      if(t<0) {
253
          switch(unknown_type) {
254
            case unk_leave: break;
255
            case unk_delete: {
256
                ovlstrcpy(p1,find_word_start(pp)); p2=p1;
257
                break;
258
            }
259
            case unk_replace: {
260
                string_modify(outbuf,p1,pp,unkbuf);
261
                p2=find_word_start(p1+strlen(unkbuf));
262
            }
263
          }
264
          continue;
265
      }
266
      string_modify(outbuf,p1,p1+strlen((char*)entry[t].original),
267
                  (char*)entry[t].replace);
268
      p2=find_word_start(p1+strlen((char*)entry[t].replace));
10 reyssat 269
    }
270
    outbuf[MAX_LINELEN]=0; printf("%s",outbuf);
271
}
272
 
273
void switches(void)
274
{
275
    char *sw;
276
    unknown=getenv("w_translator_unknown");
277
    if(unknown==NULL) unknown="";
278
    snprintf(unkbuf,sizeof(unkbuf),"%s",find_word_start(unknown));
279
    *find_word_end(unkbuf)=0;
280
    if(strcasecmp(unkbuf,"leave")==0) unknown_type=unk_leave;
281
    else if(unkbuf[0]!=0) unknown_type=unk_replace;
282
    sw=getenv("w_translator_switch");
283
    if(sw==NULL) return;
284
    if(strstr(sw,"leaveline")) leaveline=1;
285
    if(strstr(sw,"nocase")) nocase=1;
286
    if(strstr(sw,"file")) fromfile=1;
287
}
288
 
289
int main()
290
{
291
    char c, *p1, *p2, *s;
292
    unsigned int l;
293
 
294
    switches();
295
    if(!fromfile) {
7676 bpr 296
      s=getenv("wims_exec_parm");
297
      if(s==NULL || *s==0) return 0; /* Nothing to translate */
298
      l=strlen(s); if(l<=0 || l>sourcelim) return 0; /* too long */
299
      inpbuf=xmalloc(l+16); memmove(inpbuf,s,l+1);
10 reyssat 300
    }
301
    else {
7676 bpr 302
      FILE *f;
303
      s=getenv("translator_input"); if(s==NULL || *s==0) return 0;
304
      f=fopen(s,"r"); if(f==NULL) return 0; /* no file */
305
      fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
306
      if(l<=0 || l>sourcelim) return 0; /* too long */
307
      inpbuf=xmalloc(l+16); (void)fread(inpbuf,1,l,f); fclose(f); inpbuf[l]=0;
10 reyssat 308
    }
309
    p1=inpbuf; prepare_dic();
310
    if(leaveline) c='\n'; else c=' ';
311
    do {
7676 bpr 312
      l=strlen(p1);
313
      if(l>MAX_LINELEN-1024) l=MAX_LINELEN-1024; p2=p1+l;
314
      if(*p2) {
315
          while(p2>p1 && *p2!=c) p2--;
316
      }
317
      if(p2<=p1) return 0;
318
      memmove(outbuf,p1,p2-p1); outbuf[p2-p1]=0;
319
      singlespace(outbuf);
320
      s=getenv("w_suffix_dictionary");
321
      if(s!=NULL && *s!=0) suffix(outbuf,s);
322
      translate();
323
      if(*p2==c) {printf("%c",c); p1=++p2;}
10 reyssat 324
    }
325
    while(*p2);
326
    return 0;
327
}