]> mj.ucw.cz Git - umpf.git/blobdiff - int.c
rename to umpf, guess default mailbox
[umpf.git] / int.c
diff --git a/int.c b/int.c
index b83b435178cf96029f0f2139c2f6b3a82db21034..f7fe023b592f5c5538c33d003251a2494e02d86a 100644 (file)
--- a/int.c
+++ b/int.c
@@ -4,19 +4,22 @@
 #include <ctype.h>
 
 #include "cond.tab.h"
-#include "brum.h"
+#include "umpf.h"
 
 #define OVECCOUNT 3
 #define HASHSIZE 103
 #define MAGIC 19
 
-struct variable**
+struct list* 
 new_var_hash(void)
 {
-       struct variable** res;
+       struct list* res;
+       int i;
 
-       res = xmalloc (HASHSIZE * sizeof(struct variable*));
-       memset(res, 0, sizeof(struct variable*)*HASHSIZE);
+       res = xmalloc (HASHSIZE * sizeof(struct list));
+       for (i = 0; i < HASHSIZE; i++)
+               list_init(res + i);
+       
 
        return res;
 }
@@ -37,38 +40,28 @@ get_bucket_number(char* name)
 
 /* value NULL for finding without modyfiing */
 static struct variable*
-find_var(char* name, char* value, struct variable** hash)
+find_var(char* name, char* value, struct list* hash)
 {
        int n;
        struct variable *p;
 
        n = get_bucket_number(name);
+       int nocase = isupper(*name);
+       LIST_FOREACH(p, hash + n)
+               if (!(nocase ? strcasecmp : strcmp)(p->name,name)){
+                       if (value){
+                               free(p->value);
+                               p->value = value;
+                               p->modified = 1;
+                       }
+                       return p;
+               }
 
-       p = hash[n];
-
-       if (isupper(*name)){
-               while(p && strcasecmp(p->name,name))
-                       p = p->next;
-       } else {
-               while(p && strcmp(p->name,name))
-                       p = p->next;
-       }
-
-       if (p && value){
-               free(p->value);
-               p->value = value;
-               p->modified = 1;
-       } else if (p && !value)
-               return p;
-       else {
-               p = xmalloc(sizeof(struct variable));
-               p->next = hash[n];
-               hash[n] = p;
-               p->name = name; 
-               p->value = (value? value:xstrdup(""));
-               p->modified = 1;
-       }
-
+       p = xmalloc(sizeof(struct variable));
+       p->name = xstrdup(name); 
+       p->value = (value? value:xstrdup(""));
+       p->modified = 1;
+       list_add_last(hash+n, &p->car);
        return p;
 }
 
@@ -170,8 +163,8 @@ xcat(char* left, char* right)
        return res;
 }
 
-char*
-interp_ass_right(struct tree* t, struct variable** hash)
+static char*
+interp_ass_right(struct tree* t, struct list* hash)
 {
        switch (t->st){
                case ST_LEAF:
@@ -192,8 +185,8 @@ interp_ass_right(struct tree* t, struct variable** hash)
 }      
 
 // FIXME: we would like to be able also do things like ($a & $b) == $c
-int
-interp_cond(struct tree* t, struct variable** hash)
+static int
+interp_cond(struct tree* t, struct list* hash)
 {
        if (t->st != ST_COND)
                die("Muhehehechlemst?");
@@ -213,6 +206,8 @@ interp_cond(struct tree* t, struct variable** hash)
                                return regex_cmp(left,right);
                        case CC('!','~'):
                                return !regex_cmp(left,right);
+                       default:
+                               die("Brrrchlemst!");
                } //TODO: add numbers
 
        } else {
@@ -231,67 +226,65 @@ interp_cond(struct tree* t, struct variable** hash)
                                return (left || right) && !(left && right);
                        case '!':
                                return !left;
+                       default:
+                               die("Brrrchlemst!");
                }
        }
 }
 
 static void
