/[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.1.1 by masse, Mon Jan 7 23:09:01 2002 UTC revision 1.5 by teddy, Tue Jan 8 00:03:16 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;    void* handle;
# Line 121  int push_ref(stackitem** stack_head, has Line 131  int push_ref(stackitem** stack_head, has
131    if(new_item->content.ptr==NULL) {    if(new_item->content.ptr==NULL) {
132      handle= dlopen(NULL, RTLD_LAZY);      handle= dlopen(NULL, RTLD_LAZY);
133      symbol= dlsym(handle, in_string);      symbol= dlsym(handle, in_string);
134      if(dlerror()==NULL) {      if(dlerror()==NULL)
135        def_func(in_hash, symbol, in_string);        def_func(in_hash, symbol, in_string);
136        new_item->content.ptr= *hash(in_hash, in_string);      else
137        new_item->type= ref;        def_sym(in_hash, in_string);
138      }        
139        new_item->content.ptr= *hash(in_hash, in_string);
140        new_item->type= ref;
141    }    }
142    
143    push(stack_head, new_item);    push(stack_head, new_item);
# Line 147  void print_st(stackitem* stack_head, lon Line 159  void print_st(stackitem* stack_head, lon
159    if(stack_head->next != NULL)    if(stack_head->next != NULL)
160      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
161    
162    if(stack_head->type==value)    switch(stack_head->type){
163      case value:
164      printf("%ld: %d\n", counter, (int)stack_head->content.val);      printf("%ld: %d\n", counter, (int)stack_head->content.val);
165    else if(stack_head->type==string)      break;
166      case string:
167      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);      printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);
168    else      break;
169      case ref:
170      case func:
171      case symbol:
172      printf("%ld: %p\n", counter, stack_head->content.ptr);      printf("%ld: %p\n", counter, stack_head->content.ptr);
173        break;
174      }
175  }  }
176    
177  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
# Line 217  extern void print(stackitem** stack_head Line 236  extern void print(stackitem** stack_head
236    if((*stack_head)==NULL)    if((*stack_head)==NULL)
237      return;      return;
238    
239    if((*stack_head)->type==value)    switch((*stack_head)->type){
240      case value:
241      printf("%d", (*stack_head)->content.val);      printf("%d", (*stack_head)->content.val);
242    else if((*stack_head)->type==string)      break;
243      case string:
244      printf("%s", (char*)(*stack_head)->content.ptr);      printf("%s", (char*)(*stack_head)->content.ptr);
245    else      break;
246      case symbol:
247        printf("%s", (*stack_head)->id);
248      case ref:
249      default:
250      printf("%p", (*stack_head)->content.ptr);      printf("%p", (*stack_head)->content.ptr);
251        break;
252      }
253    
254    toss(stack_head);    toss(stack_head);
255  }  }
# Line 232  extern void nl() Line 259  extern void nl()
259    printf("\n");    printf("\n");
260  }  }
261    
262    extern void quit()
263    {
264      exit(EXIT_SUCCESS);
265    }
266    
267  int main()  int main()
268  {  {
269    stackitem* s= NULL;    stackitem* s= NULL;
# Line 250  int main() Line 282  int main()
282    
283    return EXIT_SUCCESS;    return EXIT_SUCCESS;
284  }  }
285    
286    /* Local Variables: */
287    /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
288    /* End: */

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.5

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26