--- stack/stack.c 2002/01/07 23:09:01 1.1 +++ stack/stack.c 2002/01/08 13:46:05 1.7 @@ -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, list} type; union { void* ptr; int val; @@ -109,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); @@ -136,6 +150,12 @@ { stackitem* temp= *stack_head; + if((*stack_head)==NULL) + return; + + if((*stack_head)->type==string) + free((*stack_head)->content.ptr); + *stack_head= (*stack_head)->next; free(temp); } @@ -147,12 +167,22 @@ if(stack_head->next != NULL) print_st(stack_head->next, counter+1); - if(stack_head->type==value) + switch(stack_head->type){ + case value: printf("%ld: %d\n", counter, (int)stack_head->content.val); - else if(stack_head->type==string) + break; + case string: printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); - else + break; + case ref: + printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); + break; + case func: + case symbol: + default: printf("%ld: %p\n", counter, stack_head->content.ptr); + break; + } } extern void printstack(stackitem** stack_head) @@ -217,21 +247,66 @@ 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); } +extern void pack(stackitem** stack_head) +{ + void* delimiter; + stackitem *iterator, *temp, *pack; + + if((*stack_head)==NULL) + return; + + delimiter= (*stack_head)->content.ptr; + toss(stack_head); + + iterator= *stack_head; + + while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) + iterator= iterator->next; + + temp= *stack_head; + *stack_head= iterator->next; + iterator->next= NULL; + + if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) + toss(stack_head); + + pack= malloc(sizeof(stackitem)); + pack->type= list; + pack->content.ptr= temp; + + push(stack_head, pack); +} + + extern void nl() { printf("\n"); } +extern void quit() +{ + exit(EXIT_SUCCESS); +} + int main() { stackitem* s= NULL; @@ -250,3 +325,7 @@ return EXIT_SUCCESS; } + +/* Local Variables: */ +/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ +/* End: */