Annotation of OpenXM/src/util/oxweave.c, Revision 1.4
1.1 takayama 1: /*&eg
2: \documentclass{article}
3: \title{On oxweave.c}
4: \author{} \date{}
5: \begin{document}
6: \maketitle
7: \section{Introduction}
8: */
9: /*&jp
10: \documentclass{jarticle}
11: \title{oxweave $B$N%=!<%9%3!<%I$K$D$$$F$N2r@b(B}
12: \author{} \date{}
13: \begin{document}
14: \maketitle
15: \section{$BA0=q$-(B}
16: */
17: /* OpenXM: OpenXM/src/kxx/oxweave.c,v 1.7 2001/05/06 07:53:01 takayama Exp
1.4 ! ohara 18: $OpenXM: OpenXM/src/util/oxweave.c,v 1.3 2005/07/03 08:27:38 ohara Exp $
1.1 takayama 19: */
20: #include <stdio.h>
1.3 ohara 21: #include <stdlib.h>
22: #include <string.h>
23:
24: void usage();
25: void findEndTag(int tagc,char *tagv[],int rule);
1.4 ! ohara 26: int findNextTag(int tagc, char *tagv[],int tagc2,char *tagv2[]);
1.3 ohara 27: void skipToEndTag(int tagc,char *tagv[],int rule);
1.4 ! ohara 28: int notEOF();
! 29: void irregularExit();
! 30: int wcmp(char *s);
1.1 takayama 31:
32: /* Modify here to change the begin tag and EndComment. Less than 9 characters.
33: */
34: char *BeginTag0="/*\x026"; /* 0x26 = & */
35: char *BeginTag1="//\x026"; /* 0x26 = & */
36: char *EndComment0="*/";
37: char *EndComment1="\n";
38:
39: #define BSIZE 256
40: #define VSIZE 256
41: static int Debug = 0;
42: static int Debug2 = 0;
43: static int Plain = 0;
44: /*&jp \noindent
45: $B:F5"(B option $B$r(B on $B$K$7$?>l9g(B ({\tt Recursive = 1},
46: {\tt LevelState1} $B$G(B, $B0u:~$9$Y$-(B comment $B$N%M%9%H$N%l%Y%k$rI=$9(B.
47: {\tt LevelState2} $B$G(B, $B:o=|$9$Y$-(B comment $B$N%M%9%H$N%l%Y%k$rI=$9(B.
48: */
49: static int Recursive = 0;
50: static int LevelState1 = 0;
51: static int LevelState2 = 0;
52: /*&jp \noindent
53: {\tt Buf} $B$OI8=`=PNO$h$j$N%G!<%?$r0l;~3JG<$7$F$*$/NN0h(B.
54: {\tt Head} $B$,:G=i$NJ8;z$r$5$7(B, {\tt Tail} $B$O:G8e$NJ8;z$r$5$9(B.
55: {\tt Buf} $B$r%j%s%0>u$K;HMQ$9$k$N$G(B, $B%j%s%0%P%C%U%!$H$h$V(B
56: $B$3$H$K$9$k(B.
57: */
58: int Buf[BSIZE];
59: int Head = 0;
60: int Tail = 0;
61: char *Tag = NULL;
62: /*&jp \noindent {\tt OutputNoTaggedSegment = 1}
63: $B$J$i(B $B%3%a%s%H5-9f$N30$O(B verbatim $B$rMQ$$$F=PNO(B.
64: {\tt --source} $B%*%W%7%g%s$G$3$NJQ?t$r(B1$B$K$G$-$k(B.
65: {\tt --plain} $B$G$O(B, verbatim $B$rMxMQ$;$:$K@8$G=PNO(B.
66: */
67: int OutputNoTaggedSegment = 0;
68: /*&jp \noindent 1 $B$J$i%?%0$N$D$$$?>l=j$r=PNOCf(B. */
69: int OutputtingTaggedSegment = 0;
70: int BeginVerbatim = 0;
71:
72: /*&jp \section{$B%W%m%0%i%`K\BN(B} */
1.4 ! ohara 73: int
1.1 takayama 74: main(int argc,char *argv[]) {
75: extern char *EndComment0;
76: extern char *EndComment1;
77: extern int Plain;
78: int tagc,i;
79: char *tagv[VSIZE];
80: int tagc2;
81: char *tagv2[VSIZE];
82: int pos;
83: Head = Tail = 0; Buf[0] = ' '; /* initialize */
84:
85: /*&jp {\tt tagv[]} $B$K%?%0$N$"$D$^$j$r$$$l$k(B.
86: {\tt tagv2[]} $B$KBP1~$9$k%?%0$N$*$o$j$N5-9f$r$$$l$k(B.
87: */
88: tagc = tagc2 = 0;
89: if (argc <= 1 || argc >= VSIZE) {
90: usage();
1.2 ohara 91: exit(0);
1.1 takayama 92: }else{
93: for (i=1; i< argc ; i++) {
94: if (strcmp(argv[i],"--source") == 0) {
95: OutputNoTaggedSegment = 1;
96: }else if (strcmp(argv[i],"--plain") == 0) {
97: Plain = 1; OutputNoTaggedSegment = 1;
98: }else if (strcmp(argv[i],"--recursive") == 0) {
99: Recursive = 1;
100: }else if (strcmp(argv[i],"--debug") == 0) {
101: Debug2 = 1;
102: } else{
103: if (strcmp(argv[i]," ") == 0) {
104: argv[i] = "";
105: }
106: tagv[tagc] = (char *) malloc(sizeof(char)*(strlen(argv[i])+10));
107: tagv2[tagc2] = (char *) malloc(sizeof(char)*10);
108: strcpy(tagv[tagc],BeginTag0);
109: strcat(tagv[tagc],argv[i]);
110: tagv2[tagc] = EndComment0;
111: /* $B%3%a%s%H$N$*$o$j$N5-9f(B. */
112: tagc2++;
113: tagc++;
114:
115: tagv[tagc] = (char *) malloc(sizeof(char)*(strlen(argv[i])+10));
116: tagv2[tagc2] = (char *) malloc(sizeof(char)*10);
117: strcpy(tagv[tagc],BeginTag1);
118: strcat(tagv[tagc],argv[i]);
119: tagv2[tagc] = EndComment1;
120: tagc2++;
121: tagc++;
122: }
123: }
124: }
125: /*&jp $B%W%m%0%i%`$O#3$D$N>uBV$r;}$D(B. $B>uBV(B 0 $B$O%?%0IU$-%3%a%s%H5-9f$N30(B.
126: $B>uBV(B 1 $B$O;XDj$5$l$?%?%0$NIU$$$?%3%a%s%H$NCf(B.
127: $B>uBV(B 2 $B$O;XDj$5$l$F$$$J$$%?%0$NIU$$$?%3%a%s%H$NCf(B
128: ($B>uBV#2$K$"$k$H$-$O0u:~$5$l$J$$(B.) */
129: /*
130: state 0 -- / * & jp ---> state 1
131: if ( BeginVerbatim & OutputNoTaggedSegment ) end-verbatim
132: <--- * / --- state 1
133: if ( OutputNoTaggedSegment ) begin-verbatim
134:
135: state 0 -- / * & unknown ---> state 2
136: <--- * / --- state 2
137:
138: state 0 & OutputNoTaggedSegment ==> putchar()
139: state 1 ==> putchar()
140: state 2 ==> skip
141: */
142: while (notEOF()) {
143: /* We are in the state 0. */
144: pos = findNextTag(tagc,tagv,tagc2,tagv2);
145: /* printf(" ===pos=%d=== ",pos); */
146: /* We are in the state 1. */
147: findEndTag(tagc2,tagv2,pos);
148: }
149: if (BeginVerbatim) {
150: if (!Plain) printf("\n\\end{verbatim\x07d}\n");
151: }
152: exit(0);
153: }
154:
155: /*&jp \noindent $B<!$N4X?t$OMxMQK!$rI=<($9$k(B. */
1.3 ohara 156: void usage() {
1.1 takayama 157: #include "oxweaveUsage.h"
158: }
159:
160: #define inc(a) ((a+1) % BSIZE)
161: /*&jp \noindent {\tt wread()} $B$O(B $BI8=`F~NO$h$j$N%G!<%?$rFI$a$k$@$1(B
162: $B%j%s%0%P%C%U%!(B {\tt Buf} $B$XFI$_9~$`(B.*/
1.4 ! ohara 163: int
1.1 takayama 164: wread() {
165: int c,i;
166: static int eof = 0;
167: if (eof) return(-1);
168: while (inc(Tail) != Head) {
169: c = getchar();
170: Tail = inc(Tail);
171: Buf[Tail] = c;
172: if (c == EOF) { eof = 1; return(-1); }
173: }
174: if (Debug == 1) {
175: fprintf(stderr,"[Head=%d, Tail=%d, ",Head,Tail);
176: for (i = Head; inc(i) != Tail; i = inc(i)) {
177: fprintf(stderr,"%c",Buf[i]);
178: }
179: fprintf(stderr,"]\n");
180: }
181: return(0);
182: }
183:
184: /*&jp \noindent {\tt wgetc(p)} $B$O(B
185: {\tt p} $BJ8;zFI$_9~$s$G:G8e$N(B 1 $BJ8;z$N%"%9%-!<%3!<%I$rLa$9(B.
186: */
187: int wgetc(int p) {
188: int c;
189: int i;
190: wread();
191: if (p < 1) p = 1;
192: for (i=0; i<p; i++) {
193: c = Buf[Head];
194: if (c == EOF) return(c);
195: Head = inc(Head);
196: wread();
197: }
198: return(c);
199: }
200:
201: /*&jp \noindent {\tt findNextTag()} $B$O<!$N(B {\tt / *\&} $B$J$k%?%0$r$5$,$9(B.
202: ( $B$3$l$O(B, {\tt BeginTag0} $B$NCM$rJQ$($k$HJQ99$G$-$k(B.)
203: {\tt OutputNoTaggedSegment} $B$,(B 1 $B$J$i%G!<%?$r$=$N$^$^$J$,$9(B.
204: $BL5;k$9$Y$-%?%0$N$H$-$O(B, $B%?%0FbIt$r%9%-%C%W$7$?$N$A(B
205: {\tt findNextTag} $B$r:F5"E*$K8F$V(B.
206: */
1.4 ! ohara 207: int
1.1 takayama 208: findNextTag(int tagc, char *tagv[],int tagc2,char *tagv2[]) {
209: int i;
210: int c,d;
211: extern char *BeginTag0;
212: extern char *BeginTag1;
213: do {
214: for (i=0; i<tagc; i++) {
215: /* fprintf(stderr,"\nChecking %s : ",tagv[i]); */
216: if (wcmp(tagv[i]) == 0) {
217: LevelState1++;
218: /* fprintf(stderr," : matched."); */
219: wgetc(strlen(tagv[i])+1);
220: if (OutputNoTaggedSegment == 1 && BeginVerbatim == 1) {
221: BeginVerbatim = 0;
222: if (!Plain) printf("\\end{verbatim\x07d}\n");
223: }
224: OutputtingTaggedSegment = 1;
225: return(i); /* Now, state is 1. */
226: }
227: }
228: /*&jp {\tt / *\&} $B$@$1$I$I$N%?%0$K$b0lCW$7$J$$(B */
229: if (wcmp(BeginTag0) == 1) {
230: LevelState2++;
231: wgetc(strlen(BeginTag0));
232: while ((d=wgetc(1)) > ' ') ;
233: /* We are in the state 2. */
234: skipToEndTag(tagc2,tagv2,0);
235: /* We are in the state 0. */
236: return(findNextTag(tagc,tagv,tagc2,tagv2));
237: }else if (wcmp(BeginTag1) == 1) {
238: LevelState2++;
239: wgetc(strlen(BeginTag1));
240: while ((d=wgetc(1)) > ' ') ;
241: /* We are in the state 2. */
242: skipToEndTag(tagc2,tagv2,1);
243: /* We are in the state 0. */
244: return(findNextTag(tagc,tagv,tagc2,tagv2));
245: }
246: /* We are in the state 0 */
247: c = wgetc(1);
248: if (OutputNoTaggedSegment) {
249: if (c != EOF) putchar(c);
250: }
251: }while( c!= EOF);
252: if (BeginVerbatim == 1) {
253: if (!Plain) printf("\n\\quad\n\\end{verbatim\x07d}\n");
254: }
255: exit(0);
256: }
257:
258: /*&jp \noindent {\tt findEndTag()} $B$O<!$N(B {\tt * /} $B$J$k%?%0$r$5$,$9(B.
259: ( $B$3$l$O(B, EndComment0 $B$NCM$rJQ$($k$HJQ992DG=(B. )
260: {\tt / /} $B$G;O$^$k>l9g$O(B, 0xa $B$,$*$o$j(B.
261: */
1.3 ohara 262: void findEndTag(int tagc,char *tagv[],int rule) {
1.1 takayama 263: int i;
264: int c;
265: /* We are in the state 1. */
266: do {
267: i = rule;
268: if (wcmp(tagv[i]) == 0) {
269: LevelState1--;
270: if (Debug2) printf("[LevelState1=%d by end of comment in the state 1.]\n",LevelState1);
271: if (LevelState1 > 0 && Recursive) {
272: wgetc(strlen(tagv[i]));
273: printf("%s",tagv[i]);
1.4 ! ohara 274: findEndTag(tagc,tagv,rule);
! 275: return;
1.1 takayama 276: }else{
277: wgetc(strlen(tagv[i]));
278: if (strcmp(tagv[i],"\n")==0) putchar('\n');
279: OutputtingTaggedSegment = 0;
280: if (OutputNoTaggedSegment) {
281: if (!Plain) printf("\n{\\footnotesize \\begin{verbatim}\n");
282: BeginVerbatim = 1;
283: }
284: return; /* Our state is 0. */
285: }
286: }
287: /* Our state is 1. */
288: if (wcmp("/*") >= 0 ) {
289: LevelState1++;
290: if (Debug2) printf("[LevelState1++=%d by / * in state 1.]\n",LevelState1);
291: }
292: c = wgetc(1);
293: putchar(c);
294: }while( c!= EOF);
295: fprintf(stderr,"findEndTag: unexpected EOF.\n");
296: irregularExit();
297: }
298:
1.3 ohara 299: void skipToEndTag(int tagc,char *tagv[],int rule) {
1.1 takayama 300: int i;
301: int c;
302: extern char *EndComment0;
303: extern char *EndComment1;
304: /* our state is 2. */
305: do {
306: if (rule == 0) {
307: if (wcmp(EndComment0) == 0) {
308: LevelState2--;
309: if (LevelState2 > 0 && Recursive) {
310: wgetc(strlen(EndComment0));
1.4 ! ohara 311: skipToEndTag(tagc,tagv,rule);
! 312: return;
1.1 takayama 313: }else{
314: wgetc(strlen(EndComment0));
315: return; /* our state is 0. */
316: }
317: }
318: }else if (rule == 1) {
319: if (wcmp(EndComment1) == 0) {
320: LevelState2--;
321: if (LevelState2 > 0 && Recursive) {
322: wgetc(strlen(EndComment0));
1.4 ! ohara 323: skipToEndTag(tagc,tagv,rule);
! 324: return;
1.1 takayama 325: }else{
326: wgetc(strlen(EndComment1));
327: return; /* our state is 0. */
328: }
329: }
330: }else{
331: for (i=0; i<tagc; i++) {
332: if (wcmp(tagv[i]) == 0) {
333: LevelState2--;
334: if (LevelState2 > 0 && Recursive) {
335: wgetc(strlen(EndComment0));
1.4 ! ohara 336: skipToEndTag(tagc,tagv,rule);
! 337: return;
1.1 takayama 338: }else{
339: wgetc(strlen(tagv[i]));
340: return; /* our state is 0. */
341: }
342: }
343: }
344:
345: }
346: /* our state is 2. */
347: if (wcmp("/*") >= 0) LevelState2++;
348: c = wgetc(1);
349: }while( c!= EOF);
350: fprintf(stderr,"findEndTag: unexpected EOF.\n");
351: irregularExit();
352: }
353:
354: /*&jp \noindent {\tt wcmp(s)} $B$OJ8;zNs(B {\tt s} $B$H(B {\tt Buf[Head]} $B$+$i(B
355: $B$O$8$^$kJ8;zNs$rHf3S$9$k(B.
356: {\tt Buf[Head+strlen(s) % BSIZE]} $B$,(B 0x20 $B0J2<$G$"$j(B, $B$"$H$N%P%$%H$,(B
357: $B0lCW$9$l$P(B 0 $B$rLa$9(B.
358: $B$"$H$N%P%$%H$,(B 0x20 $B0J2<$G$J$$$,(B, $B$=$NB>$N%P%$%H$,0lCW$9$k$H$-$O(B
359: 1 $B$rLa$9(B.
360: $B0J>eFs$D$N>l9g$K9gCW$7$J$$>l9g$O(B -1 $B$rLa$9(B.
361: {\tt s} $B$,(B 0xa,0 $B$N$H$-$O(B, Buf[Head] $B$,(B 0xa $B$J$i(B, 0 $B$rLa$9(B.
362: $B$=$&$G$J$$$J$i(B, -1 $B$rLa$9(B.
363: */
1.4 ! ohara 364: int
1.1 takayama 365: wcmp(char *s) {
366: int n;
367: int i,j;
368: wread();
369: if (Debug == 2) fprintf(stderr,"[Checking %s]\n",s);
370: if (strcmp(s,"\n") == 0) {
371: if (s[0] == Buf[Head]) return(0);
372: else return(-1);
373: }
374: n = strlen(s);
375: j = Head;
376: for (i=0; i<n; i++) {
377: if (s[i] != Buf[j]) return(-1);
378: j = inc(j);
379: }
380: if (Buf[j] <= ' ') {
381: if (Debug == 2) fprintf(stderr,"[Matched %s]\n",s);
382: return(0);
383: } else return(1);
384: }
385:
1.4 ! ohara 386: int
1.1 takayama 387: notEOF() {
388: wread();
389: if (Buf[Head] != -1) return(1);
390: else return(0);
391: }
392:
1.4 ! ohara 393: void
1.1 takayama 394: irregularExit() {
395: if (BeginVerbatim == 1) {
396: if (!Plain) printf("\\end{verbatim\x07d}\n");
397: }
398: exit(-1);
399: }
400:
401:
402: /*&jp
403: \end{document}
404: */
405: /*&eg
406: \end{document}
407: */
408:
409:
410:
411:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>