/[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.53 by teddy, Thu Feb 7 23:56:54 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 198  void push_cstring(stackitem **stack_head Line 196  void push_cstring(stackitem **stack_head
196  }  }
197    
198  /* Mangle a symbol name to a valid C identifier name */  /* Mangle a symbol name to a valid C identifier name */
199  char *mangle_(const char *old_string){  char *mangle_str(const char *old_string){
200    char validchars[]    char validchars[]
201      ="0123456789abcdef";      ="0123456789abcdef";
202    char *new_string, *current;    char *new_string, *current;
203    
204    new_string=malloc(strlen(old_string)+4);    new_string=malloc((strlen(old_string)*2)+4);
205    strcpy(new_string, "sx_");    /* Stack eXternal */    strcpy(new_string, "sx_");    /* Stack eXternal */
206    current=new_string+3;    current=new_string+3;
207    while(old_string[0] != '\0'){    while(old_string[0] != '\0'){
208      current[0]=validchars[old_string[0]/16];      current[0]=validchars[(unsigned char)(old_string[0])/16];
209      current[1]=validchars[old_string[0]%16];      current[1]=validchars[(unsigned char)(old_string[0])%16];
210      current+=2;      current+=2;
211      old_string++;      old_string++;
212    }    }
# Line 233  extern void mangle(environment *env){ Line 231  extern void mangle(environment *env){
231      return;      return;
232    }    }
233    
234    new_string= mangle_((const char *)(env->head->item->content.ptr));    new_string= mangle_str((const char *)(env->head->item->content.ptr));
235    
236    toss(env);    toss(env);
237    if(env->err) return;    if(env->err) return;
# Line 296  void push_sym(environment *env, const ch Line 294  void push_sym(environment *env, const ch
294      funcptr= dlsym(handle, in_string); /* Get function pointer */      funcptr= dlsym(handle, in_string); /* Get function pointer */
295      dlerr=dlerror();      dlerr=dlerror();
296      if(dlerr != NULL) {         /* If no function was found */      if(dlerr != NULL) {         /* If no function was found */
297        mangled=mangle_(in_string);        mangled=mangle_str(in_string);
298        funcptr= dlsym(handle, mangled); /* try mangling it */        funcptr= dlsym(handle, mangled); /* try mangling it */
299        free(mangled);        free(mangled);
300        dlerr=dlerror();        dlerr=dlerror();
# Line 531  extern void eval(environment *env) Line 529  extern void eval(environment *env)
529      toss(env);      toss(env);
530      if(env->err) return;      if(env->err) return;
531      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);      temp_string= malloc(strlen((char*)temp_val->content.ptr)+5);
532      strcat(temp_string, "[ ");      strcpy(temp_string, "[ ");
533      strcat(temp_string, (char*)temp_val->content.ptr);      strcat(temp_string, (char*)temp_val->content.ptr);
534      strcat(temp_string, " ]");      strcat(temp_string, " ]");
535      stack_read(env, temp_string);      stack_read(env, temp_string);
# Line 905  int main() Line 903  int main()
903  /* + */  /* + */
904  extern void sx_2b(environment *env) {  extern void sx_2b(environment *env) {
905    int a, b;    int a, b;
906      size_t len;
907      char* new_string;
908      value *a_val, *b_val;
909    
910    if((env->head)==NULL || env->head->next==NULL) {    if((env->head)==NULL || env->head->next==NULL) {
911      printerr("Too Few Arguments");      printerr("Too Few Arguments");
912      env->err=1;      env->err=1;
913      return;      return;
914    }    }
915    
916      if(env->head->item->type==string
917         && env->head->next->item->type==string) {
918        a_val= env->head->item;
919        b_val= env->head->next->item;
920        a_val->refcount++;
921        b_val->refcount++;
922        toss(env); if(env->err) return;
923        toss(env); if(env->err) return;
924        len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1;
925        new_string= malloc(len);
926        strcpy(new_string, b_val->content.ptr);
927        strcat(new_string, a_val->content.ptr);
928        free_val(a_val); free_val(b_val);
929        push_cstring(&(env->head), new_string);
930        free(new_string);
931        return;
932      }
933        
934    if(env->head->item->type!=integer    if(env->head->item->type!=integer
935       || env->head->next->item->type!=integer) {       || env->head->next->item->type!=integer) {

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26