--- stack/stack.c 2002/01/07 23:13:14 1.2 +++ stack/stack.c 2002/01/08 00:03:16 1.5 @@ -4,13 +4,14 @@ #include /* NULL */ #include +/* dlopen, dlsym, dlerror */ #include #define HASHTBLSIZE 65536 typedef struct stack_item { - enum {value, string, ref, func} type; + enum {value, string, ref, func, symbol} type; union { void* ptr; int val; @@ -109,6 +110,15 @@ mk_hashentry(in_hashtbl, temp, id); } +void def_sym(hashtbl in_hashtbl, const char* id) +{ + stackitem* temp= malloc(sizeof(stackitem)); + + temp->type= symbol; + + mk_hashentry(in_hashtbl, temp, id); +} + int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) { void* handle; @@ -121,11 +131,13 @@ if(new_item->content.ptr==NULL) { handle= dlopen(NULL, RTLD_LAZY); symbol= dlsym(handle, in_string); - if(dlerror()==NULL) { + if(dlerror()==NULL) def_func(in_hash, symbol, in_string); - new_item->content.ptr= *hash(in_hash, in_string); - new_item->type= ref; - } + else + def_sym(in_hash, in_string); + + new_item->content.ptr= *hash(in_hash, in_string); + new_item->type= ref; } push(stack_head, new_item); @@ -156,6 +168,7 @@ break; case ref: case func: + case symbol: printf("%ld: %p\n", counter, stack_head->content.ptr); break; } @@ -223,12 +236,20 @@ if((*stack_head)==NULL) return; - if((*stack_head)->type==value) + switch((*stack_head)->type){ + case value: printf("%d", (*stack_head)->content.val); - else if((*stack_head)->type==string) + break; + case string: printf("%s", (char*)(*stack_head)->content.ptr); - else + break; + case symbol: + printf("%s", (*stack_head)->id); + case ref: + default: printf("%p", (*stack_head)->content.ptr); + break; + } toss(stack_head); } @@ -238,6 +259,11 @@ printf("\n"); } +extern void quit() +{ + exit(EXIT_SUCCESS); +} + int main() { stackitem* s= NULL; @@ -256,3 +282,7 @@ return EXIT_SUCCESS; } + +/* Local Variables: */ +/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ +/* End: */