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