]> diplodocus.org Git - nmh/blobdiff - uip/forw.c
fdcompare.c: Move interface to own file.
[nmh] / uip / forw.c
index 12a323bb874cfe2746ffa161846e64871f3a1dd5..e0c5423d2117d9f477c60130618d353dce13f5c2 100644 (file)
@@ -1,16 +1,28 @@
-
-/*
- * 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
  * complete copyright information.
  */
 
-#include <h/mh.h>
+#include "h/mh.h"
+#include "sbr/folder_read.h"
+#include "sbr/context_save.h"
+#include "sbr/context_replace.h"
+#include "sbr/context_find.h"
+#include "sbr/ambigsw.h"
+#include "sbr/pidstatus.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/arglist.h"
+#include "sbr/error.h"
 #include <fcntl.h>
-#include <h/tws.h>
-#include <h/utils.h>
+#include "h/tws.h"
+#include "h/done.h"
+#include "h/utils.h"
+#include "sbr/m_maildir.h"
+#include "forwsbr.h"
 
 
 #define        IFORMAT "digest-issue-%s"
@@ -41,7 +53,7 @@
     X("nodashstuffing", 0, NBITSTUFFSW) \
     X("version", 0, VERSIONSW) \
     X("help", 0, HELPSW) \
-    X("file file", 4, FILESW)                 /* interface from msh */ \
+    X("file file", 4, FILESW) \
     X("build", 5, BILDSW)                     /* interface from mhe */ \
     X("from address", 0, FROMSW) \
     X("to address", 0, TOSW) \
@@ -102,9 +114,13 @@ static void copy_mime_draft (int);
 int
 main (int argc, char **argv)
 {
-    int anot = 0, inplace = 1, mime = 0;
+    bool anot = false;
+    bool inplace = true;
+    bool mime = false;
     int issue = 0, volume = 0, dashstuff = 0;
-    int nedit = 0, nwhat = 0, i, in;
+    bool nedit = false;
+    bool nwhat = false;
+    int i, in;
     int out, isdf = 0, msgnum = 0;
     int outputlinelen = OUTPUTLINELEN;
     int dat[5];
@@ -112,13 +128,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;
+    bool buildsw = false;
 
-    if (nmh_init(argv[0], 1)) { return 1; }
+    if (nmh_init(argv[0], true, true)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -130,7 +146,7 @@ main (int argc, char **argv)
                    ambigsw (cp, switches);
                    done (1);
                case UNKWNSW: 
-                   adios (NULL, "-%s unknown", cp);
+                   die("-%s unknown", cp);
 
                case HELPSW: 
                    snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
@@ -142,104 +158,105 @@ main (int argc, char **argv)
                    done (0);
 
                case ANNOSW: 
-                   anot++;
+                   anot = true;
                    continue;
                case NANNOSW: 
-                   anot = 0;
+                   anot = false;
                    continue;
 
                case EDITRSW: 
                    if (!(ed = *argp++) || *ed == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   nedit = 0;
+                       die("missing argument to %s", argp[-2]);
+                   nedit = false;
                    continue;
                case NEDITSW:
-                   nedit++;
+                   nedit = true;
                    continue;
 
                case WHATSW: 
                    if (!(whatnowproc = *argp++) || *whatnowproc == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   nwhat = 0;
+                       die("missing argument to %s", argp[-2]);
+                   nwhat = false;
                    continue;
                case BILDSW:
-                   buildsw++;  /* fall... */
+                   buildsw = true;
+                   /* FALLTHRU */
                case NWHATSW: 
-                   nwhat++;
+                   nwhat = true;
                    continue;
 
                case FILESW: 
                    if (file)
-                       adios (NULL, "only one file at a time!");
+                       die("only one file at a time!");
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    file = path (cp, TFILE);
                    continue;
                case FILTSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   filter = getcpy (etcpath (cp));
-                   mime = 0;
+                       die("missing argument to %s", argp[-2]);
+                   filter = mh_xstrdup(etcpath(cp));
+                   mime = false;
                    continue;
                case FORMSW: 
                    if (!(form = *argp++) || *form == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    continue;
 
                case FRMTSW:
-                   filter = getcpy (etcpath (mhlforward));
+                   filter = mh_xstrdup(etcpath(mhlforward));
                    continue;
                case NFRMTSW:
                    filter = NULL;
                    continue;
 
                case INPLSW: 
-                   inplace++;
+                   inplace = true;
                    continue;
                case NINPLSW: 
-                   inplace = 0;
+                   inplace = false;
                    continue;
 
                case MIMESW:
-                   mime++;
+                   mime = true;
                    filter = NULL;
                    continue;
                case NMIMESW: 
-                   mime = 0;
+                   mime = false;
                    continue;
 
                case DGSTSW: 
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   digest = getcpy(cp);
-                   mime = 0;
+                       die("missing argument to %s", argp[-2]);
+                   digest = mh_xstrdup(cp);
+                   mime = false;
                    continue;
                case ISSUESW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    if ((issue = atoi (cp)) < 1)
-                       adios (NULL, "bad argument %s %s", argp[-2], cp);
+                       die("bad argument %s %s", argp[-2], cp);
                    continue;
                case VOLUMSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    if ((volume = atoi (cp)) < 1)
-                       adios (NULL, "bad argument %s %s", argp[-2], cp);
+                       die("bad argument %s %s", argp[-2], cp);
                    continue;
 
                case DFOLDSW: 
                    if (dfolder)
-                       adios (NULL, "only one draft folder at a time!");
+                       die("only one draft folder at a time!");
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
                                    *cp != '@' ? TFOLDER : TSUBCWF);
                    continue;
                case DMSGSW:
                    if (dmsg)
-                       adios (NULL, "only one draft message at a time!");
+                       die("only one draft message at a time!");
                    if (!(dmsg = *argp++) || *dmsg == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    continue;
                case NDFLDSW: 
                    dfolder = NULL;
@@ -247,62 +264,61 @@ 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:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    from = addlist(from, cp);
                    continue;
                case TOSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    to = addlist(to, cp);
                    continue;
                case CCSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    cc = addlist(cc, cp);
                    continue;
                case FCCSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    fcc = addlist(fcc, cp);
                    continue;
                case SUBJECTSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   subject = getcpy(cp);
+                       die("missing argument to %s", argp[-2]);
+                   subject = mh_xstrdup(cp);
                    continue;
 
                case WIDTHSW:
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    if ((outputlinelen = atoi(cp)) < 10)
-                       adios (NULL, "impossible width %d", outputlinelen);
+                       die("impossible width %d", outputlinelen);
                    continue;
            }
        }
        if (*cp == '+' || *cp == '@') {
            if (folder)
-               adios (NULL, "only one folder at a time!");
-           else
-               folder = pluspath (cp);
+               die("only one folder at a time!");
+            folder = pluspath (cp);
        } else {
            app_msgarg(&msgs, cp);
        }
     }
 
-    cwd = getcpy (pwd ());
+    cwd = mh_xstrdup(pwd ());
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
     if (file && (msgs.size || folder))
-       adios (NULL, "can't mix files and folders/msgs");
+       die("can't mix files and folders/msgs");
 
 try_it_again:
 
@@ -313,7 +329,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: 
@@ -331,7 +348,7 @@ try_it_again:
                        i = YESW;
                    break;
                default: 
-                   advise (NULL, "say what?");
+                   inform("say what?");
                    break;
            }
        }
