[BACK]Return to oxweave.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / util

Annotation of OpenXM/src/util/oxweave.c, Revision 1.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>