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

Diff of /stack/stack.c

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

revision 1.48 by teddy, Thu Feb 7 04:34:42 2002 UTC revision 1.54 by teddy, Fri Feb 8 00:25:57 2002 UTC
# Line 1  Line 1 
1  /* printf */  /* printf, sscanf, fgets, fprintf */
2  #include <stdio.h>  #include <stdio.h>
3  /* EXIT_SUCCESS */  /* exit, EXIT_SUCCESS, malloc, free */
4  #include <stdlib.h>  #include <stdlib.h>
5  /* NULL */  /* NULL */
6  #include <stddef.h>  #include <stddef.h>
7  /* dlopen, dlsym, dlerror */  /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9  /* assert */  /* strcmp, strcpy, strlen, strcat, strdup */
 #include <assert.h>  
 /* strcat */  
10  #include <string.h>  #include <string.h>
11    
12  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
# Line 101  void free_val(value *val){ Line 99  void free_val(value *val){
99        }        }
100        free(val);                /* Free the actual list value */        free(val);                /* Free the actual list value */
101        break;        break;
102      default:      case integer:
103        case func:
104        case symb:
105        break;        break;
106      }      }
107    }    }
# Line 198  void push_cstring(stackitem **stack_head Line 198  void push_cstring(stackitem **stack_head
198  }  }
199    
200  /* Mangle a symbol name to a valid C identifier name */  /* Mangle a symbol name to a valid C identifier name */
201  char *mangle_(const char *old_string){  char *mangle_str(const char *old_string){
202    char validchars[]    char validchars[]
203      ="0123456789abcdef";      ="0123456789abcdef";
204    char *new_string, *current;    char *new_string, *current;
205    
206    new_string=malloc(strlen(old_string)+4);    new_string=malloc((strlen(old_string)*2)+4);
207    strcpy(new_string, "sx_");    /* Stack eXternal */    strcpy(new_string, "sx_");    /* Stack eXternal */
208    current=new_string+3;    current=new_string+3;
209    while(old_string[0] != '\0'){    while(old_string[0] != '\0'){
210      current[0]=validchars[old_string[0]/16];      current[0]=validchars[(unsigned char)(old_string[0])/16];
211      current[1]=validchars[old_string[0]%16];      current[1]=validchars[(unsigned char)(old_string[0])%16];
212      current+=2;      current+=2;
213      old_string++;      old_string++;
214    }    }
# Line 233  extern void mangle(environment *env){ Line 233  extern void mangle(environment *env){
233      return;      return;
234    }    }
235    
236    new_string= mangle_((const char *)(env->head->item->content.ptr));    new_string= mangle_str((const char *)(env->head->item->content.ptr));
237    
238    toss(env);    toss(env);
239    if(env->err) return;    if(env->err) return;
# Line 296  void push_sym(environment *env, const ch Line 296  void push_sym(environment *env, const ch
296      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
297      dlerr=dlerror();      dlerr=dlerror();
298      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
299        mangled=mangle_(in_string);        mangled=mangle_str(in_string);
300        funcptr= dlsym(handle, mangled); /* try mangling it */        funcptr= dlsym(handle, mangled); /* try mangling it */
301        free(mangled);        free(mangled);
302        dlerr=dlerror();        dlerr=dlerror();
# Line 531  extern void eval(environment *env) Line 531  extern void eval(environment *env)
531      toss(env);      toss(env);
532      if(env->err) return;      if(env->err) return;
533      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
534      strcat(temp_string, "[ ");      strcpy(temp_string, "[ ");
535      strcat(temp_string, (char*)temp_val->content.ptr);      strcat(temp_string, (char*)temp_val->content.ptr);
536      strcat(temp_string, " ]");      strcat(temp_string, " ]");
537      stack_read(env, temp_string);      stack_read(env, temp_string);
# Line 541  extern void eval(environment *env) Line 541  extern void eval(environment *env)
541      free(temp_string);      free(temp_string);
542      break;      break;
543    
544    default:    case integer:
545        break;
546    }    }
547  }  }
548    
# Line 905  int main() Line 906  int main()
906  /* + */  /* + */
907  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
908    int a, b;    int a, b;
909      size_t len;
910      char* new_string;
911      value *a_val, *b_val;
912    
913    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
914      printerr("Too Few Arguments");      printerr("Too Few Arguments");
915      env->err=1;      env->err=1;
916      return;      return;
917    }    }
918    
919      if(env->head->item->type==string
920         && env->head->next->item->type==string) {
921        a_val= env->head->item;
922        b_val= env->head->next->item;
923        a_val->refcount++;
924        b_val->refcount++;
925        toss(env); if(env->err) return;
926        toss(env); if(env->err) return;
927        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
928        new_string= malloc(len);
929        strcpy(new_string, b_val->content.ptr);
930        strcat(new_string, a_val->content.ptr);
931        free_val(a_val); free_val(b_val);
932        push_cstring(&(env->head), new_string);
933        free(new_string);
934        return;
935      }
936        
937    if(env->head->item->type!=integer    if(env->head->item->type!=integer
938       || env->head->next->item->type!=integer) {       || env->head->next->item->type!=integer) {

Legend:
Removed from v.1.48  
changed lines
  Added in v.1.54

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26