| 6 |
#include <stddef.h> |
#include <stddef.h> |
| 7 |
/* dlopen, dlsym, dlerror */ |
/* dlopen, dlsym, dlerror */ |
| 8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
| 9 |
|
/* assert */ |
| 10 |
|
#include <assert.h> |
| 11 |
|
|
| 12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
| 13 |
|
|
| 15 |
{ |
{ |
| 16 |
enum { |
enum { |
| 17 |
value, /* Integer */ |
value, /* Integer */ |
| 18 |
string, |
string, |
| 19 |
ref, /* Reference (to an element in the hash table) */ |
ref, /* Reference (to an element in the |
| 20 |
|
hash table) */ |
| 21 |
func, /* Function pointer */ |
func, /* Function pointer */ |
| 22 |
symbol, |
symbol, |
| 23 |
list |
list |
| 24 |
} type; /* Tells what kind of stack element */ |
} type; /* Type of stack element */ |
| 25 |
|
|
| 26 |
union { |
union { |
| 27 |
void* ptr; /* Pointer to the content */ |
void* ptr; /* Pointer to the content */ |
| 28 |
int val; /* ...or an integer */ |
int val; /* ...or an integer */ |
| 34 |
|
|
| 35 |
|
|
| 36 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
| 37 |
typedef void (*funcp)(stackitem**); /* Function pointer declaration */ |
typedef void (*funcp)(stackitem**); /* funcp is a pointer to a |
| 38 |
|
void function (stackitem **) */ |
| 39 |
|
|
| 40 |
/* Initiates a newly created hash table. */ |
/* Initialize a newly created hash table. */ |
| 41 |
void init_hashtbl(hashtbl out_hash) |
void init_hashtbl(hashtbl out_hash) |
| 42 |
{ |
{ |
| 43 |
long i; |
long i; |
| 51 |
{ |
{ |
| 52 |
long i= 0; |
long i= 0; |
| 53 |
unsigned long out_hash= 0; |
unsigned long out_hash= 0; |
| 54 |
char key= 0; |
char key= '\0'; |
| 55 |
stackitem** position; |
stackitem** position; |
| 56 |
|
|
| 57 |
while(1){ /* Hash in_string */ |
while(1){ /* Hash in_string */ |
| 64 |
out_hash= out_hash%HASHTBLSIZE; |
out_hash= out_hash%HASHTBLSIZE; |
| 65 |
position= &(in_hashtbl[out_hash]); |
position= &(in_hashtbl[out_hash]); |
| 66 |
|
|
| 67 |
while(1){ /* Return position if empty */ |
while(position != NULL){ |
| 68 |
if(*position==NULL) |
if(*position==NULL) /* If empty */ |
| 69 |
return position; |
return position; |
| 70 |
|
|
| 71 |
if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
| 72 |
return position; |
return position; |
| 73 |
|
|
| 74 |
position= &((*position)->next); /* Try next */ |
position= &((*position)->next); /* Try next */ |
| 75 |
} |
} |
| 76 |
|
return NULL; /* end of list reached without finding |
| 77 |
|
an empty position */ |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
/* Generic push function. */ |
/* Generic push function. */ |
| 89 |
int push_val(stackitem** stack_head, int in_val) |
int push_val(stackitem** stack_head, int in_val) |
| 90 |
{ |
{ |
| 91 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem* new_item= malloc(sizeof(stackitem)); |
| 92 |
|
assert(new_item != NULL); |
| 93 |
new_item->content.val= in_val; |
new_item->content.val= in_val; |
| 94 |
new_item->type= value; |
new_item->type= value; |
| 95 |
|
|
| 120 |
return 1; |
return 1; |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
/* Define a function a new function in the hash table. */ |
/* Define a new function in the hash table. */ |
| 124 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
| 125 |
{ |
{ |
| 126 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |