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