Back to index

wims  3.65+svn20090927
translator.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* Versatile translation according to a dictionary */
00019 
00020 /*************** Customization: change values hereafter ****************/
00021 
00022        /* limit of dictionary entries */
00023 #define entrylim 32768
00024        /* limit of dictionary length */
00025 #define diclim       2*1024*1024
00026        /* limit of source length */
00027 #define sourcelim 16*1024*1024
00028 
00029 /***************** Nothing should need change hereafter *****************/
00030 
00031 #include "../wims.h"
00032 
00033 char *inpbuf, outbuf[2*(MAX_LINELEN+1)];
00034 char *dicbuf;
00035 struct entry {
00036     unsigned char *original, *replace;
00037     int olen,earlier;
00038 } entry[entrylim];
00039 int entrycount;
00040 
00041 enum {
00042     unk_delete, unk_leave, unk_replace
00043 };
00044 
00045 int has_digits=0;
00046 int unknown_type=unk_delete;
00047 int nocase=0,leaveline=0,fromfile=0;
00048 char *unknown, unkbuf[1024];
00049 
00050 void *xmalloc(size_t n)
00051 {
00052     void *p;
00053     p=malloc(n);
00054     if(p==NULL) exit(1);
00055     return p;
00056 }
00057 
00058        /* Exit without translating anything */
00059 void escape(void)
00060 {
00061     printf("%s",inpbuf); exit(0);
00062 }
00063 
00064        /* Points to the end of the word */
00065 char *find_word_end(char *p)
00066 {
00067     int i;
00068     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
00069     return p;
00070 }
00071 
00072        /* Strips leading spaces */
00073 char *find_word_start(char *p)
00074 {
00075     int i;
00076     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
00077     return p;
00078 }
00079 
00080        /* strip trailing spaces; return string end. */
00081 char *strip_trailing_spaces(char *p)
00082 {
00083     char *pp;
00084     if(*p==0) return p;
00085     for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
00086     return pp;
00087 }
00088 
00089 int compare(int i1, const char *s2)
00090 {
00091     int k;
00092     if(nocase) k=strncasecmp(entry[i1].original,s2,entry[i1].olen);
00093     else k=strncmp(entry[i1].original,s2,entry[i1].olen);
00094     if(k==0 && (isalnum(*(s2+entry[i1].olen)) || (*(s2+entry[i1].olen)&128)!=0)) return -1;
00095     else return k;
00096 }
00097 
00098        /* searches a list. Returns index if found, -1 if nomatch. 
00099         * Uses binary search, list must be sorted. */
00100 int search_list(struct entry *list, int items, size_t item_size, const char *str)
00101 {
00102     int i1,i2,j,k,t,t1;
00103     unsigned char c;
00104 
00105     if(items<=0) return -1;
00106     j=0; c=str[0];
00107     k=list[0].original[0]-c; if(k==0) k=compare(0,str);
00108     if(k==0) goto more; if(k>0) return -1;
00109     j=items-1; k=list[j].original[0]-c; if(k==0) k=compare(j,str);
00110     if(k==0) return j;
00111     if(k>0) for(i1=0,i2=j;i2>i1+1;) {
00112        j=i1+(i2-i1)/2;
00113        k=list[j].original[0]-c; if(k==0) k=compare(j,str);
00114        if(k==0) goto more;
00115        if(k>0) {i2=j; continue;}
00116        if(k<0) {i1=j; continue;}   
00117     }
00118     if(k>0) {j--;k=compare(j,str);}
00119     more:
00120     if((t=list[j].earlier)<0) {
00121        if(k==0) return j; else return -1;
00122     }
00123     if(compare(t,str)!=0) return -1;
00124     for(j=t1=t,k=0;j<items && list[j].earlier==t1 && (k=compare(j,str))<=0; j++) {
00125        if(k==0) t=j;
00126     }
00127     return t;
00128 }
00129 
00130        /* modify a string. Bufferlen must be ast least 2*MAX_LINELEN */
00131 void string_modify(char *start, char *bad_beg, char *bad_end, char *good,...)
00132 {
00133     char buf[MAX_LINELEN+1];
00134     va_list vp;
00135     
00136     va_start(vp,good);
00137     vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
00138     if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=2*MAX_LINELEN)
00139       return; /* this is an error situation. */
00140     strcat(buf,bad_end);
00141     strcpy(bad_beg,buf);
00142 }
00143 
00144        /* change all spaces into ' ', and collapse multiple occurences */
00145 void singlespace(char *p)
00146 {
00147     char *pp, *p2;
00148     for(pp=p;*pp;pp++) {
00149        if(!isspace(*pp)) continue;
00150        if(leaveline) {
00151            if(*pp==13) strcpy(pp,pp+1);
00152            if(*pp=='\n') {
00153               pp++;
00154               gopt: for(p2=pp; isspace(*p2) && *p2!='\n'; p2++);
00155               if(p2>pp) strcpy(pp,p2); pp--;
00156            }
00157            else {
00158               pp++; if(!isspace(*pp) || *pp=='\n') continue;
00159               goto gopt;
00160            }
00161        }
00162        else {
00163            if(*pp!=' ') *pp=' ';
00164            pp++; if(!isspace(*pp)) continue;
00165            for(p2=pp;isspace(*p2);p2++);
00166            strcpy(pp,p2); pp--;
00167        }
00168     }
00169 }
00170 
00171 #include "suffix.c"
00172 
00173        /* Prepare dictionary */
00174 void prepare_dic(void)
00175 {
00176     int i,l;
00177     FILE *dicf;
00178     char *fname, *p1, *p2, *pp, buf[1024];
00179     long int flen;
00180     fname=getenv("w_dictionary");
00181     if(fname==NULL || *fname==0 || *fname=='/' || strstr(fname,"..")) {
00182        p1=getenv("w_module"); if(p1 && strncmp(p1,"classes/",strlen("classes/"))==0) {
00183            p1=getenv("w_wims_class"); p2=getenv("w_wims_home");
00184            if(p1 && p2) {
00185               snprintf(buf,sizeof(buf),"%s/log/classes/%s/",p2,p1);
00186               if(strncmp(fname,buf,strlen(buf))!=0) escape();
00187            }
00188            else escape();
00189        }
00190        else {
00191            p1=getenv("untrust"); if(p1 && strstr(p1,"yes")) escape();
00192        }
00193     }
00194     dicf=fopen(fname,"r"); if(dicf==NULL) escape();
00195     fseek(dicf,0,SEEK_END);flen=ftell(dicf); fseek(dicf,0,SEEK_SET);
00196     if(flen>diclim) escape();
00197     dicbuf=xmalloc(flen+16);flen=fread(dicbuf,1,flen,dicf);
00198     fclose(dicf);
00199     if(flen>0 && flen<diclim) dicbuf[flen]=0;
00200     else escape();
00201     for(i=0,p1=dicbuf;p1!=NULL && *p1!=0 && i<entrylim;p1=p2) {
00202        p2=strchr(p1+1,'\n'); if(p2>p1) *p2++=0;
00203        pp=strchr(p1,':'); if(pp==NULL) continue;
00204        *pp++=0;
00205        strip_trailing_spaces(p1); strip_trailing_spaces(pp);
00206        singlespace(p1);
00207        p1=find_word_start(p1); pp=find_word_start(pp);
00208        if(*p1==0) continue;
00209        if(has_digits==0) {
00210            char *p;
00211            for(p=p1;*p!=0 && p<pp && !isdigit(*p);p++);
00212            if(isdigit(*p)) has_digits=1;
00213        }
00214        entry[i].original=p1; entry[i].replace=pp; 
00215        entry[i].olen=l=strlen(p1); entry[i].earlier=-1;
00216        if(i>0) {
00217            int l1,l2;
00218            l1=entry[i-1].earlier; if(l1>=0) l2=entry[l1].olen;
00219            else {l2=entry[i-1].olen;l1=i-1;}
00220            if(l>l2 && isspace(p1[l2])
00221               && strncmp(entry[l1].original,p1,l2)==0) 
00222              entry[i].earlier=entry[i-1].earlier=l1;
00223        }
00224        i++;
00225     }
00226     entrycount=i; if(entrycount<=0) escape();
00227 }
00228 
00229        /* now make the translation. */
00230 void translate(void)
00231 {
00232     char *p1, *p2, *pp;
00233     int t;
00234 
00235     for(p1=find_word_start(outbuf);
00236        p1!=NULL && p1-outbuf<MAX_LINELEN && *p1!=0;
00237        p1=p2) {
00238        p2=find_word_end(p1);
00239        for(pp=p1;pp<p2 && 
00240            ((!has_digits && isalpha(*pp)) ||
00241             (has_digits && isalnum(*pp)) || (*pp&128)!=0 ||
00242             strchr("_",*pp)!=NULL);pp++);
00243        p2=find_word_start(p2);
00244        if(pp==p1 || 
00245           (has_digits==0 && isdigit(*pp)) || 
00246           (*pp!=0 && !isspace(*pp) && strchr(",.?!/;",*pp)==NULL)) continue;
00247        t=search_list(entry,entrycount,sizeof(entry[0]),p1);
00248        if(t<0) {
00249            switch(unknown_type) {
00250               case unk_leave: break;
00251               case unk_delete: {
00252                   strcpy(p1,find_word_start(pp)); p2=p1;
00253                   break;
00254               }
00255               case unk_replace: {
00256                   string_modify(outbuf,p1,pp,unkbuf);
00257                   p2=find_word_start(p1+strlen(unkbuf));
00258               }
00259            }
00260            continue;
00261        }
00262        string_modify(outbuf,p1,p1+strlen(entry[t].original),
00263                     entry[t].replace);
00264        p2=find_word_start(p1+strlen(entry[t].replace));
00265     }
00266     outbuf[MAX_LINELEN]=0; printf("%s",outbuf);
00267 }
00268 
00269 void switches(void)
00270 {
00271     char *sw;
00272     unknown=getenv("w_translator_unknown");
00273     if(unknown==NULL) unknown="";
00274     snprintf(unkbuf,sizeof(unkbuf),"%s",find_word_start(unknown));
00275     *find_word_end(unkbuf)=0;
00276     if(strcasecmp(unkbuf,"leave")==0) unknown_type=unk_leave;
00277     else if(unkbuf[0]!=0) unknown_type=unk_replace;
00278     sw=getenv("w_translator_switch");
00279     if(sw==NULL) return;
00280     if(strstr(sw,"leaveline")) leaveline=1;
00281     if(strstr(sw,"nocase")) nocase=1;
00282     if(strstr(sw,"file")) fromfile=1;
00283 }
00284 
00285 int main()
00286 {
00287     char c, *p1, *p2, *s;
00288     unsigned int l;
00289 
00290     switches();
00291     if(!fromfile) {
00292        s=getenv("wims_exec_parm");
00293        if(s==NULL || *s==0) return 0;     /* Nothing to translate */
00294        l=strlen(s); if(l<=0 || l>sourcelim) return 0; /* too long */
00295        inpbuf=xmalloc(l+16); memmove(inpbuf,s,l+1);
00296     }
00297     else {
00298        FILE *f;
00299        s=getenv("translator_input"); if(s==NULL || *s==0) return 0;
00300        f=fopen(s,"r"); if(f==NULL) return 0; /* no file */
00301        fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
00302        if(l<=0 || l>sourcelim) return 0; /* too long */
00303        inpbuf=xmalloc(l+16); fread(inpbuf,1,l,f); fclose(f); inpbuf[l]=0;
00304     }
00305     p1=inpbuf; prepare_dic();
00306     if(leaveline) c='\n'; else c=' ';
00307     do {
00308        l=strlen(p1);
00309        if(l>MAX_LINELEN-1024) l=MAX_LINELEN-1024; p2=p1+l;
00310        if(*p2) {
00311            while(p2>p1 && *p2!=c) p2--;
00312        }
00313        if(p2<=p1) return 0;
00314        memmove(outbuf,p1,p2-p1); outbuf[p2-p1]=0;
00315        singlespace(outbuf);
00316        s=getenv("w_suffix_dictionary");
00317        if(s!=NULL && *s!=0) suffix(outbuf,s);
00318        translate();
00319        if(*p2==c) {printf("%c",c); p1=++p2;}
00320     }
00321     while(*p2);
00322     return 0;
00323 }
00324