--- stack/stack.c 2002/01/31 22:32:59 1.16 +++ stack/stack.c 2002/02/01 23:30:55 1.18 @@ -6,6 +6,8 @@ #include /* dlopen, dlsym, dlerror */ #include +/* assert */ +#include #define HASHTBLSIZE 65536 @@ -13,13 +15,14 @@ { enum { value, /* Integer */ - string, - ref, /* Reference (to an element in the hash table) */ + string, + ref, /* Reference (to an element in the + hash table) */ func, /* Function pointer */ symbol, list - } type; /* Tells what kind of stack element */ - + } type; /* Type of stack element */ + union { void* ptr; /* Pointer to the content */ int val; /* ...or an integer */ @@ -31,10 +34,10 @@ typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ -typedef void (*funcp)(stackitem**); /* Function pointer declaration */ - +typedef void (*funcp)(stackitem**); /* funcp is a pointer to a + void function (stackitem **) */ -/* Initiates a newly created hash table. */ +/* Initialize a newly created hash table. */ void init_hashtbl(hashtbl out_hash) { long i; @@ -48,7 +51,7 @@ { long i= 0; unsigned long out_hash= 0; - char key= 0; + char key= '\0'; stackitem** position; while(1){ /* Hash in_string */ @@ -61,15 +64,17 @@ out_hash= out_hash%HASHTBLSIZE; position= &(in_hashtbl[out_hash]); - while(1){ /* Return position if empty */ - if(*position==NULL) + while(position != NULL){ + if(*position==NULL) /* If empty */ return position; - if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ + if(strcmp(in_string, (*position)->id)==0) /* If match */ return position; position= &((*position)->next); /* Try next */ } + return NULL; /* end of list reached without finding + an empty position */ } /* Generic push function. */ @@ -84,6 +89,7 @@ int push_val(stackitem** stack_head, int in_val) { stackitem* new_item= malloc(sizeof(stackitem)); + assert(new_item != NULL); new_item->content.val= in_val; new_item->type= value; @@ -114,7 +120,7 @@ return 1; } -/* Define a function a new function in the hash table. */ +/* Define a new function in the hash table. */ void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) { stackitem* temp= malloc(sizeof(stackitem)); @@ -162,13 +168,19 @@ return 1; } +void printerr(const char* in_string) { + fprintf(stderr, "Err: %s\n", in_string); +} + /* Discard the top element of the stack. */ extern void toss(stackitem** stack_head) { stackitem* temp= *stack_head; - if((*stack_head)==NULL) + if((*stack_head)==NULL) { + printerr("Stack empty"); return; + } if((*stack_head)->type==string) free((*stack_head)->content.ptr); @@ -186,8 +198,10 @@ /* Prints the top element of the stack. */ void print_(stackitem** stack_head) { - if((*stack_head)==NULL) + if((*stack_head)==NULL) { + printerr("Stack empty"); return; + } switch((*stack_head)->type) { case value: @@ -230,6 +244,8 @@ if(*stack_head != NULL) { print_st(*stack_head, 1); printf("\n"); + } else { + printerr("Stack empty"); } } @@ -239,15 +255,18 @@ { funcp in_func; - if((*stack_head)==NULL || (*stack_head)->type!=ref) + if((*stack_head)==NULL || (*stack_head)->type!=ref) { + printerr("Stack empty or not a reference"); return; + } if(((stackitem*)(*stack_head)->content.ptr)->type==func) { in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; toss(stack_head); (*in_func)(stack_head); return; - } + } else + printerr("Not a function"); } /* Parse input. */ @@ -310,8 +329,10 @@ void* delimiter; stackitem *iterator, *temp, *pack; - if((*stack_head)==NULL) /* No delimiter */ + if((*stack_head)==NULL) { + printerr("Stack empty"); return; + } delimiter= (*stack_head)->content.ptr; /* Get delimiter */ toss(stack_head); @@ -344,8 +365,10 @@ stackitem *temp, *new_head; /* Is top element a list? */ - if((*stack_head)==NULL || (*stack_head)->type!=list) + if((*stack_head)==NULL || (*stack_head)->type!=list) { + printerr("Stack empty or not a list"); return; + } /* The first list element is the new stack head */ new_head= temp= (*stack_head)->content.ptr; @@ -365,7 +388,12 @@ { stackitem* temp= (*stack_head); - if((*stack_head)==NULL || (*stack_head)->next==NULL) + if((*stack_head)==NULL) { + printerr("Stack empty"); + return; + } + + if((*stack_head)->next==NULL) return; *stack_head= (*stack_head)->next; @@ -379,8 +407,10 @@ void *left, *right; int result; - if((*stack_head)==NULL || (*stack_head)->next==NULL) + if((*stack_head)==NULL || (*stack_head)->next==NULL) { + printerr("Not enough elements to compare"); return; + } left= (*stack_head)->content.ptr; swap(stack_head); @@ -396,8 +426,10 @@ { int value; - if((*stack_head)==NULL || (*stack_head)->type!=value) + if((*stack_head)==NULL || (*stack_head)->type!=value) { + printerr("Stack empty or element is not a value"); return; + } value= (*stack_head)->content.val; toss(stack_head); @@ -418,8 +450,10 @@ stackitem *temp, *value; if(*stack_head==NULL || (*stack_head)->next==NULL - || (*stack_head)->type!=ref) + || (*stack_head)->type!=ref) { + printerr("Define what?"); return; + } temp= (*stack_head)->content.ptr; value= (*stack_head)->next;