Subversion Repositories wimsdev

Rev

Rev 8148 | Rev 8353 | 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(directives, dir_no, sizeof(directives[0]))) return -1;*/
  96.       if(verify_order(specialfn, specialfn_no, sizeof(specialfn[0]))) return -1;
  97.       puts("Table orders OK."); return 0;
  98.     }
  99. /*  is defined in public_html/scripts/docu/mkindex now for example */
  100.     p=getenv("w_msg2wims_primitives"); if(p!=NULL) {
  101.       snprintf(primbuf,sizeof(primbuf),"%s",p);
  102.       for(p=primbuf; *p; p++) if(!isalnum(*p)) *p=' ';
  103.       p=find_word_start(primbuf);
  104.       for(primcnt=0; primcnt<256 && *p; primcnt++, p=find_word_start(p1)) {
  105.           p1=find_word_end(p); if(*p1) *p1++=0;
  106.           primitive[primcnt]=p;
  107.       }
  108.       if(primcnt>0) qsort(primitive,primcnt,sizeof(primitive[0]),_scmp);
  109.     }
  110.     if(argc<3) bailout(0,0,"missing file names");
  111.     p1=argv[1]; p2=argv[2];
  112.     get_inf(p1); open_outf(p2);
  113.     for(p=tend=inbuf;*p;p++) {
  114.       switch(*p) {
  115.           case '$': fputs("&#36;",outf); break;
  116.           case '!': fputs("&#33;",outf); break;
  117.           case ':': fputs("&#58;",outf); break;
  118.  
  119.           case elsechar: {
  120.             if(primcnt>0) fputs("\n!else\n",outf);
  121.             else fputc(*p,outf);
  122.             break;
  123.           }
  124.           case endifchar: {
  125.             if(primcnt>0) fputs("\n!endif\n",outf);
  126.             else fputc(*p,outf);
  127.             break;
  128.           }
  129.           case nextchar: {
  130.             if(primcnt>0) fputs("\n!next\n",outf);
  131.             else fputc(*p,outf);
  132.             break;
  133.           }
  134.           case whilechar: {
  135.             if(primcnt>0) fputs("\n!endwhile\n",outf);
  136.             else fputc(*p,outf);
  137.             break;
  138.           }
  139. /* lines begining by > are in small; italics and pre  */
  140.           case '\n': {
  141.             if(*(p+1)=='>') {
  142.                 pre: p++; fputs("\n<i><small><pre wrap>",outf);
  143.                 for(p1=strchr(p,'\n'); p1!=NULL && *(p1+1)=='>';
  144.                   p1=strchr(++p1,'\n'));
  145.                 if(p1!=NULL) *p1++=0; else p1=p+strlen(p);
  146.                 for(p2=p;*p2;p2++) {
  147.                   if(*p2!='<') fputc(*p2,outf);
  148.                   else fputs("&lt;",outf);
  149.                 }
  150.                 fputs("</pre></small></i>\n",outf);
  151.                 p=p1-1; break;
  152.             }
  153. /* two successive lines are replaced by an <p> - has to be fixed */
  154.             if(*(p+1)!='\n') {fputc(*p,outf);break;}
  155.             p++; fputs("\n<br/>\n",outf);
  156.             while(*(p+1)=='\n') p++;
  157.             if(*(p+1)=='>') goto pre;
  158.             break;
  159.           }
  160.  
  161.           case '<': {
  162.             char *p2;
  163.             if(tend>p || (!isalpha(*(p+1)) && *(p+1)!='!')) {
  164.                 fputc(*p,outf); break;
  165.             }
  166.             p2=find_tag_end(p);
  167.             if(!*p2) {msg_error("open_tag"); p2--;}
  168.             tend=p2;
  169.             fputc(*p, outf); break;
  170.           }
  171. /* interpretation of variables */
  172.           case '\\': {
  173.             char *pe;
  174.             p++;
  175.             if(isalpha(*p)) {
  176.                 if(primcnt>0) {
  177.                   pe=doccheck(p);
  178.                   if(pe>p) {p=pe-1; break;}
  179.                 }
  180.                 for(pe=p;isalnum(*pe) || *pe=='_';pe++);
  181.                 if(*pe=='[') {
  182.                   char *pv=find_matching(pe+1,']');
  183.                   if(pv!=NULL) {
  184.                       char c=*p;
  185.                       memmove(p,p+1,pv-p); *pv=')';
  186.                       fprintf(outf,"$(m_%c",c); p--; break;
  187.                   }
  188.                 }
  189.                 fprintf(outf,"$m_%c",*p); break;
  190.             }
  191.             switch(*p) {
  192.                 case '\\': fputc(*p,outf); break;
  193.                 case '{':
  194.                 case '(': {
  195.                   char *p2, *p3, c;
  196.                   if(*p=='{') c='}'; else c=')';
  197.                         p++; p2=find_matching(p,c);
  198.                   if(c==')') p3=strstr(p,"\\)");
  199.                   else p3=strstr(p,"\\}");
  200.                   if((p2==NULL && p3!=NULL) ||
  201.                      (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
  202.                   if(p2==NULL) fputc(*p,outf);
  203.                   else {
  204.                       *p2=0; if(*(p2-1)=='\\') *(p2-1)=0;
  205.                       process_formula(p); p=p2;
  206.                   }
  207.                   break;
  208.                 }
  209.                 default: fputc(*p,outf); break;
  210.             }
  211.             break;
  212.           }
  213.  
  214.           default: fputc(*p,outf); break;
  215.       }
  216.     }
  217.     fputc('\n',outf);
  218.     outlen=ftell(outf);  fclose(outf); bailout(inlen,outlen,"");
  219.     return 0;
  220. }
  221.  
  222.