Subversion Repositories wimsdev

Rev

Rev 3247 | 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.       "require"
  392. };
  393. #define MODINDEX_NO (sizeof(modindex)/sizeof(modindex[0]))
  394. char indbuf[MODINDEX_NO][MAX_LINELEN+1];
  395. enum{i_title, i_description,
  396.       i_author,i_address,i_copyright,
  397.       i_version,i_wims_version,i_language,
  398.       i_category,i_level,i_domain,i_keywords,
  399.       i_require
  400. };
  401.  
  402. char *module_special_file[]={
  403.     "intro","help","about"
  404. };
  405. #define MODSPEC_NO (sizeof(module_special_file)/sizeof(module_special_file[0]))
  406. char module_language[4];
  407.  
  408.         /* read and treat module's INDEX file */
  409. int module_index(const char *name)
  410. {
  411.     char *p, fbuf[MAX_LINELEN+1], ibuf[MAX_LINELEN+1];
  412.     FILE *indf;
  413.     int i,l;
  414.  
  415.     snprintf(fbuf,sizeof(fbuf),"%s/%s/INDEX",moduledir,name);
  416.     indf=fopen(fbuf,"r"); if(indf==NULL) return -1;
  417.     l=fread(ibuf,1,MAX_LINELEN,indf); fclose(indf);
  418.     if(l>0 && l<MAX_LINELEN) ibuf[l]=0; else return -1;
  419.     for(i=0;i<MODINDEX_NO;i++) {
  420.         _getdef(ibuf,modindex[i],indbuf[i]);
  421.                 /* compatibility precaution */
  422.         if(indbuf[i][0]==':') indbuf[i][0]='.';
  423.     }
  424.     p=find_word_start(indbuf[i_language]);
  425.     if(isalpha(*p) && isalpha(*(p+1))) {
  426.         memmove(module_language,p,2); module_language[2]=0;
  427.     }
  428.     else ovlstrcpy(module_language,"en");
  429.     return 0;
  430. }
  431.  
  432. int sheet_index(int serial)
  433. {
  434.     char *p1, *p2, fbuf[MAX_LINELEN+1], ibuf[MAX_LINELEN+1];
  435.     FILE *indf;
  436.     int i,l;
  437.  
  438.     snprintf(fbuf,sizeof(fbuf),"%s/%s.def",sheetdir,mod[serial].name);
  439.     indf=fopen(fbuf,"r"); if(indf==NULL) return -1;
  440.     l=fread(ibuf,1,MAX_LINELEN,indf); fclose(indf);
  441.     if(l>0 && l<MAX_LINELEN) ibuf[l]=0; else return -1;
  442.     for(i=0;i<SHEETINDEX_NO;i++) sindbuf[i][0]=0;
  443.     for(i=0,p1=find_word_start(ibuf);
  444.         i<SHEETINDEX_NO-1 && *p1!=':' && *p1!=0;
  445.         i++,p1=p2) {
  446.         p2=strchr(p1,'\n');
  447.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  448.         p1=find_word_start(p1); strip_trailing_spaces(p1);
  449.         snprintf(sindbuf[i],MAX_LINELEN,"%s",p1);
  450.     }
  451.     p2=strstr(p1,"\n:"); if(p2==NULL) p2=p1+strlen(p1);
  452.     else *p2=0;
  453.     p1=find_word_start(p1); strip_trailing_spaces(p1);
  454.     for(p2=p1;*p2;p2++) if(*p2=='\n') *p2=' ';
  455.     ovlstrcpy(sindbuf[s_remark],p1);
  456.     return 0;
  457. }
  458.  
  459. unsigned char categories[16];
  460. char taken[MAX_LINELEN+1];
  461. int catcnt, takenlen, tweight;
  462.  
  463. void appenditem(char *word, int lind, int serial, int weight, char *l)
  464. {
  465.     char nbuf[MAX_LINELEN+1], buf[MAX_LINELEN+1];
  466.     int i, ll;
  467.     char *p;
  468.     FILE *f;
  469.    
  470.     if(!isalnum(*word) || (ll=strlen(word))<2 ||
  471.        wordchr(taken,word)!=NULL ||
  472.        wordchr(ignore[lind],word)!=NULL ||
  473.        takenlen>=MAX_LINELEN-ll-16)
  474.       return;
  475.     if(ll==2 && (!isdigit(word[0]) || !isalpha(word[1]))) return;
  476.     for(p=word;*p;p++) if(!isalnum(*p) && *p!=' ') return;
  477.     taken[takenlen++]=' '; taken[takenlen++]=' ';
  478.     ovlstrcpy(taken+takenlen,word);
  479.     takenlen+=ll; tweight+=weight;
  480.     snprintf(buf,sizeof(buf),"%s:%d?%d\n",word,serial,weight);
  481.     for(i=0;i<catcnt;i++) {
  482.         snprintf(nbuf,sizeof(nbuf),"%s/%c.%s",
  483.                  outdir,categories[i],lang[lind]);
  484.         f=fopen(nbuf,"a");
  485.         if(f!=NULL) {fputs(buf,f); fclose(f);}
  486.     }
  487. }
  488.  
  489. void onemodule(const char *name, int serial, int lind)
  490. {
  491.     int i;
  492.     unsigned char trlist[]={
  493.         i_title,i_description,i_category,i_domain,i_keywords,
  494.           i_require,i_author
  495.     };
  496.     #define trcnt (sizeof(trlist)/sizeof(trlist[0]))
  497.     char *p1, *p2, *pp, buf[MAX_LINELEN+1], lbuf[16];
  498.     FILE *f;
  499.    
  500.     if(module_index(name)) return;
  501.     towords(indbuf[i_category]);
  502.     for(i=catcnt=0;i<catno && catcnt<16;i++) {
  503.         if(wordchr(indbuf[i_category],cat[i].name)!=NULL)
  504.           categories[catcnt++]=cat[i].typ;
  505.     }
  506.     if(catcnt==0) return;
  507.     if(categories[0]!=cat[0].typ)
  508.       categories[catcnt++]=cat[0].typ;
  509.     for(i=0;i<catcnt;i++) {
  510.         snprintf(buf,sizeof(buf),"%s/lists/%c.%s",
  511.                  outdir,categories[i],lang[lind]);
  512.         f=fopen(buf,"a");
  513.         if(f!=NULL) {fprintf(f,"%s\n",name); fclose(f);}
  514.     }
  515.     fprintf(langf,"%d:%s\n",serial,module_language);
  516.     fprintf(titf,"%d:%s\n",serial,indbuf[i_title]);
  517.     fprintf(descf,"%d:%s\n",serial,indbuf[i_description]);
  518.     fprintf(authorf,"%d:%s\n",serial,indbuf[i_author]);
  519.     fprintf(versionf,"%d:%s\n",serial,indbuf[i_version]);
  520.     snprintf(buf,sizeof(buf),"%s",indbuf[i_description]);
  521.     for(pp=strchr(buf,','); pp; pp=strchr(pp,','))
  522.       string_modify(buf,pp,pp+1,"&#44;");
  523.     if(strcmp(module_language,lang[lind])==0)
  524.       fprintf(robotf,"%s ,%s,%s,%s,%s\n",name,module_language,name,
  525.               indbuf[i_title], buf);
  526.     entrycount=mentrycount; dicbuf=mdicbuf;
  527.     memmove(entry,mentry,mentrycount*sizeof(entry[0]));
  528.     unknown_type=unk_leave;
  529.     for(i=0;i<trcnt;i++) {
  530.         detag(indbuf[trlist[i]]);
  531.         deaccent(indbuf[trlist[i]]);
  532.         singlespace(indbuf[trlist[i]]);
  533.         suffix_translate(indbuf[trlist[i]]);
  534.         translate(indbuf[trlist[i]]);
  535.     }
  536.     taken[0]=0; takenlen=tweight=0;
  537.     ovlstrcpy(buf,indbuf[i_title]); towords(buf);
  538.     for(p1=find_word_start(buf);*p1;
  539.         p1=find_word_start(p2)) {
  540.         p2=find_word_end(p1); if(*p2) *p2++=0;
  541.         appenditem(p1,lind,serial,4,module_language);
  542.     }
  543.     snprintf(buf,sizeof(buf),"%s %s %s %s %s",
  544.              indbuf[i_description],indbuf[i_keywords],
  545.              indbuf[i_domain],indbuf[i_require],indbuf[i_author]);
  546.     towords(buf);
  547.     for(p1=find_word_start(buf);*p1;
  548.         p1=find_word_start(p2)) {
  549.         p2=find_word_end(p1); if(*p2) *p2++=0;
  550.         appenditem(p1,lind,serial,2,module_language);
  551.     }
  552.     entrycount=gentrycount; dicbuf=gdicbuf;
  553.     memmove(entry,gentry,gentrycount*sizeof(entry[0]));
  554.     unknown_type=unk_delete;
  555.     ovlstrcpy(buf,indbuf[i_title]); translate(buf);
  556.     for(p1=find_word_start(buf); *p1;
  557.         p1=find_word_start(p2)) {
  558.         p2=strchr(p1,',');
  559.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  560.         if(strlen(p1)<=0) continue;
  561.         appenditem(p1,lind,serial,4,module_language);
  562.     }
  563.     snprintf(buf,sizeof(buf),"%s, %s, %s",
  564.              indbuf[i_description],indbuf[i_keywords],
  565.              indbuf[i_domain]);
  566.     translate(buf);
  567.     for(p1=find_word_start(buf); *p1;
  568.         p1=find_word_start(p2)) {
  569.         p2=strchr(p1,',');
  570.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  571.         if(strlen(p1)<=0) continue;
  572.         appenditem(p1,lind,serial,2,module_language);
  573.     }
  574.     snprintf(buf,sizeof(buf),"%s",indbuf[i_level]);
  575.     ovlstrcpy(lbuf,"level");
  576.     for(p1=buf; *p1; p1++) if(!isalnum(*p1)) *p1=' ';
  577.     for(p1=find_word_start(buf); *p1;
  578.         p1=find_word_start(p2)) {
  579.         p2=find_word_end(p1);
  580.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  581.         if(!isalpha(*p1) ||
  582.            (!isdigit(*(p1+1)) && *(p1+1)!=0) ||
  583.            (*(p1+1)!=0 && *(p1+2)!=0))
  584.           continue;
  585.         *p1=tolower(*p1);
  586.         ovlstrcpy(lbuf+strlen("level"),p1);
  587.         appenditem(lbuf,lind,serial,2,module_language);
  588.     }
  589.     fprintf(weightf,"%d:%d\n",serial,tweight);
  590. }
  591.  
  592. void modules(void)
  593. {
  594.     int i,j,k,d;
  595.     char namebuf[MAX_LINELEN+1];
  596.     char mdic[MAX_LINELEN+1], sdic[MAX_LINELEN+1], gdic[MAX_LINELEN+1];
  597.  
  598.     for(j=0;j<langcnt;j++) {
  599.         snprintf(namebuf,sizeof(namebuf),"%s/weight.%s",outdir,lang[j]);
  600.         weightf=fopen(namebuf,"w");
  601.         snprintf(mdic,sizeof(mdic),"%s/%s.%s",dicdir,maindic,lang[j]);
  602.         snprintf(sdic,sizeof(sdic),"%s/%s.%s",dicdir,suffixdic,lang[j]);
  603.         snprintf(gdic,sizeof(gdic),"%s/%s.%s",dicdir,groupdic,lang[j]);
  604.         suffix_dic(sdic); prepare_dic(gdic);
  605.         gdicbuf=dicbuf; gentrycount=entrycount;
  606.         memmove(gentry,entry,gentrycount*sizeof(entry[0]));
  607.         prepare_dic(mdic);
  608.         mdicbuf=dicbuf; mentrycount=entrycount;
  609.         memmove(mentry,entry,mentrycount*sizeof(entry[0]));
  610.         unknown_type=unk_leave; translate(ignore[j]);
  611.         for(i=0;i<modcnt;i++) {
  612.             if(mod[i].langcnt>0) {
  613.                 for(d=k=0;k<mod[i].langcnt;k++)
  614.                   if(mod[i].langs[k]<mod[i].langs[d]) d=k;
  615.                 for(k=0;k<mod[i].langcnt && mod[i].langs[k]!=j;k++);
  616.                 if(k>=mod[i].langcnt) k=d;
  617.                 snprintf(namebuf,MAX_LINELEN,"%s.%s",mod[i].name,
  618.                          lang[mod[i].langs[k]]);
  619.                 onemodule(namebuf,mod[i].counts[k],j);
  620.             }
  621.             else {
  622.                 onemodule(mod[i].name,mod[i].counts[0],j);
  623.             }
  624.         }
  625.         if(mentrycount>0) free(mdicbuf);
  626.         if(gentrycount>0) free(gdicbuf);
  627.         if(suffixcnt>0) free(sufbuf);
  628.         if(weightf) fclose(weightf);
  629.     }
  630. }
  631.  
  632. void sappenditem(char *word, int lind, int serial, int weight)
  633. {
  634.     int ll;
  635.     char *p;
  636.    
  637.     if(!isalnum(*word) || (ll=strlen(word))<2 ||
  638.        wordchr(taken,word)!=NULL ||
  639.        wordchr(ignore[lind],word)!=NULL ||
  640.        takenlen>=MAX_LINELEN-ll-16)
  641.       return;
  642.     if(ll==2 && (!isdigit(word[0]) || !isalpha(word[1]))) return;
  643.     for(p=word;*p;p++) if(!isalnum(*p) && *p!=' ') return;
  644.     taken[takenlen++]=' ';taken[takenlen++]=' ';
  645.     ovlstrcpy(taken+takenlen,word);
  646.     takenlen+=ll; tweight+=weight;
  647.     fprintf(indf,"%s:%d?%d\n",word,serial,weight);
  648. }
  649.  
  650. void onesheet(int serial, int lind)
  651. {
  652.     int i;
  653.     unsigned char trlist[]={
  654.         s_title,s_description,s_domain,s_keywords,s_remark
  655.     };
  656.     #define trcnt (sizeof(trlist)/sizeof(trlist[0]))
  657.     char *p1, *p2, buf[MAX_LINELEN+1];
  658.    
  659.     if(sheet_index(serial)) return;
  660.     fprintf(listf,"%s\n",mod[serial].name+3);
  661.     fprintf(titf,"%d:%s\n",serial,sindbuf[s_title]);
  662.     fprintf(descf,"%d:%s\n",serial,sindbuf[s_description]);
  663.     entrycount=mentrycount; dicbuf=mdicbuf;
  664.     memmove(entry,mentry,mentrycount*sizeof(entry[0]));
  665.     unknown_type=unk_leave;
  666.     for(i=0;i<trcnt;i++) {
  667.         detag(sindbuf[trlist[i]]);
  668.         deaccent(sindbuf[trlist[i]]);
  669.         singlespace(sindbuf[trlist[i]]);
  670.         suffix_translate(sindbuf[trlist[i]]);
  671.         translate(sindbuf[trlist[i]]);
  672.     }
  673.     taken[0]=0; takenlen=tweight=0;
  674.     ovlstrcpy(buf,sindbuf[s_title]); towords(buf);
  675.     for(p1=find_word_start(buf);*p1;
  676.         p1=find_word_start(p2)) {
  677.         p2=find_word_end(p1); if(*p2) *p2++=0;
  678.         sappenditem(p1,lind,serial,4);
  679.     }
  680.     snprintf(buf,sizeof(buf),"%s %s %s %s",
  681.              sindbuf[s_description],sindbuf[s_keywords],
  682.              sindbuf[s_domain],sindbuf[s_remark]);
  683.     towords(buf);
  684.     for(p1=find_word_start(buf);*p1;
  685.         p1=find_word_start(p2)) {
  686.         p2=find_word_end(p1); if(*p2) *p2++=0;
  687.         sappenditem(p1,lind,serial,2);
  688.     }
  689.     entrycount=gentrycount; dicbuf=gdicbuf;
  690.     memmove(entry,gentry,gentrycount*sizeof(entry[0]));
  691.     unknown_type=unk_delete;
  692.     ovlstrcpy(buf,sindbuf[s_title]); translate(buf);
  693.     for(p1=find_word_start(buf); *p1;
  694.         p1=find_word_start(p2)) {
  695.         p2=strchr(p1,',');
  696.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  697.         if(strlen(p1)<=0) continue;
  698.         sappenditem(p1,lind,serial,4);
  699.     }
  700.     snprintf(buf,sizeof(buf),"%s, %s, %s, %s",
  701.              sindbuf[s_description],sindbuf[s_keywords],
  702.              sindbuf[s_domain],sindbuf[s_remark]);
  703.     translate(buf);
  704.     for(p1=find_word_start(buf); *p1;
  705.         p1=find_word_start(p2)) {
  706.         p2=strchr(p1,',');
  707.         if(p2!=NULL) *p2++=0; else p2=p1+strlen(p1);
  708.         if(strlen(p1)<=0) continue;
  709.         sappenditem(p1,lind,serial,2);
  710.     }
  711.     fprintf(weightf,"%d:%d\n",serial,tweight);
  712. }
  713.  
  714. void sheets(void)
  715. {
  716.     int i,j;
  717.     char mdic[MAX_LINELEN+1], sdic[MAX_LINELEN+1], gdic[MAX_LINELEN+1];
  718.     char buf[MAX_LINELEN+1];
  719.    
  720.     for(j=0;j<langcnt;j++) {
  721.         snprintf(buf,sizeof(buf),"%s/index/title.%s",sheetdir,lang[j]);
  722.         titf=fopen(buf,"w");
  723.         snprintf(buf,sizeof(buf),"%s/index/description.%s",sheetdir,lang[j]);
  724.         descf=fopen(buf,"w");
  725.         snprintf(buf,sizeof(buf),"%s/index/%s",sheetdir,lang[j]);
  726.         indf=fopen(buf,"w");
  727.         snprintf(buf,sizeof(buf),"%s/index/list.%s",sheetdir,lang[j]);
  728.         listf=fopen(buf,"w");
  729.         snprintf(buf,sizeof(buf),"%s/index/weight.%s",sheetdir,lang[j]);
  730.         weightf=fopen(buf,"w");
  731.         snprintf(buf,sizeof(buf),"%s/index/addr.%s",sheetdir,lang[j]);
  732.         addrf=fopen(buf,"w");
  733.         snprintf(buf,sizeof(buf),"%s/index/serial.%s",sheetdir,lang[j]);
  734.         serialf=fopen(buf,"w");
  735.         snprintf(mdic,sizeof(mdic),"%s/%s.%s",dicdir,maindic,lang[j]);
  736.         snprintf(sdic,sizeof(sdic),"%s/%s.%s",dicdir,suffixdic,lang[j]);
  737.         snprintf(gdic,sizeof(gdic),"%s/%s.%s",dicdir,groupdic,lang[j]);
  738.         suffix_dic(sdic); prepare_dic(gdic);
  739.         gdicbuf=dicbuf; gentrycount=entrycount;
  740.         memmove(gentry,entry,gentrycount*sizeof(entry[0]));
  741.         prepare_dic(mdic);
  742.         mdicbuf=dicbuf; mentrycount=entrycount;
  743.         memmove(mentry,entry,mentrycount*sizeof(entry[0]));
  744.         unknown_type=unk_leave; translate(ignore[j]);
  745.         for(i=0;i<modcnt;i++) {
  746.             if(mod[i].langs[0]!=j) continue;
  747.             fprintf(addrf,"%d:%s\n",i,mod[i].name+3);
  748.             fprintf(serialf,"%s:%d\n",mod[i].name+3,i);
  749.             onesheet(i,j);
  750.         }
  751.         if(mentrycount>0) free(mdicbuf);
  752.         if(gentrycount>0) free(gdicbuf);
  753.         if(suffixcnt>0) free(sufbuf);
  754.         fclose(titf); fclose(descf); fclose(indf); fclose(listf);
  755.         fclose(weightf); fclose(addrf); fclose(serialf);
  756.     }
  757. }
  758.  
  759. int main()
  760. {
  761.     prep();
  762.     if(modcnt>0) modules();
  763.     clean();
  764.     sprep();
  765.     if(modcnt>0) sheets();
  766.     return 0;
  767. }
  768.  
  769.