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