/[cvs]/stack/stack.c
ViewVC logotype

Contents of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Tue Jan 8 12:33:57 2002 UTC (22 years, 3 months ago) by masse
Branch: MAIN
Changes since 1.5: +10 -5 lines
File MIME type: text/plain
Now, print and printstack print the symbol name and
dlopen(NULL, RTLD_LAZY) is only called once. The handle is stored in
a static variable in push_ref().

1 /* printf */
2 #include <stdio.h>
3 /* EXIT_SUCCESS */
4 #include <stdlib.h>
5 /* NULL */
6 #include <stddef.h>
7 /* dlopen, dlsym, dlerror */
8 #include <dlfcn.h>
9
10 #define HASHTBLSIZE 65536
11
12 typedef struct stack_item
13 {
14 enum {value, string, ref, func, symbol} type;
15 union {
16 void* ptr;
17 int val;
18 } content;
19
20 char* id;
21 struct stack_item* next;
22 } stackitem;
23
24
25 typedef stackitem* hashtbl[HASHTBLSIZE];
26 typedef void (*funcp)(stackitem**);
27
28 void init_hashtbl(hashtbl out_hash)
29 {
30 long i;
31
32 for(i= 0; i<HASHTBLSIZE; i++)
33 out_hash[i]= NULL;
34 }
35
36 stackitem** hash(hashtbl in_hashtbl, const char* in_string)
37 {
38 long i= 0;
39 unsigned long out_hash= 0;
40 char key= 0;
41 stackitem** position;
42
43 while(1){
44 key= in_string[i++];
45 if(key=='\0')
46 break;
47 out_hash= out_hash*32+key;
48 }
49
50 out_hash= out_hash%HASHTBLSIZE;
51 position= &(in_hashtbl[out_hash]);
52
53 while(1){
54 if(*position==NULL)
55 return position;
56
57 if(strcmp(in_string, (*position)->id)==0)
58 return position;
59
60 position= &((*position)->next);
61 }
62 }
63
64
65 int push(stackitem** stack_head, stackitem* in_item)
66 {
67 in_item->next= *stack_head;
68 *stack_head= in_item;
69 return 1;
70 }
71
72 int push_val(stackitem** stack_head, int in_val)
73 {
74 stackitem* new_item= malloc(sizeof(stackitem));
75 new_item->content.val= in_val;
76 new_item->type= value;
77
78 push(stack_head, new_item);
79 return 1;
80 }
81
82 int push_cstring(stackitem** stack_head, const char* in_string)
83 {
84 stackitem* new_item= malloc(sizeof(stackitem));
85 new_item->content.ptr= malloc(strlen(in_string)+1);
86 strcpy(new_item->content.ptr, in_string);
87 new_item->type= string;
88
89 push(stack_head, new_item);
90 return 1;
91 }
92
93 int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id)
94 {
95 in_item->id= malloc(strlen(id)+1);
96
97 strcpy(in_item->id, id);
98 push(hash(in_hashtbl, id), in_item);
99
100 return 1;
101 }
102
103 void def_func(hashtbl in_hashtbl, funcp in_func, const char* id)
104 {
105 stackitem* temp= malloc(sizeof(stackitem));
106
107 temp->type= func;
108 temp->content.ptr= in_func;
109
110 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)
123 {
124 static void* handle= NULL;
125 void* symbol;
126
127 stackitem* new_item= malloc(sizeof(stackitem));
128 new_item->content.ptr= *hash(in_hash, in_string);
129 new_item->type= ref;
130
131 if(handle==NULL)
132 handle= dlopen(NULL, RTLD_LAZY);
133
134 if(new_item->content.ptr==NULL) {
135 symbol= dlsym(handle, in_string);
136 if(dlerror()==NULL)
137 def_func(in_hash, symbol, in_string);
138 else
139 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);
146 return 1;
147 }
148
149 extern void toss(stackitem** stack_head)
150 {
151 stackitem* temp= *stack_head;
152
153 *stack_head= (*stack_head)->next;
154 free(temp);
155 }
156
157
158 /* print_stack(stack); */
159 void print_st(stackitem* stack_head, long counter)
160 {
161 if(stack_head->next != NULL)
162 print_st(stack_head->next, counter+1);
163
164 switch(stack_head->type){
165 case value:
166 printf("%ld: %d\n", counter, (int)stack_head->content.val);
167 break;
168 case string:
169 printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr);
170 break;
171 case ref:
172 printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id);
173 break;
174 case func:
175 case symbol:
176 printf("%ld: %p\n", counter, stack_head->content.ptr);
177 break;
178 }
179 }
180
181 extern void printstack(stackitem** stack_head)
182 {
183 if(*stack_head != NULL) {
184 print_st(*stack_head, 1);
185 printf("\n");
186 }
187 }
188
189
190 extern void eval(stackitem** stack_head)
191 {
192 funcp in_func;
193
194 if((*stack_head)==NULL || (*stack_head)->type!=ref)
195 return;
196
197 if(((stackitem*)(*stack_head)->content.ptr)->type==func) {
198 in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr;
199 toss(stack_head);
200 (*in_func)(stack_head);
201 return;
202 }
203 }
204
205 int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line)
206 {
207 char *temp, *rest;
208 int itemp;
209 size_t inlength= strlen(in_line)+1;
210 int convert= 0;
211
212 temp= malloc(inlength);
213 rest= malloc(inlength);
214
215 if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1)
216 push_cstring(stack_head, temp);
217 else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1)
218 push_val(stack_head, itemp);
219 else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1)
220 push_ref(stack_head, in_hash, temp);
221 else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1)
222 if(*temp==';')
223 eval(stack_head);
224
225 free(temp);
226
227 if(convert<2) {
228 free(rest);
229 return 0;
230 }
231
232 stack_read(stack_head, in_hash, rest);
233
234 free(rest);
235 return 1;
236 }
237
238 extern void print(stackitem** stack_head)
239 {
240 if((*stack_head)==NULL)
241 return;
242
243 switch((*stack_head)->type){
244 case value:
245 printf("%d", (*stack_head)->content.val);
246 break;
247 case string:
248 printf("%s", (char*)(*stack_head)->content.ptr);
249 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);
256 break;
257 }
258
259 toss(stack_head);
260 }
261
262 extern void nl()
263 {
264 printf("\n");
265 }
266
267 extern void quit()
268 {
269 exit(EXIT_SUCCESS);
270 }
271
272 int main()
273 {
274 stackitem* s= NULL;
275 hashtbl myhash;
276 char in_string[100];
277
278 init_hashtbl(myhash);
279
280 printf("okidok\n ");
281
282 while(fgets(in_string, 100, stdin) != NULL) {
283 stack_read(&s, myhash, in_string);
284 printf("okidok\n ");
285 }
286
287
288 return EXIT_SUCCESS;
289 }
290
291 /* Local Variables: */
292 /* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */
293 /* End: */

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26