[BACK]Return to gentexi.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / asir-contrib / packages / doc

Annotation of OpenXM/src/asir-contrib/packages/doc/gentexi.c, Revision 1.2

1.2     ! takayama    1:  /*  $OpenXM: OpenXM/src/asir-contrib/packages/doc/gentexi.c,v 1.1 2002/01/27 07:13:28 takayama Exp $  */
1.1       takayama    2:
                      3: #include <stdio.h>
                      4: int Debug = 0;
                      5: #define VMAX 20
                      6: #define LIMIT 1024
                      7: #define ITEMMAX 1024
                      8: struct item {
                      9:   char *category;  /* base */
                     10:   char *category2;  /* taka_base */
                     11:   char *name;      /* base_replace */
                     12:   int argc;
                     13:   char *argv[VMAX];  /* A and Rule of base_replace(A,Rule) */
                     14:   int optc;
                     15:   char *optv[VMAX];
                     16:   char *shortDescription;
                     17:   char *description;
                     18:   char *examplev[VMAX];
                     19:   int examplec;
                     20:   int refc;
                     21:   char *refv[VMAX];
                     22: };
                     23: struct item *getItem(void);
                     24: char *str(char *key);
                     25: char *str2(char *key,int size);
                     26: int cmpItem(struct item *it,struct item *it2);
                     27:
                     28: char *S;
                     29: int Ssize = 256;
                     30: int Sp = 0;
                     31: char *Upnode;
                     32: char *Category=NULL;
                     33: char *Lang="en";
1.2     ! takayama   34: int Include = 0;
        !            35: int GenExample = 0;
1.1       takayama   36:
                     37: main(int argc,char *argv[]) {
                     38:   char *t;
                     39:   int p,c,n,i;
                     40:   struct item *tt;
                     41:   struct item *items[ITEMMAX];
                     42:
                     43:   Upnode = str("UNKNOWN");
                     44:   for (i=1; i<argc; i++) {
                     45:     if (strcmp(argv[i],"--upnode") == 0) {
                     46:       i++; if (i >= argc) { fprintf(stderr,"--upnode node-name\n"); exit(1);}
                     47:       Upnode = str(argv[i]);
                     48:     }else if (strcmp(argv[i],"--category") == 0) {
                     49:       i++; if (i >= argc) { fprintf(stderr,"--category category-name\n"); exit(1);}
                     50:       Category = str(argv[i]);
                     51:     }else if (strcmp(argv[i],"--en") == 0) {
                     52:       Lang = "en";
                     53:     }else if (strcmp(argv[i],"--ja") == 0) {
                     54:       Lang = "ja";
1.2     ! takayama   55:     }else if (strcmp(argv[i],"--include") == 0) {
        !            56:       Include = 1;
        !            57:     }else if (strcmp(argv[i],"--example") == 0) {
        !            58:       GenExample = 1;
1.1       takayama   59:     }else if (strcmp(argv[i],"--debug") == 0) {
                     60:       Debug = 1;
                     61:     }else {
                     62:       fprintf(stderr,"Unknown option\n"); exit(1);
                     63:     }
                     64:   }
                     65:   S = (char *)malloc(Ssize);
                     66:   /* Read data from stdin to the string buffer S */
                     67:   while ((c=getchar()) != EOF) {
                     68:     S[Sp++] = c; S[Sp] = 0;
                     69:     if (Sp >= Ssize-3) {
                     70:       Ssize = 2*Ssize;
                     71:       t = S;
                     72:       S = (char *)malloc(Ssize);
                     73:       if (S == NULL) {
                     74:         fprintf(stderr,"No more memory to allocate S.\n");
                     75:         exit(20);
                     76:       }
                     77:       strcpy(S,t);
                     78:     }
                     79:   }
                     80:
                     81:   /* Read items */
                     82:   n = 0;
                     83:   while ((tt = getItem()) != NULL) {
                     84:     if (Debug) printItem(tt);
                     85:     if (n >= ITEMMAX) {
                     86:       fprintf(stderr,"Too many entries.\n"); exit(1);
                     87:     }
                     88:     if (Category != NULL) {
                     89:       if (strcmp(Category,tt->category) == 0 ||
                     90:           strcmp(Category,tt->category2) == 0) {
                     91:         items[n++] = tt;
                     92:       }
                     93:     }else{
                     94:       items[n++] = tt;
                     95:     }
                     96:   }
                     97:   if (Debug) fprintf(stderr,"Sorting...\n");
                     98:   shell(items,n);
                     99:   if (Debug) fprintf(stderr,"Done.\n");
                    100:
                    101:   for (i=0; i<n; i++) {
                    102:     printTexi(stdout,items[i]);
                    103:   }
1.2     ! takayama  104:   exit(0);
1.1       takayama  105: }
                    106:
