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; |
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); |
169 |
printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); |
printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); |
170 |
break; |
break; |
171 |
case ref: |
case ref: |
172 |
|
printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); |
173 |
|
break; |
174 |
case func: |
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; |
break; |
178 |
} |
} |
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 |
} |
} |
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; |
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: */ |