19 res = xmalloc (HASHSIZE * sizeof(struct list));
20 for (i = 0; i < HASHSIZE; i++)
28 get_bucket_number(char* name)
31 unsigned char* p = name;
34 n = n * MAGIC + toupper(*p++);
41 /* value NULL for finding without modyfiing */
42 static struct variable*
43 find_var(char* name, char* value, struct list* hash)
48 n = get_bucket_number(name);
51 LIST_FOREACH(p, hash + n)
52 if (!strcasecmp(p->name,name)){
57 LIST_FOREACH(p, hash + n)
58 if (!strcmp(p->name, name)){
68 } else if (found && !value)
71 p = xmalloc(sizeof(struct variable));
73 p->value = (value? value:xstrdup(""));
76 list_add_last(hash+n, &p->car);
83 regex_cmp(char* s, char* r)
88 int ovector[OVECCOUNT];
90 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
94 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
101 print_tree(struct tree* t, int ind)
108 printf("%*s if\n", ind, "");
109 print_tree(t->pt.tif.c,ind+1);
110 printf("%*s then\n", ind, "");
111 print_tree(t->pt.tif.i,ind+1);
112 printf("%*s else\n", ind, "");
113 print_tree(t->pt.tif.e,ind+1);
116 #define UPPER(a) ((a) >> 8)
117 #define LOWER(a) ((a) & 0xFF)
118 print_tree(t->pt.cond.left, ind+1);
119 printf("%*s", ind, "");
120 if (UPPER(t->pt.cond.op) > 0)
121 putchar(UPPER(t->pt.cond.op));
122 putchar(LOWER(t->pt.cond.op));
124 print_tree(t->pt.cond.right, ind+1);
127 print_tree(t->pt.block.head,ind);
128 print_tree(t->pt.block.tail,ind);
131 print_tree(t->pt.ass.left, ind+1);
132 printf("%*s =\n", ind, "");
133 print_tree(t->pt.ass.right, ind+1);
136 printf("%*s", ind, "");
137 switch (t->pt.leaf.type){
141 puts(t->pt.leaf.value);
146 if (t->pt.arrow.kw_left)
147 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_left);
148 printf("%*s ->\n", ind, "");
149 if (t->pt.arrow.kw_right)
150 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_right);
152 print_tree(t->pt.arrow.s,ind+1);
155 print_tree(t->pt.op.left, ind+1);
156 printf("%*s%c\n", ind, "", t->pt.op.op);
157 print_tree(t->pt.op.right, ind+1);
167 xcat(char* left, char* right)
169 char* res = xmalloc(strlen(left) + strlen(right) + 1);
181 interp_ass_right(struct tree* t, struct list* hash)
185 if (t->pt.leaf.type == L_VAR)
186 return xstrdup(find_var(t->pt.leaf.value,NULL,hash)->value);
188 return xstrdup(t->pt.leaf.value);
190 switch (t->pt.op.op){
192 return xcat(interp_ass_right(t->pt.op.left, hash),interp_ass_right(t->pt.op.right, hash));
197 die("interp_ass_right: got to default");
201 // FIXME: we would like to be able also do things like ($a & $b) == $c
203 interp_cond(struct tree* t, struct list* hash)
205 if (t->st != ST_COND)
206 die("Muhehehechlemst?");
208 if (t->pt.cond.type == OP_REL){
209 if (t->pt.cond.left->st != ST_LEAF || t->pt.cond.right->st != ST_LEAF)
212 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);
213 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);
214 switch (t->pt.cond.op){
216 return !strcmp(left,right);
218 return strcmp(left,right);
220 return regex_cmp(left,right);
222 return !regex_cmp(left,right);
223 } //TODO: add numbers
226 int left = interp_cond(t->pt.cond.left, hash);
229 if (t->pt.cond.op != '!')
230 right = interp_cond(t->pt.cond.right, hash);
232 switch (t->pt.cond.op){
234 return left && right;
236 return left || right;
238 return (left || right) && !(left && right);
246 modify_headers(struct list* headers, struct list* hash)
252 LIST_FOREACH(p, headers){
253 pv = find_var(p->name,NULL,hash);
257 p->value = xstrdup(pv->value); //FIXME: fold it
261 /* find new headers */
262 for (i = 0; i < HASHSIZE; i++){
263 LIST_FOREACH(pv, hash + i){
264 if (isupper(pv->name[0]) && pv->modified){
267 p = xmalloc(sizeof(struct hlist));
268 p->name = xstrdup(pv->name);
269 p->value = xstrdup(pv->value);
271 list_add_last(headers,&p->car);
278 copy_headers(struct list* orig)
280 struct list* new = xmalloc(sizeof(struct list));
281 struct hlist* po, *pn;
285 LIST_FOREACH(po, orig){
286 pn = xmalloc(sizeof(struct hlist));
287 pn->name = xstrdup(po->name);
288 pn->value = xstrdup(po->value);
290 list_add_last(new, &pn->car);
297 new_action(char* l, char* r, char* s, struct list* hash)
301 a = xmalloc(sizeof(struct action));
303 modify_headers(current_headers, hash);
304 a->e.headers = copy_headers(current_headers);
313 interp(struct tree* t, struct list* hash)
320 interp(t->pt.block.head, hash);
321 interp(t->pt.block.tail, hash);
324 find_var(t->pt.ass.left->pt.leaf.value, interp_ass_right(t->pt.ass.right, hash), hash);
327 if (interp_cond(t->pt.tif.c, hash))
328 interp(t->pt.tif.i, hash);
330 interp(t->pt.tif.e, hash);
333 new_action(t->pt.arrow.kw_left, t->pt.arrow.kw_right, interp_ass_right(t->pt.arrow.s, hash),hash);
338 die("interp: got to default");
344 print_vars(struct list* hash)
349 for (i=0; i<HASHSIZE; i++){
350 LIST_FOREACH(p, hash + i)
351 printf("%s=%s\n",p->name, p->value);
362 new = xmalloc(strlen(u)+1);
365 #define IS_WHITE(c) ((c) == '\t' || (c)==' ' || c=='\n')
367 while (IS_WHITE(*pu))
372 while (IS_WHITE(*pu))
385 save_current_headers(struct list* hash)
391 LIST_FOREACH(p, current_headers){
392 u = unfold(p->value);
393 pv = find_var(p->name,u,hash);