16 struct variable** res;
18 res = xmalloc (HASHSIZE * sizeof(struct variable*));
19 memset(res, 0, sizeof(struct variable*)*HASHSIZE);
25 get_bucket_number(char* name)
28 unsigned char* p = name;
46 /* value NULL for finding without modyfiing */
47 static struct variable*
48 find_var(char* name, char* value, struct variable** hash)
57 n = get_bucket_number(name);
60 while(p && strcmp(p->name,name))
66 } else if (p && !value)
69 p = xmalloc(sizeof(struct variable));
73 p->value = (value? value:xstrdup(""));
80 regex_cmp(char* s, char* r)
85 int ovector[OVECCOUNT];
87 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
91 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
98 print_tree(struct tree* t, int ind)
105 printf("%*s if\n", ind, "");
106 print_tree(t->pt.tif.c,ind+1);
107 printf("%*s then\n", ind, "");
108 print_tree(t->pt.tif.i,ind+1);
109 printf("%*s else\n", ind, "");
110 print_tree(t->pt.tif.e,ind+1);
113 #define UPPER(a) ((a) >> 8)
114 #define LOWER(a) ((a) & 0xFF)
115 print_tree(t->pt.cond.left, ind+1);
116 printf("%*s", ind, "");
117 if (UPPER(t->pt.cond.op) > 0)
118 putchar(UPPER(t->pt.cond.op));
119 putchar(LOWER(t->pt.cond.op));
121 print_tree(t->pt.cond.right, ind+1);
124 print_tree(t->pt.block.head,ind);
125 print_tree(t->pt.block.tail,ind);
128 print_tree(t->pt.ass.left, ind+1);
129 printf("%*s =\n", ind, "");
130 print_tree(t->pt.ass.right, ind+1);
133 printf("%*s", ind, "");
134 switch (t->pt.leaf.type){
138 puts(t->pt.leaf.value);
143 if (t->pt.arrow.kw_left)
144 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_left);
145 printf("%*s ->\n", ind, "");
146 if (t->pt.arrow.kw_right)
147 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_right);
149 print_tree(t->pt.arrow.s,ind+1);
152 print_tree(t->pt.op.left, ind+1);
153 printf("%*s%c\n", ind, "", t->pt.op.op);
154 print_tree(t->pt.op.right, ind+1);
164 xcat(char* left, char* right)
166 char* res = xmalloc(strlen(left) + strlen(right) + 1);
178 interp_ass_right(struct tree* t, struct variable** hash)
182 if (t->pt.leaf.type == L_VAR)
183 return xstrdup(find_var(t->pt.leaf.value,NULL,hash)->value);
185 return xstrdup(t->pt.leaf.value);
187 switch (t->pt.op.op){
189 return xcat(interp_ass_right(t->pt.op.left, hash),interp_ass_right(t->pt.op.right, hash));
194 die("interp_ass_right: got to default");
198 // FIXME: we would like to be able also do things like ($a & $b) == $c
200 interp_cond(struct tree* t, struct variable** hash)
202 if (t->st != ST_COND)
203 die("Muhehehechlemst?");
205 if (t->pt.cond.type == OP_REL){
206 if (t->pt.cond.left->st != ST_LEAF || t->pt.cond.right->st != ST_LEAF)
209 char* left = (t->pt.cond.left->pt.leaf.type == L_VAR ? find_var(t->pt.cond.left->pt.leaf.value,NULL,hash)->value : t->pt.cond.left->pt.leaf.value);
210 char* right = (t->pt.cond.right->pt.leaf.type == L_VAR ? find_var(t->pt.cond.right->pt.leaf.value,NULL,hash)->value : t->pt.cond.right->pt.leaf.value);
211 switch (t->pt.cond.op){
213 return !strcmp(left,right);
215 return strcmp(left,right);
217 return regex_cmp(left,right);
219 return !regex_cmp(left,right);
220 } //TODO: add numbers
223 int left = interp_cond(t->pt.cond.left, hash);
226 if (t->pt.cond.op != '!')
227 right = interp_cond(t->pt.cond.right, hash);
229 switch (t->pt.cond.op){
231 return left && right;
233 return left || right;
235 return (left || right) && !(left && right);
243 new_action(char* l, char* r, char* s)
245 //TODO: modify headers according to variable values
249 interp(struct tree* t, struct variable** hash)
256 interp(t->pt.block.head, hash);
257 interp(t->pt.block.tail, hash);
260 find_var(t->pt.ass.left->pt.leaf.value, interp_ass_right(t->pt.ass.right, hash), hash);
263 if (interp_cond(t->pt.tif.c, hash))
264 interp(t->pt.tif.i, hash);
266 interp(t->pt.tif.e, hash);
269 new_action(t->pt.arrow.kw_left, t->pt.arrow.kw_right, interp_ass_right(t->pt.arrow.s, hash));
274 die("interp: got to default");
280 print_vars(struct variable** hash)
285 for (i=0; i<HASHSIZE; i++){
288 printf("%s=%s\n",p->name, p->value);
301 new = xmalloc(strlen(u)+1);
304 #define IS_WHITE(c) ((c) == '\t' || (c)==' ' || c=='\n')
306 while (IS_WHITE(*pu))
311 while (IS_WHITE(*pu))
324 save_current_headers(struct variable** hash)
329 for (p = current_headers;p;p = p->next){
330 u = unfold(p->value);
331 find_var(p->name,u,hash);