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