]> diplodocus.org Git - nmh/blobdiff - uip/mhbuild.c
Reworked attach to add charset to Content-Type string for
[nmh] / uip / mhbuild.c
index 2b0ad1affb8d2d2ff14ab0e178173343ee998958..ef4598581702e8c0773a33b66fca14b2871319cd 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * mhbuild.c -- expand/translate MIME composition files
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
 #include <fcntl.h>
 #include <h/signals.h>
 #include <h/md5.h>
-#include <errno.h>
-#include <signal.h>
 #include <h/mts.h>
 #include <h/tws.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
 #include <h/mhcachesbr.h>
-
-#ifdef HAVE_SYS_WAIT_H
-# include <sys/wait.h>
-#endif
-
-static struct swit switches[] = {
-#define        CHECKSW                 0
-    { "check", 0 },
-#define        NCHECKSW                1
-    { "nocheck", 0 },
-#define        EBCDICSW                2
-    { "ebcdicsafe", 0 },
-#define        NEBCDICSW               3
-    { "noebcdicsafe", 0 },
-#define        HEADSW                  4
-    { "headers", 0 },
-#define        NHEADSW                 5
-    { "noheaders", 0 },
-#define        LISTSW                  6
-    { "list", 0 },
-#define        NLISTSW                 7
-    { "nolist", 0 },
-#define        SIZESW                  8
-    { "realsize", 0 },
-#define        NSIZESW                 9
-    { "norealsize", 0 },
-#define        RFC934SW               10
-    { "rfc934mode", 0 },
-#define        NRFC934SW              11
-    { "norfc934mode", 0 },
-#define        VERBSW                 12
-    { "verbose", 0 },
-#define        NVERBSW                13
-    { "noverbose", 0 },
-#define        RCACHESW               14
-    { "rcache policy", 0 },
-#define        WCACHESW               15
-    { "wcache policy", 0 },
-#define VERSIONSW              16
-    { "version", 0 },
-#define        HELPSW                 17
-    { "help", 0 },
-#define        DEBUGSW                18
-    { "debug", -5 },
-    { NULL, 0 }
-};
-
-
-extern int errno;
+#include <h/utils.h>
+
+#define MHBUILD_SWITCHES \
+    X("check", 0, CHECKSW) \
+    X("nocheck", 0, NCHECKSW) \
+    X("directives", 0, DIRECTIVES) \
+    X("nodirectives", 0, NDIRECTIVES) \
+    X("headers", 0, HEADSW) \
+    X("noheaders", 0, NHEADSW) \
+    X("list", 0, LISTSW) \
+    X("nolist", 0, NLISTSW) \
+    X("realsize", 0, SIZESW) \
+    X("norealsize", 0, NSIZESW) \
+    X("rfc934mode", 0, RFC934SW) \
+    X("norfc934mode", 0, NRFC934SW) \
+    X("verbose", 0, VERBSW) \
+    X("noverbose", 0, NVERBSW) \
+    X("rcache policy", 0, RCACHESW) \
+    X("wcache policy", 0, WCACHESW) \
+    X("contentid", 0, CONTENTIDSW) \
+    X("nocontentid", 0, NCONTENTIDSW) \
+    X("headerencoding encoding-algorithm", 0, HEADERENCSW) \
+    X("autoheaderencoding", 0, AUTOHEADERENCSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+    X("debug", -5, DEBUGSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(MHBUILD);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(MHBUILD, switches);
+#undef X
+
+#define MIMEENCODING_SWITCHES \
+    X("base64", 0, BASE64SW) \
+    X("quoted-printable", 0, QUOTEDPRINTSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(MIMEENCODING);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(MIMEENCODING, encodingswitches);
+#undef X
 
 /* mhbuildsbr.c */
-extern int checksw;
 extern char *tmp;      /* directory to place temp files */
 
 /* mhcachesbr.c */
@@ -83,9 +75,9 @@ extern char *cache_private;
 int debugsw = 0;
 int verbosw = 0;
 
-int ebcdicsw = 0;
 int listsw   = 0;
 int rfc934sw = 0;
+int contentidsw = 1;
 
 /*
  * Temporary files
@@ -96,31 +88,30 @@ static int unlink_infile  = 0;
 static char outfile[BUFSIZ];
 static int unlink_outfile = 0;
 
+static void unlink_done (int) NORETURN;
 
 /* mhbuildsbr.c */
-CT build_mime (char *);
 int output_message (CT, char *);
+int output_message_fp (CT, FILE *, char*);
 
 /* mhlistsbr.c */
 int list_all_messages (CT *, int, int, int, int);
 
-/* mhmisc.c */
-void set_endian (void);
-
-/* mhfree.c */
-void free_content (CT);
-
 
 int
 main (int argc, char **argv)
 {
-    int sizesw = 1, headsw = 1;
+    int sizesw = 1, headsw = 1, directives = 1;
     int *icachesw;
     char *cp, buf[BUFSIZ];
     char buffer[BUFSIZ], *compfile = NULL;
     char **argp, **arguments;
     CT ct, cts[2];
-    FILE *fp;
+    FILE *fp = NULL;
+    FILE *fp_out = NULL;
+    int header_encoding = CE_UNKNOWN;
+
+    done=unlink_done;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -154,10 +145,10 @@ main (int argc, char **argv)
            case HELPSW: 
                snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
                print_help (buf, switches, 1);
-               done (1);
+               done (0);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case RCACHESW:
                icachesw = &rcachesw;
@@ -185,13 +176,6 @@ main (int argc, char **argv)
                checksw = 0;
                continue;
 
-           case EBCDICSW:
-               ebcdicsw++;
-               continue;
-           case NEBCDICSW:
-               ebcdicsw = 0;
-               continue;
-
            case HEADSW:
                headsw++;
                continue;
@@ -199,6 +183,13 @@ main (int argc, char **argv)
                headsw = 0;
                continue;
 
+           case DIRECTIVES:
+               directives = 1;
+               continue;
+           case NDIRECTIVES:
+               directives = 0;
+               continue;
+
            case LISTSW:
                listsw++;
                continue;
@@ -220,6 +211,40 @@ main (int argc, char **argv)
                sizesw = 0;
                continue;
 
+           case CONTENTIDSW:
+               contentidsw = 1;
+               continue;
+           case NCONTENTIDSW:
+               contentidsw = 0;
+               continue;
+
+           case HEADERENCSW: {
+               int encoding;
+
+               if (!(cp = *argp++) || *cp == '-')
+                   adios (NULL, "missing argument to %s", argp[-2]);
+               switch (encoding = smatch (cp, encodingswitches)) {
+               case AMBIGSW:
+                   ambigsw (cp, encodingswitches);
+                   done (1);
+               case UNKWNSW:
+                   adios (NULL, "%s unknown encoding algorithm", cp);
+               case BASE64SW:
+                   header_encoding = CE_BASE64;
+                   break;
+               case QUOTEDPRINTSW:
+                   header_encoding = CE_QUOTED;
+                   break;
+               default:
+                   adios (NULL, "Internal error: algorithm %s", cp);
+               }
+               continue;
+           }
+
+           case AUTOHEADERENCSW:
+               header_encoding = CE_UNKNOWN;
+               continue;
+
            case VERBSW: 
                verbosw++;
                continue;
@@ -237,11 +262,6 @@ main (int argc, char **argv)
            compfile = cp;
     }
 
-    set_endian ();
-
-    if ((cp = getenv ("MM_NOASK")) && !strcmp (cp, "1"))
-       listsw  = 0;
-
     /*
      * Check if we've specified an additional profile
      */
@@ -292,27 +312,25 @@ main (int argc, char **argv)
      * Process the composition file from standard input.
      */
     if (compfile[0] == '-' && compfile[1] == '\0') {
-
        /* copy standard input to temporary file */
-       strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
-       if ((fp = fopen (infile, "w")) == NULL)
-           adios (infile, "unable to open");
+       strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
        while (fgets (buffer, BUFSIZ, stdin))
            fputs (buffer, fp);
        fclose (fp);
        unlink_infile = 1;
 
        /* build the content structures for MIME message */
-       ct = build_mime (infile);
+       ct = build_mime (infile, directives, header_encoding);
        cts[0] = ct;
        cts[1] = NULL;
 
        /* output MIME message to this temporary file */
-       strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
+       strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
        unlink_outfile = 1;
 
        /* output the message */
-       output_message (ct, outfile);
+       output_message_fp (ct, fp_out, outfile);
+        fclose(fp_out);
 
        /* output the temp file to standard output */
        if ((fp = fopen (outfile, "r")) == NULL)
@@ -336,16 +354,18 @@ main (int argc, char **argv)
      */
 
     /* build the content structures for MIME message */
-    ct = build_mime (compfile);
+    ct = build_mime (compfile, directives, header_encoding);
     cts[0] = ct;
     cts[1] = NULL;
 
     /* output MIME message to this temporary file */
-    strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
+    strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
+            sizeof(outfile));
     unlink_outfile = 1;
 
     /* output the message */
-    output_message (ct, outfile);
+    output_message_fp (ct, fp_out, outfile);
+    fclose(fp_out);
 
     /*
      * List the message info
@@ -355,24 +375,26 @@ main (int argc, char **argv)
 
     /* Rename composition draft */
     snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
-    if (rename (compfile, buffer) == NOTOK)
-       adios (compfile, "unable to rename %s to", buffer);
+    if (rename (compfile, buffer) == NOTOK) {
+       adios (compfile, "unable to rename comp draft %s to", buffer);
+    }
 
     /* Rename output file to take its place */
     if (rename (outfile, compfile) == NOTOK) {
-       advise (outfile, "unable to rename %s to", compfile);
+       advise (outfile, "unable to rename output %s to", compfile);
        rename (buffer, compfile);
        done (1);
     }
     unlink_outfile = 0;
 
     free_content (ct);
-    return done (0);
+    done (0);
+    return 1;
 }
 
 
-int
-done (int status)
+static void
+unlink_done (int status)
 {
     /*
      * Check if we need to remove stray
@@ -384,5 +406,4 @@ done (int status)
        unlink (outfile);
 
     exit (status);
-    return 1;  /* dead code to satisfy the compiler */
 }