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; |
void* handle; |
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); |
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) |
269 |
|
|
270 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
271 |
} |
} |
272 |
|
|
273 |
|
/* Local Variables: */ |
274 |
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
275 |
|
/* End: */ |