]> mj.ucw.cz Git - umpf.git/blobdiff - int.c
attempt to cope with big emails
[umpf.git] / int.c
diff --git a/int.c b/int.c
index 4133f3453e6ca3564178517060bd0e30a341d7df..7d5fa2e6ef7aaaf5a3c04197673e2aafd4c3c0a8 100644 (file)
--- a/int.c
+++ b/int.c
@@ -5,6 +5,7 @@
 #include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <stdarg.h>
 
 #include "cond.tab.h"
 #include "umpf.h"
 
 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
 
+static void __attribute__ ((noreturn)) 
+bye(int code, char* msg, ...)
+{
+        va_list args;
+
+       if (current_body->tmpfile)
+               unlink(current_body->tmpfile);
+       
+       if (msg) {
+               va_start(args, msg);
+               vfprintf(stderr, msg, args);
+               fputc('\n', stderr);
+               va_end(args);
+       }
+        exit(code);
+}
+
 static void
 clear_var(int var)
 {
@@ -151,13 +169,29 @@ prepare_email(struct list* hash)
 
        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.body_len = current_body->body_len;
+       em.fd = current_body->fd; 
+       if (current_body->body) {
+               em.body = xmalloc(em.body_len);
+               memcpy(em.body, current_body->body, em.body_len);
+               em.tmpfile = NULL;
+       } else {
+               em.tmpfile = xstrdup(current_body->tmpfile);
+               em.body = NULL;
+       }
 
        return em;
 }
 
+static void
+destroy_email(struct email em)
+{
+       if (em.body)
+               free(em.body);
+       if(em.tmpfile)
+               free(em.tmpfile);
+}
+
 static void
 do_string_ternary_op(struct code* p)
 {
@@ -261,8 +295,14 @@ deliver(char* where, int copy, struct list* hash)
        
        res = deliver_local_email(where, &em);
 
-       if (!copy)
-               exit(-res);
+       destroy_email(em);
+
+       if (!copy) {
+               if (res)
+                       bye(-res, "%m");
+               else
+                       bye(0, NULL);
+       }
 }
 
 void
@@ -332,7 +372,7 @@ interp(struct list* ins, struct list* hash)
                case OPC_CALL_EXT:
                        break;
                case OPC_DISCARD:
-                       exit(0);
+                       bye(0, NULL);
                        break;
        }}
        deliver(default_mailbox, 0, hash);