]> mj.ucw.cz Git - umpf.git/commitdiff
deliver done, but it still segfaults
authorAnicka Bernathova <anicka@anicka.net>
Thu, 16 Jul 2009 18:42:54 +0000 (20:42 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Thu, 16 Jul 2009 18:42:54 +0000 (20:42 +0200)
code.c
int.c
umpf.c
umpf.h

diff --git a/code.c b/code.c
index 1f47c3da2530ea0b327df34d58a4bc71b75cb34c..bf3906f9525838915175380dc07ad63250779413 100644 (file)
--- a/code.c
+++ b/code.c
@@ -102,37 +102,33 @@ evaluate(struct tree* t, int pref_var, struct list* where)
 {
        if (t->st == ST_LEAF) { 
                return t->pt.leaf.n;
-       } else if (t->st == ST_OP) {
-               int left, right;
-               left = evaluate(t->pt.op.left, -1, where);
-               right = evaluate(t->pt.op.right, -1, where);
-               switch (t->pt.op.op) {
-                       case '.':
-                               return new_3par_instr(OPC_CAT, left, 
-                                       right, pref_var, where);
-                               break;
-                       case '+':
-                               return new_3par_instr(OPC_PLUS, left, 
-                                       right, pref_var, where);
-                               break;
-                       case '-':
-                               return new_3par_instr(OPC_MINUS, left, 
-                                       right, pref_var, where);
-                               break;
-                       case '*':
-                               return new_3par_instr(OPC_MUL, left, 
-                                       right, pref_var, where);
-                               break;
-                       case '/':
-                               return new_3par_instr(OPC_DIV, left, 
-                                       right, pref_var, where);
-                               break;
-                       default:
-                               die("evaluate: got to default");
-               }
-       } else
-               die("evaluate: I can evaluate only expressions but I got %d",
-                       t->st);
+       } 
+       int left, right;
+       left = evaluate(t->pt.op.left, -1, where);
+       right = evaluate(t->pt.op.right, -1, where);
+       switch (t->pt.op.op) {
+               case '.':
+                       return new_3par_instr(OPC_CAT, left, 
+                               right, pref_var, where);
+                       break;
+               case '+':
+                       return new_3par_instr(OPC_PLUS, left, 
+                               right, pref_var, where);
+                       break;
+               case '-':
+                       return new_3par_instr(OPC_MINUS, left, 
+                               right, pref_var, where);
+                       break;
+               case '*':
+                       return new_3par_instr(OPC_MUL, left, 
+                               right, pref_var, where);
+                       break;
+               case '/':
+                       return new_3par_instr(OPC_DIV, left, 
+                               right, pref_var, where);
+                       break;
+       }
+       die("evaluate: Never can get here :)");
 }
 
 static void
diff --git a/int.c b/int.c
index 69f22852b80d1376f666b69e9420a495fa82b155..436181b748b13d9b92574b25e8e6f9b4c7d50069 100644 (file)
--- a/int.c
+++ b/int.c
@@ -3,6 +3,8 @@
 #include <pcre.h>
 #include <ctype.h>
 #include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include "cond.tab.h"
 #include "umpf.h"
@@ -142,18 +144,16 @@ copy_headers(struct list* orig)
        return new;
 }
 
-static struct email* 
+static struct email 
 prepare_email(struct list* hash)
 {
-       struct email* em;
-
-       em = xmalloc(sizeof(struct email));
+       struct email em;
 
        modify_headers(current_headers, hash);
-       em->headers = copy_headers(current_headers);
-       em->body_len = current_body->body_len; 
-       em->body = xmalloc(em->body_len);
-       memcpy(em->body, current_body->body, em->body_len);
+       em.headers = copy_headers(current_headers);
+       em.body_len = current_body->body_len; 
+       em.body = xmalloc(em.body_len);
+       memcpy(em.body, current_body->body, em.body_len);
 
        return em;
 }
@@ -253,8 +253,84 @@ eval_cond(int var)
        return !!v;
 }
 
