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

  1. /* $Header: sets.c,v 2.3 88/09/19 12:55:30 nhall Exp $ */
  2. /* $Source: /var/home/tadl/src/argo/xebec/RCS/sets.c,v $ */
  3. /*
  4.  * This code is such a kludge that I don't want to put my name on it.
  5.  * It was a ridiculously fast hack and needs rewriting.
  6.  * However it does work...
  7.  */
  8. #include "main.h"
  9. #include "malloc.h"
  10. #include "sets.h"
  11. #include "debug.h"
  12. #include <stdio.h>
  13.  
  14. struct Object *CurrentEvent = (struct Object *)0;
  15. struct Object *Objtree;
  16. struct Object dummy;
  17. /* 
  18.  * define a set w/ type and name
  19.  * return a set number 
  20.  */
  21. #undef NULL
  22. #define NULL (struct Object *)0
  23.  
  24. static FILE *Sfile, *Efile;
  25. extern FILE *astringfile;
  26. char *Noname = "Unnamed set\0";
  27.  
  28. initsets(f,s)
  29. FILE *f, *s;
  30. {
  31.     static char errorstring[20];
  32.     extern struct Object *SameState;
  33.     Efile = f;
  34.     Sfile = s;
  35.  
  36.     IFDEBUG(X)
  37.         fprintf(astringfile, "char *%s_sstring[] = {\n", protocol);
  38.     ENDDEBUG
  39.     sprintf(errorstring, "%sERROR\0", ST_PREFIX);
  40.     defineitem(STATESET, errorstring, (char *)0);    /* state 0 */
  41.     SameState = (struct Object *) Malloc( sizeof (struct Object) );
  42.     SameState->obj_kind = OBJ_ITEM;
  43.     SameState->obj_type = STATESET;
  44.     SameState->obj_name = "SAME";
  45.     SameState->obj_struc = (char *)0;
  46.     SameState->obj_number = 0;
  47.     SameState->obj_members = (struct Object *)0;
  48.     SameState->obj_left = (struct Object *)0;
  49.     SameState->obj_right = (struct Object *)0;
  50.     SameState->obj_parent = (struct Object *)0;
  51. }
  52.  
  53. /*
  54.  * get a set based on its type and name
  55.  * returns address of an Object, may be set or item
  56.  */
  57.  
  58. struct Object *lookup(type, name)
  59. unsigned char type;
  60. char *name;
  61. {
  62.     register struct Object *p = Objtree;
  63.     int val = 1 ;
  64.  
  65.     IFDEBUG(o)
  66.         fprintf(stdout,"lookup 0x%x,%s \n",
  67.             type, name);
  68.     ENDDEBUG
  69.  
  70.     while( p && val ) {
  71.         IFDEBUG(o)
  72.         fprintf(OUT, "lookup strcmp 0x%x,%s, 0x%x,%s\n",
  73.             name, name, OBJ_NAME(p), OBJ_NAME(p));
  74.         ENDDEBUG
  75.         if( p->obj_name == (char *)0 ) {
  76.             fprintf(stderr, "Unnamed set in table!\n");
  77.             Exit(-1);
  78.         }
  79.         val =  (int) strcmp(name, OBJ_NAME(p));
  80.         if(val < 0) {
  81.             /* left */
  82.             p = p->obj_left;
  83.         } else if (val > 0) {
  84.             /* right */
  85.             p = p->obj_right;
  86.         }
  87.     }
  88.     if( p && ( p->obj_type != type)) {
  89.         fprintf(stdout, "lookup(0x%x,%s) found wrong obj type 0x%x\n",
  90.             type,name, p->obj_type);
  91.         p = NULL;
  92.     }
  93.     IFDEBUG(o)
  94.         fprintf(stdout,"lookup 0x%x,%s returning 0x%x\n",type, name, p);
  95.     ENDDEBUG
  96.     return(p);
  97. }
  98.  
  99. static int states_done  = 0;
  100.  
  101. end_states(f)
  102. FILE *f;
  103. {
  104.     register unsigned n = Nstates;
  105.     register int i;
  106.     extern char Eventshiftstring[];
  107.  
  108.     states_done = 1;
  109.  
  110.     for( i = 0; ;i++) {
  111.         if( (n >>= 1) <= 0 ) break;
  112.     }
  113.     Eventshift = i+1;
  114.     IFDEBUG(d)
  115.         fprintf(OUT, "Eventshift=%d\n", Eventshift);
  116.     ENDDEBUG
  117.     sprintf(Eventshiftstring, "%d\0",Eventshift);
  118.     fprintf(f, "struct %s_event {\n\tint ev_number;\n", &protocol[0]);
  119.     IFDEBUG(X)
  120.         /* finish sstring[] & start estring[] */
  121.         fprintf(astringfile, 
  122.         "};\n\nchar *%s_estring[] = {\n", protocol);
  123.     ENDDEBUG
  124. }
  125.  
  126. int FirstEventAttribute = 1;
  127.  
  128. static 
  129. insert(o) 
  130. struct Object *o;
  131. {
  132.     struct Object *p = Objtree;
  133.     struct Object **q = &Objtree; 
  134.     int val=1;
  135.  
  136.  
  137.     if (o->obj_name == (char *)0) {
  138.         fprintf(stderr, "Internal Error: inserting unnamed object\n");
  139.         Exit(-1);
  140.     }
  141.     if( o->obj_type == STATESET) {
  142.         if( states_done )  {
  143.             fprintf(stderr, "No states may be defined after *TRANSITIONS\n");
  144.             Exit(-1);
  145.         }
  146.         o->obj_number =  Nstates++ ; 
  147.         if(Nstates > MAXSTATES) {
  148.             fprintf(stderr, "Too many states\n");
  149.             Exit(-1);
  150.         }
  151.         fprintf(Sfile, "#define %s 0x%x\n", o->obj_name, o->obj_number);
  152.         IFDEBUG(X)
  153.             fprintf(astringfile, "\"%s(0x%x)\",\n", o->obj_name, o->obj_number);
  154.         ENDDEBUG
  155.     } else {
  156.         /* EVENTSET */ 
  157.         if( ! states_done )  {
  158.             fprintf(stderr, "states must precede events\n");
  159.             Exit(-1);
  160.         }
  161.         o->obj_number =  Nevents++ ;
  162.         if(Nevents > MAXEVENTS) {
  163.             fprintf(stderr, "Too many events\n");
  164.             Exit(-1);
  165.         }
  166.         if(o->obj_struc)  {
  167.             if( FirstEventAttribute ) {
  168.                 fprintf(Efile,  "\n\tunion{\n"); /*} */
  169.                 FirstEventAttribute = 0;
  170.             }
  171.             fprintf(Efile, 
  172.             "struct %s %s%s;\n\n", o->obj_struc, EV_PREFIX,  o->obj_name);
  173.         }
  174.         fprintf(Efile, "#define %s 0x%x\n", o->obj_name, o->obj_number);
  175.         IFDEBUG(X)
  176.             fprintf(astringfile, "\"%s(0x%x)\",\n", o->obj_name, o->obj_number);
  177.         ENDDEBUG
  178.     }
  179.     IFDEBUG(o)
  180.         fprintf(OUT, "insert(%s)\n", OBJ_NAME(o) );
  181.         if(o->obj_right != NULL) {
  182.             fprintf(OUT, "insert: unclean Object right\n");
  183.             exit(-1);
  184.         }
  185.         if(o->obj_left != NULL) {
  186.             fprintf(OUT, "insert: unclean Object left\n");
  187.             exit(-1);
  188.         }
  189.         fflush(OUT);
  190.     ENDDEBUG
  191.  
  192.     while( val ) {
  193.         if(p == NULL) {
  194.             *q = o;
  195.             o->obj_parent = (struct Object *)q;
  196.             break;
  197.         }
  198.         if(!(val = strcmp(o->obj_name, p->obj_name)) ) {
  199.             /* equal */
  200.             fprintf(stderr, "re-inserting %s\n",o->obj_name);
  201.             exit(-1);
  202.         }
  203.         if(val < 0) {
  204.             /* left */
  205.             q = &p->obj_left;
  206.             p = p->obj_left;
  207.         } else {
  208.             /* right */
  209.             q = &p->obj_right;
  210.             p = p->obj_right;
  211.         }
  212.     }
  213.     IFDEBUG(a)
  214.         dumptree(Objtree,0);
  215.     ENDDEBUG
  216. }
  217.  
  218. delete(o) 
  219. struct Object *o;
  220. {
  221.     register struct Object *p = o->obj_right; 
  222.     register struct Object *q;
  223.     register struct Object *newparent;
  224.     register struct Object **np_childlink;
  225.  
  226.     IFDEBUG(T)
  227.         fprintf(stdout, "delete(0x%x)\n", o);
  228.         dumptree(Objtree,0);
  229.     ENDDEBUG
  230.  
  231.     /* q <== lowest valued node of the right subtree */
  232.     while( p ) {
  233.         q = p;
  234.         p = p->obj_left;
  235.     }
  236.  
  237.     if (o->obj_parent == (struct Object *)&Objtree)  {
  238.         newparent =  (struct Object *)&Objtree;
  239.         np_childlink = (struct Object **)&Objtree;
  240.     } else if(o->obj_parent->obj_left == o)  {
  241.         newparent = o->obj_parent;
  242.         np_childlink = &(o->obj_parent->obj_left);
  243.     } else {
  244.         newparent = o->obj_parent;
  245.         np_childlink = &(o->obj_parent->obj_right);
  246.     }
  247.     IFDEBUG(T)
  248.         fprintf(OUT, "newparent=0x%x\n");
  249.     ENDDEBUG
  250.  
  251.     if (q) { /* q gets the left, parent gets the right */
  252.         IFDEBUG(T)
  253.             fprintf(OUT, "delete: q null\n");
  254.         ENDDEBUG
  255.         q->obj_left = p;
  256.         if(p) p->obj_parent = q;
  257.         p = o->obj_right;
  258.     } else { /* parent(instead of q) gets the left ; there is no right  */
  259.         IFDEBUG(T)
  260.             fprintf(OUT, "delete: q not null\n");
  261.         ENDDEBUG
  262.         p = o->obj_left;
  263.     }
  264.     *np_childlink = p;
  265.     if(p) 
  266.         p->obj_parent = newparent;
  267.  
  268.     IFDEBUG(T)
  269.         fprintf(OUT, "After deleting 0x%x\n",o);
  270.         dumptree(Objtree,0);
  271.     ENDDEBUG
  272. }
  273.  
  274. struct Object *
  275. defineset(type, adr, keep)
  276. unsigned char type;
  277. char *adr;
  278. int keep;
  279. {
  280.     struct Object *onew;
  281.     IFDEBUG(o)
  282.         printf("defineset(0x%x,%s, %s)\n", type , adr, keep?"KEEP":"NO_KEEP");
  283.     ENDDEBUG
  284.     
  285.     onew = (struct Object *)Malloc(sizeof (struct Object));
  286.     bzero(onew, sizeof(struct Object));
  287.     onew->obj_name = adr;
  288.     onew->obj_kind = OBJ_SET;
  289.     onew->obj_type = type;
  290.     if(keep) 
  291.         insert( onew );
  292.         /* address already stashed before calling defineset */
  293.     IFDEBUG(o)
  294.         printf("defineset(0x%x,%s) returning 0x%x\n", type , adr, onew);
  295.         dumptree(Objtree,0);
  296.     ENDDEBUG
  297.     return(onew);
  298. }
  299.  
  300. dumpit(o, s)
  301. char *o;
  302. char *s;
  303. {
  304.     register int i;
  305.  
  306. IFDEBUG(o)
  307.     fprintf(OUT, "object 0x%x, %s\n",o, s);
  308.     for(i=0; i< sizeof(struct Object); i+=4) {
  309.         fprintf(OUT, "0x%x: 0x%x 0x%x 0x%x 0x%x\n",
  310.         *((int *)o), *o, *(o+1), *(o+2), *(o+3) );
  311.     }
  312. ENDDEBUG
  313. }
  314.  
  315. defineitem(type, adr, struc)
  316. unsigned char type;
  317. char *adr;
  318. char *struc;
  319. {
  320.     struct Object *onew;
  321.     IFDEBUG(o)
  322.         printf("defineitem(0x%x, %s at 0x%x, %s)\n", type, adr, adr, struc);
  323.     ENDDEBUG
  324.     
  325.     if( onew = lookup( type, adr ) ) {
  326.         fprintf(stderr, 
  327.     "Internal error at defineitem: trying to redefine obj type 0x%x, adr %s\n",
  328.             type, adr);
  329.         exit(-1);
  330.     } else {
  331.         onew = (struct Object *)Malloc(sizeof (struct Object));
  332.         bzero(onew, sizeof(struct Object));
  333.         onew->obj_name = stash(adr);
  334.         onew->obj_kind = OBJ_ITEM;
  335.         onew->obj_type =  type;
  336.         onew->obj_struc = struc?stash(struc):struc;
  337.         insert( onew );
  338.     }
  339.     IFDEBUG(o)
  340.         fprintf(OUT, "defineitem(0x%x, %s) returning 0x%x\n", type, adr, onew);
  341.     ENDDEBUG
  342. }
  343.  
  344. member(o, adr)
  345. struct Object *o;
  346. char *adr;
  347. {
  348.     struct Object *onew, *oold;
  349.     IFDEBUG(o)
  350.         printf("member(0x%x, %s)\n", o, adr);
  351.     ENDDEBUG
  352.     
  353.     oold = lookup(  o->obj_type, adr );
  354.  
  355.     onew = (struct Object *)Malloc(sizeof (struct Object));
  356.     if( oold == NULL ) {
  357.         extern int lineno;
  358.  
  359.         fprintf(stderr,
  360.         "Warning at line %d: set definition of %s causes definition of\n",
  361.             lineno, OBJ_NAME(o));
  362.         fprintf(stderr, "\t (previously undefined) member %s\n", adr);
  363.         bzero(onew, sizeof(struct Object));
  364.         onew->obj_name = stash(adr);
  365.         onew->obj_kind = OBJ_ITEM;
  366.         onew->obj_type = o->obj_type;
  367.         onew->obj_members = NULL;
  368.         insert( onew );
  369.     } else {
  370.         if(oold->obj_kind != OBJ_ITEM) {
  371.             fprintf(stderr, "Sets cannot be members of sets; %s\n", adr);
  372.             exit(-1);
  373.         }
  374.         bcopy(oold, onew, sizeof(struct Object));
  375.         onew->obj_members = onew->obj_left = onew->obj_right = NULL;
  376.     }
  377.     onew->obj_members = o->obj_members;
  378.     o->obj_members = onew;
  379. }
  380.  
  381. struct Object *Lookup(type, name)
  382. unsigned char type;
  383. char *name;
  384. {
  385.     register struct Object *o = lookup(type,name);
  386.  
  387.     if(o == NULL) {
  388.         fprintf(stderr, "Trying to use undefined %s: %s\n",
  389.             type==STATESET?"state":"event", name);
  390.         Exit(-1);
  391.     }
  392.     return(o);
  393. }
  394.  
  395. AddCurrentEventName(x)
  396. register char **x;
  397. {
  398.     register char *n = EV_PREFIX; ;
  399.     
  400.     if( CurrentEvent == (struct Object *)0 ) {
  401.         fprintf(stderr, "No event named!  BARF!\n"); Exit(-1);
  402.     }
  403.  
  404.     if( ! CurrentEvent->obj_struc ) {
  405.         fprintf(stderr, "No attributes for current event!\n"); Exit(-1);
  406.     }
  407.  
  408.     /* add prefix first */
  409.     while(*n) {
  410.         *(*x)++ = *n++;
  411.     }
  412.  
  413.     n = CurrentEvent->obj_name;
  414.  
  415.     while(*n) {
  416.         *(*x)++ = *n++;
  417.     }
  418. }
  419.  
  420. dumptree(o,i)
  421.     register struct Object *o;
  422.     int i;
  423. {
  424.     register int j;
  425.  
  426.     if(o == NULL) {
  427.         for(j=0; j<i; j++)
  428.             fputc(' ', stdout);
  429.         fprintf(stdout, "%3d NULL\n", i);
  430.     } else {
  431.         dumptree(o->obj_left, i+1);
  432.         for(j=0; j<i; j++) 
  433.             fputc(' ', stdout);
  434.         fprintf(stdout, "%3d 0x%x: %s\n", i,o, OBJ_NAME(o));
  435.         dumptree(o->obj_right, i+1);
  436.     }
  437. }
  438.  
  439. dump(c,a)
  440. {
  441.     register int x = 8;
  442.     int zero = 0;
  443. #include <sys/signal.h>
  444.  
  445.     fprintf(stderr, "dump: c 0x%x, a 0x%x\n",c,a);
  446.  
  447.     x = x/zero;
  448.     kill(0, SIGQUIT);
  449. }
  450.  
  451. dump_trans( pred, oldstate, newstate, action, event )
  452. struct Object *oldstate, *newstate, *event;
  453. char *pred, *action;
  454. {
  455.     extern int transno;
  456.     struct Object *o;
  457.  
  458.     fprintf(stdout, "\n%d:  ", transno);
  459. #define dumpit(x)\
  460.     if((x)->obj_kind == OBJ_SET) {\
  461.         o = (x)->obj_members; fprintf( stdout, "[ " );\
  462.         while(o) { fprintf(stdout, "%s ", o->obj_name); o = o->obj_members; }\
  463.         fprintf( stdout, " ] ");\
  464.     } else { fprintf(stdout, "%s ", (x)->obj_name); }
  465.  
  466.     dumpit(newstate);
  467.     fprintf(stdout, " <== ");
  468.     dumpit(oldstate);
  469.     dumpit(event);
  470.     fprintf(stdout, "\n\t\t%s\n\t\t%s\n", pred?pred:"DEFAULT", 
  471.         action);
  472. }
  473.