--- stack/stack.c 2002/02/14 19:49:48 1.82 +++ stack/stack.c 2002/02/15 14:44:24 1.86 @@ -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 @@ -61,6 +67,8 @@ 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 */ @@ -72,10 +80,14 @@ { int i; - env->in_string= NULL; - env->err= 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) { @@ -111,7 +123,7 @@ free(val->content.ptr); break; } - free(val); /* Free the actual list value */ + free(val); /* Free the actual value structure */ } } @@ -177,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); } @@ -190,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); } @@ -280,12 +292,12 @@ if(handle==NULL) /* If no handle */ handle= dlopen(NULL, RTLD_LAZY); - funcptr= dlsym(handle, in_string); /* Get function pointer */ + mangled=mangle_str(in_string); /* mangle the name */ + funcptr= dlsym(handle, mangled); /* and try to find it */ + free(mangled); dlerr=dlerror(); if(dlerr != NULL) { /* If no function was found */ - mangled=mangle_str(in_string); - funcptr= dlsym(handle, mangled); /* try mangling it */ - free(mangled); + funcptr= dlsym(handle, in_string); /* Get function pointer */ dlerr=dlerror(); } if(dlerr==NULL) { /* If a function was found */ @@ -609,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); @@ -738,6 +750,7 @@ extern void clear(environment *); void forget_sym(symbol **); +extern void words(environment *); /* Quit stack. */ extern void quit(environment *env) @@ -745,6 +758,7 @@ long i; clear(env); + if (env->err) return; for(i= 0; isymbols[i]!= NULL) { @@ -752,6 +766,12 @@ } env->symbols[i]= NULL; } + + if(env->free_string!=NULL) + free(env->free_string); + + muntrace(); + exit(EXIT_SUCCESS); } @@ -820,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]==';') { @@ -849,7 +904,7 @@ return EXIT_FAILURE; } -/* + */ +/* "+" */ extern void sx_2b(environment *env) { int a, b; size_t len; @@ -899,7 +954,7 @@ } } -/* - */ +/* "-" */ extern void sx_2d(environment *env) { int a, b; @@ -928,7 +983,7 @@ } } -/* > */ +/* ">" */ extern void sx_3e(environment *env) { int a, b; @@ -1002,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; @@ -1079,7 +1134,7 @@ eval(env); } -/* while */ +/* "while" */ extern void sx_7768696c65(environment *env) { int truth; @@ -1125,7 +1180,7 @@ free_val(loop); } -/* For-loop */ +/* "for"; For-loop */ extern void sx_666f72(environment *env) { value *loop, *foo; @@ -1164,7 +1219,7 @@ free_val(foo); } -/* 'to' */ +/* "to" */ extern void to(environment *env) { int i, start, ending; stackitem *temp_head; @@ -1201,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); @@ -1211,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"; @@ -1227,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 */ @@ -1244,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) { @@ -1272,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); }