--- stack/stack.c 2002/01/07 23:09:01 1.1.1.1 +++ stack/stack.c 2002/01/08 14:31:23 1.9 @@ -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) { - handle= dlopen(NULL, RTLD_LAZY); + if(handle==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); @@ -136,10 +150,48 @@ { 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); } +extern void nl() +{ + printf("\n"); +} + +void prin(stackitem** stack_head) +{ + if((*stack_head)==NULL) + return; + + switch((*stack_head)->type) { + case value: + printf("%d", (*stack_head)->content.val); + break; + case string: + printf("%s", (char*)(*stack_head)->content.ptr); + break; + case ref: + printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); + break; + case symbol: + default: + printf("%p", (*stack_head)->content.ptr); + break; + } +} + +extern void print(stackitem** stack_head) +{ + prin(stack_head); + toss(stack_head); +} /* print_stack(stack); */ void print_st(stackitem* stack_head, long counter) @@ -147,12 +199,9 @@ if(stack_head->next != NULL) print_st(stack_head->next, counter+1); - if(stack_head->type==value) - printf("%ld: %d\n", counter, (int)stack_head->content.val); - else if(stack_head->type==string) - printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); - else - printf("%ld: %p\n", counter, stack_head->content.ptr); + printf("%ld: ", counter); + prin(&stack_head); + nl(); } extern void printstack(stackitem** stack_head) @@ -212,24 +261,56 @@ return 1; } -extern void print(stackitem** stack_head) +extern void pack(stackitem** stack_head) { + void* delimiter; + stackitem *iterator, *temp, *pack; + if((*stack_head)==NULL) return; - if((*stack_head)->type==value) - printf("%d", (*stack_head)->content.val); - else if((*stack_head)->type==string) - printf("%s", (char*)(*stack_head)->content.ptr); - else - printf("%p", (*stack_head)->content.ptr); + 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 expand(stackitem** stack_head) +{ + stackitem *temp, *new_head; + + if((*stack_head)==NULL || (*stack_head)->type!=list) + return; + + new_head= temp= (*stack_head)->content.ptr; toss(stack_head); + + while(temp->next!=NULL) + temp= temp->next; + + temp->next= *stack_head; + *stack_head= new_head; } -extern void nl() +extern void quit() { - printf("\n"); + exit(EXIT_SUCCESS); } int main() @@ -250,3 +331,7 @@ return EXIT_SUCCESS; } + +/* Local Variables: */ +/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ +/* End: */