From 0c9fd11c75d2392a8a1041bd8d386cf449f6d183 Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Thu, 16 Jul 2009 20:42:54 +0200 Subject: [PATCH] deliver done, but it still segfaults --- code.c | 58 +++++++++++++++----------------- int.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++--------- umpf.c | 20 +++++------ umpf.h | 2 +- 4 files changed, 126 insertions(+), 58 deletions(-) diff --git a/code.c b/code.c index 1f47c3d..bf3906f 100644 --- 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 69f2285..436181b 100644 --- a/int.c +++ b/int.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #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 78ea955..8b4627c 100644 --- 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 e4c8033..926c476 100644 --- 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; -- 2.39.5