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

Contents of /stack/stack.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Mon Jan 7 23:09:01 2002 UTC (22 years, 3 months ago) by masse
Branch: hack
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
First import

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26