Annotation of OpenXM/src/util/oxlistusage.c, Revision 1.3
1.3 ! takayama 1: /* $OpenXM: OpenXM/src/util/oxlistusage.c,v 1.2 2017/03/19 12:39:52 takayama Exp $ */
1.1 takayama 2: #include <stdio.h>
3: #include <ctype.h>
4: #include <stdlib.h>
5: #include <sys/types.h>
6: #include <sys/stat.h>
7: #include <string.h>
8: #include "oxlistlocalf.h"
9:
1.2 takayama 10: void putchar0(int c);
11: int fsgetc(FILE *fp);
12: int readchar(void);
13: int isReserved(char *name);
14: void printf0(char *s);
15: int shouldReplace(char *name);
16: void printf1(char *name);
17: int readcomment(void);
18: int readcomment2(void);
19:
20: #define MAX_ITEM 1024*10
1.1 takayama 21: int Mydebug=0;
22:
23: objectp KClval;
24: int Replace = 0;
25: int Linenumber = 0;
26: int Saki = 0;
27: int Debug2 = 0;
28: FILE *Inop = NULL;
1.2 takayama 29: char *Functions[MAX_ITEM];
30: char *Args[MAX_ITEM];
31: char *Usages[MAX_ITEM];
32: int Entries=0;
1.1 takayama 33:
1.2 takayama 34: char *readstring(int endquote);
1.1 takayama 35:
36: void *mymalloc(int n) { return malloc(n); }
37:
38: void myerror(char *s) {
39: fprintf(stderr,"Error: %s\n",s);
40: exit(-1);
41: }
42:
43: objectp newObject_d() {
44: objectp obj;
45: obj = (objectp) mymalloc(sizeof(struct Object));
46: if (obj == (objectp) NULL) {
47: fprintf(stderr,"Error: No more memory in newObject_d().\n");
48: exit(10);
49: }
50: obj->tag = Snull; obj->attr = NULL;
51: return(obj);
52: }
53:
54: char *newString(int size) {
55: char *str;
56: if (size <= 0) size=1;
57: str = (char *)mymalloc(sizeof(char)*size);
58: if (str == (char *)NULL) {
59: fprintf(stderr,"Error: No more memory in newObject_d().\n");
60: exit(10);
61: }
62: return(str);
63: }
64:
65: void printObject_d(FILE *fp,objectp op)
66: {
67: if (op == (objectp) NULL) {
68: fprintf(fp," <null objectp> ");
69: return;
70: }
71: switch(op->tag) {
72: case Sinteger:
73: fprintf(fp,"%d",op->lc.ival);
74: return;
75: break;
76: case Sstring:
77: fprintf(fp,"%s",op->lc.str);
78: return;
79: break;
80: default:
81: fprintf(stderr,"printObject_d(): Sorry. Not implemented.");
82: break;
83: }
84: }
85:
86: void printObjectSymbol(objectp op)
87: {
88: static char tmp[1024];
89: if (op == (objectp) NULL) {
90: printf(" <null objectp> ");
91: return;
92: }
93: switch(op->tag) {
94: case Sinteger:
95: printf("%d",op->lc.ival);
96: return; break;
97: case Sstring:
98: printf("%s",op->lc.str);
99: return; break;
100: default:
101: fprintf(stderr,"(printObjectSymbol(): Sorry. Not implemented.)");
102: break;
103: }
104: }
105:
106: int KClex() {
107: extern int Saki; /* Saki-yomi */
108: int d;
109: int state = 0;
110: int i;
111: static char nametmp[1024];
112: char *name = nametmp;
113: char *str;
114: KClval = (objectp) NULL;
115: while (1) {
116: switch( state ) {
117: case 0:
118: /* printf(" <%x> ",Saki); */
119: if (Saki == EOF) {
120: return(Saki);
121: }
122: if (Saki <= ' ') {
123: if (Saki == '\n') ++Linenumber;
124: if (Replace) putchar0(Saki);
125: Saki = fsgetc(Inop); break;
126: }
127: if ( Saki == '\"' ) {
1.2 takayama 128: str = readstring('\"');
1.1 takayama 129: if (Mydebug) printf("[string: %s]",str);
130:
131: KClval = newObject_d();
132: KClval->tag = Sstring;
133: KClval->lc.str = newString(strlen(str)+1);
134: strcpy(KClval->lc.str,str);
135:
136: Saki = fsgetc(Inop);
137: return(QUOTE);
138: break;
139: }
140: if (Saki == '\'' ) {
1.2 takayama 141: str = readstring('\'');
142: if (strlen(str)==1) d=str[0];
143: else d='E';
1.1 takayama 144: Saki = fsgetc(Inop);
145:
146: KClval = newObject_d();
147: KClval->tag = Sinteger;
148: KClval->lc.ival = d;
149:
150: return(SINGLEQUOTE);
151: }
152: /* single */
153: if ( Saki == '(' || Saki == ')' || Saki == ';' ||
154: Saki == '{' || Saki == '}' || Saki == ',' ||
155: Saki == '[' || Saki == ']' || Saki == '~' ||
156: Saki == '?' || Saki == '.' || Saki == '$') {
157: d = Saki; Saki = fsgetc(Inop);
158: if (Replace) putchar0(d);
159: return(d);
160: }
161:
162: /* single or combination =, == */
163: if ( Saki == '=') {
164: state = 51; Saki = fsgetc(Inop); break;
165: } else if ( Saki == '<' ) {
166: state = 52; Saki = fsgetc(Inop); break;
167: } else if ( Saki == '>' ) {
168: state = 53; Saki = fsgetc(Inop); break;
169: } else if ( Saki == '/' ) {
170: state = 54; Saki = fsgetc(Inop); break;
171: } else if ( Saki == '&' ) {
172: state = 55; Saki = fsgetc(Inop); break;
173: } else if ( Saki == '|' ) {
174: state = 56; Saki = fsgetc(Inop); break;
175: } else if ( Saki == '!' ) {
176: state = 57; Saki = fsgetc(Inop); break;
177: } else if ( Saki == ':' ) {
178: state = 58; Saki = fsgetc(Inop); break;
179: } else if ( Saki == '+' ) {
180: state = 59; Saki = fsgetc(Inop); break;
181: } else if ( Saki == '-' ) {
182: state = 60; Saki = fsgetc(Inop); break;
183: } else if ( Saki == '%' ) {
184: state = 61; Saki = fsgetc(Inop); break;
185: } else if ( Saki == '^' ) {
186: state = 62; Saki = fsgetc(Inop); break;
187: } else if ( Saki == '*' ) {
188: state = 63; Saki = fsgetc(Inop); break;
189: } else ;
190:
191:
192: /* else : Identifier or function names. */
193: name[0] = Saki; i=1; name[i] = '\0';
194: Saki = fsgetc(Inop);
195: if (isdigit(name[0])) {
196: /*while (isdigit(Saki) || isalpha(Saki) || Saki=='.') */
197: while (isdigit(Saki) || isalpha(Saki)) {
198: name[i++] = Saki; name[i] = '\0';
199: Saki = fsgetc(Inop);
200: }
201: }else{
202: while (isdigit(Saki) || isalpha(Saki) || (Saki=='_') ||
203: ( Saki>= 256)) {
204: name[i++] = Saki; name[i] = '\0';
205: Saki = fsgetc(Inop);
206: }
207: }
208: if (Mydebug) printf("identifier string=[%s]",name);
209: if (isdigit(name[0])) {
210: /****************************
1.2 takayama 211: case : machine integer.
1.1 takayama 212: KClval = newObject_d();
213: KClval->tag = Sinteger;
214: sscanf(name,"%d",&(KClval->lc.ival));*************/
215: /* Other cases. */
216: KClval = newObject_d();
217: KClval->tag = Sstring;
218: KClval->lc.str = newString(strlen(name)+1);
219: strcpy(KClval->lc.str,name);
220: return(NUMBER);
221: break;
222: } /* else : Identifier case.*/
223:
1.2 takayama 224: if ((d = isReserved(name))) {
1.1 takayama 225: if (Replace) printf0(name);
226: return(d);
227: } else {
228: if (Replace) {
229: if (shouldReplace(name))
230: printf1(name); /* do your own replacement in printf1*/
231: else
232: printf0(name);
233: }
234: KClval = newObject_d();
235: KClval->tag = Sstring;
236: KClval->lc.str = newString(strlen(name)+1);
237: strcpy(KClval->lc.str,name);
238: return(ID);
239: }
240: break;
241:
242: case 51:
243: if (Replace) putchar0('=');
244: if ( Saki == '=') {
245: if (Replace) putchar0('=');
246: Saki = fsgetc(Inop);state = 0;return(EQUAL); /* == */
247: }else{
248: state = 0;return('=');
249: }
250: break;
251: case 52: /* 52 --- 60 tmporary return values */
252: if (Replace) putchar0('<');
253: if ( Saki == '=') {
254: if (Replace) putchar0('=');
255: Saki = fsgetc(Inop);state = 0;return(LESSEQUAL); /* <= */
256: } else if ( Saki == '<') {
257: if (Replace) putchar0('<');
258: Saki = fsgetc(Inop);state = 0;return(LEFTSHIFT); /* << */
259: }else{
260: state = 0;return('<');
261: }
262: break;
263: case 53:
264: if (Replace) putchar0('>');
265: if ( Saki == '=') {
266: if (Replace) putchar0('=');
267: Saki = fsgetc(Inop);state = 0;return(GREATEREQUAL); /* >= */
268: } else if ( Saki == '>') {
269: if (Replace) putchar0('>');
270: Saki = fsgetc(Inop);state = 0;return(RIGHTSHIFT); /* >> */
271: }else{
272: state = 0;return('>');
273: }
274: break;
275: case 54:
276: if ( Saki == '*') {
277: readcomment();
278: Saki = fsgetc(Inop);state = 0;break; /* clike-comment */
279: }else if (Saki == '/') {
280: readcomment2();
281: Saki = fsgetc(Inop);state = 0;break; /* comment by // */
282: }else {
283: if (Replace) putchar0('/');
284: state = 0;return('/');
285: }
286: break;
287: case 55:
288: if (Replace) putchar0('&');
289: if ( Saki == '&') {
290: if (Replace) putchar0('&');
291: Saki = fsgetc(Inop);state = 0;return(AND); /* && */
292: }else{
293: state = 0;return('&');
294: }
295: break;
296: case 56:
297: if (Replace) putchar0('|');
298: if ( Saki == '|') {
299: if (Replace) putchar0('|');
300: Saki = fsgetc(Inop);state = 0;return(OR); /* || */
301: }else{
302: state = 0;return('|');
303: }
304: break;
305: case 57:
306: if (Replace) putchar0('!');
307: if ( Saki == '=') {
308: if (Replace) putchar0('=');
309: Saki = fsgetc(Inop);state = 0;return(NOTEQUAL); /* != */
310: }else{
311: state = 0;return('!');
312: }
313: break;
314: case 58:
315: if (Replace) putchar0(':');
316: if ( Saki == '=') {
317: if (Replace) putchar0('=');
318: Saki = fsgetc(Inop);state = 0;return(PUT); /* := */
319: }else{
320: state = 0;return(':');
321: }
322: break;
323: case 59:
324: if (Replace) putchar0('+');
325: if ( Saki == '+') {
326: if (Replace) putchar0('+');
327: Saki = fsgetc(Inop);state = 0;return(INCREMENT); /* ++ */
328: }else{
329: state = 0;return('+');
330: }
331: break;
332: case 60:
333: if (Replace) putchar0('-');
334: if ( Saki == '-') {
335: if (Replace) putchar0('-');
336: Saki = fsgetc(Inop);state = 0;return(DECREMENT); /* -- */
337: }else if (Saki == '>') {
338: if (Replace) putchar0('>');
339: Saki = fsgetc(Inop);state = 0;return(MEMBER); /* -> */
340: }else{
341: state = 0;return('-');
342: }
343: break;
344: case 61:
345: if (Replace) putchar0('%');
346: if ( Saki == '=') {
347: if (Replace) putchar0('=');
348: Saki = fsgetc(Inop);state = 0;return(RESIDUEPUT); /* %= */
349: }else{
350: state = 0;return('%');
351: }
352: break;
353: case 62:
354: if (Replace) putchar0('^');
355: if ( Saki == '=') {
356: if (Replace) putchar0('=');
357: Saki = fsgetc(Inop);state = 0;return(NEGATEPUT); /* ^= */
358: }else{
359: state = 0;return('^');
360: }
361: break;
362: case 63:
363: if (Replace) putchar0('*');
364: if ( Saki == '=') {
365: if (Replace) putchar0('=');
366: Saki = fsgetc(Inop);state = 0;return(MULTPUT); /* *= */
367: }else{
368: state = 0;return('*');
369: }
370: break;
371:
372:
373: default:
374: fprintf(stderr,"%d: Error in KClex().\n",Linenumber);
375: }
376: }
377:
378: }
379:
380:
1.2 takayama 381: int readcomment() {
1.1 takayama 382: int c;
383: while (1) {
384: c = fsgetc(Inop);
385: if (c == EOF) {
386: fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber);
387: return 0;
388: }
389: if (c == '*') {
390: c = fsgetc(Inop);
391: if (c == '/') return 0;
392: }
393: }
394: }
395:
1.2 takayama 396: int readcomment2() {
1.1 takayama 397: int c;
398: while (1) {
399: c = fsgetc(Inop);
400: if (c == EOF) {
401: fprintf(stderr,"%d: Unexpected end of file in a comment.\n",Linenumber);
402: return 0;
403: }
404: if (c == '\n') {
405: return 0;
406: }
407: }
408: }
409:
410:
1.2 takayama 411: char *readstring(int endquote) {
1.1 takayama 412: static char strtmp[1024]; /* temporary */
413: static int size = 1024;
414: static char *str = strtmp;
415: int i=0;
416: int c;
417: static char *strwork;
418:
419:
420: if (Replace) putchar0('\"'); /* output " */
421: while (1) {
422: c = fsgetc(Inop);
423: if (Replace) putchar0(c);
1.2 takayama 424: if (c == endquote) {
1.1 takayama 425: str[i] = '\0';
426: return(str);
427: }
428: if (c == EOF) {
429: fprintf(stderr,"%d: Unexpected end of file in the string.\n",Linenumber);
430: str[i]= '\0';
431: return(str);
432: }
433: if (c == '\\') {
434: c = fsgetc(Inop);
435: if (Replace) {
1.2 takayama 436: putchar0(c);
437: }
438: switch(c) { /* interprete as escape sequence. */
439: case 'n': c='\n'; break;
440: case 't': c='\t'; break;
441: default: break;
1.1 takayama 442: }
443: if (c == EOF) {
444: fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
445: str[i]= '\0';
446: return(str);
447: }
448: }
449:
450: str[i++] = c;
451: if (i >= size-10) {
452: size = size*2;
453: strwork = newString(size);
454: strcpy(strwork,str);
455: str = strwork;
456: }
457: }
458: }
459:
460:
1.2 takayama 461: int readchar() {
1.1 takayama 462: int c;
463: if (Replace) putchar0('\'');
464: c = fsgetc(Inop); /* 'c.' '\.c' */
465: if (Replace) putchar0(c);
466: if ( c == '\\') {
467: c = fsgetc(Inop); /* '\c.' */
468: if (Replace) putchar0(c);
469: if (c == EOF) {
470: fprintf(stderr,"%d: Unexpected end of file in the escape sequence.\n",Linenumber);
471: return(-1);
472: }
473: if (fsgetc(Inop) != '\'') {
474: fprintf(stderr,"%d: Error in single quote string (escape seq)\n",Linenumber);
475: return(c);
476: }
477: if (Replace) putchar0('\'');
478: return(c);
479: }
480:
481: if (fsgetc(Inop) != '\'') {
482: fprintf(stderr,"%d: Error in single quote string\n",Linenumber);
483: }
484: if (Replace) putchar0('\'');
485: return(c);
486: }
487:
1.2 takayama 488: void putchar0(c)
1.1 takayama 489: int c;
490: {
491: if (c > 0) putchar(c);
492: }
493:
1.2 takayama 494: void printf0(s)
1.1 takayama 495: char *s;
496: {
497: int i = 0;
498: while (s[i] != '\0') putchar0(s[i++]);
499: }
500:
1.2 takayama 501: void printf1(s)
1.1 takayama 502: char *s;
503: {
504: int i = 0;
505: /* putchar0('K'); */ /* do your own replacement */
506: while (s[i] != '\0') putchar0(s[i++]);
507: }
508:
1.2 takayama 509: int isReserved(s)
1.1 takayama 510: char *s;
511: {
512: char *r[] = {"auto","break","case","char","const","continue",
513: "default","do","double","else","enum","extern",
514: "float","for","goto","if","int","long","register",
515: "return","short","signed","sizeof","static","struct",
516: "switch","typedef","union","unsigned","volatile",
517: "void","while",
518: "print","module","local","def","sm1","load","Test","special",
1.2 takayama 519: "class","super","final","extends",
1.1 takayama 520: "incetanceVariable","this","new","startOfThisClass",
521: "sizeOfThisClass","PSfor","OutputPrompt"};
522:
523: int val[]= {AUTO, BREAK, CASE, CHAR, CONST, CONTINUE, DEFAULT, DO, DOUBLE,
524: ELSE, ENUM,
525: EXTERN, FLOAT, FOR, GOTO, IF, INT, LONG, REGISTER,
526: RETURN, SHORT, SIGNED, SIZEOF, STATIC, STRUCT, SWITCH,
527: TYPEDEF, UNION,
528: UNSIGNED, VOLATILE, VOID, WHILE,
529: PRINT,MODULE,LOCAL,DEF,SM1,LOAD,TEST,SPECIAL,
1.2 takayama 530: CLASS,SUPER,FINAL,EXTENDS,INCETANCEVARIABLE,
1.1 takayama 531: THIS, NEW, STARTOFTHISCLASS, SIZEOFTHISCLASS,PSFOR,PROMPT};
1.2 takayama 532: int n = 51; /* Length of the list above */
1.1 takayama 533: /* You have to change simple.y, too. */
534:
535: int i;
536: for (i=0; i<n; i++) {
537: if (strcmp(r[i],s) == 0) {
538: if (Debug2) printf("\nReserved word: %s ---\n",s);
539: return(val[i]);
540: }
541: }
542: return(0);
543:
544: }
545:
1.2 takayama 546: int shouldReplace(s)
1.1 takayama 547: char *s;
548: {
549: char *r[] = {"dummy"};
550: int n = 1;
551: int i;
552: for (i=0; i<n; i++) {
553: if (strcmp(r[i],s) == 0) {
554: /* printf("\n--- %s ---\n",s); */
555: return(1);
556: }
557: }
558: return(1); /* change to 0. */
559: }
560:
561:
562:
563: int fsgetc(FILE *fp) {
564: int c;
565: return(fgetc(fp));
566: }
567:
1.2 takayama 568: int oxgOut(char *inFname) {
569: FILE *fp;
570: int i;
571: fp = stdout;
572: for (i=0; i<Entries; i++) {
573: fprintf(fp,"/*&usage begin:\n");
574: fprintf(fp,"%s%s\n",Functions[i],Args[i]);
575: fprintf(fp,"description: %s\n",Usages[i]);
576: fprintf(fp,"end: */\n\n");
577: }
578: return(0);
579: }
1.1 takayama 580:
1.2 takayama 581: int outUsage(char *inFname,int oxg) {
1.1 takayama 582: int c;
1.2 takayama 583: char usage[1024*100];
1.1 takayama 584: char fname[1024];
585: char *args[1024];
1.2 takayama 586: char outbuf[1024*10];
1.1 takayama 587: char *tt;
588: int prevtype;
589: int i;
1.2 takayama 590: Entries=0;
1.1 takayama 591: while ((c=KClex()) != EOF) {
592: if (c == DEF) {
593: c=KClex();
594: if (c != ID) {
1.2 takayama 595: fprintf(stderr,"ID (identifier) is expected, but the token type is %d. See isReserved() in oxlistusage.c\n",c);
1.1 takayama 596: }else {
597: if (Mydebug) printf("ID=%s",(KClval->lc).str);
598: strcpy(fname,(KClval->lc).str); // def ...
599: if (Mydebug) printf("fname=%s,",fname);
600: }
601: prevtype=0;
602: i=0; args[i]=NULL;
603: while (c != '{') {
604: prevtype=c;
605: c=KClex();
606: if (c == QUOTE) {
607: strcpy(usage,(KClval->lc).str);
608: if (Mydebug) printf("usage=%s,",usage);
609: }else if (c == ID) {
610: if (Mydebug) { printf("tag=%d ",KClval->tag); fflush(NULL);}
611: if (KClval->tag == Sstring) {
612: tt = (char *) mymalloc(strlen((KClval->lc).str)+2);
613: if (tt == NULL) myerror("No memory\n");
614: strcpy(tt,(KClval->lc).str);
615: args[i++] = tt;
616: args[i] = NULL;
617: if (i >1024-2) {fprintf(stderr,"def %s: ",fname); myerror("Too many args.\n");
618: }
619: }
620: }
621: if (prevtype == QUOTE) {
1.2 takayama 622: /**/printf("\nFunction: %s(",fname);
623: Functions[Entries]=(char *)mymalloc(strlen(fname)+2);
624: strcpy(Functions[Entries],fname);
625: sprintf(outbuf,"(");
1.1 takayama 626: for (i=0; args[i] != NULL; i++) {
1.2 takayama 627: /**/printf("%s",args[i]);
628: sprintf(&(outbuf[strlen(outbuf)]),"%s",args[i]);
629: if (args[i+1] != NULL) {
630: /**/printf(",");
631: sprintf(&(outbuf[strlen(outbuf)]),",");
632: }
1.1 takayama 633: }
1.2 takayama 634: /**/printf(")\n");
635: sprintf(&(outbuf[strlen(outbuf)]),")");
636: Args[Entries] = (char *)mymalloc(strlen(outbuf)+2);
637: strcpy(Args[Entries],outbuf);
638: /**/printf("Usage: %s\n",usage);
639: Usages[Entries] = (char *)mymalloc(strlen(usage)+2);
640: strcpy(Usages[Entries],usage);
641: Entries++;
1.1 takayama 642: }
643: }
644: }
645: }
1.2 takayama 646: if (oxg) { oxgOut(inFname); Entries=0;}
1.1 takayama 647: return(0);
648: }
1.2 takayama 649:
1.3 ! takayama 650: int show_help() {
! 651: printf("oxlistusage [--oxgentexi --help] filename1 ...\n");
! 652: printf(" See also oxgentexi, keys: Example:\n");
! 653: }
! 654:
1.2 takayama 655: int main(int argc,char *argv[]) {
656: int i;
657: int oxg=0;
658: Inop = stdin;
659: for (i=1; i<argc; i++) {
660: if (argv[i][0] == '-') {
661: if (strcmp(argv[i],"--oxgentexi")==0) oxg=1;
1.3 ! takayama 662: else if (strcmp(argv[i],"--help")==0) {
! 663: show_help(); return(0);
! 664: }
1.2 takayama 665: }else {
666: Inop = fopen(argv[i],"r");
667: if (Inop == NULL) {
668: fprintf(stderr,"File %s is not found.\n",argv[1]); return(-1);
669: }
670: outUsage(argv[i],oxg);
671: fclose(Inop);
672: }
673: }
674: if (Inop == stdin) outUsage("stdin",oxg);
675: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>