| 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; |
| 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); |
| 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 |
} |
} |
| 167 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
| 168 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
| 169 |
|
|
| 170 |
if(stack_head->type==value) |
switch(stack_head->type){ |
| 171 |
|
case value: |
| 172 |
printf("%ld: %d\n", counter, (int)stack_head->content.val); |
printf("%ld: %d\n", counter, (int)stack_head->content.val); |
| 173 |
else if(stack_head->type==string) |
break; |
| 174 |
|
case string: |
| 175 |
printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); |
printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); |
| 176 |
else |
break; |
| 177 |
|
case ref: |
| 178 |
|
printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); |
| 179 |
|
break; |
| 180 |
|
case func: |
| 181 |
|
case symbol: |
| 182 |
|
default: |
| 183 |
printf("%ld: %p\n", counter, stack_head->content.ptr); |
printf("%ld: %p\n", counter, stack_head->content.ptr); |
| 184 |
|
break; |
| 185 |
|
} |
| 186 |
} |
} |
| 187 |
|
|
| 188 |
extern void printstack(stackitem** stack_head) |
extern void printstack(stackitem** stack_head) |
| 247 |
if((*stack_head)==NULL) |
if((*stack_head)==NULL) |
| 248 |
return; |
return; |
| 249 |
|
|
| 250 |
if((*stack_head)->type==value) |
switch((*stack_head)->type){ |
| 251 |
|
case value: |
| 252 |
printf("%d", (*stack_head)->content.val); |
printf("%d", (*stack_head)->content.val); |
| 253 |
else if((*stack_head)->type==string) |
break; |
| 254 |
|
case string: |
| 255 |
printf("%s", (char*)(*stack_head)->content.ptr); |
printf("%s", (char*)(*stack_head)->content.ptr); |
| 256 |
else |
break; |
| 257 |
|
case ref: |
| 258 |
|
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
| 259 |
|
break; |
| 260 |
|
case symbol: |
| 261 |
|
default: |
| 262 |
printf("%p", (*stack_head)->content.ptr); |
printf("%p", (*stack_head)->content.ptr); |
| 263 |
|
break; |
| 264 |
|
} |
| 265 |
|
|
| 266 |
toss(stack_head); |
toss(stack_head); |
| 267 |
} |
} |
| 268 |
|
|
| 269 |
|
extern void pack(stackitem** stack_head) |
| 270 |
|
{ |
| 271 |
|
void* delimiter; |
| 272 |
|
stackitem *iterator, *temp, *pack; |
| 273 |
|
|
| 274 |
|
if((*stack_head)==NULL) |
| 275 |
|
return; |
| 276 |
|
|
| 277 |
|
delimiter= (*stack_head)->content.ptr; |
| 278 |
|
toss(stack_head); |
| 279 |
|
|
| 280 |
|
iterator= *stack_head; |
| 281 |
|
|
| 282 |
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
| 283 |
|
iterator= iterator->next; |
| 284 |
|
|
| 285 |
|
temp= *stack_head; |
| 286 |
|
*stack_head= iterator->next; |
| 287 |
|
iterator->next= NULL; |
| 288 |
|
|
| 289 |
|
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
| 290 |
|
toss(stack_head); |
| 291 |
|
|
| 292 |
|
pack= malloc(sizeof(stackitem)); |
| 293 |
|
pack->type= list; |
| 294 |
|
pack->content.ptr= temp; |
| 295 |
|
|
| 296 |
|
push(stack_head, pack); |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
|
| 300 |
extern void nl() |
extern void nl() |
| 301 |
{ |
{ |
| 302 |
printf("\n"); |
printf("\n"); |
| 303 |
} |
} |
| 304 |
|
|
| 305 |
|
extern void quit() |
| 306 |
|
{ |
| 307 |
|
exit(EXIT_SUCCESS); |
| 308 |
|
} |
| 309 |
|
|
| 310 |
int main() |
int main() |
| 311 |
{ |
{ |
| 312 |
stackitem* s= NULL; |
stackitem* s= NULL; |
| 325 |
|
|
| 326 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
|
/* Local Variables: */ |
| 330 |
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
| 331 |
|
/* End: */ |