Annotation of OpenXM/src/util/oxgentexi.c, Revision 1.2
1.2 ! takayama 1: /* $OpenXM: OpenXM/src/util/oxgentexi.c,v 1.1 2005/04/04 06:10:58 takayama Exp $ */
1.1 takayama 2:
3: #include <stdio.h>
4: int Debug = 0;
5: #define VMAX 20
1.2 ! takayama 6: #define LIMIT 65536
1.1 takayama 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 *algorithm;
19: char *examplev[VMAX];
20: char *exampleDescv[VMAX];
21: int examplec;
22: int refc;
23: char *refv[VMAX];
24: char *author;
1.2 ! takayama 25: char *sortKey;
! 26: int type;
1.1 takayama 27: };
28: struct item *getItem(void);
29: char *str(char *key);
30: char *str2(char *key,int size);
31: int cmpItem(struct item *it,struct item *it2);
32:
33: char *S;
34: int Ssize = 256;
35: int Sp = 0;
36: char *Upnode;
37: char *Category=NULL;
38: char *Lang="en";
39: int Include = 0;
40: int GenExample = 0;
1.2 ! takayama 41: int DebugItem = 0;
1.1 takayama 42:
43: main(int argc,char *argv[]) {
44: char *t;
45: int p,c,n,i;
46: struct item *tt;
47: struct item *items[ITEMMAX];
48:
49: Upnode = str("UNKNOWN");
50: for (i=1; i<argc; i++) {
51: if (strcmp(argv[i],"--upnode") == 0) {
52: i++; if (i >= argc) { fprintf(stderr,"--upnode node-name\n"); exit(1);}
53: Upnode = str(argv[i]);
54: }else if (strcmp(argv[i],"--category") == 0) {
55: i++; if (i >= argc) { fprintf(stderr,"--category category-name\n"); exit(1);}
56: Category = str(argv[i]);
57: }else if (strcmp(argv[i],"--en") == 0) {
58: Lang = "en";
59: }else if (strcmp(argv[i],"--ja") == 0) {
60: Lang = "ja";
61: }else if (strcmp(argv[i],"--include") == 0) {
62: Include = 1;
63: }else if (strcmp(argv[i],"--example") == 0) {
64: GenExample = 1;
65: }else if (strcmp(argv[i],"--debug") == 0) {
66: Debug = 1;
1.2 ! takayama 67: }else if (strcmp(argv[i],"--debugItem") == 0) {
! 68: DebugItem = 1;
1.1 takayama 69: }else {
70: fprintf(stderr,"Unknown option\n"); exit(1);
71: }
72: }
73: S = (char *)malloc(Ssize);
74: /* Read data from stdin to the string buffer S */
75: while ((c=getchar()) != EOF) {
76: S[Sp++] = c; S[Sp] = 0;
77: if (Sp >= Ssize-3) {
78: Ssize = 2*Ssize;
79: t = S;
80: S = (char *)malloc(Ssize);
81: if (S == NULL) {
82: fprintf(stderr,"No more memory to allocate S.\n");
83: exit(20);
84: }
85: strcpy(S,t);
86: }
87: }
88:
89: /* Read items */
90: n = 0;
91: while ((tt = getItem()) != NULL) {
92: if (Debug) printItem(tt);
93: if (n >= ITEMMAX) {
94: fprintf(stderr,"Too many entries.\n"); exit(1);
95: }
96: if (Category != NULL) {
97: if (strcmp(Category,tt->category) == 0 ||
98: strcmp(Category,tt->category2) == 0) {
99: items[n++] = tt;
100: }
101: }else{
102: items[n++] = tt;
103: }
104: }
105: if (Debug) fprintf(stderr,"Sorting...\n");
106: shell(items,n);
107: if (Debug) fprintf(stderr,"Done.\n");
1.2 ! takayama 108:
! 109: if (DebugItem) {
! 110: for (i=0; i<n; i++) {
! 111: printItem(items[i]);
! 112: }
! 113: exit(0);
! 114: }
! 115:
1.1 takayama 116: printMenu(stdout,items,n);
117:
118: for (i=0; i<n; i++) {
119: printTexi(stdout,items[i]);
120: }
121: exit(0);
122: }
123:
124: genInclude(char *name) {
125: char fname[4098];
126: FILE *fp;
127: int c;
128:
129: sprintf(fname,"tmp/%s-auto-%s.texi",name,Lang);
130: fp = fopen(fname,"r");
131: if (fp == NULL) {
132: /* fprintf(stderr,"No file %s\n",fname); */
133: return 0;
134: }
135: while ((c=fgetc(fp)) != EOF) {
136: putchar(c);
137: }
138: putchar('\n');
139: fclose(fp);
140: return 0;
141: }
142:
143: cmpItem(struct item *it,struct item *it2) {
1.2 ! takayama 144: return strcmp(it->sortKey,it2->sortKey);
1.1 takayama 145: }
146: struct item * newItem(){
147: struct item *a;
148: a = (struct item *)malloc(sizeof(struct item));
149: if (a == NULL) {
150: fprintf(stderr,"newItem: No more memory.\n");
151: exit(20);
152: }
153: a->argc = 0; a->optc = 0; a->refc=0; a->examplec = 0;
1.2 ! takayama 154: a->type=0;
1.1 takayama 155: a->category = a->category2 = a->name = a->shortDescription
1.2 ! takayama 156: = a->description = a->author = a->algorithm = a->sortKey = NULL;
1.1 takayama 157: return a;
158: }
159:
160: nextToken(char *key,int n) {
161: static int pos = 0;
162: int i = 0;
163: if (pos >= Ssize) return -1;
164: while (S[pos] <= ' ') {
165: pos++;
166: if (pos >= Ssize) return -1;
167: }
168: while (S[pos] > ' ') {
169: key[i++] = S[pos++]; key[i] = 0;
170: if (i >= n-1) {
171: fprintf(stderr,"Too big key word.\n");
172: fprintf(stderr,"key=%s\n",key);
173: exit(10);
174: }
175: if (S[pos-1] == '(' ||
176: S[pos-1] == ')' ||
177: S[pos-1] == ',' ||
178: S[pos-1] == '{' ||
179: S[pos-1] == '}' ||
180: S[pos-1] == '|' ) {
181: return pos;
182: }
183: if (S[pos] == '(' ||
184: S[pos] == ')' ||
185: S[pos] == ',' ||
186: S[pos] == '{' ||
187: S[pos] == '}' ||
188: S[pos] == '|' ) {
189: return pos;
190: }
191:
192: }
193: if (Debug) fprintf(stderr,"token=%s\n",key);
194: return pos;
195: }
196:
197: printItem(struct item *it) {
198: int i;
199: if (it == NULL) return;
200: if (it->category != NULL)
201: printf("category=%s\n",it->category);
202: if (it->category2 != NULL)
203: printf("category2=%s\n",it->category2);
204: if (it->name != NULL)
205: printf("name=%s\n",it->name);
206: for (i=0; i<it->argc; i++)
207: printf(" argv[%d]=%s\n",i,it->argv[i]);
208: for (i=0; i<it->optc; i++)
209: printf(" optv[%d]=%s\n",i,it->optv[i]);
210: if (it->shortDescription != NULL)
211: printf("shortDescription=%s\n",it->shortDescription);
212: if (it->description != NULL)
213: printf("description=%s\n",it->description);
214: if (it->algorithm != NULL)
215: printf("algorithm=%s\n",it->algorithm);
216: for (i=0; i <it->examplec; i++)
217: printf("examplev[%d]=%s\n",i,it->examplev[i]);
218: for (i=0; i <it->examplec; i++)
219: printf("exampleDescv[%d]=%s\n",i,it->exampleDescv[i]);
220: for (i=0; i<it->refc; i++)
221: printf(" refv[%d]=%s\n",i,it->refv[i]);
222: if (it->author != NULL)
223: printf("author=%s\n",it->author);
1.2 ! takayama 224: if (it->sortKey != NULL)
! 225: printf("sortKey=%s\n",it->sortKey);
1.1 takayama 226: printf("\n");
227: }
228:
229: char *str(char *key) {
230: char *s;
231: s = (char *)malloc(strlen(key)+1);
232: if (s == NULL) {
233: fprintf(stderr,"str: No more memory.\n");
234: exit(20);
235: }
236: strcpy(s,key);
237: return s;
238: }
239: char *str2(char *key,int size) {
240: char *s;
241: int i;
242: s = (char *)malloc(size+1);
243: if (s == NULL) {
244: fprintf(stderr,"str2: No more memory.\n");
245: exit(20);
246: }
247: for (i=0; i<size; i++) {
248: s[i] = key[i]; s[i+1] = 0;
249: }
250: return s;
251: }
252: char *getCategory(char *key) {
253: int i,n;
254: char *s;
255: s = str(key);
256: for (i=0; i<strlen(s); i++) {
257: if ((s[i] == '_') || s[i] == '.') {
258: s[i] = 0;
259: return s;
260: }
261: }
262: return s;
263: }
264: char *getCategory2(char *key) {
265: int i,n;
266: char *s;
267: int count;
268: s = str(key);
269: for (i=0; i<strlen(s); i++) {
270: if ((s[i] == '_') || (s[i] == '.')) count++;
271: if (count == 2) {
272: s[i] = 0; return s;
273: }
274: }
275: return s;
276: }
277:
278:
279: struct item *getItem() {
280: char key[LIMIT];
281: char key2[LIMIT];
282: struct item *it;
283: int p;
284: int pp,pOld;
285: int argc;
286: int examplec = 0;
287: it = newItem();
288: do {
289: p = nextToken(key,LIMIT);
290: /* printf("%s\n",key); */
291: if (strcmp(key,"begin:") == 0) break;
292: }while (p >= 0);
293: if (p < 0) {
294: /* fprintf(stderr,"gentexi: End of input file.\n"); */
295: return NULL;
296: }
297: p = nextToken(key,LIMIT);
1.2 ! takayama 298: it->name = it->sortKey = str(key);
1.1 takayama 299: it->category = getCategory(key);
300: it->category2 = getCategory2(key);
301: nextToken(key,LIMIT);
302: if (strcmp(key,"(") != 0) {
1.2 ! takayama 303: pp = p+1;
! 304: it->type = 1; /* For non-functions */
! 305: }else{
! 306: it->type = 0; /* For functions */
! 307: argc = 0;
! 308: while ((pp=nextToken(key,LIMIT)) >= 0) {
! 309: if (strcmp(key,"|") == 0) {
! 310: /* options */
! 311: argc = 0;
! 312: while ((pp=nextToken(key,LIMIT)) >= 0) {
! 313: if (strcmp(key,")") == 0) {
! 314: break;
! 315: }
! 316: if (strcmp(key,",") != 0) {
! 317: it->optv[argc] = str(key);
! 318: argc++; it->optc = argc;
! 319: }
! 320: if (argc >+ VMAX -1) {
! 321: fprintf(stderr,"Too many opt args at %s\n",it->name);
! 322: exit(10);
! 323: }
1.1 takayama 324: }
325: }
1.2 ! takayama 326: if (strcmp(key,")") == 0) {
! 327: break;
! 328: }else if (strcmp(key,",") != 0) {
! 329: it->argv[argc] = str(key);
! 330: argc++; it->argc=argc;
! 331: }
! 332: if (argc >= VMAX-1) {
! 333: fprintf(stderr,"Too many args at %s\n",it->name);
! 334: exit(10);
! 335: }
1.1 takayama 336: }
337: }
338:
339: /* Getting the short Description */
340: p = pp;
341: do {
342: pOld = p;
343: p = nextToken(key,LIMIT);
344: /* printf("%s\n",key); */
345: if (key[strlen(key)-1] == ':') break; /* Next keyword. */
346: }while (p >= 0);
347: it->shortDescription = str2(&(S[pp]),pOld-pp);
348:
349:
350: do {
351: /* Get Description or Examples */
352: if (strcmp(key,"end:") == 0) break;
353: if (strcmp(key,"description:") == 0 ||
354: strcmp(key,"algorithm:") == 0 ||
355: strcmp(key,"author:") == 0 ||
1.2 ! takayama 356: strcmp(key,"sortKey:") == 0 ||
1.1 takayama 357: strcmp(key,"example:") == 0 ||
1.2 ! takayama 358: strcmp(key,"example_description:") ==0 ) {
1.1 takayama 359: pp = p;
360: strcpy(key2,key);
361: do {
362: pOld = p;
363: p = nextToken(key,LIMIT);
364: /* printf("%s\n",key); */
365: if (key[strlen(key)-1] == ':') break; /* Next keyword. */
366: }while (p >= 0);
367: if (strcmp(key2,"description:") == 0) {
368: it->description = str2(&(S[pp]),pOld-pp);
369: }
370: if (strcmp(key2,"example:") == 0) {
371: it->examplev[examplec++] = str2(&(S[pp]),pOld-pp);
372: it->exampleDescv[examplec-1] = "";
373: it->examplec = examplec;
374: if (examplec > VMAX-1) {
375: fprintf(stderr,"Too many examples. \n");
376: exit(20);
377: }
378: }
379: if (strcmp(key2,"example_description:") == 0) {
380: it->exampleDescv[examplec-1] = str2(&(S[pp]),pOld-pp);
381: }
382: if (strcmp(key2,"author:") == 0) {
383: it->author = str2(&(S[pp]),pOld-pp);
384: }
1.2 ! takayama 385: if (strcmp(key2,"sortKey:") == 0) {
! 386: while (S[pp] <= ' ') pp++;
! 387: it->sortKey = str2(&(S[pp]),pOld-pp);
! 388: }
1.1 takayama 389: if (strcmp(key2,"algorithm:") == 0) {
390: it->algorithm = str2(&(S[pp]),pOld-pp);
391: }
392: }else if (strcmp(key,"ref:") == 0) {
393: argc = 0;
394: while ((pp=nextToken(key,LIMIT)) >= 0) {
395: p = pp;
396: if (key[strlen(key)-1] == ':') break;
397: if (strcmp(key,",") != 0) {
398: it->refv[argc] = str(key);
399: argc++; it->refc = argc;
400: }
401: if (argc >= VMAX-1) {
402: fprintf(stderr,"Too many args for Ref at %s\n",it->name);
403: exit(10);
404: }
405: }
406: }else{
407: fprintf(stderr,"Warning: unknown keyword << %s >> at %s. Ignored.\n",key, it->name);
408: p = nextToken(key,LIMIT);
409: }
410: }while (p >= 0);
411:
412: return it;
413: }
414:
415: shell(struct item *v[],int n) {
416: int gap,i,j;
417: struct item *temp;
418:
419: for (gap = n/2; gap > 0; gap /= 2) {
420: for (i = gap; i<n; i++) {
421: for (j=i-gap ; j>=0 && cmpItem(v[j],v[j+gap])>0 ; j -= gap) {
422: temp = v[j];
423: v[j] = v[j+gap];
424: v[j+gap] = temp;
425: }
426: }
427: }
428: }
429:
430: printMenu(FILE *fp, struct item **it, int n) {
1.2 ! takayama 431: int i,m;
1.1 takayama 432:
1.2 ! takayama 433: m = 0;
1.1 takayama 434: for ( i = 0; i < n; i++ )
1.2 ! takayama 435: if (it[i]->type != 1) m++;
! 436: if (m != 0) {
! 437: fprintf(fp,"@menu\n");
! 438: for ( i = 0; i < n; i++ )
! 439: if (it[i]->type != 1) fprintf(fp,"* %s::\n",it[i]->name);
! 440: fprintf(fp,"@end menu\n");
! 441: }
1.1 takayama 442: }
443:
444: printTexi(FILE *fp, struct item *it) {
445: int i;
1.2 ! takayama 446: if (it->type == 1) return printTexi1(fp,it);
! 447: else return printTexi0(fp,it);
! 448: }
! 449:
! 450: printTexi_common(FILE *fp,struct item *it) {
! 451: int i;
! 452: if (it->description != NULL) {
! 453: fprintf(fp,"%s\n\n",it->description);
! 454: }
! 455:
! 456: if (it->algorithm != NULL) {
! 457: fprintf(fp,"\n\n@noindent\nAlgorithm: \n");
! 458: fprintf(fp,"%s\n\n",it->algorithm);
! 459: }
! 460:
! 461: if (it->examplec > 0) {
! 462: for (i=0; i<it->examplec; i++) {
! 463: if (it->examplec == 1) {
! 464: fprintf(fp,"Example:\n");
! 465: }else{
! 466: fprintf(fp,"Example %d:\n",i);
! 467: }
! 468: fprintf(fp,"@example\n");
! 469: fprintf(fp,"%s\n",it->examplev[i]);
! 470: if (GenExample) {
! 471: outputOfExample(it->examplev[i]);
! 472: }
! 473: fprintf(fp,"@end example\n");
! 474: if (it->exampleDescv[i] != NULL && strlen(it->exampleDescv[i]) > 0) {
! 475: fprintf(fp,"%s\n\n",it->exampleDescv[i]);
! 476: }
! 477: }
! 478: }
! 479: if (it->author != NULL) {
! 480: fprintf(fp,"Author : %s\n\n",it->author);
! 481: }
! 482: if (it->refc > 0) {
! 483: fprintf(fp,"@table @t\n");
! 484: fprintf(fp,"@item References\n");
! 485: for (i=0; i <it->refc; i++) {
! 486: fprintf(fp,"@code{%s} ",it->refv[i]);
! 487: if (i != it->refc-1) fprintf(fp,", ");
! 488: }
! 489: fprintf(fp,"\n@end table\n");
! 490: }
! 491: fprintf(fp,"\n");
! 492: }
! 493:
! 494: printTexi0(FILE *fp, struct item *it) {
! 495: int i;
! 496:
! 497: fprintf(fp,"@c DO NOT EDIT THIS FILE. Generated by gentexi0.\n");
1.1 takayama 498: if (it == NULL) {
499: fprintf(fp,"@c item is NULL.\n");
500: return ;
501: }
502: if (it->name == NULL) {
503: fprintf(fp,"@c item name is missing.\n");
504: return ;
505: }
506:
507: #if 0
508: fprintf(fp,"@menu\n");
509: fprintf(fp,"* %s::\n",it->name);
510: fprintf(fp,"@end menu\n");
511: #endif
512: fprintf(fp,"@node %s,,, %s\n",it->name,Upnode);
513: fprintf(fp,"@subsection @code{%s}\n",it->name);
514: fprintf(fp,"@findex %s\n",it->name);
515: fprintf(fp,"@table @t\n");
516: fprintf(fp,"@item %s(",it->name);
517: for (i=0; i<it->argc; i++) {
518: fprintf(fp,"@var{%s}",it->argv[i]);
519: if (i != it->argc-1) fprintf(fp,",");
520: }
521: fprintf(fp,")\n");
522: if (it->shortDescription != NULL) {
523: fprintf(fp,": ");
524: for (i=0; i<strlen(it->shortDescription); i++) {
525: if (it->shortDescription[i] == '{') {
526: fprintf(fp,"@var{");
527: }else {
528: fprintf(fp,"%c",it->shortDescription[i]);
529: }
530: }
531: fprintf(fp," \n");
532: }
533: if (it->optc > 0) {
534: fprintf(fp,"@item %s(",it->name);
535: for (i=0; i<it->argc; i++) {
536: fprintf(fp,"@var{%s}",it->argv[i]);
537: if (i != it->argc-1) fprintf(fp,",");
538: }
539: fprintf(fp," | ");
540: for (i=0; i<it->optc; i++) {
541: fprintf(fp,"@var{%s}=key%d",it->optv[i],i);
542: if (i != it->optc-1) fprintf(fp,",");
543: }
544: fprintf(fp,")\n");
545: fprintf(fp,": This function allows optional variables \n ");
546: for (i=0; i<it->optc; i++) {
547: fprintf(fp,"@var{%s}",it->optv[i]);
548: if (i != it->optc-1) fprintf(fp,", ");
549: }
550: fprintf(fp,"\n");
551: }
552: fprintf(fp,"@end table\n");
553:
554: /* include file */
555: if (Include) {
556: if (genInclude(it->name))
557: fprintf(fp,"@c @include tmp/%s-auto-en.texi\n",it->name);
558: }
559: fprintf(fp,"@c @itemize @bullet \n");
560: fprintf(fp,"@c @item \n");
561: fprintf(fp,"@c @end itemize\n");
562:
1.2 ! takayama 563: printTexi_common(fp,it);
! 564: }
! 565:
! 566: printTexi1(FILE *fp, struct item *it) {
! 567: int i;
! 568: /* For it->type == 1 */
! 569:
! 570: fprintf(fp,"@c DO NOT EDIT THIS FILE. Generated by gentexi1.\n");
! 571: if (it == NULL) {
! 572: fprintf(fp,"@c item is NULL.\n");
! 573: return ;
1.1 takayama 574: }
575:
1.2 ! takayama 576: if (it->shortDescription != NULL) {
! 577: for (i=0; i<strlen(it->shortDescription); i++) {
! 578: fprintf(fp,"%c",it->shortDescription[i]);
1.1 takayama 579: }
1.2 ! takayama 580: fprintf(fp," \n");
1.1 takayama 581: }
1.2 ! takayama 582:
! 583: /* include file */
! 584: if (Include) {
! 585: if (genInclude(it->name))
! 586: fprintf(fp,"@c @include tmp/%s-auto-en.texi\n",it->name);
1.1 takayama 587: }
1.2 ! takayama 588:
! 589: printTexi_common(fp,it);
1.1 takayama 590: }
591:
592: outputOfExample(char *com) {
593: FILE *fp2;
594: int c;
595: fp2 = fopen("gentexi-in.tmp","w");
596: if (fp2 == NULL) {
597: fprintf(stderr,"Cannot open tentexi-in.tmp\n");
598: exit(10);
599: }
600: system("rm -f gentexi-out.tmp");
601: fprintf(fp2,"output(\"gentexi-out.tmp\")$\n");
602: fprintf(fp2,"%s\n",com);
603: fprintf(fp2,"output()$\n");
604: fprintf(fp2,"quit;");
605: fclose(fp2);
606: system("asir <gentexi-in.tmp >/dev/null");
607:
608: fp2 = fopen("gentexi-out.tmp","r");
609: if (fp2 == NULL) {
610: fprintf(stderr,"Cannot open tentexi-in.tmp\n");
611: exit(10);
612: }
613: while ((c=fgetc(fp2)) != EOF) {
614: putchar(c);
615: }
616: putchar('\n');
617: }
618:
619: /* Old file was OpenXM/src/asir-contrib/packages/doc/gentexi.c */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>