{
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
#include <pcre.h>
#include <ctype.h>
#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
#include "cond.tab.h"
#include "umpf.h"
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;
}
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;
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
}
}
-
-static void
-get_default_mailbox(char* mb)
-{
- default_mailbox = mb;
-}
{
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]);
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;
}
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;