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