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