--- stack/stack.c 2002/02/14 12:20:09 1.80 +++ stack/stack.c 2002/02/15 12:45:28 1.85 @@ -1,4 +1,4 @@ -/* printf, sscanf, fgets, fprintf */ +/* printf, sscanf, fgets, fprintf, fopen, perror */ #include /* exit, EXIT_SUCCESS, malloc, free */ #include @@ -8,6 +8,12 @@ #include /* strcmp, strcpy, strlen, strcat, strdup */ #include +/* getopt, STDIN_FILENO, STDOUT_FILENO */ +#include +/* EX_NOINPUT, EX_USAGE */ +#include +/* mtrace, muntrace */ +#include #define HASHTBLSIZE 2048 @@ -58,10 +64,11 @@ stackitem *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ int err; /* Error flag */ - int non_eval_flag; char *in_string; /* Input pending to be read */ char *free_string; /* Free this string when all input is read from in_string */ + FILE *inputstream; /* stdin or a file, most likely */ + int interactive; /* print prompts, stack, etc */ } environment; /* A type for pointers to external functions */ @@ -73,11 +80,14 @@ { int i; - env->in_string= NULL; - env->err= 0; - env->non_eval_flag= 0; + env->head= NULL; for(i= 0; isymbols[i]= NULL; + env->err= 0; + env->in_string= NULL; + env->free_string= NULL; + env->inputstream= stdin; + env->interactive= 1; } void printerr(const char* in_string) { @@ -102,13 +112,18 @@ free(item); /* free the stackitem */ item=temp; /* go to next stackitem */ } - free(val); /* Free the actual list value */ break; case integer: case func: + break; case symb: + free(((symbol*)(val->content.ptr))->id); + if(((symbol*)(val->content.ptr))->val!=NULL) + free_val(((symbol*)(val->content.ptr))->val); + free(val->content.ptr); break; } + free(val); /* Free the actual value structure */ } } @@ -174,7 +189,7 @@ new_value->content.val= in_val; new_value->type= integer; - new_value->refcount=1; + new_value->refcount= 0; push_val(env, new_value); } @@ -187,7 +202,7 @@ new_value->content.ptr= malloc(strlen(in_string)+1); strcpy(new_value->content.ptr, in_string); new_value->type= string; - new_value->refcount=1; + new_value->refcount= 0; push_val(env, new_value); } @@ -213,7 +228,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -233,12 +247,7 @@ toss(env); if(env->err) return; - new_value= malloc(sizeof(value)); - new_value->content.ptr= new_string; - new_value->type= string; - new_value->refcount=1; - - push_val(env, new_value); + push_cstring(env, new_string); } /* Push a symbol onto the stack. */ @@ -612,7 +621,7 @@ pack= malloc(sizeof(value)); pack->type= list; pack->content.ptr= temp; - pack->refcount= 1; + pack->refcount= 0; push_val(env, pack); rev(env); @@ -741,6 +750,7 @@ extern void clear(environment *); void forget_sym(symbol **); +extern void words(environment *); /* Quit stack. */ extern void quit(environment *env) @@ -748,6 +758,7 @@ long i; clear(env); + if (env->err) return; for(i= 0; isymbols[i]!= NULL) { @@ -755,6 +766,12 @@ } env->symbols[i]= NULL; } + + if(env->free_string!=NULL) + free(env->free_string); + + muntrace(); + exit(EXIT_SUCCESS); } @@ -823,24 +840,59 @@ push_int(env, env->err); } -extern void read(environment*); +extern void sx_72656164(environment*); -int main() +int main(int argc, char **argv) { environment myenv; + int c; /* getopt option character */ + + mtrace(); + init_env(&myenv); + myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); + + while ((c = getopt (argc, argv, "i")) != -1) + switch (c) + { + case 'i': + myenv.interactive = 1; + break; + case '?': + fprintf (stderr, + "Unknown option character `\\x%x'.\n", + optopt); + return EX_USAGE; + default: + abort (); + } + + if (optind < argc) { + myenv.interactive = 0; + myenv.inputstream= fopen(argv[optind], "r"); + if(myenv.inputstream== NULL) { + perror(argv[0]); + exit (EX_NOINPUT); + } + } + while(1) { if(myenv.in_string==NULL) { - nl(); - printstack(&myenv); - printf("> "); - } - read(&myenv); - if(myenv.err) { - printf("(error %d) ", myenv.err); + if (myenv.interactive) { + if(myenv.err) { + printf("(error %d)\n", myenv.err); + } + nl(); + printstack(&myenv); + printf("> "); + } myenv.err=0; + } + sx_72656164(&myenv); + if (myenv.err==4) { + return EX_NOINPUT; } else if(myenv.head!=NULL && myenv.head->item->type==symb && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { @@ -852,7 +904,7 @@ return EXIT_FAILURE; } -/* + */ +/* "+" */ extern void sx_2b(environment *env) { int a, b; size_t len; @@ -902,7 +954,7 @@ } } -/* - */ +/* "-" */ extern void sx_2d(environment *env) { int a, b; @@ -931,7 +983,7 @@ } } -/* > */ +/* ">" */ extern void sx_3e(environment *env) { int a, b; @@ -1005,8 +1057,8 @@ return new_value; } -/* duplicates an item on the stack */ -extern void dup(environment *env) { +/* "dup"; duplicates an item on the stack */ +extern void sx_647570(environment *env) { if((env->head)==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -1082,7 +1134,7 @@ eval(env); } -/* while */ +/* "while" */ extern void sx_7768696c65(environment *env) { int truth; @@ -1128,7 +1180,7 @@ free_val(loop); } -/* For-loop */ +/* "for"; For-loop */ extern void sx_666f72(environment *env) { value *loop, *foo; @@ -1167,7 +1219,7 @@ free_val(foo); } -/* 'to' */ +/* "to" */ extern void to(environment *env) { int i, start, ending; stackitem *temp_head; @@ -1204,7 +1256,7 @@ temp_val= malloc(sizeof(value)); temp_val->content.ptr= env->head; - temp_val->refcount= 1; + temp_val->refcount= 0; temp_val->type= list; env->head= temp_head; push_val(env, temp_val); @@ -1214,12 +1266,14 @@ extern void readline(environment *env) { char in_string[101]; - fgets(in_string, 100, stdin); - push_cstring(env, in_string); + if(fgets(in_string, 100, env->inputstream)==NULL) + push_cstring(env, ""); + else + push_cstring(env, in_string); } -/* Read a value and place on stack */ -extern void read(environment *env) { +/* "read"; Read a value and place on stack */ +extern void sx_72656164(environment *env) { const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; const char strform[]= "\"%[^\"]\"%n"; const char intform[]= "%i%n"; @@ -1230,14 +1284,19 @@ int itemp, readlength= -1; static int depth= 0; - char *rest, *match; + char *match; size_t inlength; if(env->in_string==NULL) { - if(depth > 0) { + if(depth > 0 && env->interactive) { printf("]> "); } readline(env); if(env->err) return; + + if(((char *)(env->head->item->content.ptr))[0]=='\0'){ + env->err= 4; /* "" means EOF */ + return; + } env->in_string= malloc(strlen(env->head->item->content.ptr)+1); env->free_string= env->in_string; /* Save the original pointer */ @@ -1247,7 +1306,6 @@ inlength= strlen(env->in_string)+1; match= malloc(inlength); - rest= malloc(inlength); if(sscanf(env->in_string, blankform, &readlength)!=EOF && readlength != -1) { @@ -1275,12 +1333,13 @@ } else { free(env->free_string); env->in_string = env->free_string = NULL; - free(match); } if ( env->in_string != NULL) { env->in_string += readlength; } + free(match); + if(depth) - return read(env); + return sx_72656164(env); }