]> diplodocus.org Git - nmh/blobdiff - uip/forw.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / forw.c
index 2d6709e8fd5c6c9e4064e55e256367ac7549ee1f..ce8a4fccd8a583f53a27687e449ffe139431d4c1 100644 (file)
@@ -1,6 +1,4 @@
-
-/*
- * forw.c -- forward a message, or group of messages.
+/* forw.c -- forward a message, or group of messages.
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
 #include <fcntl.h>
 #include <h/tws.h>
 #include <h/utils.h>
+#include "sbr/m_maildir.h"
 
 
 #define        IFORMAT "digest-issue-%s"
 #define        VFORMAT "digest-volume-%s"
 
-static struct swit switches[] = {
-#define        ANNOSW                 0
-    { "annotate", 0 },
-#define        NANNOSW                1
-    { "noannotate", 0 },
-#define        DFOLDSW                2
-    { "draftfolder +folder", 0 },
-#define        DMSGSW                 3
-    { "draftmessage msg", 0 },
-#define        NDFLDSW                4
-    { "nodraftfolder", 0 },
-#define        EDITRSW                5
-    { "editor editor", 0 },
-#define        NEDITSW                6
-    { "noedit", 0 },
-#define        FILTSW                 7
-    { "filter filterfile", 0 },
-#define        FORMSW                 8
-    { "form formfile", 0 },
-#define        FRMTSW                 9
-    { "format", 5 },
-#define        NFRMTSW               10
-    { "noformat", 7 },
-#define        INPLSW                11
-    { "inplace", 0 },
-#define        NINPLSW               12
-    { "noinplace", 0 },
-#define MIMESW                13
-    { "mime", 0 },
-#define NMIMESW               14
-    { "nomime", 0 },
-#define        DGSTSW                15
-    { "digest list", 0 },
-#define        ISSUESW               16
-    { "issue number", 0 },
-#define        VOLUMSW               17
-    { "volume number", 0 },
-#define        WHATSW                18
-    { "whatnowproc program", 0 },
-#define        NWHATSW               19
-    { "nowhatnowproc", 0 },
-#define        BITSTUFFSW            20
-    { "dashstuffing", 0 },             /* interface to mhl */
-#define        NBITSTUFFSW           21
-    { "nodashstuffing", 0 },
-#define VERSIONSW             22
-    { "version", 0 },
-#define        HELPSW                23
-    { "help", 0 },
-#define        FILESW                24
-    { "file file", 4 },                        /* interface from msh */
-#define        BILDSW                25
-    { "build", 5 },                    /* interface from mhe */
-#define FROMSW                26
-    { "from address", 0 },
-#define TOSW                  27
-    { "to address", 0 },
-#define CCSW                  28
-    { "cc address", 0 },
-#define SUBJECTSW             29
-    { "subject text", 0 },
-#define FCCSW                 30
-    { "fcc mailbox", 0 },
-#define WIDTHSW               31
-    { "width columns", 0 },
-    { NULL, 0 }
-};
-
-static struct swit aqrnl[] = {
-#define        NOSW         0
-    { "quit", 0 },
-#define        YESW         1
-    { "replace", 0 },
-#define        LISTDSW      2
-    { "list", 0 },
-#define        REFILSW      3
-    { "refile +folder", 0 },
-#define NEWSW        4
-    { "new", 0 },
-    { NULL, 0 }
-};
+#define FORW_SWITCHES \
+    X("annotate", 0, ANNOSW) \
+    X("noannotate", 0, NANNOSW) \
+    X("draftfolder +folder", 0, DFOLDSW) \
+    X("draftmessage msg", 0, DMSGSW) \
+    X("nodraftfolder", 0, NDFLDSW) \
+    X("editor editor", 0, EDITRSW) \
+    X("noedit", 0, NEDITSW) \
+    X("filter filterfile", 0, FILTSW) \
+    X("form formfile", 0, FORMSW) \
+    X("format", 5, FRMTSW) \
+    X("noformat", 7, NFRMTSW) \
+    X("inplace", 0, INPLSW) \
+    X("noinplace", 0, NINPLSW) \
+    X("mime", 0, MIMESW) \
+    X("nomime", 0, NMIMESW) \
+    X("digest list", 0, DGSTSW) \
+    X("issue number", 0, ISSUESW) \
+    X("volume number", 0, VOLUMSW) \
+    X("whatnowproc program", 0, WHATSW) \
+    X("nowhatnowproc", 0, NWHATSW) \
+    X("dashstuffing", 0, BITSTUFFSW)          /* interface to mhl */ \
+    X("nodashstuffing", 0, NBITSTUFFSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+    X("file file", 4, FILESW) \
+    X("build", 5, BILDSW)                     /* interface from mhe */ \
+    X("from address", 0, FROMSW) \
+    X("to address", 0, TOSW) \
+    X("cc address", 0, CCSW) \
+    X("subject text", 0, SUBJECTSW) \
+    X("fcc mailbox", 0, FCCSW) \
+    X("width columns", 0, WIDTHSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(FORW);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(FORW, switches);
+#undef X
+
+#define DISPO_SWITCHES \
+    X("quit", 0, NOSW) \
+    X("replace", 0, YESW) \
+    X("list", 0, LISTDSW) \
+    X("refile +folder", 0, REFILSW) \
+    X("new", 0, NEWSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(DISPO);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
+#undef X
 
 static struct swit aqrl[] = {
-    { "quit", 0 },
-    { "replace", 0 },
-    { "list", 0 },
-    { "refile +folder", 0 },
-    { NULL, 0 }
+    { "quit", 0, NOSW },
+    { "replace", 0, YESW },
+    { "list", 0, LISTDSW },
+    { "refile +folder", 0, REFILSW },
+    { NULL, 0, 0 }
 };
 
 static char drft[BUFSIZ];
@@ -137,20 +111,13 @@ main (int argc, char **argv)
     char *dmsg = NULL, *digest = NULL, *ed = NULL;
     char *file = NULL, *filter = NULL, *folder = NULL, *fwdmsg = NULL;
     char *from = NULL, *to = NULL, *cc = NULL, *subject = NULL, *fcc = NULL;
-    char *form = NULL, buf[BUFSIZ], value[10];
+    char *form = NULL, buf[BUFSIZ];
     char **argp, **arguments;
     struct stat st;
     struct msgs_array msgs = { 0, 0, NULL };
-
     int buildsw = 0;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 1)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -195,7 +162,8 @@ main (int argc, char **argv)
                    nwhat = 0;
                    continue;
                case BILDSW:
-                   buildsw++;  /* fall... */
+                   buildsw++;
+                   /* FALLTHRU */
                case NWHATSW: 
                    nwhat++;
                    continue;
@@ -243,7 +211,7 @@ main (int argc, char **argv)
                case DGSTSW: 
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   digest = getcpy(cp);
+                   digest = mh_xstrdup(cp);
                    mime = 0;
                    continue;
                case ISSUESW:
@@ -279,10 +247,10 @@ main (int argc, char **argv)
                    continue;
 
                case BITSTUFFSW: 
-                   dashstuff = 1;      /* trinary logic */
+                   dashstuff = 1;      /* ternary logic */
                    continue;
                case NBITSTUFFSW: 
-                   dashstuff = -1;     /* trinary logic */
+                   dashstuff = -1;     /* ternary logic */
                    continue;
 
                case FROMSW:
@@ -308,7 +276,7 @@ main (int argc, char **argv)
                case SUBJECTSW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
-                   subject = getcpy(cp);
+                   subject = mh_xstrdup(cp);
                    continue;
 
                case WIDTHSW:
@@ -322,14 +290,13 @@ main (int argc, char **argv)
        if (*cp == '+' || *cp == '@') {
            if (folder)
                adios (NULL, "only one folder at a time!");
-           else
-               folder = pluspath (cp);
+            folder = pluspath (cp);
        } else {
            app_msgarg(&msgs, cp);
        }
     }
 
-    cwd = getcpy (pwd ());
+    cwd = mh_xstrdup(pwd ());
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
@@ -345,7 +312,8 @@ try_it_again:
     if (!buildsw && stat (drft, &st) != NOTOK) {
        printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
        for (i = LISTDSW; i != YESW;) {
-           if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
+           if (!(argp = read_switch_multiword ("\nDisposition? ",
+                                               isdf ? aqrnl : aqrl)))
                done (1);
            switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
                case NOSW: 
@@ -363,7 +331,7 @@ try_it_again:
                        i = YESW;
                    break;
                default: 
-                   advise (NULL, "say what?");
+                   inform("say what?");
                    break;
            }
        }
@@ -388,7 +356,7 @@ try_it_again:
            adios (maildir, "unable to change directory to");
 
        /* read folder and create message structure */
-       if (!(mp = folder_read (folder)))
+       if (!(mp = folder_read (folder, 1)))
            adios (NULL, "unable to read folder %s", folder);
 
        /* check for empty folder */
@@ -409,7 +377,7 @@ try_it_again:
 
        for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
            if (is_selected (mp, msgnum)) {
-               fwdmsg = strdup(m_name(msgnum));
+               fwdmsg = mh_xstrdup(m_name(msgnum));
                break;
            }
 
@@ -485,11 +453,9 @@ try_it_again:
 
        if (digest) {
            snprintf (buf, sizeof(buf), IFORMAT, digest);
-           snprintf (value, sizeof(value), "%d", issue);
-           context_replace (buf, getcpy (value));
+           context_replace (buf, mh_xstrdup(m_str(issue)));
            snprintf (buf, sizeof(buf), VFORMAT, digest);
-           snprintf (value, sizeof(value), "%d", volume);
-           context_replace (buf, getcpy (value));
+           context_replace (buf, mh_xstrdup(m_str(volume)));
        }
 
        context_replace (pfolder, folder);      /* update current folder   */
@@ -518,19 +484,18 @@ mhl_draft (int out, char *digest, int volume, int issue,
             char *file, char *filter, int dashstuff)
 {
     pid_t child_id;
-    int i, msgnum, pd[2];
+    int msgnum, pd[2];
     char buf1[BUFSIZ];
     char buf2[BUFSIZ];
+    char *program;
     struct msgs_array vec = { 0, 0, NULL };
 
     if (pipe (pd) == NOTOK)
        adios ("pipe", "unable to create");
 
-    app_msgarg(&vec, r1bindex (mhlproc, '/'));
-
-    for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-       sleep (5);
+    argsplit_msgarg(&vec, mhlproc, &program);
 
+    child_id = fork();
     switch (child_id) {
        case NOTOK: 
            adios ("fork", "unable to");
@@ -540,7 +505,6 @@ mhl_draft (int out, char *digest, int volume, int issue,
            dup2 (pd[1], 1);
            close (pd[1]);
 
-           i = 1;
            app_msgarg(&vec, "-forwall");
            app_msgarg(&vec, "-form");
            app_msgarg(&vec, filter);
@@ -568,12 +532,12 @@ mhl_draft (int out, char *digest, int volume, int issue,
 
            for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
                if (is_selected (mp, msgnum))
-                   app_msgarg(&vec, getcpy (m_name (msgnum)));
+                   app_msgarg(&vec, mh_xstrdup(m_name (msgnum)));
            }
 
            app_msgarg(&vec, NULL);
 
-           execvp (mhlproc, vec.msgs);
+           execvp (program, vec.msgs);
            fprintf (stderr, "unable to exec ");
            perror (mhlproc);
            _exit (-1);
@@ -599,7 +563,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 {
     int fd,i, msgcnt, msgnum;
     int len, buflen;
-    register char *bp, *msgnam;
+    char *bp, *msgnam;
     char buffer[BUFSIZ];
 
     msgcnt = 1;
@@ -620,7 +584,7 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 
                if (msgnum == mp->lowsel) {
                    snprintf (bp, buflen, " Forwarded Message%s",
-                       mp->numsel > 1 ? "s" : "");
+                       PLURALS(mp->numsel));
                } else {
                    snprintf (bp, buflen, " Message %d", msgcnt);
                }
@@ -630,7 +594,9 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
 
                strncpy (bp, "\n\n", buflen);
            }
-           write (out, buffer, strlen (buffer));
+           if (write (out, buffer, strlen (buffer)) < 0) {
+               advise (drft, "write");
+           }
 
            if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
                admonish (msgnam, "unable to read message");
@@ -655,9 +621,11 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
        strncpy (buffer, delim4, sizeof(buffer));
     } else {
        snprintf (buffer, sizeof(buffer), "\n------- End of Forwarded Message%s\n",
-               mp->numsel > 1 ? "s" : "");
+               PLURALS(mp->numsel));
+    }
+    if (write (out, buffer, strlen (buffer)) < 0) {
+       advise (drft, "write");
     }
-    write (out, buffer, strlen (buffer));
 
     if (digest) {
        snprintf (buffer, sizeof(buffer), "End of %s Digest [Volume %d Issue %d]\n",
@@ -667,7 +635,9 @@ copy_draft (int out, char *digest, char *file, int volume, int issue, int dashst
            *bp++ = '*';
        *bp++ = '\n';
        *bp = 0;
-       write (out, buffer, strlen (buffer));
+       if (write (out, buffer, strlen (buffer)) < 0) {
+           advise (drft, "write");
+       }
     }
 }
 
@@ -683,12 +653,18 @@ copy_mime_draft (int out)
     char buffer[BUFSIZ];
 
     snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
-       mp->numsel == 1 ? "" : "s", mp->foldpath);
-    write (out, buffer, strlen (buffer));
+       PLURALS(mp->numsel), mp->foldpath);
+    if (write (out, buffer, strlen (buffer)) < 0) {
+       advise (drft, "write");
+    }
     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
        if (is_selected (mp, msgnum)) {
            snprintf (buffer, sizeof(buffer), " %s", m_name (msgnum));
-           write (out, buffer, strlen (buffer));
+           if (write (out, buffer, strlen (buffer)) < 0) {
+               advise (drft, "write");
+           }
        }
-    write (out, "\n", 1);
+    if (write (out, "\n", 1) < 0) {
+       advise (drft, "write newline");
+    }
 }