Subversion Repositories wimsdev

Rev

Rev 7676 | Rev 8123 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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