Subversion Repositories wimsdev

Rev

Rev 17221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 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
 
8085 bpr 18
 /* This program translates doc format into wims internal data format. */
10 reyssat 19
 
13380 bpr 20
#define MAX_TITLEN  80
8135 bpr 21
 
22
#define MAX_PARM    10
23
#define MAX_PARAM   1024
10 reyssat 24
#define MAX_DEFINE  1024
25
 
26
#include "../Lib/libwims.h"
8135 bpr 27
#include "msg2wims.h"
10 reyssat 28
 
29
struct {
12247 bpr 30
  char *name;
31
  int serial;
32
  int type;
10 reyssat 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
{
12247 bpr 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]=' ';
10 reyssat 59
}
60
 
61
void open_outf(char *fname)
62
{
12247 bpr 63
  outf=fopen(fname,"w");
64
  if(outf==NULL) bailout(inlen,0,"error opening output file");
10 reyssat 65
}
66
 
67
void process_formula(char *p)
68
{
12247 bpr 69
  char *p3, bf[MAX_LINELEN+1];
8085 bpr 70
 
12247 bpr 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;
5821 bpr 81
      }
12247 bpr 82
      *p3=','; *(p3+1)=' ';
10 reyssat 83
    }
12247 bpr 84
  }
85
  fprintf(outf,"\n!insmath %s\n",bf);
10 reyssat 86
}
87
 
88
int main(int argc, char *argv[])
89
{
12247 bpr 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
  }
14813 bpr 98
 /* is defined in public_html/scripts/docu/mkindex and in public_html/modules/adm/doc/var.proc */
12247 bpr 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;
10 reyssat 106
    }
12247 bpr 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;
8085 bpr 117
 
12247 bpr 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
      }
14813 bpr 138
      /* lines begining by > are in blockquote class="wims_citation" */
12247 bpr 139
      case '\n': {
140
        if(*(p+1)=='>') {
14813 bpr 141
          pre: p++; fputs("\n<blockquote class=\"wims_citation\"><pre>",outf);
12247 bpr 142
          for(p1=strchr(p,'\n'); p1!=NULL && *(p1+1)=='>';
14813 bpr 143
            p1=strchr(++p1,'\n'));
12247 bpr 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);
5821 bpr 148
          }
14813 bpr 149
          fputs("</pre></blockquote>\n",outf);
12247 bpr 150
          p=p1-1; break;
151
        }
17221 bpr 152
        /* two successive lines are replaced by an <br> */
12247 bpr 153
        if(*(p+1)!='\n') {fputc(*p,outf);break;}
17221 bpr 154
        p++; fputs("\n<br>\n",outf);
12247 bpr 155
        while(*(p+1)=='\n') p++;
156
        if(*(p+1)=='>') goto pre;
157
        break;
158
      }
159
      case '<': {
160
        char *p2;
161
        if(tend>p || (!isalpha(*(p+1)) && *(p+1)!='!')) {
162
          fputc(*p,outf); break;
163
        }
164
        p2=find_tag_end(p);
165
        if(!*p2) {msg_error("open_tag"); p2--;}
166
        tend=p2;
167
        fputc(*p, outf); break;
168
      }
14813 bpr 169
      /* interpretation of variables */
12247 bpr 170
      case '\\': {
17864 bpr 171
        char *pe;
12247 bpr 172
        p++;
173
        if(isalpha(*p)) {
17864 bpr 174
          if(primcnt>0) {
175
            pe=doccheck(p);
176
            if(pe>p) {p=pe-1; break;}
177
          }
178
          for(pe=p;isalnum(*pe) || *pe=='_';pe++);
179
          if(*pe=='[') {
180
            char *pv=find_matching(pe+1,']');
181
            if(pv!=NULL) {
182
              char c=*p;
183
              memmove(p,p+1,pv-p); *pv=')';
184
              fprintf(outf,"$(m_%c",c); p--; break;
185
            }
186
          }
187
          fprintf(outf,"$m_%c",*p); break;
12247 bpr 188
        }
189
        switch(*p) {
190
          case '\\': fputc(*p,outf); break;
191
          case '{':
192
          case '(': {
193
            char *p2, *p3, c;
194
            if(*p=='{') c='}'; else c=')';
195
            p++; p2=find_matching(p,c);
196
            if(c==')') p3=strstr(p,"\\)");
197
            else p3=strstr(p,"\\}");
198
            if((p2==NULL && p3!=NULL) ||
199
               (p2!=NULL && p3!=NULL && p3<p2)) p2=p3+1;
200
              if(p2==NULL) fputc(*p,outf);
201
            else {
202
              *p2=0; if(*(p2-1)=='\\') *(p2-1)=0;
203
              process_formula(p); p=p2;
5821 bpr 204
            }
205
            break;
206
          }
207
          default: fputc(*p,outf); break;
12247 bpr 208
        }
209
        break;
5821 bpr 210
      }
12247 bpr 211
 
212
      default: fputc(*p,outf); break;
10 reyssat 213
    }
12247 bpr 214
  }
215
  fputc('\n',outf);
216
  outlen=ftell(outf);  fclose(outf); bailout(inlen,outlen,"");
217
  return 0;
10 reyssat 218
}