Annotation of OpenXM/src/kxx/oxweave.c, Revision 1.1
1.1 ! takayama 1: /*&jp
! 2: \documentclass{jarticle}
! 3: \title{oxweave $B$N%=!<%9%3!<%I$K$D$$$F$N2r@b(B}
! 4: \author{} \date{}
! 5: \begin{document}
! 6: \maketitle
! 7: \section{$BA0=q$-(B}
! 8: */
! 9: /* $OpenXM$ */
! 10: #include <stdio.h>
! 11: #define BSIZE 10
! 12: #define VSIZE 10
! 13: static int Debug = 0;
! 14: /*&jp \noindent
! 15: {\tt Buf} $B$OI8=`=PNO$h$j$N%G!<%?$r0l;~3JG<$7$F$*$/NN0h(B.
! 16: {\tt Head} $B$,:G=i$NJ8;z$r$5$7(B, {\tt Tail} $B$O:G8e$NJ8;z$r$5$9(B.
! 17: {\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
! 18: $B$3$H$K$9$k(B.
! 19: */
! 20: int Buf[BSIZE];
! 21: int Head = 0;
! 22: int Tail = 0;
! 23: char *Tag = NULL;
! 24: /*&jp \noindent {\tt OutputNoTaggedSegment = 1}
! 25: $B$J$i(B $B%3%a%s%H5-9f$N30$O(B verbatim $B$rMQ$$$F=PNO(B.
! 26: {\tt --source} $B%*%W%7%g%s$G$3$NJQ?t$r(B1$B$K$G$-$k(B.
! 27: */
! 28: int OutputNoTaggedSegment = 0;
! 29: /*&jp \noindent 1 $B$J$i%?%0$N$D$$$?>l=j$r=PNOCf(B. */
! 30: int OutputtingTaggedSegment = 0;
! 31: int BeginVerbatim = 0;
! 32:
! 33: /*&jp \section{$B%W%m%0%i%`K\BN(B} */
! 34: main(int argc,char *argv[]) {
! 35: int c;
! 36: int tagc,i;
! 37: char *tagv[VSIZE];
! 38: int tagc2;
! 39: char *tagv2[VSIZE];
! 40: char *s = "/*\x026"; /* 0x26 = & */
! 41: Head = Tail = 0; Buf[0] = ' '; /* initialize */
! 42:
! 43: /* $B%3%a%s%H$N$*$o$j$N5-9f(B. sm1 $B$J$i(B 0xa $B$G$"$k(B. */
! 44: tagc2 = 1; tagv2[0] = "*/";
! 45: /*&jp {\tt tagv[]} $B$K%?%0$N$"$D$^$j$r$$$l$k(B. */
! 46: tagc = 0;
! 47: if (argc <= 1 || argc >= VSIZE) {
! 48: usage();
! 49: exit();
! 50: }else{
! 51: for (i=1; i< argc ; i++) {
! 52: if (strcmp(argv[i],"--source") == 0) {
! 53: OutputNoTaggedSegment = 1;
! 54: }else{
! 55: tagv[tagc] = (char *) malloc(sizeof(char)*(strlen(argv[i])+4));
! 56: strcpy(tagv[tagc],s);
! 57: strcat(tagv[tagc],argv[i]);
! 58: tagc++;
! 59: }
! 60: }
! 61: }
! 62: /*&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.
! 63: $B>uBV(B 1 $B$O;XDj$5$l$?%?%0$NIU$$$?%3%a%s%H$NCf(B.
! 64: $B>uBV(B 2 $B$O;XDj$5$l$F$$$J$$%?%0$NIU$$$?%3%a%s%H$NCf(B
! 65: ($B>uBV#2$K$"$k$H$-$O0u:~$5$l$J$$(B.) */
! 66: /*
! 67: state 0 -- / * & jp ---> state 1
! 68: if ( BeginVerbatim & OutputNoTaggedSegment ) end-verbatim
! 69: <--- * / --- state 1
! 70: if ( OutputNoTaggedSegment ) begin-verbatim
! 71:
! 72: state 0 -- / * & unknown ---> state 2
! 73: <--- * / --- state 2
! 74:
! 75: state 0 & OutputNoTaggedSegment ==> putchar()
! 76: state 1 ==> putchar()
! 77: state 2 ==> skip
! 78: */
! 79: while (notEOF()) {
! 80: /* We are in the state 0 */
! 81: findNextTag(tagc,tagv,tagc2,tagv2);
! 82: /* We are int the state 1 */
! 83: findEndTag(tagc2,tagv2);
! 84: }
! 85: if (BeginVerbatim) {
! 86: printf("\n\\end{verbatim\x07d}\n");
! 87: }
! 88: exit(0);
! 89: }
! 90:
! 91: /*&jp \noindent $B<!$N4X?t$OMxMQK!$rI=<($9$k(B. */
! 92: usage() {
! 93: fprintf(stderr,"oxweave [--source] [key1 key2 ...]\n");
! 94: fprintf(stderr,"Example 1: oxweave --source jp <oxweave.c >t.tex\n");
! 95: fprintf(stderr,"Example 2: oxweave jp <oxweave.c >t.tex\n");
! 96: }
! 97:
! 98: #define inc(a) ((a+1) % BSIZE)
! 99: /*&jp \noindent {\tt wread()} $B$O(B $BI8=`F~NO$h$j$N%G!<%?$rFI$a$k$@$1(B
! 100: $B%j%s%0%P%C%U%!(B {\tt Buf] $B$XFI$_9~$`(B.*/
! 101: wread() {
! 102: int c,i;
! 103: static int eof = 0;
! 104: if (eof) return(-1);
! 105: while (inc(Tail) != Head) {
! 106: c = getchar();
! 107: Tail = inc(Tail);
! 108: Buf[Tail] = c;
! 109: if (c == EOF) { eof = 1; return(-1); }
! 110: }
! 111: if (Debug == 1) {
! 112: fprintf(stderr,"[Head=%d, Tail=%d, ",Head,Tail);
! 113: for (i = Head; inc(i) != Tail; i = inc(i)) {
! 114: fprintf(stderr,"%c",Buf[i]);
! 115: }
! 116: fprintf(stderr,"]\n");
! 117: }
! 118: return(0);
! 119: }
! 120:
! 121: /*&jp \noindent {\tt wgetc(p)} $B$O(B
! 122: {\tt p} $BJ8;zFI$_9~$s$G:G8e$N(B 1 $BJ8;z$N%"%9%-!<%3!<%I$rLa$9(B.
! 123: */
! 124: int wgetc(int p) {
! 125: int c;
! 126: int i;
! 127: wread();
! 128: if (p < 1) p = 1;
! 129: for (i=0; i<p; i++) {
! 130: c = Buf[Head];
! 131: if (c == EOF) return(c);
! 132: Head = inc(Head);
! 133: wread();
! 134: }
! 135: return(c);
! 136: }
! 137:
! 138: /*&jp \noindent {\tt findNextTag()} $B$O<!$N(B {\tt /\*\&} $B$J$k%?%0$r$5$,$9(B.
! 139: {\tt OutputNoTaggedSegment} $B$,(B 1 $B$J$i%G!<%?$r$=$N$^$^$J$,$9(B.
! 140: $BL5;k$9$Y$-%?%0$N$H$-$O(B, $B%?%0FbIt$r%9%-%C%W$7$?$N$A(B
! 141: {\tt findNextTag} $B$r:F5"E*$K8F$V(B.
! 142: */
! 143: findNextTag(int tagc, char *tagv[],int tagc2,char *tagv2[]) {
! 144: int i;
! 145: int c,d;
! 146: do {
! 147: for (i=0; i<tagc; i++) {
! 148: if (wcmp(tagv[i]) == 0) {
! 149: wgetc(strlen(tagv[i]));
! 150: if (OutputNoTaggedSegment == 1 && BeginVerbatim == 1) {
! 151: BeginVerbatim = 0;
! 152: printf("\\end{verbatim\x07d}\n");
! 153: }
! 154: OutputtingTaggedSegment = 1;
! 155: return; /* Now, state is 1. */
! 156: }
! 157: }
! 158: /*&jp {\tt /\*\&} $B$@$1$I$I$N%?%0$K$b0lCW$7$J$$(B */
! 159: if (wcmp("/*\x026") == 1) {
! 160: wgetc(3);
! 161: while ((d=wgetc(1)) > ' ') ;
! 162: /* We are in the state 2. */
! 163: skipToEndTag(tagc2,tagv2);
! 164: /* We are in the state 0. */
! 165: findNextTag(tagc,tagv,tagc2,tagv2);
! 166: return;
! 167: }
! 168: /* We are in the state 0 */
! 169: c = wgetc(1);
! 170: if (OutputNoTaggedSegment) {
! 171: putchar(c);
! 172: }
! 173: }while( c!= EOF);
! 174: if (BeginVerbatim == 1) {
! 175: printf("\n\\quad\n\\end{verbatim\x07d}\n");
! 176: }
! 177: exit(0);
! 178: }
! 179:
! 180: /*&jp \noindent {\tt findEndTag()} $B$O<!$N(B {\tt \*\/} $B$J$k%?%0$r$5$,$9(B.
! 181: */
! 182: findEndTag(int tagc,char *tagv[]) {
! 183: int i;
! 184: int c;
! 185: /* We are in the state 1. */
! 186: do {
! 187: for (i=0; i<tagc; i++) {
! 188: if (wcmp(tagv[i]) == 0) {
! 189: wgetc(strlen(tagv[i]));
! 190: OutputtingTaggedSegment = 0;
! 191: if (OutputNoTaggedSegment) {
! 192: printf("\n{\\footnotesize \\begin{verbatim}\n");
! 193: BeginVerbatim = 1;
! 194: }
! 195: return; /* Our state is 0. */
! 196: }
! 197: }
! 198: /* Our state is 1. */
! 199: c = wgetc(1);
! 200: putchar(c);
! 201: }while( c!= EOF);
! 202: fprintf(stderr,"findEndTag: unexpected EOF.\n");
! 203: irregularExit();
! 204: }
! 205:
! 206: skipToEndTag(int tagc,char *tagv[]) {
! 207: int i;
! 208: int c;
! 209: /* our state is 2. */
! 210: do {
! 211: for (i=0; i<tagc; i++) {
! 212: if (wcmp(tagv[i]) == 0) {
! 213: wgetc(strlen(tagv[i]));
! 214: return; /* our state is 0. */
! 215: }
! 216: }
! 217: /* our state is 2. */
! 218: c = wgetc(1);
! 219: }while( c!= EOF);
! 220: fprintf(stderr,"findEndTag: unexpected EOF.\n");
! 221: irregularExit();
! 222: }
! 223:
! 224: /*&jp \noindent {\tt wcmp(s)} $B$OJ8;zNs(B {\tt s} $B$H(B {\tt Buf[Head]} $B$+$i(B
! 225: $B$O$8$^$kJ8;zNs$rHf3S$9$k(B.
! 226: {\tt Buf[Head+strlen(s) % BSIZE]} $B$,(B 0x20 $B0J2<$G$"$j(B, $B$"$H$N%P%$%H$,(B
! 227: $B0lCW$9$l$P(B 0 $B$rLa$9(B.
! 228: $B$"$H$N%P%$%H$,(B 0x20 $B0J2<$G$J$$$,(B, $B$=$NB>$N%P%$%H$,0lCW$9$k$H$-$O(B
! 229: 1 $B$rLa$9(B.
! 230: $B0J>eFs$D$N>l9g$K9gCW$7$J$$>l9g$O(B -1 $B$rLa$9(B.
! 231: */
! 232: wcmp(char *s) {
! 233: int n;
! 234: int i,j;
! 235: wread();
! 236: if (Debug == 2) fprintf(stderr,"[Checking %s]\n",s);
! 237: n = strlen(s);
! 238: j = Head;
! 239: for (i=0; i<n; i++) {
! 240: if (s[i] != Buf[j]) return(-1);
! 241: j = inc(j);
! 242: }
! 243: if (Buf[j] <= ' ') {
! 244: if (Debug == 2) fprintf(stderr,"[Matched %s]\n",s);
! 245: return(0);
! 246: } else return(1);
! 247: }
! 248:
! 249: notEOF() {
! 250: wread();
! 251: if (Buf[Head] != -1) return(1);
! 252: else return(0);
! 253: }
! 254:
! 255: irregularExit() {
! 256: if (BeginVerbatim == 1) {
! 257: printf("\\end{verbatim\x07d}\n");
! 258: }
! 259: exit(-1);
! 260: }
! 261:
! 262: /*&jp
! 263: \end{document}
! 264: */
! 265:
! 266:
! 267:
! 268:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>