--- stack/stack.c 2002/01/31 22:32:59 1.16 +++ stack/stack.c 2002/01/31 23:09:07 1.17 @@ -162,13 +162,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 +192,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 +238,8 @@ if(*stack_head != NULL) { print_st(*stack_head, 1); printf("\n"); + } else { + printerr("Stack empty"); } } @@ -239,15 +249,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 +323,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 +359,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 +382,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 +401,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 +420,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 +444,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;