+static int 
+last_rescue(void)
+{
+       //FIXME: add some magic
+       return 1;
+}
+
+static void
+deliver(char* where, int copy, int last_try, struct list* hash)
+{
+       int ok = 0;
+       int fd, len, written;
+       off_t off;
+       struct email em = prepare_email(hash);
+       struct hlist* p;
+
+       fd = open_mailbox(where, last_try);
+       if (fd < 0)
+               goto end;
+       off = lseek(fd, 0, SEEK_END);
+       if (off < 0)
+               goto end;
+       LIST_FOREACH(p, em.headers) {
+               len = strlen(p->name);
+               written = write(fd, p->name, len);
+               if (written < len) {
+                       ftruncate(fd, off);
+                       goto end;
+               }
+
+               written = write(fd, ": ", 2);
+               if (written < 2) {
+                       ftruncate(fd, off);
+                       goto end;
+               }
+
+               len = strlen(p->value);
+               written = write(fd, p->value, len);
+               if (written < len) {
+                       ftruncate(fd, off);
+                       goto end;
+               }
+
+               written = write(fd, "\n", 1);
+               if (written < len) {
+                       ftruncate(fd, off);
+                       goto end;
+               }
+       }
+       written = write(fd, "\n", 1);
+       if (written < 1) {
+               ftruncate(fd, off);
+               goto end;
+       }
+
+       written = write(fd, em.body, em.body_len);
+       if (written < em.body_len) {
+               ftruncate(fd, off);
+               goto end;
+       }
+
+       ok = 1;
+       close_mailbox(fd, where, last_try);
+
+       
+end:
+       if (ok) {
+               if (!copy || last_try)
+                       exit(0);
+       } else {
+               if (!copy || last_try)
+                       ok = last_rescue();
+                       exit(ok);       
+       }
+}
+
 void
-interp(struct list* ins)
+interp(struct list* ins, struct list* hash)
 {
        struct code* p;
        char* result;
@@ -312,12 +388,16 @@ interp(struct list* ins)
                case OPC_MAIL:
                        break;
                case OPC_DELIVER:
+                       deliver(get_var(p->u.arrow.what), 
+                               p->u.arrow.copy, 0, hash);
                        break;
                case OPC_CALL_EXT:
                        break;
                case OPC_DISCARD:
+                       exit(0);
                        break;
        }}
+       deliver(default_mailbox, 0, 1, hash);
 }
 
 void
@@ -379,9 +459,3 @@ save_current_headers(struct list* hash)
        }
 
 }
-
-static void
-get_default_mailbox(char* mb)
-{
-       default_mailbox = mb;
-}
diff --git a/umpf.c b/umpf.c
index 78ea955950ac3cf9522727e946f8f02926e9b127..8b4627cfcd2d7fb04ce0e58a9b8178e3ad3ca6e1 100644 (file)
--- a/umpf.c
+++ b/umpf.c
@@ -18,14 +18,15 @@ main(int argc, char** argv)
 {
        int res;
        
-/*     //FIXME:
+       if (argc < 2)
+               die("Usage: ./umpf conf_file [default_mailbox]");
+
        struct passwd* p;
        p = getpwuid(getuid());
-       char* default_mbox = cat("/var/mail/", p->pw_name);
-       get_default_mailbox(default_mbox);
-*/
-       if (argc < 2)
-               die("Usage: ./umpf conf_file");
+       if (argc < 3)
+                default_mailbox = argv[2];
+       else
+                default_mailbox = cat("/var/mail/", p->pw_name);
 
        save_gids();
        read_conf(argv[1]);
@@ -39,14 +40,11 @@ main(int argc, char** argv)
        temp_varcode_start = current_varcode;
        compile(input_tree, NULL);
 
-//     print_code();
-
        current_headers = make_hlist();
        current_body = get_body();
-       print_headers(current_headers);
        save_current_headers(var_hash);
-       interp(&input_code);
-       print_vars(var_hash);
+
+       interp(&input_code, var_hash);
 
        return 0;
 }
diff --git a/umpf.h b/umpf.h
index e4c80333d019d17a3c55d62429e4417e2854a917..926c4768b55efbf3a9f6920e383977e5d5f7d00c 100644 (file)
--- a/umpf.h
+++ b/umpf.h
@@ -198,7 +198,7 @@ struct email {
 
 void save_current_headers(struct list* hash);
 void print_vars(struct list* hash);
-void interp(struct list* ins);
+void interp(struct list* ins, struct list* hash);
 
 /* ham.c */
 char* default_mailbox;