Rev 8135 | Rev 8195 | Go to most recent revision | 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 | |
8135 | bpr | 20 | #define MAX_TITLEN 60 |
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 { |
||
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); |
||
8085 | bpr | 52 | l=fread(inbuf,1,l,inf); |
10 | reyssat | 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 || |
||
5821 | bpr | 58 | inbuf[i]==nextchar || inbuf[i]==whilechar) inbuf[i]=' '; |
10 | reyssat | 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]; |
||
8085 | bpr | 70 | |
10 | reyssat | 71 | if(strlen(p)>=MAX_LINELEN) |
72 | bailout(inlen,0,"formula too long"); |
||
73 | while((p3=strstr(p,"<"))!=NULL) memmove(p3," < ",4); |
||
74 | while((p3=strstr(p,">"))!=NULL) memmove(p3," > ",4); |
||
75 | for(p3=strchr(p,'\n'); p3!=NULL; p3=strchr(p3,'\n')) *p3=' '; |
||
8085 | bpr | 76 | snprintf(bf,sizeof(bf),"%s",p); |
10 | reyssat | 77 | if(strchr(bf,'\\')==NULL && strchr(bf,'}')==NULL && strlen(bf)>2) { |
5821 | bpr | 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 | } |
||
10 | reyssat | 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 | |||
8086 | bpr | 92 | string_modify=string_modify2; |
10 | reyssat | 93 | substitute=substit; |
94 | if(argc==2 && strcmp(argv[1],"table")==0) { |
||
5821 | bpr | 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; |
||
10 | reyssat | 98 | } |
5821 | bpr | 99 | /* is defined in public_html/scripts/docu/mkindex now for example */ |
10 | reyssat | 100 | p=getenv("w_msg2wims_primitives"); if(p!=NULL) { |
5821 | bpr | 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); |
||
10 | reyssat | 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++) { |
||
5821 | bpr | 114 | switch(*p) { |
115 | case '$': fputs("$",outf); break; |
||
116 | case '!': fputs("!",outf); break; |
||
117 | case ':': fputs(":",outf); break; |
||
8085 | bpr | 118 | |
5821 | bpr | 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 | } |
||
8085 | bpr | 139 | /* lines begining by > are in small; italics and pre */ |
5821 | bpr | 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("<",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;} |
||
5898 | bpr | 155 | p++; fputs("\n<br/>\n",outf); |
5821 | bpr | 156 | while(*(p+1)=='\n') p++; |
157 | if(*(p+1)=='>') goto pre; |
||
158 | break; |
||
159 | } |
||
8085 | bpr | 160 | |
5821 | bpr | 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) {error("open_tag"); p2--;} |
||
168 | tend=p2; |
||
169 | fputc(*p, outf); break; |
||
170 | } |
||
8085 | bpr | 171 | /* interpretation of variables */ |
5821 | bpr | 172 | case '\\': { |
173 | char *pe; |
||
8085 | bpr | 174 | p++; |
5821 | bpr | 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 | } |
||
8085 | bpr | 213 | |
5821 | bpr | 214 | default: fputc(*p,outf); break; |
215 | } |
||
10 | reyssat | 216 | } |
217 | fputc('\n',outf); |
||
218 | outlen=ftell(outf); fclose(outf); bailout(inlen,outlen,""); |
||
219 | return 0; |
||
220 | } |
||
221 |