home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / netiso / xebec / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-05  |  9.3 KB  |  411 lines

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