Annotation of Net2/netiso/xebec/main.c, revision 1.1.1.3

1.1.1.3 ! root        1: /* main.c,v 1.2 1993/05/20 05:28:24 cgd Exp */
1.1       root        2: /*
                      3:  * TODO:
                      4:  * rewrite the command line stuff altogether - it's kludged beyond
                      5:  * belief (as is the rest of the code...)
                      6:  *
                      7:  * DISCLAIMER DISCLAIMER DISCLAIMER
                      8:  * This code is such a kludge that I don't want to put my name on it.
                      9:  * It was a ridiculously fast hack and needs rewriting.
                     10:  * However it does work...
                     11:  */
                     12: 
                     13: #include <stdio.h>
                     14: #include <strings.h>
                     15: #include "malloc.h"
                     16: #include "debug.h"
                     17: #include "main.h"
                     18: 
                     19: int    debug[128];
                     20: 
                     21: int lineno = 1;
                     22: 
                     23: FILE *statefile, *actfile, *eventfile_h, *statevalfile;
                     24: FILE *infile, *astringfile;
                     25: char *Transfilename;
                     26: char *astringfile_name = DEBUGFILE;
                     27: char *actfile_name = ACTFILE;
                     28: char *statefile_name = STATEFILE;
                     29: char *statevalfile_name = STATEVALFILE;
                     30: char *eventfile_h_name = EVENTFILE_H;
                     31: int print_trans = 0;
                     32: int print_protoerrs = 0;
                     33: int pgoption = 0;
                     34: char kerneldirname[50] = "\0";
                     35: 
                     36: char protocol[50];
                     37: 
                     38: char *synonyms[] = {
                     39:        "EVENT",
                     40:        "PCB",
                     41:        0
                     42: };
                     43: 
                     44: usage(a)
                     45: char *a;
                     46: {
                     47:        fprintf(stderr, 
                     48:        "usage: %s <transition file> {-D<debug options>} <other options>\n",
                     49:                a);
                     50:        fprintf(stderr, "\t<other options> is any combination of:\n");
                     51:        fprintf(stderr, "\t\t-A<action file name>\n");
                     52:        fprintf(stderr, "\t\t-E<event file name>\n");
                     53:        fprintf(stderr, "\t\t-S<state file name>\n");
                     54:        fprintf(stderr, "\t\t-I<initial values file name>\n");
                     55:        fprintf(stderr, "\t\t-X<debugging file name>\n");
                     56:        fprintf(stderr, "\t\t-K<directory name>\n");
                     57:        fprintf(stderr, 
                     58:        "\tThese names do NOT include the suffices (.c, .h)\n");
                     59:        fprintf(stderr, 
                     60:        "\t\t-D<options> to turn on debug options for xebec itself\n");
                     61:        fprintf(stderr, "\t-<nn> for levels of debugging output\n");
                     62:        fprintf(stderr, "\t\t<nn> ranges from 1 to 3, 1 is default(everything)\n");
                     63:        fprintf(stderr, "\t\t-T to print transitions\n");
                     64:        fprintf(stderr, "\t\t-e to print list of combinations of\n");
                     65:        fprintf(stderr, "\t\t\t [event,old_state] that produce protocol errors\n");
                     66:        fprintf(stderr, "\t\t-g include profiling code in driver\n");
                     67:        Exit(-1);
                     68: }
                     69: 
                     70: openfiles(proto)
                     71: register char *proto;
                     72: {
                     73:        register char *junk;
                     74:        register int lenp = strlen(proto);
                     75: 
                     76:        IFDEBUG(b)
                     77:                fprintf(OUT, "openfiles %s\n",proto);
                     78:        ENDDEBUG
                     79: 
                     80: #define HEADER Header
                     81: #define SOURCE Source
                     82: #define DOIT(X)\
                     83:        /* GAG */\
                     84:        junk = Malloc( 2 + lenp + strlen(X/**/_name) );\
                     85:        (void) sprintf(junk, "%s_", proto);\
                     86:        X/**/_name = strcat(junk, X/**/_name);\
                     87:        X = fopen(X/**/_name, "w");\
                     88:        if((X)==(FILE *)0)\
                     89:        { fprintf(stderr,"Open failed: %s\n", "X"); Exit(-1); }\
                     90:        fprintf(X, "/* %cHeader%c */\n",'$', '$' );\
                     91:        fprintf(X, "/* %cSource%c */\n",'$', '$' );
                     92: 
                     93:        DOIT(eventfile_h);
                     94: 
                     95:        IFDEBUG(X)
                     96: #ifdef DEBUG
                     97:                DOIT(astringfile);
                     98: #endif DEBUG
                     99:                fprintf(astringfile, 
                    100:                                "#ifndef _NFILE\n#include <stdio.h>\n#endif _NFILE\n" );
                    101:        ENDDEBUG
                    102: 
                    103:        DOIT(statevalfile);
                    104:        DOIT(statefile);
                    105:        DOIT(actfile);
                    106:        fprintf(actfile,
                    107:                "#ifndef lint\nstatic char *rcsid = \"$Header/**/$\";\n#endif lint\n");
                    108: 
                    109:        if(pgoption)
                    110:                putdriver(actfile, 15);
                    111:        else 
                    112:                putdriver(actfile, 14);
                    113: 
                    114:        FakeFilename(actfile, Transfilename, lineno);
                    115:        putdriver(actfile, 1);
                    116:        FakeFilename(actfile, Transfilename, lineno);
                    117:        putdriver(actfile, 12);
                    118:        fprintf(actfile, "#include \"%s%s\"\n", kerneldirname, statevalfile_name);
                    119:        FakeFilename(actfile, Transfilename, lineno);
                    120:        putdriver(actfile, 2);
                    121: 
                    122:        initsets(eventfile_h, statefile);
                    123: }
                    124: 
                    125: includecode(file, f)
                    126: FILE *file;
                    127: register char *f;
                    128: {
                    129:        register int count=1;
                    130:        static char o='{';
                    131:        static char c='}';
                    132:        register char *g;
                    133: 
                    134:        IFDEBUG(a)
                    135:                fprintf(stdout, "including: %s, f=0x%x", f,f);
                    136:        ENDDEBUG
                    137:        g = ++f;
                    138:        while(count>0) {
                    139:                if(*g == o) count++;
                    140:                if(*g == c) count--;
                    141:                g++;
                    142:        }
                    143:        *(--g) = '\0';
                    144:        IFDEBUG(a)
                    145:                fprintf(stdout, "derived: %s", f);
                    146:        ENDDEBUG
                    147:        fprintf(file, "%s", f);
                    148:        FakeFilename(file, Transfilename, lineno);
                    149: }
                    150: 
                    151: putincludes()
                    152: {
                    153:        FakeFilename(actfile, Transfilename, lineno);
                    154:        fprintf(actfile, "\n#include \"%s%s\"\n", kerneldirname, eventfile_h_name);
                    155:        IFDEBUG(X)
                    156:                if( !debug['K'] )
                    157:                        fprintf(actfile, "\n#include \"%s\"\n", astringfile_name);
                    158:                        /* not in kernel mode */
                    159:        ENDDEBUG
                    160:        FakeFilename(actfile, Transfilename, lineno);
                    161: }
                    162: 
                    163: main(argc, argv)
                    164: int argc;
                    165: char *argv[];
                    166: {
                    167:        register int i = 2;
                    168:        extern char *strcpy();
                    169:        int start, finish;
                    170:        extern int FirstEventAttribute;
                    171:        extern int Nevents, Nstates;
                    172: 
                    173:        start = time(0);
                    174:        if(argc < 2) {
                    175:                usage(argv[0]);
                    176:        }
                    177:        IFDEBUG(a)
                    178:                fprintf(stdout, "infile = %s\n",argv[1]);
                    179:        ENDDEBUG
                    180:        Transfilename = argv[1];
                    181:        infile = fopen(argv[1], "r");
                    182: 
                    183:        if(argc > 2) while(i < argc) {
                    184:                register int j=0;
                    185:                char c;
                    186:                char *name;
                    187: 
                    188:                if(argv[i][j] == '-') j++;
                    189:                switch(c = argv[i][j]) {
                    190: 
                    191:                /* GROT */
                    192:                case 'A':
                    193:                        name = &argv[i][++j];
                    194:                        actfile_name = Malloc( strlen(name)+4);
                    195:                        actfile_name =  (char *)strcpy(actfile_name,name);
                    196: #ifdef LINT
                    197:                        name =
                    198: #endif LINT
                    199:                        strcat(actfile_name, ".c");
                    200:                        fprintf(stdout, "debugging file is %s\n",actfile_name);
                    201:                        break;
                    202:                case 'K':
                    203:                        debug[c]=1;
                    204:                        fprintf(OUT, "option %c file %s\n",c, &argv[i][j+1]);
                    205:                        (void) strcpy(kerneldirname,&argv[i][++j]);
                    206:                        break;
                    207:                case 'X':
                    208:                        debug[c]=1;
                    209:                        name = &argv[i][++j];
                    210:                        astringfile_name = Malloc( strlen(name)+4);
                    211:                        astringfile_name =  (char *)strcpy(astringfile_name,name);
                    212: #ifdef LINT
                    213:                        name =
                    214: #endif LINT
                    215:                        strcat(astringfile_name, ".c");
                    216:                        fprintf(OUT, "option %c, astringfile name %s\n",c, name);
                    217:                        break;
                    218:                case 'E':
                    219:                        name = &argv[i][++j];
                    220:                        eventfile_h_name = Malloc( strlen(name)+4);
                    221:                        eventfile_h_name =  (char *)strcpy(eventfile_h_name,name);
                    222: #ifdef LINT
                    223:                        name =
                    224: #endif LINT
                    225:                        strcat(eventfile_h_name, ".h");
                    226:                        fprintf(stdout, "event files is %s\n",eventfile_h_name);
                    227:                        break;
                    228:                case 'I':
                    229:                        name = &argv[i][++j];
                    230:                        statevalfile_name = Malloc( strlen(name)+4 );
                    231:                        statevalfile_name =  (char *)strcpy(statevalfile_name,name);
                    232: #ifdef LINT
                    233:                        name =
                    234: #endif LINT
                    235:                        strcat(statevalfile_name, ".init");
                    236:                        fprintf(stdout, "state table initial values file is %s\n",statevalfile_name);
                    237:                        break;
                    238:                case 'S':
                    239:                        name = &argv[i][++j];
                    240:                        statefile_name = Malloc( strlen(name)+4);
                    241:                        statefile_name =  (char *)strcpy(statefile_name,name);
                    242: #ifdef LINT
                    243:                        name =
                    244: #endif LINT
                    245:                        strcat(statefile_name, ".h");
                    246:                        fprintf(stdout, "state file is %s\n",statefile_name);
                    247:                        break;
                    248:                /* END GROT */
                    249:                case '1':
                    250:                case '2':
                    251:                case '3':
                    252:                        debug['X']= (int)argv[i][j] - (int) '0';
                    253:                        fprintf(OUT, "value of debug['X'] is 0x%x,%d\n", debug['X'],
                    254:                                debug['X']);
                    255:                        break;
                    256:                case 'D':
                    257:                        while( c = argv[i][++j] ) {
                    258:                                if(c ==  'X') {
                    259:                                        fprintf(OUT, "debugging on");
                    260:                                        if(debug['X']) fprintf(OUT,
                    261:                                                " - overrides any -%d flags used\n", debug['X']);
                    262:                                }
                    263:                                debug[c]=1;
                    264:                                fprintf(OUT, "debug %c\n",c);
                    265:                        }
                    266:                        break;
                    267:                case 'g':
                    268:                        pgoption = 1;
                    269:                        fprintf(stdout, "Profiling\n");
                    270:                        break;
                    271:                case 'e':
                    272:                        print_protoerrs = 1;
                    273:                        fprintf(stdout, "Protocol error table:\n");
                    274:                        break;
                    275: 
                    276:                case 'T':
                    277:                        print_trans = 1;
                    278:                        fprintf(stdout, "Transitions:\n");
                    279:                        break;
                    280:                default:
                    281:                        usage(argv[0]);
                    282:                        break;
                    283:                }
                    284:                i++;
                    285:        }
                    286:        if(kerneldirname[0]) {
                    287:                char *c;
                    288: #ifdef notdef
                    289:                if(debug['X']) {
                    290:                        fprintf(OUT, "Option K overrides option X\n");
                    291:                        debug['X'] = 0;
                    292:                }
                    293: #endif notdef
                    294:                if(strlen(kerneldirname)<1) {
                    295:                        fprintf(OUT, "K option: dir name too short!\n");
                    296:                        exit(-1);
                    297:                }
                    298:                /* add ../name/ */
                    299:                c = (char *) Malloc(strlen(kerneldirname)+6) ;
                    300:                if(c <= (char *)0) {
                    301:                        fprintf(OUT, "Cannot allocate %d bytes for kerneldirname\n",
                    302:                                strlen(kerneldirname + 6) );
                    303:                        fprintf(OUT, "kerneldirname is %s\n", kerneldirname  );
                    304:                        exit(-1);
                    305:                }
                    306:                *c = '.';
                    307:                *(c+1) = '.';
                    308:                *(c+2) = '/';
                    309:                (void) strcat(c, kerneldirname);
                    310:                (void) strcat(c, "/\0");
                    311:                strcpy(kerneldirname, c);
                    312:        }
                    313: 
                    314:        init_alloc();
                    315: 
                    316:        (void) llparse();
                    317: 
                    318:        /* {{ */
                    319:        if( !FirstEventAttribute )
                    320:                fprintf(eventfile_h, "\t}ev_union;\n");
                    321:        fprintf(eventfile_h, "};/* end struct event */\n");
                    322:        fprintf(eventfile_h, "\n#define %s_NEVENTS 0x%x\n", protocol, Nevents);
                    323:        fprintf(eventfile_h,
                    324:                "\n#define ATTR(X)ev_union.%s/**/X/**/\n",EV_PREFIX);
                    325:        (void) fclose(eventfile_h);
                    326: 
                    327:        /* {{ */ fprintf(actfile, "\t}\nreturn 0;\n}\n"); /* end switch; end action() */
                    328:        dump_predtable(actfile);
                    329: 
                    330:        putdriver(actfile, 3);
                    331:        IFDEBUG(X)
                    332:                if(!debug['K'])
                    333:                        putdriver(actfile, 4);
                    334:        ENDDEBUG
                    335:        putdriver(actfile, 6);
                    336:        IFDEBUG(X)
                    337:                /*
                    338:                putdriver(actfile, 10);
                    339:                */
                    340:                if(debug['K']) { 
                    341:                        putdriver(actfile, 11);
                    342:                } else {
                    343:                        switch(debug['X']) {
                    344:                        case 1:
                    345:                        default:
                    346:                                putdriver(actfile, 7);
                    347:                                break;
                    348:                        case 2:
                    349:                                putdriver(actfile, 13);
                    350:                                break;
                    351:                        case 3:
                    352:                                break;
                    353:                        }
                    354:                }
                    355:        ENDDEBUG
                    356:        putdriver(actfile, 8);
                    357:        (void) fclose(actfile);
                    358:        IFDEBUG(X) 
                    359:                /* { */ 
                    360:                fprintf(astringfile, "};\n");
                    361:                (void) fclose(astringfile);
                    362:        ENDDEBUG
                    363: 
                    364:        (void) fclose(statevalfile);
                    365: 
                    366:        fprintf(statefile, "\n#define %s_NSTATES 0x%x\n", protocol, Nstates);
                    367:        (void) fclose(statefile);
                    368: 
                    369:        finish = time(0);
                    370:        fprintf(stdout, "%d seconds\n", finish - start);
                    371:        if( print_protoerrs ) 
                    372:                printprotoerrs();
                    373: }
                    374: 
                    375: int transno = 0;
                    376: 
                    377: Exit(n)
                    378: {
                    379:        fprintf(stderr, "Error at line %d\n",lineno);
                    380:        if(transno) fprintf(stderr, "Transition number %d\n",transno);
                    381:        (void) fflush(stdout);
                    382:        (void) fflush(statefile);
                    383:        (void) fflush(eventfile_h);
                    384:        (void) fflush(actfile);
                    385:        exit(n);
                    386: }
                    387: 
                    388: syntax() 
                    389: {
                    390:        static char *synt[] = {
                    391:                "*PROTOCOL <string>\n",
                    392:                "*PCB <string> <optional: SYNONYM synonymstring>\n",
                    393:                "<optional: *INCLUDE {\n<C source>\n} >\n",
                    394:                "*STATES <string>\n",
                    395:                "*EVENTS <string>\n",
                    396:                "*TRANSITIONS <string>\n",
                    397:        };
                    398: }
                    399:        
                    400: FakeFilename(outfile, name, l)
                    401: FILE *outfile;
                    402: char *name;
                    403: int l;
                    404: {
                    405:        /*
                    406:        doesn't work
                    407:        fprintf(outfile, "\n\n\n\n# line %d \"%s\"\n", l, name);
                    408:        */
                    409: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.