Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 6564 | 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.         /* This is an internal program,
  19.          * used to index modules for search engine. */
  20.  
  21. #include "../wims.h"
  22. #include "../Lib/basicstr.c"
  23.  
  24. #define MAX_LANGS       MAX_LANGUAGES
  25. #define MAX_MODULES     65536
  26. char *moduledir=        "public_html/modules";
  27. char *sheetdir=         "public_html/bases/sheet";
  28. char *dicdir=           "public_html/bases";
  29. char *outdir=           "public_html/bases/site2";
  30. char *maindic=          "sys/words";
  31. char *groupdic=         "sys/wgrp/wgrp";
  32. char *suffixdic=        "sys/suffix";
  33. char *ignoredic=        "sys/indignore";
  34. char *conffile=         "log/wims.conf";
  35. char *mlistbase=        "list";
  36.  
  37. char lang[MAX_LANGS][4]={
  38.     "en","fr","cn","es","it","nl","si","ca","pt"
  39. };
  40. #define DEFAULT_LANGCNT 6
  41. char allang[MAX_LANGS][4]={
  42.     "en","fr","cn","es","it","nl","tw","de","si","ca","pt"
  43. };
  44. #define allangcnt 8
  45. char ignore[MAX_LANGS][MAX_LINELEN+1];
  46. char mlistfile[MAX_LANGS][256];
  47. int langcnt;
  48. FILE *langf, *titf, *descf, *weightf, *robotf, *indf, *listf, *addrf, *serialf, *authorf, *versionf;
  49.  
  50. struct cat {
  51.     char *name;
  52.     char typ;
  53. } cat[]={
  54.         {"all_types",   'A'},
  55.         {"exercise",    'X'},
  56.         {"oef",         'O'},
  57.         {"tool",        'T'},
  58.         {"recreation",  'R'},
  59.         {"reference",   'Y'},
  60.         {"document",    'D'},
  61.         {"popup",       'P'},
  62.         {"datamodule",  'M'}
  63. };
  64. #define catno (sizeof(cat)/sizeof(cat[0]))
  65.  
  66. struct mod {
  67.     char *name;
  68.     unsigned char langs[MAX_LANGS];
  69.     int counts[MAX_LANGS];
  70.     int  langcnt;
  71. } mod[MAX_MODULES];
  72. int modcnt;
  73.  
  74. char *mlist;
  75.  
  76. void *xmalloc(size_t n)
  77. {
  78.     void *p;
  79.     p=malloc(n);
  80.     if(p==NULL) {
  81.         printf("Malloc failure.\n");
  82.         exit(1);
  83.     }
  84.     return p;
  85. }
  86.  
  87. char *acctab="çéèêëúùûüáàâäãóòôöõíìïîñýÇÉÈÊËÚÙÛÜÁÀÂÃÄÓÒÔÖÕÍÌÏÎÑÝ",
  88.      *deatab="ceeeeuuuuaaaaaoooooiiiinyCEEEEUUUUAAAAAOOOOOIIIINY";
  89.  
  90.         /* fold accented letters to unaccented */
  91. void deaccent(char *p)
  92. {
  93.     char *sp;
  94.     char *v;
  95.     for(sp=p;*sp;sp++) {
  96.         if(*sp<0 && (v=strchr(acctab,*sp))!=NULL)
  97.           *sp=*(deatab+(v-acctab));
  98.         if(!isalnum(*sp) && strchr(",.&$+*",*sp)==0) *sp=' ';
  99.         else *sp=tolower(*sp);
  100.     }
  101. }
  102.  
  103.         /* translate everything non-alphanumeric into space */
  104. void towords(char *p)
  105. {
  106.     char *pp;
  107.     for(pp=p;*pp;pp++) if(!isalnum(*pp) && strchr("&$+*",*pp)==0) *pp=' ';
  108. }
  109.  
  110.         /* Points to the end of the word */
  111. char *find_word_end(char *p)
  112. {
  113.     int i;
  114.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  115.     return p;
  116. }
  117.  
  118.         /* Strips leading spaces */
  119. char *find_word_start(char *p)
  120. {
  121.     int i;
  122.     for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++);
  123.     return p;
  124. }
  125.  
  126.         /* Find first occurrence of word */
  127. char *wordchr(char *p, char *w)
  128. {
  129.     char *r;
  130.  
  131.     for(r=strstr(p,w);r!=NULL &&
  132.         ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) );
  133.         r=strstr(r+1,w));
  134.     return r;
  135. }
  136.  
  137.         /* find a variable in a string (math expression).
  138.          * Returns the pointer or NULL. */
  139. char *varchr(char *p, char *v)
  140. {
  141.     char *pp; int n=strlen(v);
  142.     for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) {
  143.         if((pp==p || !isalnum(*(pp-1))) &&
  144.            (!isalnum(*(pp+n)) || *(pp+n)==0)) break;
  145.     }
  146.     return pp;
  147. }
  148.  
  149.         /* strip trailing spaces; return string end. */
  150. char *strip_trailing_spaces(char *p)
  151. {
  152.     char *pp;
  153.     if(*p==0) return p;
  154.     for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0);
  155.     return pp;
  156. }
  157.  
  158. char *find_tag_end(char *p)
  159. {
  160.     char *pp;
  161.     pp=p; if(*pp=='<') pp++;
  162.     for(; *pp && *pp!='>'; pp++) {
  163.         if(*pp=='<') {
  164.             pp=find_tag_end(pp)-1; continue;
  165.         }
  166.         if(*pp=='"') {
  167.             pp=strchr(pp+1,'"');
  168.             if(pp==NULL) return p+strlen(p); else continue;
  169.         }
  170.         if(*pp=='\'') {
  171.             pp=strchr(pp+1,'\'');
  172.             if(pp==NULL) return p+strlen(p); else continue;
  173.         }
  174.     }
  175.     if(*pp=='>') pp++; return pp;
  176. }
  177.  
  178. char *find_tag(char *p, char *tag)
  179. {
  180.     char *pp;
  181.     int len;
  182.     len=strlen(tag);
  183.     for(pp=strchr(p,'<'); pp!=NULL && *pp; pp=strchr(pp+1,'<')) {
  184.         if(strncasecmp(pp+1,tag,len)==0 && !isalnum(*(pp+1+len))) return pp;
  185.     }
  186.     return p+strlen(p);
  187. }
  188.  
  189.         /* remove all html tags */
  190. void detag(char *p)
  191. {
  192.     char *pp, *p2;
  193.     for(pp=strchr(p,'<'); pp!=NULL; pp=strchr(pp,'<')) {
  194.         p2=find_tag_end(pp);
  195.         if(*p2==0) {*pp=0; return; }
  196.         ovlstrcpy(pp,p2);
  197.     }
  198. }
  199.  
  200.         /* modify a string. Bufferlen must be ast least MAX_LINELEN */
  201. void string_modify(char *start, char *bad_beg, char *bad_end, char *good,...)
  202. {
  203.     char buf[MAX_LINELEN+1];
  204.     va_list vp;
  205.    
  206.     va_start(vp,good);
  207.     vsnprintf(buf,sizeof(buf),good,vp); va_end(vp);
  208.     if(strlen(start)-(bad_end-bad_beg)+strlen(buf)>=MAX_LINELEN)
  209.       return;
  210.     strcat(buf,bad_end);
  211.     ovlstrcpy(bad_beg,buf);
  212. }
  213.  
  214. void _getdef(char buf[], char *name, char value[])
  215. {
  216.     char *p1, *p2, *p3;
  217.  
  218.     value[0]=0;
  219.     for(p1=strstr(buf,name); p1!=NULL; p1=strstr(p1+1,name)) {
  220.         p2=find_word_start(p1+strlen(name));
  221.         if((p1>buf && !isspace(*(p1-1))) || *p2!='=') continue;
  222.         p3=p1; while(p3>buf && isspace(*(p3-1)) && *(p3-1)!='\n') p3--;
  223.         if(p3>buf && *(p3-1)!='\n') continue;
  224.         p2=find_word_start(p2+1);
  225.         p3=strchr(p2,'\n');
  226.         snprintf(value,MAX_LINELEN,"%s",p2);
  227.         if(p3!=NULL && p3-p2<MAX_LINELEN) value[p3-p2]=0;
  228.         strip_trailing_spaces(value);
  229.         break;
  230.     }
  231. }
  232.  
  233.         /* Get variable definition from a file.
  234.          * Result stored in buffer value of length MAX_LINELEN. */
  235. void getdef(char *fname, char *name, char value[])
  236. {
  237.     FILE *f;
  238.     char *buf;
  239.     int l;
  240.    
  241.     value[0]=0;
  242.     f=fopen(fname,"r"); if(f==NULL) return;
  243.     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  244.     buf=xmalloc(l+256); l=fread(buf,1,l,f);
  245.     fclose(f);
  246.     if(l<=0) return; else buf[l]=0;
  247.     _getdef(buf,name,value);
  248.     free(buf);
  249. }
  250.  
  251. #include "translator_.c"
  252.  
  253. char *mdicbuf, *gdicbuf;
  254. char gentry[sizeof(entry)], mentry[sizeof(entry)];
  255. int gentrycount, mentrycount;
  256.  
  257.         /* Preparation of data */
  258. void prep(void)
  259. {
  260.     char buf[MAX_LINELEN+1];
  261.     char *p1,*p2,*s,*old;
  262.     int i,l,thislang,t;
  263.     FILE *f;
  264.    
  265.     s=getenv("modind_outdir"); if(s!=NULL && *s!=0) outdir=s;
  266.     s=getenv("modind_sheetdir"); if(s!=NULL && *s!=0) sheetdir=s;
  267.     snprintf(buf,sizeof(buf),"%s/addr",outdir);
  268.     addrf=fopen(buf,"w");
  269.     snprintf(buf,sizeof(buf),"%s/serial",outdir);
  270.     serialf=fopen(buf,"w");
  271.     modcnt=langcnt=0;
  272.     getdef(conffile,"site_languages",buf);
  273.     for(p1=buf;*p1;p1++) if(!isalnum(*p1)) *p1=' ';
  274.     for(p1=find_word_start(buf); *p1 && langcnt<MAX_LANGS; p1=find_word_start(p2)) {
  275.         p2=find_word_end(p1);
  276.         if(p2!=p1+2 || !isalpha(*p1) || !isalpha(*(p1+1))) continue;
  277.         memmove(lang[langcnt],p1,2); lang[langcnt++][2]=0;
  278.     }
  279.     if(langcnt==0) {    /* default languages */
  280.         langcnt=DEFAULT_LANGCNT;
  281.     }
  282.     s=getenv("mlist"); if(s==NULL) exit(1);
  283.     l=strlen(s); if(l<0 || l>100*MAX_LINELEN) exit(1);
  284.     mlist=xmalloc(l+16); ovlstrcpy(mlist,s); old="";
  285.     for(i=0;i<langcnt;i++) {
  286.         snprintf(buf,sizeof(buf),"%s/%s.%s",dicdir,ignoredic,lang[i]);
  287.         f=fopen(buf,"r"); if(f==NULL) continue;
  288.         l=fread(ignore[i],1,MAX_LINELEN,f);fclose(f);
  289.         if(l<0 || l>=MAX_LINELEN) l=0;
  290.         ignore[i][l]=0;
  291.     }
  292.     for(t=0, p1=find_word_start(mlist);
  293.         *p1 && modcnt<MAX_MODULES;
  294.         p1=find_word_start(p2), t++) {
  295.         p2=find_word_end(p1);
  296.         l=p2-p1; if(*p2) *p2++=0;
  297.         fprintf(addrf,"%d:%s\n",t,p1);
  298.         fprintf(serialf,"%s:%d\n",p1,t);
  299.         thislang=-1;
  300.         if(l>3 && p1[l-3]=='.') {
  301.             for(i=0;i<langcnt;i++) if(strcasecmp(lang[i],p1+l-2)==0) break;
  302.             if(i<langcnt) {p1[l-3]=0; thislang=i;}
  303.             else {      /* unknown language, not referenced */
  304.                 continue;
  305.             }
  306.         }
  307.         if(modcnt>0 && strcmp(old,p1)==0 && thislang>=0) {
  308.             if(mod[modcnt-1].langcnt<langcnt) {
  309.                 mod[modcnt-1].langs[mod[modcnt-1].langcnt]=thislang;
  310.                 mod[modcnt-1].counts[mod[modcnt-1].langcnt]=t;
  311.                 (mod[modcnt-1].langcnt)++;
  312.             }
  313.         }
  314.         else {
  315.             mod[modcnt].name=old=p1;
  316.             if(thislang>=0) {
  317.                 mod[modcnt].langs[0]=thislang;
  318.                 mod[modcnt].langcnt=1;
  319.             }
  320.             else mod[modcnt].langcnt=0;
  321.             mod[modcnt].counts[0]=t;
  322.             modcnt++;
  323.         }
  324.     }
  325.     snprintf(buf,sizeof(buf),"%s/language",outdir);
  326.     langf=fopen(buf,"w");
  327.     snprintf(buf,sizeof(buf),"%s/title",outdir);
  328.     titf=fopen(buf,"w");
  329.     snprintf(buf,sizeof(buf),"%s/description",outdir);
  330.     descf=fopen(buf,"w");
  331.     snprintf(buf,sizeof(buf),"%s/author",outdir);
  332.     authorf=fopen(buf,"w");
  333.     snprintf(buf,sizeof(buf),"%s/version",outdir);
  334.     versionf=fopen(buf,"w");
  335.     snprintf(buf,sizeof(buf),"%s/lists/robot.phtml",outdir);
  336.     robotf=fopen(buf,"w");
  337.     fclose(addrf); fclose(serialf);
  338.     if(!robotf || !versionf || !authorf || !descf || !titf || !descf) {
  339.         fprintf(stderr,"modind: error creating output files.\n");
  340.         exit(1);
  341.     }
  342. }
  343.  
  344. void sprep(void)
  345. {
  346.     char *p1,*p2,*s;
  347.     int i,l,thislang;
  348.    
  349.     modcnt=0;
  350.     s=getenv("slist"); if(s==NULL) return;
  351.     l=strlen(s); if(l<0 || l>100*MAX_LINELEN) return;
  352.     mlist=xmalloc(l+16); ovlstrcpy(mlist,s);
  353.     for(p1=find_word_start(mlist); *p1 && modcnt<MAX_MODULES; p1=find_word_start(p2)) {
  354.         p2=find_word_end(p1);
  355.         l=p2-p1; if(*p2) *p2++=0;
  356.         for(i=0;i<langcnt;i++) if(strncasecmp(lang[i],p1,2)==0) break;
  357.         if(i<langcnt) thislang=i; else continue;
  358.         mod[modcnt].name=p1;
  359.         mod[modcnt].langs[0]=thislang;
  360.         mod[modcnt].langcnt=1;
  361.         modcnt++;
  362.     }
  363. }
  364.  
  365. void clean(void)
  366. {
  367.     fclose(langf); fclose(titf); fclose(descf); fclose(robotf);
  368.     fclose(authorf); fclose(versionf);
  369. }
  370.  
  371. char *sheetindex[]={
  372.       "title", "description",
  373.       "duration", "severity",
  374.       "level", "domain",
  375.       "keywords", "reserved1", "reserved2", "remark"
  376. };
  377. #define SHEETINDEX_NO (sizeof(sheetindex)/sizeof(sheetindex[0]))
  378. char sindbuf[SHEETINDEX_NO][MAX_LINELEN+1];
  379. enum{s_title, s_description,
  380.       s_duration, s_severity,
  381.       s_level, s_domain,
  382.       s_keywords, s_reserved1, s_reserved2,
  383.       s_remark
  384. };
  385.  
  386. char *modindex[]={
  387.       "title", "description",
  388.       "author", "address", "copyright",
  389.       "version", "wims_version", "language",
  390.       "category", "level", "domain", "keywords",
  391.       "keywords_ca", "keywords_fr", "keywords_it", "keywords_nl",
  392.       "require"
  393. };
  394. #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
  395. char indbuf[MODINDEX_NO][MAX_LINELEN+1];
  396. enum{i_title, i_description,
  397.       i_author,i_address,i_copyright,
  398.       i_version,i_wims_version,i_language,
  399.       i_category,i_level,i_domain,i_keywords,
  400.       i_keywords_ca,i_keywords_fr,i_keywords_it,i_keywords_nl,
  401.       i_require
  402. };
  403.  
  404. char *module_special_file[]={
  405.     "intro","help","about"
  406. };
  407. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  408. char module_language[4];
  409.  
  410.         /* read and treat module's INDEX file */
  411. int module_index(const char *name)
  412. {
  413.     char *p, fbuf[MAX_LINELEN+1], ibuf[MAX_LINELEN+1];
  414.     FILE *indf;
  415.     int i,l;
  416.  
  417.     snprintf(fbuf,sizeof(fbuf),"%s/%s/INDEX",moduledir,name);
  418.     indf=fopen(fbuf,"r"); if(indf==NULL) return -1;
  419.     l=fread(ibuf,1,MAX_LINELEN,indf); fclose(indf);
  420.     if(l>0 && l<MAX_LINELEN) ibuf[l]=0; else return -1;
  421.     for(i=0;i<MODINDEX_NO;i++) {
  422.         _getdef(ibuf,modindex[i],indbuf[i]);
  423.                 /* compatibility precaution */
  424.         if(indbuf[i][0]==':') indbuf[i][0]='.';
  425.     }
  426.     p=find_word_start(indbuf[i_language]);
  427.     if(isalpha(*p) && isalpha(*(p+1))) {
  428.         memmove(module_language,p,2); module_language[2]=0;
  429.     }
  430.     else ovlstrcpy(module_language,"en");
  431.     return 0;
  432. }
  433.  
  434. int sheet_index(int serial)
  435. {
  436.     char *p1, *p2, fbuf[MAX_LINELEN+1], ibuf[MAX_LINELEN+1];
  437.     FILE *indf;
  438.     int i,l;
  439.  
  440.     snprintf(fbuf,sizeof(fbuf),"%s/%s.def",sheetdir,mod[serial].name);
  441.     indf=fopen(fbuf,"r"); if(indf==NULL) return -1;
  442.     l=fread(ibuf,1,MAX_LINELEN,indf); fclose(indf);
  443.     if(l>0 && l<MAX_LINELEN) ibuf[l]=0; else return -1;
  444.     for(i=0;i<SHEETINDEX_NO;i++) sindbuf[i][0]=0;
  445.     for(i=0,p1=find_word_start(ibuf);
  446.         i<SHEETINDEX_NO-1 && *p1!=':' && *p1!=0;
  447.         i++,p1=p2) {
  448.         p2=strchr(p1,'\n');
  449.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  450.         p1=find_word_start(p1); strip_trailing_spaces(p1);
  451.         snprintf(sindbuf[i],MAX_LINELEN,"%s",p1);
  452.     }
  453.     p2=strstr(p1,"\n:"); if(p2==NULL) p2=p1+strlen(p1);
  454.     else *p2=0;
  455.     p1=find_word_start(p1); strip_trailing_spaces(p1);
  456.     for(p2=p1;*p2;p2++) if(*p2=='\n') *p2=' ';
  457.     ovlstrcpy(sindbuf[s_remark],p1);
  458.     return 0;
  459. }
  460.  
  461. unsigned char categories[16];
  462. char taken[MAX_LINELEN+1];
  463. int catcnt, takenlen, tweight;
  464.  
  465. void appenditem(char *word, int lind, int serial, int weight, char *l)
  466. {
  467.     char nbuf[MAX_LINELEN+1], buf[MAX_LINELEN+1];
  468.     int i, ll;
  469.     char *p;
  470.     FILE *f;
  471.    
  472.     if(!isalnum(*word) || (ll=strlen(word))<2 ||
  473.        wordchr(taken,word)!=NULL ||
  474.        wordchr(ignore[lind],word)!=NULL ||
  475.        takenlen>=MAX_LINELEN-ll-16)
  476.       return;
  477.     if(ll==2 && (!isdigit(word[0]) || !isalpha(word[1]))) return;
  478.     for(p=word;*p;p++) if(!isalnum(*p) && *p!=' ') return;
  479.     taken[takenlen++]=' '; taken[takenlen++]=' ';
  480.     ovlstrcpy(taken+takenlen,word);
  481.     takenlen+=ll; tweight+=weight;
  482.     snprintf(buf,sizeof(buf),"%s:%d?%d\n",word,serial,weight);
  483.     for(i=0;i<catcnt;i++) {
  484.         snprintf(nbuf,sizeof(nbuf),"%s/%c.%s",
  485.                  outdir,categories[i],lang[lind]);
  486.         f=fopen(nbuf,"a");
  487.         if(f!=NULL) {fputs(buf,f); fclose(f);}
  488.     }
  489. }
  490.  
  491. void onemodule(const char *name, int serial, int lind)
  492. {
  493.     int i;
  494.     unsigned char trlist[]={
  495.         i_title,i_description,i_category,i_domain,i_keywords,
  496.           i_require,i_author,
  497.           i_keywords_ca,i_keywords_fr,i_keywords_it,i_keywords_nl
  498.     };
  499.     #define trcnt (sizeof(trlist)/sizeof(trlist[0]))
  500.     char *p1, *p2, *pp, buf[MAX_LINELEN+1], lbuf[16];
  501.     FILE *f;
  502.    
  503.     if(module_index(name)) return;
  504.     towords(indbuf[i_category]);
  505.     for(i=catcnt=0;i<catno && catcnt<16;i++) {
  506.         if(wordchr(indbuf[i_category],cat[i].name)!=NULL)
  507.           categories[catcnt++]=cat[i].typ;
  508.     }
  509.     if(catcnt==0) return;
  510.     if(categories[0]!=cat[0].typ)
  511.       categories[catcnt++]=cat[0].typ;
  512.     for(i=0;i<catcnt;i++) {
  513.         snprintf(buf,sizeof(buf),"%s/lists/%c.%s",
  514.                  outdir,categories[i],lang[lind]);
  515.         f=fopen(buf,"a");
  516.         if(f!=NULL) {fprintf(f,"%s\n",name); fclose(f);}
  517.     }
  518.     fprintf(langf,"%d:%s\n",serial,module_language);
  519.     fprintf(titf,"%d:%s\n",serial,indbuf[i_title]);
  520.     fprintf(descf,"%d:%s\n",serial,indbuf[i_description]);
  521.     fprintf(authorf,"%d:%s\n",serial,indbuf[i_author]);
  522.     fprintf(versionf,"%d:%s\n",serial,indbuf[i_version]);
  523.     snprintf(buf,sizeof(buf),"%s",indbuf[i_description]);
  524.     for(pp=strchr(buf,','); pp; pp=strchr(pp,','))
  525.       string_modify(buf,pp,pp+1,"&#44;");
  526.     if(strcmp(module_language,lang[lind])==0)
  527.       fprintf(robotf,"%s ,%s,%s,%s,%s\n",name,module_language,name,
  528.               indbuf[i_title], buf);
  529.     entrycount=mentrycount; dicbuf=mdicbuf;
  530.     memmove(entry,mentry,mentrycount*sizeof(entry[0]));
  531.     unknown_type=unk_leave;
  532.     for(i=0;i<trcnt;i++) {
  533.         detag(indbuf[trlist[i]]);
  534.         deaccent(indbuf[trlist[i]]);
  535.         singlespace(indbuf[trlist[i]]);
  536.         suffix_translate(indbuf[trlist[i]]);
  537.         translate(indbuf[trlist[i]]);
  538.     }
  539.     taken[0]=0; takenlen=tweight=0;
  540.     ovlstrcpy(buf,indbuf[i_title]); towords(buf);
  541.     for(p1=find_word_start(buf);*p1;
  542.         p1=find_word_start(p2)) {
  543.         p2=find_word_end(p1); if(*p2) *p2++=0;
  544.         appenditem(p1,lind,serial,4,module_language);
  545.     }
  546.     snprintf(buf,sizeof(buf),"%s %s %s %s %s %s %s %s %s",
  547.              indbuf[i_description],indbuf[i_keywords],
  548.              indbuf[i_keywords_ca],indbuf[i_keywords_fr],
  549.              indbuf[i_keywords_it],indbuf[i_keywords_nl],
  550.              indbuf[i_domain],indbuf[i_require],indbuf[i_author]);
  551.     towords(buf);
  552.     for(p1=find_word_start(buf);*p1;
  553.         p1=find_word_start(p2)) {
  554.         p2=find_word_end(p1); if(*p2) *p2++=0;
  555.         appenditem(p1,lind,serial,2,module_language);
  556.     }
  557.     entrycount=gentrycount; dicbuf=gdicbuf;
  558.     memmove(entry,gentry,gentrycount*sizeof(entry[0]));
  559.     unknown_type=unk_delete;
  560.     ovlstrcpy(buf,indbuf[i_title]); translate(buf);
  561.     for(p1=find_word_start(buf); *p1;
  562.         p1=find_word_start(p2)) {
  563.         p2=strchr(p1,',');
  564.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  565.         if(strlen(p1)<=0) continue;
  566.         appenditem(p1,lind,serial,4,module_language);
  567.     }
  568.     snprintf(buf,sizeof(buf),"%s, %s, %s, %s, %s, %s, %s",
  569.              indbuf[i_description],indbuf[i_keywords],
  570.              indbuf[i_keywords_ca], indbuf[i_keywords_fr],
  571.              indbuf[i_keywords_it], indbuf[i_keywords_nl],
  572.              indbuf[i_domain]);
  573.     translate(buf);
  574.     for(p1=find_word_start(buf); *p1;
  575.         p1=find_word_start(p2)) {
  576.         p2=strchr(p1,',');
  577.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  578.         if(strlen(p1)<=0) continue;
  579.         appenditem(p1,lind,serial,2,module_language);
  580.     }
  581.     snprintf(buf,sizeof(buf),"%s",indbuf[i_level]);
  582.     ovlstrcpy(lbuf,"level");
  583.     for(p1=buf; *p1; p1++) if(!isalnum(*p1)) *p1=' ';
  584.     for(p1=find_word_start(buf); *p1;
  585.         p1=find_word_start(p2)) {
  586.         p2=find_word_end(p1);
  587.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  588.         if(!isalpha(*p1) ||
  589.            (!isdigit(*(p1+1)) && *(p1+1)!=0) ||
  590.            (*(p1+1)!=0 && *(p1+2)!=0))
  591.           continue;
  592.         *p1=tolower(*p1);
  593.         ovlstrcpy(lbuf+strlen("level"),p1);
  594.         appenditem(lbuf,lind,serial,2,module_language);
  595.     }
  596.     fprintf(weightf,"%d:%d\n",serial,tweight);
  597. }
  598.  
  599. void modules(void)
  600. {
  601.     int i,j,k,d;
  602.     char namebuf[MAX_LINELEN+1];
  603.     char mdic[MAX_LINELEN+1], sdic[MAX_LINELEN+1], gdic[MAX_LINELEN+1];
  604.  
  605.     for(j=0;j<langcnt;j++) {
  606.         snprintf(namebuf,sizeof(namebuf),"%s/weight.%s",outdir,lang[j]);
  607.         weightf=fopen(namebuf,"w");
  608.         snprintf(mdic,sizeof(mdic),"%s/%s.%s",dicdir,maindic,lang[j]);
  609.         snprintf(sdic,sizeof(sdic),"%s/%s.%s",dicdir,suffixdic,lang[j]);
  610.         snprintf(gdic,sizeof(gdic),"%s/%s.%s",dicdir,groupdic,lang[j]);
  611.         suffix_dic(sdic); prepare_dic(gdic);
  612.         gdicbuf=dicbuf; gentrycount=entrycount;
  613.         memmove(gentry,entry,gentrycount*sizeof(entry[0]));
  614.         prepare_dic(mdic);
  615.         mdicbuf=dicbuf; mentrycount=entrycount;
  616.         memmove(mentry,entry,mentrycount*sizeof(entry[0]));
  617.         unknown_type=unk_leave; translate(ignore[j]);
  618.         for(i=0;i<modcnt;i++) {
  619.             if(mod[i].langcnt>0) {
  620.                 for(d=k=0;k<mod[i].langcnt;k++)
  621.                   if(mod[i].langs[k]<mod[i].langs[d]) d=k;
  622.                 for(k=0;k<mod[i].langcnt && mod[i].langs[k]!=j;k++);
  623.                 if(k>=mod[i].langcnt) k=d;
  624.                 snprintf(namebuf,MAX_LINELEN,"%s.%s",mod[i].name,
  625.                          lang[mod[i].langs[k]]);
  626.                 onemodule(namebuf,mod[i].counts[k],j);
  627.             }
  628.             else {
  629.                 onemodule(mod[i].name,mod[i].counts[0],j);
  630.             }
  631.         }
  632.         if(mentrycount>0) free(mdicbuf);
  633.         if(gentrycount>0) free(gdicbuf);
  634.         if(suffixcnt>0) free(sufbuf);
  635.         if(weightf) fclose(weightf);
  636.     }
  637. }
  638.  
  639. void sappenditem(char *word, int lind, int serial, int weight)
  640. {
  641.     int ll;
  642.     char *p;
  643.    
  644.     if(!isalnum(*word) || (ll=strlen(word))<2 ||
  645.        wordchr(taken,word)!=NULL ||
  646.        wordchr(ignore[lind],word)!=NULL ||
  647.        takenlen>=MAX_LINELEN-ll-16)
  648.       return;
  649.     if(ll==2 && (!isdigit(word[0]) || !isalpha(word[1]))) return;
  650.     for(p=word;*p;p++) if(!isalnum(*p) && *p!=' ') return;
  651.     taken[takenlen++]=' ';taken[takenlen++]=' ';
  652.     ovlstrcpy(taken+takenlen,word);
  653.     takenlen+=ll; tweight+=weight;
  654.     fprintf(indf,"%s:%d?%d\n",word,serial,weight);
  655. }
  656.  
  657. void onesheet(int serial, int lind)
  658. {
  659.     int i;
  660.     unsigned char trlist[]={
  661.         s_title,s_description,s_domain,s_keywords,s_remark
  662.     };
  663.     #define trcnt (sizeof(trlist)/sizeof(trlist[0]))
  664.     char *p1, *p2, buf[MAX_LINELEN+1];
  665.    
  666.     if(sheet_index(serial)) return;
  667.     fprintf(listf,"%s\n",mod[serial].name+3);
  668.     fprintf(titf,"%d:%s\n",serial,sindbuf[s_title]);
  669.     fprintf(descf,"%d:%s\n",serial,sindbuf[s_description]);
  670.     entrycount=mentrycount; dicbuf=mdicbuf;
  671.     memmove(entry,mentry,mentrycount*sizeof(entry[0]));
  672.     unknown_type=unk_leave;
  673.     for(i=0;i<trcnt;i++) {
  674.         detag(sindbuf[trlist[i]]);
  675.         deaccent(sindbuf[trlist[i]]);
  676.         singlespace(sindbuf[trlist[i]]);
  677.         suffix_translate(sindbuf[trlist[i]]);
  678.         translate(sindbuf[trlist[i]]);
  679.     }
  680.     taken[0]=0; takenlen=tweight=0;
  681.     ovlstrcpy(buf,sindbuf[s_title]); towords(buf);
  682.     for(p1=find_word_start(buf);*p1;
  683.         p1=find_word_start(p2)) {
  684.         p2=find_word_end(p1); if(*p2) *p2++=0;
  685.         sappenditem(p1,lind,serial,4);
  686.     }
  687.     snprintf(buf,sizeof(buf),"%s %s %s %s",
  688.              sindbuf[s_description],sindbuf[s_keywords],
  689.              sindbuf[s_domain],sindbuf[s_remark]);
  690.     towords(buf);
  691.     for(p1=find_word_start(buf);*p1;
  692.         p1=find_word_start(p2)) {
  693.         p2=find_word_end(p1); if(*p2) *p2++=0;
  694.         sappenditem(p1,lind,serial,2);
  695.     }
  696.     entrycount=gentrycount; dicbuf=gdicbuf;
  697.     memmove(entry,gentry,gentrycount*sizeof(entry[0]));
  698.     unknown_type=unk_delete;
  699.     ovlstrcpy(buf,sindbuf[s_title]); translate(buf);
  700.     for(p1=find_word_start(buf); *p1;
  701.         p1=find_word_start(p2)) {
  702.         p2=strchr(p1,',');
  703.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  704.         if(strlen(p1)<=0) continue;
  705.         sappenditem(p1,lind,serial,4);
  706.     }
  707.     snprintf(buf,sizeof(buf),"%s, %s, %s, %s",
  708.              sindbuf[s_description],sindbuf[s_keywords],
  709.              sindbuf[s_domain],sindbuf[s_remark]);
  710.     translate(buf);
  711.     for(p1=find_word_start(buf); *p1;
  712.         p1=find_word_start(p2)) {
  713.         p2=strchr(p1,',');
  714.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  715.         if(strlen(p1)<=0) continue;
  716.         sappenditem(p1,lind,serial,2);
  717.     }
  718.     fprintf(weightf,"%d:%d\n",serial,tweight);
  719. }
  720.  
  721. void sheets(void)
  722. {
  723.     int i,j;
  724.     char mdic[MAX_LINELEN+1], sdic[MAX_LINELEN+1], gdic[MAX_LINELEN+1];
  725.     char buf[MAX_LINELEN+1];
  726.    
  727.     for(j=0;j<langcnt;j++) {
  728.         snprintf(buf,sizeof(buf),"%s/index/title.%s",sheetdir,lang[j]);
  729.         titf=fopen(buf,"w");
  730.         snprintf(buf,sizeof(buf),"%s/index/description.%s",sheetdir,lang[j]);
  731.         descf=fopen(buf,"w");
  732.         snprintf(buf,sizeof(buf),"%s/index/%s",sheetdir,lang[j]);
  733.         indf=fopen(buf,"w");
  734.         snprintf(buf,sizeof(buf),"%s/index/list.%s",sheetdir,lang[j]);
  735.         listf=fopen(buf,"w");
  736.         snprintf(buf,sizeof(buf),"%s/index/weight.%s",sheetdir,lang[j]);
  737.         weightf=fopen(buf,"w");
  738.         snprintf(buf,sizeof(buf),"%s/index/addr.%s",sheetdir,lang[j]);
  739.         addrf=fopen(buf,"w");
  740.         snprintf(buf,sizeof(buf),"%s/index/serial.%s",sheetdir,lang[j]);
  741.         serialf=fopen(buf,"w");
  742.         snprintf(mdic,sizeof(mdic),"%s/%s.%s",dicdir,maindic,lang[j]);
  743.         snprintf(sdic,sizeof(sdic),"%s/%s.%s",dicdir,suffixdic,lang[j]);
  744.         snprintf(gdic,sizeof(gdic),"%s/%s.%s",dicdir,groupdic,lang[j]);
  745.         suffix_dic(sdic); prepare_dic(gdic);
  746.         gdicbuf=dicbuf; gentrycount=entrycount;
  747.         memmove(gentry,entry,gentrycount*sizeof(entry[0]));
  748.         prepare_dic(mdic);
  749.         mdicbuf=dicbuf; mentrycount=entrycount;
  750.         memmove(mentry,entry,mentrycount*sizeof(entry[0]));
  751.         unknown_type=unk_leave; translate(ignore[j]);
  752.         for(i=0;i<modcnt;i++) {
  753.             if(mod[i].langs[0]!=j) continue;
  754.             fprintf(addrf,"%d:%s\n",i,mod[i].name+3);
  755.             fprintf(serialf,"%s:%d\n",mod[i].name+3,i);
  756.             onesheet(i,j);
  757.         }
  758.         if(mentrycount>0) free(mdicbuf);
  759.         if(gentrycount>0) free(gdicbuf);
  760.         if(suffixcnt>0) free(sufbuf);
  761.         fclose(titf); fclose(descf); fclose(indf); fclose(listf);
  762.         fclose(weightf); fclose(addrf); fclose(serialf);
  763.     }
  764. }
  765.  
  766. int main()
  767. {
  768.     prep();
  769.     if(modcnt>0) modules();
  770.     clean();
  771.     sprep();
  772.     if(modcnt>0) sheets();
  773.     return 0;
  774. }
  775.  
  776.