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

Diff of /stack/stack.c

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

revision 1.1 by masse, Mon Jan 7 23:09:01 2002 UTC revision 1.6 by masse, Tue Jan 8 12:33:57 2002 UTC
# Line 4  Line 4 
4  #include <stdlib.h>  #include <stdlib.h>
5  /* NULL */  /* NULL */
6  #include <stddef.h>  #include <stddef.h>
7    /* dlopen, dlsym, dlerror */
8  #include <dlfcn.h>  #include <dlfcn.h>
9    
10  #define HASHTBLSIZE 65536  #define HASHTBLSIZE 65536
11    
12  typedef struct stack_item  typedef struct stack_item
13  {  {
14    enum {value, string, ref, func} type;    enum {value, string, ref, func, symbol} type;
15    union {    union {
16      void* ptr;      void* ptr;
17      int val;      int val;
# Line 109  void def_func(hashtbl in_hashtbl, funcp Line 110  void def_func(hashtbl in_hashtbl, funcp
110    mk_hashentry(in_hashtbl, temp, id);    mk_hashentry(in_hashtbl, temp, id);
111  }  }
112    
113    void def_sym(hashtbl in_hashtbl, const char* id)
114    {
115      stackitem* temp= malloc(sizeof(stackitem));
116      
117      temp->type= symbol;
118    
119      mk_hashentry(in_hashtbl, temp, id);
120    }
121    
122  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)  int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string)
123  {  {
124    void* handle;    static void* handle= NULL;
125    void* symbol;    void* symbol;
126      
127    stackitem* new_item= malloc(sizeof(stackitem));    stackitem* new_item= malloc(sizeof(stackitem));
128    new_item->content.ptr= *hash(in_hash, in_string);    new_item->content.ptr= *hash(in_hash, in_string);
129    new_item->type= ref;    new_item->type= ref;
130    
131    if(new_item->content.ptr==NULL) {    if(handle==NULL)
132      handle= dlopen(NULL, RTLD_LAZY);      handle= dlopen(NULL, RTLD_LAZY);
133    
134      if(new_item->content.ptr==NULL) {
135      symbol= dlsym(handle, in_string);      symbol= dlsym(handle, in_string);
136      if(dlerror()==NULL) {      if(dlerror()==NULL)
137        def_func(in_hash, symbol, in_string);        def_func(in_hash, symbol, in_string);
138        new_item->content.ptr= *hash(in_hash, in_string);      else
139        new_item->type= ref;        def_sym(in_hash, in_string);
140      }        
141        new_item->content.ptr= *hash(in_hash, in_string);
142        new_item->type= ref;
143    }    }
144    
145    push(stack_head, new_item);    push(stack_head, new_item);
# Line 147  void print_st(stackitem* stack_head, lon Line 161  void print_st(stackitem* stack_head, lon
161    if(stack_head->next != NULL)    if(stack_head->next != NULL)
162      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
163    
164    if(stack_head->type==value)    switch(stack_head->type){
165      case value:
166      printf("%ld: %d\n", counter, (int)stack_head->content.val);      printf("%ld: %d\n", counter, (int)stack_head->content.val);
167    else if(stack_head->type==string)      break;
168      case string:
169      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);
170    else      break;
171      case ref:
172        printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id);
173        break;
174      case func:
175      case symbol:
176      printf("%ld: %p\n", counter, stack_head->content.ptr);      printf("%ld: %p\n", counter, stack_head->content.ptr);
177        break;
178      }
179  }  }
180    
181  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
# Line 217  extern void print(stackitem** stack_head Line 240  extern void print(stackitem** stack_head
240    if((*stack_head)==NULL)    if((*stack_head)==NULL)
241      return;      return;
242    
243    if((*stack_head)->type==value)    switch((*stack_head)->type){
244      case value:
245      printf("%d", (*stack_head)->content.val);      printf("%d", (*stack_head)->content.val);
246    else if((*stack_head)->type==string)      break;
247      case string:
248      printf("%s", (char*)(*stack_head)->content.ptr);      printf("%s", (char*)(*stack_head)->content.ptr);
249    else      break;
250      case ref:
251        printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
252        break;
253      case symbol:
254      default:
255      printf("%p", (*stack_head)->content.ptr);      printf("%p", (*stack_head)->content.ptr);
256        break;
257      }
258    
259    toss(stack_head);    toss(stack_head);
260  }  }
# Line 232  extern void nl() Line 264  extern void nl()
264    printf("\n");    printf("\n");
265  }  }
266    
267    extern void quit()
268    {
269      exit(EXIT_SUCCESS);
270    }
271    
272  int main()  int main()
273  {  {
274    stackitem* s= NULL;    stackitem* s= NULL;
# Line 250  int main() Line 287  int main()
287    
288    return EXIT_SUCCESS;    return EXIT_SUCCESS;
289  }  }
290    
291    /* Local Variables: */
292    /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
293    /* End: */

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26