Subversion Repositories wimsdev

Rev

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