--- stack/stack.c 2002/01/07 23:31:34 1.3 +++ stack/stack.c 2002/01/08 12:33:57 1.6 @@ -11,7 +11,7 @@ typedef struct stack_item { - enum {value, string, ref, func} type; + enum {value, string, ref, func, symbol} type; union { void* ptr; int val; @@ -110,23 +110,36 @@ 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; + static void* handle= NULL; void* symbol; - + stackitem* new_item= malloc(sizeof(stackitem)); new_item->content.ptr= *hash(in_hash, in_string); new_item->type= ref; - if(new_item->content.ptr==NULL) { + if(handle==NULL) handle= dlopen(NULL, RTLD_LAZY); + + if(new_item->content.ptr==NULL) { 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,7 +169,10 @@ printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); break; case ref: + printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); + break; case func: + case symbol: printf("%ld: %p\n", counter, stack_head->content.ptr); break; } @@ -224,12 +240,21 @@ 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 ref: + printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); + break; + case symbol: + default: printf("%p", (*stack_head)->content.ptr); + break; + } toss(stack_head); } @@ -239,6 +264,11 @@ printf("\n"); } +extern void quit() +{ + exit(EXIT_SUCCESS); +} + int main() { stackitem* s= NULL;