@@ -341,7 +358,7 @@ try_it_again:
        /*
         * Forwarding a file.
          */
-       anot = 0;       /* don't want to annotate a file */
+       anot = false;   /* don't want to annotate a file */
     } else {
        /*
         * Forwarding a message.
@@ -357,11 +374,11 @@ try_it_again:
 
        /* read folder and create message structure */
        if (!(mp = folder_read (folder, 1)))
-           adios (NULL, "unable to read folder %s", folder);
+           die("unable to read folder %s", folder);
 
        /* check for empty folder */
        if (mp->nummsg == 0)
-           adios (NULL, "no messages in %s", folder);
+           die("no messages in %s", folder);
 
        /* parse all the message ranges/sequences and set SELECTED */
        for (msgnum = 0; msgnum < msgs.size; msgnum++)
@@ -377,12 +394,12 @@ 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;
            }
 
        if (! fwdmsg)
-           adios (NULL, "Unable to find input message");
+           die("Unable to find input message");
     }
 
     if (filter && access (filter, R_OK) == NOTOK)
@@ -453,11 +470,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   */
@@ -486,7 +501,7 @@ 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;
@@ -497,9 +512,7 @@ mhl_draft (int out, char *digest, int volume, int issue,
 
     argsplit_msgarg(&vec, mhlproc, &program);
 
-    for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
-       sleep (5);
-
+    child_id = fork();
     switch (child_id) {
        case NOTOK: 
            adios ("fork", "unable to");
@@ -536,7 +549,7 @@ 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);
@@ -544,7 +557,7 @@ mhl_draft (int out, char *digest, int volume, int issue,
            execvp (program, vec.msgs);
            fprintf (stderr, "unable to exec ");
            perror (mhlproc);
-           _exit (-1);
+           _exit(1);
 
        default: 
            close (pd[1]);
@@ -567,7 +580,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;
@@ -588,7 +601,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);
                }
@@ -625,7 +638,7 @@ 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");
@@ -657,7 +670,7 @@ copy_mime_draft (int out)
     char buffer[BUFSIZ];
 
     snprintf (buffer, sizeof(buffer), "#forw [forwarded message%s] +%s",
-       mp->numsel == 1 ? "" : "s", mp->foldpath);
+       PLURALS(mp->numsel), mp->foldpath);
     if (write (out, buffer, strlen (buffer)) < 0) {
        advise (drft, "write");
     }