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