Subversion Repositories wimsdev

Rev

Rev 8195 | Rev 12247 | 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 program translates doc format into wims internal data format. */
  19.  
  20. #define MAX_TITLEN  60
  21.  
  22. #define MAX_PARM    10
  23. #define MAX_PARAM   1024
  24. #define MAX_DEFINE  1024
  25.  
  26. #include "../Lib/libwims.h"
  27. #include "msg2wims.h"
  28.  
  29. struct {
  30.     char *name;
  31.     int serial;
  32.     int type;
  33. } param[MAX_PARAM+32];
  34.  
  35. char *inbuf;
  36. FILE *inf, *outf;
  37. int inlen=0,outlen=0;
  38. int primserial=1;
  39. char *primitive_dir="docu/primitives";
  40.  
  41. void internal_warn(char *p)
  42. {}
  43.  
  44. void get_inf(char *fname)
  45. {
  46.     int i,l;
  47.     inf=fopen(fname,"r");
  48.     if(inf==NULL) bailout(0,0,"");
  49.     fseek(inf,0,SEEK_END); l=ftell(inf); fseek(inf,0,SEEK_SET);
  50.     if(l<=0) bailout(0,0,"");
  51.     inbuf=xmalloc(l+16);
  52.     l=fread(inbuf,1,l,inf);
  53.     if(l<=0) bailout(0,0,"error opening input file");
  54.     else inbuf[l]=0;
  55.     fclose(inf); inlen=l;
  56.     for(i=0;i<l;i++)
  57.       if(inbuf[i]==elsechar || inbuf[i]==endifchar ||
  58.        inbuf[i]==nextchar || inbuf[i]==whilechar) inbuf[i]=' ';
  59. }
  60.  
  61. void open_outf(char *fname)
  62. {
  63.     outf=fopen(fname,"w");
  64.     if(outf==NULL) bailout(inlen,0,"error opening output file");
  65. }
  66.  
  67. void process_formula(char *p)
  68. {
  69.     char *p3, bf[MAX_LINELEN+1];
  70.  
  71.     if(strlen(p)>=MAX_LINELEN)
  72.       bailout(inlen,0,"formula too long");
  73.     while((p3=strstr(p,"&lt;"))!=NULL) memmove(p3," <  ",4);
  74.     while((p3=strstr(p,"&gt;"))!=NULL) memmove(p3," >  ",4);
  75.     for(p3=strchr(p,'\n'); p3!=NULL; p3=strchr(p3,'\n')) *p3=' ';
  76.     snprintf(bf,sizeof(bf),"%s",p);
  77.     if(strchr(bf,'\\')==NULL && strchr(bf,'}')==NULL && strlen(bf)>2) {
  78.       for(p3=strstr(bf,".."); p3!=NULL; p3=strstr(p3,"..")) {
  79.           if(*(p3+2)=='.' || *(p3+2)==',') {
  80.             do p3++; while(*p3=='.'); continue;
  81.           }
  82.           *p3=','; *(p3+1)=' ';
  83.       }
  84.     }
  85.     fprintf(outf,"\n!insmath %s\n",bf);
  86. }
  87.  
  88. int main(int argc, char *argv[])
  89. {
  90.     char *p, *p1, *p2, *tend;
  91.     sp_error=msg_error;
  92.     string_modify=string_modify2;
  93.     substitute=substit;
  94.     if(argc==2 && strcmp(argv[1],"table")==0) {
  95.       if(verify_order(specialfn, specialfn_no, sizeof(specialfn[0]))) return -1;
  96.       puts("Table orders OK."); return 0;
  97.     }
  98. /* is defined in public_html/scripts/docu/mkindex and in public_html/modules/adm/doc/var.proc */
  99.     p=getenv("w_msg2wims_primitives"); if(p!=NULL) {
  100.       snprintf(primbuf,sizeof(primbuf),"%s",p);
  101.       for(p=primbuf; *p; p++) if(!isalnum(*p)) *p=' ';
  102.       p=find_word_start(primbuf);
  103.       for(primcnt=0; primcnt<256 && *p; primcnt++, p=find_word_start(p1)) {
  104.           p1=find_word_end(p); if(*p1) *p1++=0;
  105.           primitive[primcnt]=p;
  106.       }
  107.       if(primcnt>0) qsort(primitive,primcnt,sizeof(primitive[0]),_scmp);
  108.     }
  109.     if(argc<3) bailout(0,0,"missing file names");
  110.     p1=argv[1]; p2=argv[2];
  111.     get_inf(p1); open_outf(p2);
  112.     for(p=tend=inbuf;*p;p++) {
  113.       switch(*p) {
  114.           case '$': fputs("&#36;",outf); break;
  115.           case '!': fputs("&#33;",outf); break;
  116.           case ':': fputs("&#58;",outf); break;
  117.  
  118.           case elsechar: {
  119.             if(primcnt>0) fputs("\n!else\n",outf);
  120.             else fputc(*p,outf);
  121.             break;
  122.           }
  123.           case endifchar: {
  124.             if(primcnt>0) fputs("\n!endif\n",outf);
  125.             else fputc(*p,outf);
  126.             break;
  127.           }
  128.           case nextchar: {
  129.             if(primcnt>0) fputs("\n!next\n",outf);
  130.             else fputc(*p,outf);
  131.             break;
  132.           }
  133.           case whilechar: {
  134.             if(primcnt>0) fputs("\n!endwhile\n",outf);
  135.             else fputc(*p,outf);
  136.             break;
  137.           }
  138. /* lines begining by > are in small; italics and pre  */
  139.           case '\n': {
  140.             if(*(p+1)=='>') {
  141.                 pre: p++; fputs("\n<i><small><pre wrap>",outf);
  142.                 for(p1=strchr(p,'\n'); p1!=NULL && *(p1+1)=='>';
  143.                   p1=strchr(++p1,'\n'));
  144.                 if(p1!=NULL) *p1++=0; else p1=p+strlen(p);
  145.                 for(p2=p;*p2;p2++) {
  146.                   if(*p2!='<') fputc(*p2,outf);
  147.                   else fputs("&lt;",outf);
  148.                 }
  149.                 fputs("</pre></small></i>\n",outf);
  150.                 p=p1-1; break;
  151.             }
  152. /* two successive lines are replaced by an <p> - has to be fixed */
  153.             if(*(p+1)!='\n') {fputc(*p,outf);break;}
  154.             p++; fputs("\n<br/>\n",outf);
  155.             while(*(p+1)=='\n') p++;
  156.             if(*(p+1)=='>') goto pre;
  157.             break;
  158.           }
  159.  
  160.           case '<': {
  161.             char *p2;
  162.             if(tend>p || (!isalpha(*(p+1)) && *(p+1)!='!')) {
  163.                 fputc(*p,outf); break;
  164.             }
  165.             p2=find_tag_end(p);
  166.             if(!*p2) {msg_error("open_tag"); p2--;}
  167.             tend=p2;
  168.             fputc(*p, outf); break;
  169.           }
  170. /* interpretation of variables */
  171.           case '\\': {
  172.             char *pe;
  173.             p++;
  174.             if(isalpha(*p)) {
  175.                 if(primcnt>0) {
  176.                   pe=doccheck(p);
  177.                   if(pe>p) {p=pe-1; break;}
  178.                 }
  179.                 for(pe=p;isalnum(*pe) || *pe=='_';pe++);
  180.                 if(*pe=='[') {
  181.                   char *pv=find_matching(pe+1,']');
  182.                   if(pv!=NULL) {
  183.                       char c=*p;
  184.                       memmove(p,p+1,pv-p); *pv=')';
  185.                       fprintf(outf,"$(m_%c",c); p--; break;
  186.                   }
  187.                 }
  188.                 fprintf(outf,"$m_%c",*p); break;
  189.             }
  190.             switch(*p) {
  191.                 case '\\': fputc(*p,outf); break;
  192.                 case '{':
  193.                 case '(': {
  194.                   char *p2, *p3, c;
  195.                   if(*p=='{') c='}'; else c=')';
  196.                         p++; p2=find_matching(p,c);
  197.                   if(c==')') p3=strstr(p,"\\)");
  198.                   else p3=strstr(p,"\\}");
  199.                   if((p2==NULL && p3!=NULL) ||
  200.                      (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
  201.                   if(p2==NULL) fputc(*p,outf);
  202.                   else {
  203.                       *p2=0; if(*(p2-1)=='\\') *(p2-1)=0;
  204.                       process_formula(p); p=p2;
  205.                   }
  206.                   break;
  207.                 }
  208.                 default: fputc(*p,outf); break;
  209.             }
  210.             break;
  211.           }
  212.  
  213.           default: fputc(*p,outf); break;
  214.       }
  215.     }
  216.     fputc('\n',outf);
  217.     outlen=ftell(outf);  fclose(outf); bailout(inlen,outlen,"");
  218.     return 0;
  219. }
  220.