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

Diff of /stack/stack.c

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

revision 1.81 by masse, Thu Feb 14 19:20:28 2002 UTC revision 1.86 by teddy, Fri Feb 15 14:44:24 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 */
16    #include <mcheck.h>
17    
18  #define HASHTBLSIZE 2048  #define HASHTBLSIZE 2048
19    
# Line 61  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 72  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 103  void free_val(value *val){ Line 115  void free_val(value *val){
115        break;        break;
116      case integer:      case integer:
117      case func:      case func:
118          break;
119      case symb:      case symb:
120          free(((symbol*)(val->content.ptr))->id);
121          if(((symbol*)(val->content.ptr))->val!=NULL)
122            free_val(((symbol*)(val->content.ptr))->val);
123          free(val->content.ptr);
124        break;        break;
125      }      }
126      if(val->id!=NULL)      free(val);          /* Free the actual value structure */
       free(val->id);  
     free(val);          /* Free the actual list value */  
127    }    }
128  }  }
129    
# Line 174  void push_int(environment *env, int in_v Line 189  void push_int(environment *env, int in_v
189        
190    new_value->content.val= in_val;    new_value->content.val= in_val;
191    new_value->type= integer;    new_value->type= integer;
192    new_value->refcount= 1;    new_value->refcount= 0;
   new_value->id= NULL;  
193    
194    push_val(env, new_value);    push_val(env, new_value);
195  }  }
# Line 188  void push_cstring(environment *env, cons Line 202  void push_cstring(environment *env, cons
202    new_value->content.ptr= malloc(strlen(in_string)+1);    new_value->content.ptr= malloc(strlen(in_string)+1);
203    strcpy(new_value->content.ptr, in_string);    strcpy(new_value->content.ptr, in_string);
204    new_value->type= string;    new_value->type= string;
205    new_value->refcount= 1;    new_value->refcount= 0;
   new_value->id= NULL;  
206    
207    push_val(env, new_value);    push_val(env, new_value);
208  }  }
# Line 279  void push_sym(environment *env, const ch Line 292  void push_sym(environment *env, const ch
292      if(handle==NULL)            /* If no handle */      if(handle==NULL)            /* If no handle */
293        handle= dlopen(NULL, RTLD_LAZY);        handle= dlopen(NULL, RTLD_LAZY);
294    
295      funcptr= dlsym(handle, in_string); /* Get function pointer */      mangled=mangle_str(in_string); /* mangle the name */
296        funcptr= dlsym(handle, mangled); /* and try to find it */
297        free(mangled);
298      dlerr=dlerror();      dlerr=dlerror();
299      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
300        mangled=mangle_str(in_string);        funcptr= dlsym(handle, in_string); /* Get function pointer */
       funcptr= dlsym(handle, mangled); /* try mangling it */  
       free(mangled);  