-modify_headers(struct hlist* headers, struct variable** hash)
+modify_headers(struct list* headers, struct list* hash)
 {
        struct hlist* p;
-       struct hlist* last = NULL;
        struct variable* pv;
        int i;
 
-       for(p = headers; p; p = p->next){
+       LIST_FOREACH(p, headers){
                pv = find_var(p->name,NULL,hash);
                if (pv->modified){
                        pv->modified = 0;
-                       free(pv->value);
-                       pv->value = xstrdup(p->value); //FIXME: fold it
+                       free(p->value);
+                       p->value = xstrdup(pv->value); //FIXME: fold it
                }
-               last = p;
        }
 
        /* find new headers */
        for (i = 0; i < HASHSIZE; i++){
-               for(pv = hash[i]; pv; pv = pv->next){
+               LIST_FOREACH(pv, hash + i){
                        if (isupper(pv->name[0]) && pv->modified){
                                pv->modified = 0;
-                               p = xmalloc(sizeof(struct hlist));
 
-                               last->next = p; //FIXME
-                               p->next = NULL;
+                               p = xmalloc(sizeof(struct hlist));
                                p->name = xstrdup(pv->name);
                                p->value = xstrdup(pv->value);
+
+                               list_add_last(headers,&p->car);
                        }
                }
        }
 }
 
-static struct hlist*
-copy_headers(struct hlist* orig)
+static struct list*
+copy_headers(struct list* orig)
 {
-       struct hlist* new = NULL;
-       struct hlist* po, * pn = NULL;
-
-       for (po = orig; po; po = po->next){
-               if (!pn)
-                       pn = xmalloc(sizeof(struct hlist));
-               pn->next = xmalloc(sizeof(struct hlist));
-               pn = pn->next;
-               pn->next = NULL;
+       struct list* new = xmalloc(sizeof(struct list));
+       struct hlist* po, *pn;
+
+       list_init(new);
+
+       LIST_FOREACH(po, orig){
+               pn = xmalloc(sizeof(struct hlist));
                pn->name = xstrdup(po->name);
                pn->value = xstrdup(po->value);
-               if (!new)
-                       new = pn;
+
+               list_add_last(new, &pn->car);
        }
 
        return new;
 }
 
 static void
-new_action(char* l, char* r, char* s, struct variable** hash)
+new_action(char* l, char* r, char* s, struct list* hash)
 {
        struct action* a;
 
@@ -299,6 +292,9 @@ new_action(char* l, char* r, char* s, struct variable** hash)
 
        modify_headers(current_headers, hash);
        a->e.headers = copy_headers(current_headers);
+       a->e.body_len = current_body->body_len; 
+       a->e.body = xmalloc(a->e.body_len);
+       memcpy(a->e.body, current_body->body, a->e.body_len);
        a->l = l;
        a->r = r;
        a->s = s;
@@ -307,7 +303,7 @@ new_action(char* l, char* r, char* s, struct variable** hash)
 }
 
 void
-interp(struct tree* t, struct variable** hash)
+interp(struct tree* t, struct list* hash)
 {
        if (!t)
                return;
@@ -338,17 +334,14 @@ interp(struct tree* t, struct variable** hash)
 }
 
 void
-print_vars(struct variable** hash)
+print_vars(struct list* hash)
 {
        int i;
        struct variable* p;
 
        for (i=0; i<HASHSIZE; i++){
-               p = hash[i];
-               while(p){
+               LIST_FOREACH(p, hash + i)
                        printf("%s=%s\n",p->name, p->value);
-                       p = p->next;
-               }               
        }
 }
 
@@ -382,16 +375,22 @@ unfold(char* u)
 }
 
 void
-save_current_headers(struct variable** hash)
+save_current_headers(struct list* hash)
 {
        struct hlist* p;
        struct variable* pv;
        char* u;
 
-       for (p = current_headers;p;p = p->next){
+       LIST_FOREACH(p, current_headers){
                u = unfold(p->value);
                pv = find_var(p->name,u,hash);
                pv->modified = 0;
        }
 
 }
+
+void
+get_default_mailbox(char* mb)
+{
+       default_mailbox = mb;
+}