1.2     ! takayama  107: genInclude(char *name) {
1.1       takayama  108:   char fname[4098];
                    109:   FILE *fp;
1.2     ! takayama  110:   int c;
1.1       takayama  111:
                    112:   sprintf(fname,"tmp/%s-auto-%s.texi",name,Lang);
1.2     ! takayama  113:   fp = fopen(fname,"r");
        !           114:   if (fp == NULL) {
        !           115:     fprintf(stderr,"No file %s\n",fname);
        !           116:     return 0;
        !           117:   }
        !           118:   while ((c=fgetc(fp)) != EOF) {
        !           119:     putchar(c);
        !           120:   }
        !           121:   putchar('\n');
        !           122:   fclose(fp);
        !           123:   return 0;
1.1       takayama  124: }
                    125:
                    126: cmpItem(struct item *it,struct item *it2) {
                    127:   return strcmp(it->name,it2->name);
                    128: }
                    129: struct item * newItem(){
                    130:   struct item *a;
                    131:   a = (struct item *)malloc(sizeof(struct item));
                    132:   if (a == NULL) {
                    133:     fprintf(stderr,"newItem: No more memory.\n");
                    134:     exit(20);
                    135:   }
                    136:   a->argc = 0; a->optc = 0; a->refc=0; a->examplec = 0;
                    137:   a->category = a->category2 = a->name = a->shortDescription
                    138:     = a->description = NULL;
                    139:   return a;
                    140: }
                    141:
                    142: nextToken(char *key,int n) {
                    143:   static int pos = 0;
                    144:   int i = 0;
                    145:   if (pos >= Ssize) return -1;
                    146:   while (S[pos] <= ' ') {
                    147:     pos++;
                    148:     if (pos >= Ssize) return -1;
                    149:   }
                    150:   while (S[pos] > ' ') {
                    151:     key[i++] = S[pos++]; key[i] = 0;
                    152:     if (i >= n-1) {
                    153:       fprintf(stderr,"Too big key word.\n");
                    154:       fprintf(stderr,"key=%s\n",key);
                    155:       exit(10);
                    156:     }
                    157:     if (S[pos-1] == '(' ||
                    158:         S[pos-1] == ')' ||
                    159:         S[pos-1] == ',' ||
                    160:         S[pos-1] == '{' ||
                    161:         S[pos-1] == '}' ||
                    162:         S[pos-1] == '|' ) {
                    163:       return pos;
                    164:     }
                    165:     if (S[pos] == '(' ||
                    166:         S[pos] == ')' ||
                    167:         S[pos] == ',' ||
                    168:         S[pos] == '{' ||
                    169:         S[pos] == '}' ||
                    170:         S[pos] == '|' ) {
                    171:       return pos;
                    172:     }
                    173:
                    174:   }
                    175:   if (Debug) fprintf(stderr,"token=%s\n",key);
                    176:   return pos;
                    177: }
                    178:
                    179: printItem(struct item *it) {
                    180:   int i;
                    181:   if (it == NULL) return;
                    182:   if (it->category != NULL)
                    183:     printf("category=%s\n",it->category);
                    184:   if (it->category2 != NULL)
                    185:     printf("category2=%s\n",it->category2);
                    186:   if (it->name != NULL)
                    187:     printf("name=%s\n",it->name);
                    188:   for (i=0; i<it->argc; i++)
                    189:     printf("  argv[%d]=%s\n",i,it->argv[i]);
                    190:   for (i=0; i<it->optc; i++)
                    191:     printf("  optv[%d]=%s\n",i,it->optv[i]);
                    192:   if (it->shortDescription != NULL)
                    193:     printf("shortDescription=%s\n",it->shortDescription);
                    194:   if (it->description != NULL)
                    195:     printf("description=%s\n",it->description);
                    196:   for (i=0; i <it->examplec; i++)
                    197:     printf("examplev[%d]=%s\n",i,it->examplev[i]);
                    198:   for (i=0; i<it->refc; i++)
                    199:     printf("  refv[%d]=%s\n",i,it->refv[i]);
                    200:   printf("\n");
                    201: }
                    202:
                    203: char *str(char *key) {
                    204:   char *s;
                    205:   s = (char *)malloc(strlen(key)+1);
                    206:   if (s == NULL) {
                    207:     fprintf(stderr,"str: No more memory.\n");
                    208:     exit(20);
                    209:   }
                    210:   strcpy(s,key);
                    211:   return s;
                    212: }
                    213: char *str2(char *key,int size) {
                    214:   char *s;
                    215:   int i;
                    216:   s = (char *)malloc(size+1);
                    217:   if (s == NULL) {
                    218:     fprintf(stderr,"str2: No more memory.\n");
                    219:     exit(20);
                    220:   }
                    221:   for (i=0; i<size; i++) {
                    222:     s[i] = key[i]; s[i+1] = 0;
                    223:   }
                    224:   return s;
                    225: }
                    226: char *getCategory(char *key) {
                    227:   int i,n;
                    228:   char *s;
                    229:   s = str(key);
                    230:   for (i=0; i<strlen(s); i++) {
                    231:     if (s[i] == '_') {
                    232:       s[i] = 0;
                    233:       return s;
                    234:     }
                    235:   }
                    236:   return s;
                    237: }
                    238: char *getCategory2(char *key) {
                    239:   int i,n;
                    240:   char *s;
                    241:   int count;
                    242:   s = str(key);
                    243:   for (i=0; i<strlen(s); i++) {
                    244:     if (s[i] == '_') count++;
                    245:     if (count == 2) {
                    246:       s[i] = 0; return s;
                    247:     }
                    248:   }
                    249:   return s;
                    250: }
                    251:
                    252:
                    253: struct item *getItem() {
                    254:   char key[LIMIT];
                    255:   char key2[LIMIT];
                    256:   struct item *it;
                    257:   int p;
                    258:   int pp,pOld;
                    259:   int argc;
                    260:   int examplec = 0;
                    261:   it = newItem();
                    262:   do {
                    263:     p = nextToken(key,LIMIT);
                    264:     /* printf("%s\n",key); */
                    265:     if (strcmp(key,"begin:") == 0) break;
                    266:   }while (p >= 0);
                    267:   if (p < 0) {
                    268:     fprintf(stderr,"gentexi: End of input file.\n");
                    269:     return NULL;
                    270:   }
                    271:   p = nextToken(key,LIMIT);
                    272:   it->name = str(key);
                    273:   it->category = getCategory(key);
                    274:   it->category2 = getCategory2(key);
                    275:   nextToken(key,LIMIT);
                    276:   if (strcmp(key,"(") != 0) {
                    277:     fprintf(stderr," ( is expected at %s\n",it->name);
                    278:     exit(10);
                    279:   }
                    280:   argc = 0;
                    281:   while ((pp=nextToken(key,LIMIT)) >= 0) {
                    282:     if (strcmp(key,"|") == 0) {
                    283:       /* options */
                    284:       argc = 0;
                    285:       while ((pp=nextToken(key,LIMIT)) >= 0) {
                    286:         if (strcmp(key,")") == 0) {
                    287:           break;
                    288:         }
                    289:         if (strcmp(key,",") != 0) {
                    290:           it->optv[argc] = str(key);
                    291:           argc++; it->optc = argc;
                    292:         }
                    293:         if (argc >+ VMAX -1) {
                    294:           fprintf(stderr,"Too many opt args at %s\n",it->name);
                    295:           exit(10);
                    296:         }
                    297:       }
                    298:     }
                    299:     if (strcmp(key,")") == 0) {
                    300:       break;
                    301:     }else if (strcmp(key,",") != 0) {
                    302:       it->argv[argc] = str(key);
                    303:       argc++; it->argc=argc;
                    304:     }
                    305:     if (argc >= VMAX-1) {
                    306:       fprintf(stderr,"Too many args at %s\n",it->name);
                    307:       exit(10);
                    308:     }
                    309:   }
                    310:
                    311:   /* Getting the short Description */
                    312:   p = pp;
                    313:   do {
                    314:     pOld = p;
                    315:     p = nextToken(key,LIMIT);
                    316:     /* printf("%s\n",key); */
                    317:     if (key[strlen(key)-1] == ':') break; /* Next keyword. */
                    318:   }while (p >= 0);
                    319:   it->shortDescription = str2(&(S[pp]),pOld-pp);
                    320:
                    321:
                    322:   do {
                    323:     /* Get Description or Examples */
                    324:     if (strcmp(key,"end:") == 0) break;
                    325:     if (strcmp(key,"description:") == 0 ||
                    326:         strcmp(key,"example:") == 0) {
                    327:       pp = p;
                    328:       strcpy(key2,key);
                    329:       do {
                    330:         pOld = p;
                    331:         p = nextToken(key,LIMIT);
                    332:         /* printf("%s\n",key); */
                    333:         if (key[strlen(key)-1] == ':') break; /* Next keyword. */
                    334:       }while (p >= 0);
                    335:       if (strcmp(key2,"description:") == 0) {
                    336:         it->description = str2(&(S[pp]),pOld-pp);
                    337:       }
                    338:       if (strcmp(key2,"example:") == 0) {
                    339:         it->examplev[examplec++] = str2(&(S[pp]),pOld-pp);
                    340:         it->examplec = examplec;
                    341:         if (examplec > VMAX-1) {
                    342:           fprintf(stderr,"Too many examples. \n");
                    343:           exit(20);
                    344:         }
                    345:       }
                    346:     }else if (strcmp(key,"ref:") == 0) {
                    347:       argc = 0;
                    348:       while ((pp=nextToken(key,LIMIT)) >= 0) {
                    349:         p = pp;
                    350:         if (key[strlen(key)-1] == ':') break;
                    351:         if (strcmp(key,",") != 0) {
                    352:           it->refv[argc] = str(key);
                    353:           argc++; it->refc = argc;
                    354:         }
                    355:         if (argc >= VMAX-1) {
                    356:           fprintf(stderr,"Too many args for Ref at %s\n",it->name);
                    357:           exit(10);
                    358:         }
                    359:       }
                    360:     }else{
                    361:       fprintf(stderr,"Unknown keyword at %s\n",it->name);
                    362:       exit(10);
                    363:     }
                    364:   }while (p >= 0);
                    365:
                    366:   return it;
                    367: }
                    368:
                    369: shell(struct item *v[],int n) {
                    370:   int gap,i,j;
                    371:   struct item *temp;
                    372:
                    373:   for (gap = n/2; gap > 0; gap /= 2) {
                    374:     for (i = gap; i<n; i++) {
                    375:       for (j=i-gap ; j>=0 && cmpItem(v[j],v[j+gap])>0 ; j -= gap) {
                    376:         temp = v[j];
                    377:         v[j] = v[j+gap];
                    378:         v[j+gap] = temp;
                    379:       }
                    380:     }
                    381:   }
                    382: }
                    383:
                    384: printTexi(FILE *fp, struct item *it) {
                    385:   int i;
                    386:   fprintf(fp,"@c DO NOT EDIT THIS FILE. Generated by gentexi.\n");
                    387:   if (it == NULL) {
                    388:     fprintf(fp,"@c item is NULL.\n");
                    389:     return ;
                    390:   }
                    391:   if (it->name == NULL) {
                    392:     fprintf(fp,"@c item name is missing.\n");
                    393:     return ;
                    394:   }
                    395:
                    396:   fprintf(fp,"@menu\n");
                    397:   fprintf(fp,"* %s\n",it->name);
                    398:   fprintf(fp,"@end menu\n");
                    399:   fprintf(fp,"@node %s,,, %s\n",it->name,Upnode);
                    400:   fprintf(fp,"@subsection @code{%s}\n",it->name);
                    401:   fprintf(fp,"@findex %s\n",it->name);
                    402:   fprintf(fp,"@table @t\n");
                    403:   fprintf(fp,"@item %s(",it->name);
                    404:   for (i=0; i<it->argc; i++) {
                    405:     fprintf(fp,"@var{%s}",it->argv[i]);
                    406:     if (i != it->argc-1) fprintf(fp,",");
                    407:   }
                    408:   fprintf(fp,")\n");
                    409:   if (it->shortDescription != NULL) {
                    410:     fprintf(fp,": ");
                    411:     for (i=0; i<strlen(it->shortDescription); i++) {
                    412:       if (it->shortDescription[i] == '{') {
                    413:         fprintf(fp,"@var{");
                    414:       }else {
                    415:         fprintf(fp,"%c",it->shortDescription[i]);
                    416:       }
                    417:     }
                    418:     fprintf(fp," \n");
                    419:   }
                    420:   if (it->optc > 0) {
                    421:     fprintf(fp,"@item %s(",it->name);
                    422:     for (i=0; i<it->argc; i++) {
                    423:       fprintf(fp,"@var{%s}",it->argv[i]);
                    424:       if (i != it->argc-1) fprintf(fp,",");
                    425:     }
                    426:     fprintf(fp," | ");
                    427:     for (i=0; i<it->optc; i++) {
                    428:       fprintf(fp,"@var{%s}=key%d",it->optv[i],i);
                    429:       if (i != it->optc-1) fprintf(fp,",");
                    430:     }
                    431:     fprintf(fp,")\n");
                    432:     fprintf(fp,": This function allows optional variables \n  ");
                    433:     for (i=0; i<it->optc; i++) {
                    434:       fprintf(fp,"@var{%s}",it->optv[i]);
                    435:       if (i != it->optc-1) fprintf(fp,", ");
                    436:     }
                    437:     fprintf(fp,"\n");
                    438:   }
                    439:   fprintf(fp,"@end table\n");
                    440:
                    441:   /* include file */
1.2     ! takayama  442:   if (Include) {
        !           443:     if (genInclude(it->name))
        !           444:       fprintf(fp,"@c @include tmp/%s-auto-en.texi\n",it->name);
        !           445:   }
1.1       takayama  446:   fprintf(fp,"@c @itemize @bullet \n");
                    447:   fprintf(fp,"@c @item \n");
                    448:   fprintf(fp,"@c @end itemize\n");
                    449:
                    450:   if (it->examplec > 0) {
                    451:     for (i=0; i<it->examplec; i++) {
                    452:       fprintf(fp,"@example\n");
                    453:       fprintf(fp,"%s\n",it->examplev[i]);
1.2     ! takayama  454:       if (GenExample) {
        !           455:         outputOfExample(it->examplev[i]);
        !           456:       }
1.1       takayama  457:       fprintf(fp,"@end example\n");
                    458:     }
                    459:   }
                    460:   if (it->refc > 0) {
                    461:     fprintf(fp,"@table @t\n");
                    462:     fprintf(fp,"@item References\n");
                    463:     for (i=0; i <it->refc; i++) {
                    464:       fprintf(fp,"@code{%s} ",it->refv[i]);
                    465:       if (i != it->refc-1) fprintf(fp,", ");
                    466:     }
                    467:     fprintf(fp,"\n@end table\n");
                    468:   }
                    469:   fprintf(fp,"\n");
                    470: }
                    471:
1.2     ! takayama  472: outputOfExample(char *com) {
        !           473:   FILE *fp2;
        !           474:   int c;
        !           475:   fp2 = fopen("gentexi-in.tmp","w");
        !           476:   if (fp2 == NULL) {
        !           477:     fprintf(stderr,"Cannot open tentexi-in.tmp\n");
        !           478:     exit(10);
        !           479:   }
        !           480:   system("rm -f gentexi-out.tmp");
        !           481:   fprintf(fp2,"output(\"gentexi-out.tmp\")$\n");
        !           482:   fprintf(fp2,"%s\n",com);
        !           483:   fprintf(fp2,"output()$\n");
        !           484:   fprintf(fp2,"quit;");
        !           485:   fclose(fp2);
        !           486:   system("asir <gentexi-in.tmp >/dev/null");
        !           487:
        !           488:   fp2 = fopen("gentexi-out.tmp","r");
        !           489:   if (fp2 == NULL) {
        !           490:     fprintf(stderr,"Cannot open tentexi-in.tmp\n");
        !           491:     exit(10);
        !           492:   }
        !           493:   while ((c=fgetc(fp2)) != EOF) {
        !           494:     putchar(c);
        !           495:   }
        !           496:   putchar('\n');
        !           497: }

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