[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.1

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

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