/[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.13 by masse, Wed Jan 9 05:01:24 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, list} 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(new_item->content.ptr==NULL) {
132      handle= dlopen(NULL, RTLD_LAZY);      if(handle==NULL)
133          handle= dlopen(NULL, RTLD_LAZY);    
134    
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 136  extern void toss(stackitem** stack_head) Line 150  extern void toss(stackitem** stack_head)
150  {  {
151    stackitem* temp= *stack_head;    stackitem* temp= *stack_head;
152    
153      if((*stack_head)==NULL)
154        return;
155      
156      if((*stack_head)->type==string)
157        free((*stack_head)->content.ptr);
158    
159    *stack_head= (*stack_head)->next;    *stack_head= (*stack_head)->next;
160    free(temp);    free(temp);
161  }  }
162    
163    extern void nl()
164    {
165      printf("\n");
166    }
167    
168    void print_(stackitem** stack_head)
169    {
170      if((*stack_head)==NULL)
171        return;
172    
173      switch((*stack_head)->type) {
174      case value:
175        printf("%d", (*stack_head)->content.val);
176        break;
177      case string:
178        printf("%s", (char*)(*stack_head)->content.ptr);
179        break;
180      case ref:
181        printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id);
182        break;
183      case symbol:
184      default:
185        printf("%p", (*stack_head)->content.ptr);
186        break;
187      }
188    }
189    
190    extern void print(stackitem** stack_head)
191    {
192      print_(stack_head);
193      toss(stack_head);
194    }
195    
196  /* print_stack(stack); */  /* print_stack(stack); */
197  void print_st(stackitem* stack_head, long counter)  void print_st(stackitem* stack_head, long counter)
# Line 147  void print_st(stackitem* stack_head, lon Line 199  void print_st(stackitem* stack_head, lon
199    if(stack_head->next != NULL)    if(stack_head->next != NULL)
200      print_st(stack_head->next, counter+1);      print_st(stack_head->next, counter+1);
201    
202    if(stack_head->type==value)    printf("%ld: ", counter);
203      printf("%ld: %d\n", counter, (int)stack_head->content.val);    print_(&stack_head);
204    else if(stack_head->type==string)    nl();
     printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);  
   else  
     printf("%ld: %p\n", counter, stack_head->content.ptr);  
205  }  }
206    
207  extern void printstack(stackitem** stack_head)  extern void printstack(stackitem** stack_head)
# Line 193  int stack_read(stackitem** stack_head, h Line 242  int stack_read(stackitem** stack_head, h
242      push_cstring(stack_head, temp);      push_cstring(stack_head, temp);
243    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)    else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)
244      push_val(stack_head, itemp);      push_val(stack_head, itemp);
245    else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)    else if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest)) >= 1) {
246        temp[1]= '\0'; push_ref(stack_head, in_hash, temp);
247      } else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)
248      push_ref(stack_head, in_hash, temp);      push_ref(stack_head, in_hash, temp);
249    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)    else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)
250      if(*temp==';')      if(*temp==';')
# Line 212  int stack_read(stackitem** stack_head, h Line 263  int stack_read(stackitem** stack_head, h
263    return 1;    return 1;
264  }  }
265    
266  extern void print(stackitem** stack_head)  extern void pack(stackitem** stack_head)
267  {  {
268      void* delimiter;
269      stackitem *iterator, *temp, *pack;
270    
271    if((*stack_head)==NULL)    if((*stack_head)==NULL)
272      return;      return;
273    
274    if((*stack_head)->type==value)    delimiter= (*stack_head)->content.ptr;
275      printf("%d", (*stack_head)->content.val);    toss(stack_head);
276    else if((*stack_head)->type==string)  
277      printf("%s", (char*)(*stack_head)->content.ptr);    iterator= *stack_head;
278    else  
279      printf("%p", (*stack_head)->content.ptr);    while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter)
280        iterator= iterator->next;
281    
282      temp= *stack_head;
283      *stack_head= iterator->next;
284      iterator->next= NULL;
285      
286      if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter)
287        toss(stack_head);
288    
289      pack= malloc(sizeof(stackitem));
290      pack->type= list;
291      pack->content.ptr= temp;
292    
293      push(stack_head, pack);
294    }
295    
296    extern void expand(stackitem** stack_head)
297    {
298      stackitem *temp, *new_head;
299    
300      if((*stack_head)==NULL || (*stack_head)->type!=list)
301        return;
302    
303      new_head= temp= (*stack_head)->content.ptr;
304    toss(stack_head);    toss(stack_head);
305    
306      while(temp->next!=NULL)
307        temp= temp->next;
308    
309      temp->next= *stack_head;
310      *stack_head= new_head;
311  }  }
312    
313  extern void nl()  extern void swap(stackitem** stack_head)
314  {  {
315    printf("\n");    stackitem* temp= (*stack_head);
316      
317      if((*stack_head)==NULL || (*stack_head)->next==NULL)
318        return;
319    
320      *stack_head= (*stack_head)->next;
321      temp->next= (*stack_head)->next;
322      (*stack_head)->next= temp;
323    }
324    
325    extern void eq(stackitem** stack_head)
326    {
327      void *left, *right;
328      int result;
329    
330      if((*stack_head)==NULL || (*stack_head)->next==NULL)
331        return;
332    
333      left= (*stack_head)->content.ptr;
334      swap(stack_head);
335      right= (*stack_head)->content.ptr;
336      result= (left==right);
337      
338      toss(stack_head); toss(stack_head);
339      push_val(stack_head, (left==right));
340    }
341    
342    extern void not(stackitem** stack_head)
343    {
344      int value;
345    
346      if((*stack_head)==NULL)
347        return;
348    
349      value= (*stack_head)->content.val;
350      toss(stack_head);
351      push_val(stack_head, !value);
352    }
353    
354    extern void neq(stackitem** stack_head)
355    {
356      eq(stack_head);
357      not(stack_head);
358    }
359    
360    extern void def(stackitem** stack_head)
361    {
362      stackitem *temp, *value;
363    
364      if(*stack_head==NULL || (*stack_head)->next==NULL
365         || (*stack_head)->type!=ref)
366        return;
367    
368      temp= (*stack_head)->content.ptr;
369      value= (*stack_head)->next;
370      temp->content= value->content;
371      value->content.ptr=NULL;
372      temp->type= value->type;
373    
374      toss(stack_head); toss(stack_head);
375    }
376    
377    extern void quit()
378    {
379      exit(EXIT_SUCCESS);
380  }  }
381    
382  int main()  int main()
# Line 247  int main() Line 394  int main()
394      printf("okidok\n ");      printf("okidok\n ");
395    }    }
396    
397      exit(EXIT_SUCCESS);
   return EXIT_SUCCESS;  
398  }  }
399    
400    /* Local Variables: */
401    /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
402    /* End: */

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26