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