]> diplodocus.org Git - nmh/blobdiff - uip/dist.c
getpass.c: Move interface to own file.
[nmh] / uip / dist.c
index 807eb78bbf4afe2fe27612b5ef24ace2275cbd4f..28217b7314a5b8b0ef7814d307f0eea3e608ae90 100644 (file)
@@ -1,91 +1,99 @@
-
-/*
- * dist.c -- re-distribute a message
+/* dist.c -- re-distribute a message
  *
  * 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/utils.h>
+#include "h/mh.h"
+#include "sbr/refile.h"
+#include "sbr/cpydata.h"
+#include "sbr/m_draft.h"
+#include "sbr/m_convert.h"
+#include "sbr/getfolder.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/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/error.h"
+#include "h/utils.h"
+#include "sbr/m_maildir.h"
 #include <fcntl.h>
-
-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        FORMSW  7
-    { "form formfile", 0 },
-#define        INPLSW  8
-    { "inplace", 0 },
-#define        NINPLSW 9
-    { "noinplace", 0 },
-#define        WHATSW  10
-    { "whatnowproc program", 0 },
-#define        NWHATSW 11
-    { "nowhatnowproc", 0 },
-#define VERSIONSW 12
-    { "version", 0 },
-#define        HELPSW  13
-    { "help", 0 },
-#define        FILESW  14
-    { "file file", -4 },       /* interface from msh */
-#define FROMSW  15
-    { "from address", 0 },
-#define TOSW    16
-    { "to address", 0 },
-#define CCSW    17
-    { "cc address", 0 },
-#define FCCSW   18
-    { "fcc mailbox", 0 },
-#define WIDTHSW 19
-    { "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 }
-};
+#include "h/done.h"
+#include "forwsbr.h"
+
+#define DIST_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("form formfile", 0, FORMSW) \
+    X("inplace", 0, INPLSW) \
+    X("noinplace", 0, NINPLSW) \
+    X("whatnowproc program", 0, WHATSW) \
+    X("nowhatnowproc", 0, NWHATSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+    X("file file", -4, FILESW) \
+    X("from address", 0, FROMSW) \
+    X("to address", 0, TOSW) \
+    X("cc address", 0, CCSW) \
+    X("fcc mailbox", 0, FCCSW) \
+    X("width columns", 0, WIDTHSW) \
+    X("atfile", 0, ATFILESW) \
+    X("noatfile", 0, NOATFILESW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(DIST);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(DIST, 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 }
 };
 
 
 int
 main (int argc, char **argv)
 {
-    int anot = 0, inplace = 1, nedit = 0;
-    int nwhat = 0, i, in, isdf = 0, out;
+    bool anot = false;
+    bool inplace = true;
+    bool nedit = false;
+    bool nwhat = false;
+    int i, in, isdf = 0, out;
     int outputlinelen = OUTPUTLINELEN;
     int dat[5];
+    bool atfile = false;
     char *cp, *cwd, *maildir, *msgnam, *dfolder = NULL;
     char *dmsg = NULL, *ed = NULL, *file = NULL, *folder = NULL;
     char *form = NULL, *msg = NULL, buf[BUFSIZ], drft[BUFSIZ];
@@ -94,13 +102,7 @@ main (int argc, char **argv)
     struct msgs *mp = NULL;
     struct stat st;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], true, true)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -112,74 +114,74 @@ 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] [msg] [switches]",
                        invo_name);
                    print_help (buf, switches, 1);
-                   done (1);
+                   done (0);
                case VERSIONSW:
                    print_version(invo_name);
-                   done (1);
+                   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 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 FORMSW: 
                    if (!(form = *argp++) || *form == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    continue;
 
                case INPLSW: 
-                   inplace++;
+                   inplace = true;
                    continue;
                case NINPLSW: 
-                   inplace = 0;
+                   inplace = false;
                    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;
@@ -188,52 +190,57 @@ main (int argc, char **argv)
 
                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 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;
+
+               case ATFILESW:
+                   atfile = true;
+                   continue;
+               case NOATFILESW:
+                   atfile = false;
                    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 {
            if (msg)
-               adios (NULL, "only one message at a time!");
-           else
-               msg = cp;
+               die("only one message at a time!");
+            msg = cp;
        }
     }
 
-    cwd = getcpy (pwd ());
+    cwd = mh_xstrdup(pwd ());
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
     if (file && (msg || folder))
-       adios (NULL, "can't mix files and folders/msgs");
+       die("can't mix files and folders/msgs");
 
 try_it_again:
     strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
@@ -242,7 +249,8 @@ try_it_again:
     if (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: 
@@ -260,7 +268,7 @@ try_it_again:
                        i = YESW;
                    break;
                default: 
-                   advise (NULL, "say what?");
+                   inform("say what?");
                    break;
            }
        }
@@ -270,7 +278,7 @@ try_it_again:
        /*
         * Dist a file
         */
-       anot = 0;       /* don't want to annotate a file */
+       anot = false;   /* don't want to annotate a file */
     } else {
        /*
         * Dist a message
@@ -285,12 +293,12 @@ try_it_again:
            adios (maildir, "unable to change directory to");
 
        /* read folder and create message structure */
-       if (!(mp = folder_read (folder)))
-           adios (NULL, "unable to read folder %s", folder);
+       if (!(mp = folder_read (folder, 1)))
+           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 the message range/sequence/name and set SELECTED */
        if (!m_convert (mp, msg))
@@ -298,12 +306,12 @@ try_it_again:
        seq_setprev (mp);       /* set the previous-sequence */
 
        if (mp->numsel > 1)
-           adios (NULL, "only one message at a time!");
+           die("only one message at a time!");
     }
 
-    msgnam = file ? file : getcpy (m_name (mp->lowsel));
+    msgnam = file ? file : mh_xstrdup(m_name (mp->lowsel));
 
-    dat[0] = mp->lowsel;
+    dat[0] = mp ? mp->lowsel : 0;
     dat[1] = 0;
     dat[2] = 0;
     dat[3] = outputlinelen;
@@ -333,8 +341,8 @@ try_it_again:
 
     if (nwhat)
        done (0);
-    what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp,
-       anot ? "Resent" : NULL, inplace, cwd);
+    what_now (ed, nedit, NOUSE, drft, msgnam, 1, mp, anot ? "Resent" : NULL,
+           inplace, cwd, atfile);
     done (1);
     return 1;
 }