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

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

unix.superglobalmegacorp.com

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