301        dlerr=dlerror();        dlerr=dlerror();
302      }      }
303      if(dlerr==NULL) {           /* If a function was found */      if(dlerr==NULL) {           /* If a function was found */
# Line 608  extern void pack(environment *env) Line 621  extern void pack(environment *env)
621    pack= malloc(sizeof(value));    pack= malloc(sizeof(value));
622    pack->type= list;    pack->type= list;
623    pack->content.ptr= temp;    pack->content.ptr= temp;
624    pack->refcount= 1;    pack->refcount= 0;
625    
626    push_val(env, pack);    push_val(env, pack);
627    rev(env);    rev(env);
# Line 737  extern void def(environment *env) Line 750  extern void def(environment *env)
750    
751  extern void clear(environment *);  extern void clear(environment *);
752  void forget_sym(symbol **);  void forget_sym(symbol **);
753    extern void words(environment *);
754    
755  /* Quit stack. */  /* Quit stack. */
756  extern void quit(environment *env)  extern void quit(environment *env)
# Line 744  extern void quit(environment *env) Line 758  extern void quit(environment *env)
758    long i;    long i;
759    
760    clear(env);    clear(env);
761    
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 751  extern void quit(environment *env) Line 766  extern void quit(environment *env)
766      }      }
767      env->symbols[i]= NULL;      env->symbols[i]= NULL;
768    }    }
769    
770      if(env->free_string!=NULL)
771        free(env->free_string);
772      
773      muntrace();
774    
775    exit(EXIT_SUCCESS);    exit(EXIT_SUCCESS);
776  }  }
777    
# Line 819  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();
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) {
883        nl();        if (myenv.interactive) {
884        printstack(&myenv);          if(myenv.err) {
885        printf("> ");            printf("(error %d)\n", myenv.err);
886      }          }
887      read(&myenv);          nl();
888      if(myenv.err) {          printstack(&myenv);
889        printf("(error %d) ", myenv.err);          printf("> ");
890          }
891        myenv.err=0;        myenv.err=0;
892        }
893        sx_72656164(&myenv);
894        if (myenv.err==4) {
895          return EX_NOINPUT;
896      } else if(myenv.head!=NULL      } else if(myenv.head!=NULL
897                && myenv.head->item->type==symb                && myenv.head->item->type==symb
898                && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {                && ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') {
# Line 848  int main() Line 904  int main()
904    return EXIT_FAILURE;    return EXIT_FAILURE;
905  }  }
906    
907  /* + */  /* "+" */
908  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
909    int a, b;    int a, b;
910    size_t len;    size_t len;
# Line 898  extern void sx_2b(environment *env) { Line 954  extern void sx_2b(environment *env) {
954    }    }
955  }  }
956    
957  /* - */  /* "-" */
958  extern void sx_2d(environment *env) {  extern void sx_2d(environment *env) {
959    int a, b;    int a, b;
960    
# Line 927  extern void sx_2d(environment *env) { Line 983  extern void sx_2d(environment *env) {
983    }    }
984  }  }
985    
986  /* > */  /* ">" */
987  extern void sx_3e(environment *env) {  extern void sx_3e(environment *env) {
988    int a, b;    int a, b;
989    
# Line 1001  value *copy_val(value *old_value){ Line 1057  value *copy_val(value *old_value){
1057    return new_value;    return new_value;
1058  }  }
1059    
1060  /* duplicates an item on the stack */  /* "dup"; duplicates an item on the stack */
1061  extern void dup(environment *env) {  extern void sx_647570(environment *env) {
1062    if((env->head)==NULL) {    if((env->head)==NULL) {
1063      printerr("Too Few Arguments");      printerr("Too Few Arguments");
1064      env->err=1;      env->err=1;
# Line 1078  extern void ifelse(environment *env) { Line 1134  extern void ifelse(environment *env) {
1134    eval(env);    eval(env);
1135  }  }
1136    
1137  /* while */  /* "while" */
1138  extern void sx_7768696c65(environment *env) {  extern void sx_7768696c65(environment *env) {
1139    
1140    int truth;    int truth;
# Line 1124  extern void sx_7768696c65(environment *e Line 1180  extern void sx_7768696c65(environment *e
1180    free_val(loop);    free_val(loop);
1181  }  }
1182    
1183  /* For-loop */  /* "for"; For-loop */
1184  extern void sx_666f72(environment *env) {  extern void sx_666f72(environment *env) {
1185        
1186    value *loop, *foo;    value *loop, *foo;
# Line 1163  extern void sx_666f72(environment *env) Line 1219  extern void sx_666f72(environment *env)
1219    free_val(foo);    free_val(foo);
1220  }  }
1221    
1222  /* 'to' */  /* "to" */
1223  extern void to(environment *env) {  extern void to(environment *env) {
1224    int i, start, ending;    int i, start, ending;
1225    stackitem *temp_head;    stackitem *temp_head;
# Line 1200  extern void to(environment *env) { Line 1256  extern void to(environment *env) {
1256    
1257    temp_val= malloc(sizeof(value));    temp_val= malloc(sizeof(value));
1258    temp_val->content.ptr= env->head;    temp_val->content.ptr= env->head;
1259    temp_val->refcount= 1;    temp_val->refcount= 0;
1260    temp_val->type= list;    temp_val->type= list;
1261    env->head= temp_head;    env->head= temp_head;
1262    push_val(env, temp_val);    push_val(env, temp_val);
# Line 1210  extern void to(environment *env) { Line 1266  extern void to(environment *env) {
1266  extern void readline(environment *env) {  extern void readline(environment *env) {
1267    char in_string[101];    char in_string[101];
1268    
1269    fgets(in_string, 100, stdin);    if(fgets(in_string, 100, env->inputstream)==NULL)
1270    push_cstring(env, in_string);      push_cstring(env, "");
1271      else
1272        push_cstring(env, in_string);
1273  }  }
1274    
1275  /* Read a value and place on stack */  /* "read"; Read a value and place on stack */
1276  extern void read(environment *env) {  extern void sx_72656164(environment *env) {
1277    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";    const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n";
1278    const char strform[]= "\"%[^\"]\"%n";    const char strform[]= "\"%[^\"]\"%n";
1279    const char intform[]= "%i%n";    const char intform[]= "%i%n";
# Line 1226  extern void read(environment *env) { Line 1284  extern void read(environment *env) {
1284    
1285    int itemp, readlength= -1;    int itemp, readlength= -1;
1286    static int depth= 0;    static int depth= 0;
1287    char *rest, *match;    char *match;
1288    size_t inlength;    size_t inlength;
1289    
1290    if(env->in_string==NULL) {    if(env->in_string==NULL) {
1291      if(depth > 0) {      if(depth > 0 && env->interactive) {
1292        printf("]> ");        printf("]> ");
1293      }      }
1294      readline(env); if(env->err) return;      readline(env); if(env->err) return;
1295    
1296        if(((char *)(env->head->item->content.ptr))[0]=='\0'){
1297          env->err= 4;              /* "" means EOF */
1298          return;
1299        }
1300            
1301      env->in_string= malloc(strlen(env->head->item->content.ptr)+1);      env->in_string= malloc(strlen(env->head->item->content.ptr)+1);
1302      env->free_string= env->in_string; /* Save the original pointer */      env->free_string= env->in_string; /* Save the original pointer */
# Line 1243  extern void read(environment *env) { Line 1306  extern void read(environment *env) {
1306        
1307    inlength= strlen(env->in_string)+1;    inlength= strlen(env->in_string)+1;
1308    match= malloc(inlength);    match= malloc(inlength);
   rest= malloc(inlength);  
1309    
1310    if(sscanf(env->in_string, blankform, &readlength)!=EOF    if(sscanf(env->in_string, blankform, &readlength)!=EOF
1311       && readlength != -1) {       && readlength != -1) {
# Line 1271  extern void read(environment *env) { Line 1333  extern void read(environment *env) {
1333    } else {    } else {
1334      free(env->free_string);      free(env->free_string);
1335      env->in_string = env->free_string = NULL;      env->in_string = env->free_string = NULL;
     free(match);  
1336    }    }
1337    if ( env->in_string != NULL) {    if ( env->in_string != NULL) {
1338      env->in_string += readlength;      env->in_string += readlength;
1339    }    }
1340    
1341      free(match);
1342    
1343    if(depth)    if(depth)
1344      return read(env);      return sx_72656164(env);
1345  }  }

Legend:
Removed from v.1.81  
changed lines
  Added in v.1.86

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26