Subversion Repositories wimsdev

Rev

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