1 /* $NetBSD: sets.c,v 1.15 2009/03/18 17:06:53 cegger Exp $ */
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...
10 __KERNEL_RCSID(0, "$NetBSD: sets.c,v 1.15 2009/03/18 17:06:53 cegger Exp $");
18 #include <sys/types.h>
23 struct Object
*CurrentEvent
= (struct Object
*)0;
24 struct Object
*Objtree
;
27 * define a set w/ type and name
31 #define NULL (struct Object *)0
33 static FILE *Sfile
, *Efile
;
34 extern FILE *astringfile
;
35 char *Noname
= "Unnamed set\0";
41 initsets(FILE *f
,FILE *s
)
43 static char errorstring
[20];
44 extern struct Object
*SameState
;
49 fprintf(astringfile
, "char *%s_sstring[] = {\n", protocol
);
51 sprintf(errorstring
, "%sERROR", ST_PREFIX
);
52 defineitem(STATESET
, errorstring
, (char *)0); /* state 0 */
53 SameState
= (struct Object
*) Malloc( sizeof (struct Object
) );
54 SameState
->obj_kind
= OBJ_ITEM
;
55 SameState
->obj_type
= STATESET
;
56 SameState
->obj_name
= "SAME";
57 SameState
->obj_struc
= (char *)0;
58 SameState
->obj_number
= 0;
59 SameState
->obj_members
= (struct Object
*)0;
60 SameState
->obj_left
= (struct Object
*)0;
61 SameState
->obj_right
= (struct Object
*)0;
62 SameState
->obj_parent
= (struct Object
*)0;
66 * get a set based on its type and name
67 * returns address of an Object, may be set or item
70 struct Object
*lookup(type
, name
)
74 register struct Object
*p
= Objtree
;
78 fprintf(stdout
,"lookup 0x%x,%s \n",
84 fprintf(OUT
, "lookup strcmp %p,%s, %p,%s\n",
85 name
, name
, OBJ_NAME(p
), OBJ_NAME(p
));
87 if( p
->obj_name
== (char *)0 ) {
88 fprintf(stderr
, "Unnamed set in table!\n");
91 val
= (int) strcmp(name
, OBJ_NAME(p
));
100 if( p
&& ( p
->obj_type
!= type
)) {
101 fprintf(stdout
, "lookup(0x%x,%s) found wrong obj type 0x%x\n",
102 type
,name
, p
->obj_type
);
106 fprintf(stdout
,"lookup 0x%x,%s returning %p\n",type
, name
, p
);
111 static int states_done
= 0;
116 register unsigned n
= Nstates
;
118 extern char Eventshiftstring
[];
123 if( (n
>>= 1) <= 0 ) break;
127 fprintf(OUT
, "Eventshift=%d\n", Eventshift
);
129 sprintf(Eventshiftstring
, "%d",Eventshift
);
130 fprintf(f
, "struct %s_event {\n\tint ev_number;\n", &protocol
[0]);
132 /* finish sstring[] & start estring[] */
134 "};\n\nchar *%s_estring[] = {\n", protocol
);
138 int FirstEventAttribute
= 1;
141 insert(struct Object
*o
)
143 struct Object
*p
= Objtree
;
144 struct Object
**q
= &Objtree
;
148 if (o
->obj_name
== (char *)0) {
149 fprintf(stderr
, "Internal Error: inserting unnamed object\n");
152 if( o
->obj_type
== STATESET
) {
154 fprintf(stderr
, "No states may be defined after *TRANSITIONS\n");
157 o
->obj_number
= Nstates
++ ;
158 if(Nstates
> MAXSTATES
) {
159 fprintf(stderr
, "Too many states\n");
162 fprintf(Sfile
, "#define %s 0x%x\n", o
->obj_name
, o
->obj_number
);
164 fprintf(astringfile
, "\"%s(0x%x)\",\n", o
->obj_name
, o
->obj_number
);
168 if( ! states_done
) {
169 fprintf(stderr
, "states must precede events\n");
172 o
->obj_number
= Nevents
++ ;
173 if(Nevents
> MAXEVENTS
) {
174 fprintf(stderr
, "Too many events\n");
178 if( FirstEventAttribute
) {
179 fprintf(Efile
, "\n\tunion{\n"); /*} */
180 FirstEventAttribute
= 0;
183 "struct %s %s%s;\n\n", o
->obj_struc
, EV_PREFIX
, o
->obj_name
);
185 fprintf(Efile
, "#define %s 0x%x\n", o
->obj_name
, o
->obj_number
);
187 fprintf(astringfile
, "\"%s(0x%x)\",\n", o
->obj_name
, o
->obj_number
);
191 fprintf(OUT
, "insert(%s)\n", OBJ_NAME(o
) );
192 if(o
->obj_right
!= NULL
) {
193 fprintf(OUT
, "insert: unclean Object right\n");
196 if(o
->obj_left
!= NULL
) {
197 fprintf(OUT
, "insert: unclean Object left\n");
206 o
->obj_parent
= (struct Object
*)q
;
209 if(!(val
= strcmp(o
->obj_name
, p
->obj_name
)) ) {
211 fprintf(stderr
, "re-inserting %s\n",o
->obj_name
);
230 delete(struct Object
*o
)
232 register struct Object
*p
= o
->obj_right
;
233 register struct Object
*q
;
234 register struct Object
*newparent
;
235 register struct Object
**np_childlink
;
238 fprintf(stdout
, "delete(%p)\n", o
);
242 /* q <== lowest valued node of the right subtree */
248 if (o
->obj_parent
== (struct Object
*)&Objtree
) {
249 newparent
= (struct Object
*)&Objtree
;
250 np_childlink
= (struct Object
**)&Objtree
;
251 } else if(o
->obj_parent
->obj_left
== o
) {
252 newparent
= o
->obj_parent
;
253 np_childlink
= &(o
->obj_parent
->obj_left
);
255 newparent
= o
->obj_parent
;
256 np_childlink
= &(o
->obj_parent
->obj_right
);
259 fprintf(OUT
, "newparent=%p\n", newparent
);
262 if (q
) { /* q gets the left, parent gets the right */
264 fprintf(OUT
, "delete: q null\n");
267 if(p
) p
->obj_parent
= q
;
269 } else { /* parent(instead of q) gets the left ; there is no right */
271 fprintf(OUT
, "delete: q not null\n");
277 p
->obj_parent
= newparent
;
280 fprintf(OUT
, "After deleting %p\n",o
);
286 defineset(unsigned char type
, char *adr
, int keep
)
290 printf("defineset(0x%x,%s, %s)\n", type
, adr
, keep
?"KEEP":"NO_KEEP");
293 onew
= (struct Object
*)Malloc(sizeof (struct Object
));
294 memset(onew
, 0, sizeof(struct Object
));
295 onew
->obj_name
= adr
;
296 onew
->obj_kind
= OBJ_SET
;
297 onew
->obj_type
= type
;
300 /* address already stashed before calling defineset */
302 printf("defineset(0x%x,%s) returning %p\n", type
, adr
, onew
);
309 dumpit(char *o
, char *s
)
314 fprintf(OUT
, "object %p, %s\n",o
, s
);
315 for(i
=0; i
< sizeof(struct Object
); i
+=4) {
316 fprintf(OUT
, "0x%x: 0x%x 0x%x 0x%x 0x%x\n",
317 *((int *)o
), *o
, *(o
+1), *(o
+2), *(o
+3) );
323 defineitem(unsigned char type
, char *adr
, char *struc
)
327 printf("defineitem(0x%x, %s at %p, %s)\n", type
, adr
, adr
, struc
);
330 if((onew
= lookup( type
, adr
))) {
332 "Internal error at defineitem: trying to redefine obj type 0x%x, adr %s\n",
336 onew
= (struct Object
*)Malloc(sizeof (struct Object
));
337 memset(onew
, 0, sizeof(struct Object
));
338 onew
->obj_name
= stash(adr
);
339 onew
->obj_kind
= OBJ_ITEM
;
340 onew
->obj_type
= type
;
341 onew
->obj_struc
= struc
?stash(struc
):struc
;
345 fprintf(OUT
, "defineitem(0x%x, %s) returning %p\n", type
, adr
, onew
);
350 member(struct Object
*o
, char *adr
)
352 struct Object
*onew
, *oold
;
354 printf("member(%p, %s)\n", o
, adr
);
357 oold
= lookup( o
->obj_type
, adr
);
359 onew
= (struct Object
*)Malloc(sizeof (struct Object
));
364 "Warning at line %d: set definition of %s causes definition of\n",
365 lineno
, OBJ_NAME(o
));
366 fprintf(stderr
, "\t (previously undefined) member %s\n", adr
);
367 memset(onew
, 0, sizeof(struct Object
));
368 onew
->obj_name
= stash(adr
);
369 onew
->obj_kind
= OBJ_ITEM
;
370 onew
->obj_type
= o
->obj_type
;
371 onew
->obj_members
= NULL
;
374 if(oold
->obj_kind
!= OBJ_ITEM
) {
375 fprintf(stderr
, "Sets cannot be members of sets; %s\n", adr
);
378 memcpy(onew
, oold
, sizeof(struct Object
));
379 onew
->obj_members
= onew
->obj_left
= onew
->obj_right
= NULL
;
381 onew
->obj_members
= o
->obj_members
;
382 o
->obj_members
= onew
;
385 struct Object
*Lookup(type
, name
)
389 register struct Object
*o
= lookup(type
,name
);
392 fprintf(stderr
, "Trying to use undefined %s: %s\n",
393 type
==STATESET
?"state":"event", name
);
400 AddCurrentEventName(register char **x
)
402 register char *n
= EV_PREFIX
; ;
404 if( CurrentEvent
== (struct Object
*)0 ) {
405 fprintf(stderr
, "No event named! BARF!\n"); Exit(-1);
408 if( ! CurrentEvent
->obj_struc
) {
409 fprintf(stderr
, "No attributes for current event!\n"); Exit(-1);
412 /* add prefix first */
417 n
= CurrentEvent
->obj_name
;
425 dumptree(register struct Object
*o
,int i
)
432 fprintf(stdout
, "%3d NULL\n", i
);
434 dumptree(o
->obj_left
, i
+1);
437 fprintf(stdout
, "%3d %p: %s\n", i
,o
, OBJ_NAME(o
));
438 dumptree(o
->obj_right
, i
+1);
445 fprintf(stderr
, "dump: c 0x%x, a 0x%x\n",c
,a
);
452 dump_trans( pred
, oldstate
, newstate
, action
, event
)
453 struct Object
*oldstate
, *newstate
, *event
;
459 fprintf(stdout
, "\n%d: ", transno
);
461 if((x)->obj_kind == OBJ_SET) {\
462 o = (x)->obj_members; fprintf( stdout, "[ " );\
463 while(o) { fprintf(stdout, "%s ", o->obj_name); o = o->obj_members; }\
464 fprintf( stdout, " ] ");\
465 } else { fprintf(stdout, "%s ", (x)->obj_name); }
468 fprintf(stdout
, " <== ");
471 fprintf(stdout
, "\n\t\t%s\n\t\t%s\n", pred
?pred
:"DEFAULT",