/[cvs]/stack/stack.c
ViewVC logotype

Diff of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.83 by masse, Thu Feb 14 22:44:49 2002 UTC revision 1.84 by teddy, Fri Feb 15 01:21:13 2002 UTC
# Line 1  Line 1 
1  /* printf, sscanf, fgets, fprintf */  /* printf, sscanf, fgets, fprintf, fopen, perror */
2  #include <stdio.h>  #include <stdio.h>
3  /* exit, EXIT_SUCCESS, malloc, free */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
# Line 8  Line 8 
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* strcmp, strcpy, strlen, strcat, strdup */  /* strcmp, strcpy, strlen, strcat, strdup */
10  #include <string.h>  #include <string.h>
11    /* getopt, STDIN_FILENO, STDOUT_FILENO */
12    #include <unistd.h>
13    /* EX_NOINPUT, EX_USAGE */
14    #include <sysexits.h>
15  /* mtrace, muntrace */  /* mtrace, muntrace */
16  #include <mcheck.h>  #include <mcheck.h>
17    
# Line 63  typedef struct { Line 67  typedef struct {
67    char *in_string;              /* Input pending to be read */    char *in_string;              /* Input pending to be read */
68    char *free_string;            /* Free this string when all input is    char *free_string;            /* Free this string when all input is
69                                     read from in_string */                                     read from in_string */
70      FILE *inputstream;            /* stdin or a file, most likely */
71      int interactive;              /* print prompts, stack, etc */
72  } environment;  } environment;
73    
74  /* A type for pointers to external functions */  /* A type for pointers to external functions */
# Line 74  void init_env(environment *env) Line 80  void init_env(environment *env)
80  {  {
81    int i;    int i;
82    
83    env->in_string= NULL;    env->head= NULL;
   env->err= 0;  
84    for(i= 0; i<HASHTBLSIZE; i++)    for(i= 0; i<HASHTBLSIZE; i++)
85      env->symbols[i]= NULL;      env->symbols[i]= NULL;
86      env->err= 0;
87      env->in_string= NULL;
88      env->free_string= NULL;
89      env->inputstream= stdin;
90      env->interactive= 1;
91  }  }
92    
93  void printerr(const char* in_string) {  void printerr(const char* in_string) {
# Line 749  extern void quit(environment *env) Line 759  extern void quit(environment *env)
759    
760    clear(env);    clear(env);
761    
   words(env);  
   
762    if (env->err) return;    if (env->err) return;
763    for(i= 0; i<HASHTBLSIZE; i++) {    for(i= 0; i<HASHTBLSIZE; i++) {
764      while(env->symbols[i]!= NULL) {      while(env->symbols[i]!= NULL) {
# Line 759  extern void quit(environment *env) Line 767  extern void quit(environment *env)
767      env->symbols[i]= NULL;      env->symbols[i]= NULL;
768    }    }
769    
   words(env);  
   
770    if(env->free_string!=NULL)    if(env->free_string!=NULL)
771      free(env->free_string);      free(env->free_string);
772        
# Line 834  extern void errn(environment *env){ Line 840  extern void errn(environment *env){
840    push_int(env, env->err);    push_int(env, env->err);
841  }  }
842    
843  extern void read(environment*);  extern void sx_72656164(environment*);
844    
845  int main()  int main(int argc, char **argv)
846  {  {
847    environment myenv;    environment myenv;
848    
849      int c;                        /* getopt option character */
850    
851    mtrace();    mtrace();
852    
853    init_env(&myenv);    init_env(&myenv);
854    
855      myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
856    
857      while ((c = getopt (argc, argv, "i")) != -1)
858        switch (c)
859          {
860          case 'i':
861            myenv.interactive = 1;
862            break;
863          case '?':
864            fprintf (stderr,
865                     "Unknown option character `\\x%x'.\n",
866                     optopt);
867            return EX_USAGE;
868          default:
869            abort ();
870          }
871      
872      if (optind < argc) {
873        myenv.interactive = 0;
874        myenv.inputstream= fopen(argv[optind], "r");
875        if(myenv.inputstream== NULL) {
876          perror(argv[0]);
877          exit (EX_NOINPUT);
878        }
879      }
880    
881    while(1) {    while(1) {
882      if(myenv.in_string==NULL) {      if(myenv.in_string==NULL && myenv.interactive) {
883        nl();        nl();
884        printstack(&myenv);        printstack(&myenv);
885        printf("> ");        printf("> ");
886      }      }
887      read(&myenv);      sx_72656164(&myenv);
888      if(myenv.err) {      if(myenv.err) {
889        printf("(error %d) ", myenv.err);        printf("(error %d) ", myenv.err);
890          if (myenv.err==4)
891            return EX_NOINPUT;
892        myenv.err=0;        myenv.err=0;
893      } else if(myenv.head!=NULL      } else if(myenv.head!=NULL
894                && myenv.head->item->type==symb                && myenv.head->item->type==symb
# Line 1018  value *copy_val(value *old_value){ Line 1054  value *copy_val(value *old_value){
1054    return new_value;    return new_value;
1055  }  }
1056    
1057  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1058  extern void dup(environment *env) {  extern void sx_647570(environment *env) {
1059    if((env->head)==NULL) {    if((env->head)==NULL) {
1060      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1061      env->err=1;      env->err=1;
# Line 1141  extern void sx_7768696c65(environment *e Line 1177  extern void sx_7768696c65(environment *e
1177    free_val(loop);    free_val(loop);
1178  }  }
1179    
1180  /* For-loop */  /* "for"; For-loop */
1181  extern void sx_666f72(environment *env) {  extern void sx_666f72(environment *env) {
1182        
1183    value *loop, *foo;    value *loop, *foo;
# Line 1227  extern void to(environment *env) { Line 1263  extern void to(environment *env) {
1263  extern void readline(environment *env) {  extern void readline(environment *env) {
1264    char in_string[101];    char in_string[101];
1265    
1266    fgets(in_string, 100, stdin);    if(fgets(in_string, 100, env->inputstream)==NULL)
1267    push_cstring(env, in_string);      push_cstring(env, "");
1268      else
1269        push_cstring(env, in_string);
1270  }  }
1271    
1272  /* Read a value and place on stack */  /* "read"; Read a value and place on stack */
1273  extern void read(environment *env) {  extern void sx_72656164(environment *env) {
1274    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1275    const char strform[]= "\"%[^\"]\"%n";    const char strform[]= "\"%[^\"]\"%n";
1276    const char intform[]= "%i%n";    const char intform[]= "%i%n";
# Line 1247  extern void read(environment *env) { Line 1285  extern void read(environment *env) {
1285    size_t inlength;    size_t inlength;
1286    
1287    if(env->in_string==NULL) {    if(env->in_string==NULL) {
1288      if(depth > 0) {      if(depth > 0 && env->interactive) {
1289        printf("]> ");        printf("]> ");
1290      }      }
1291      readline(env); if(env->err) return;      readline(env); if(env->err) return;
1292    
1293        if(((char *)(env->head->item->content.ptr))[0]=='\0'){
1294          env->err= 4;              /* EOF */
1295          return;
1296        }
1297            
1298      env->in_string= malloc(strlen(env->head->item->content.ptr)+1);      env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1299      env->free_string= env->in_string; /* Save the original pointer */      env->free_string= env->in_string; /* Save the original pointer */
# Line 1295  extern void read(environment *env) { Line 1338  extern void read(environment *env) {
1338    free(match);    free(match);
1339    
1340    if(depth)    if(depth)
1341      return read(env);      return sx_72656164(env);
1342  }  }

Legend:
Removed from v.1.83  
changed lines
  Added in v.1.84

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26