Whatnow prompt: From now on, detaching only by numbers.
[mmh] / uip / whatnow.c
index 08e3be8..273921c 100644 (file)
@@ -31,9 +31,8 @@
 **        alist                   This option lists the attachments on the
 **                                draft.
 **
-**        detach files            This option removes attachments from the
-**        detach -n numbers       draft.  This can be done by file name or
-**                                by attachment number.
+**        detach numbers          This option removes attachments by
+**                                attachment number from the draft.
 */
 
 #include <h/mh.h>
@@ -46,9 +45,9 @@ static struct swit whatnowswitches[] = {
 #define EDITRSW  0
        { "editor editor", 0 },
 #define PRMPTSW  1
-       { "prompt string", 4 },
+       { "prompt string", 0 },
 #define VERSIONSW  2
-       { "version", 0 },
+       { "Version", 0 },
 #define HELPSW  3
        { "help", 0 },
        { NULL, 0 }
@@ -81,7 +80,7 @@ static struct swit aleqs[] = {
 #define ATTACHCMDSW  10
        { "attach", 0 },
 #define DETACHCMDSW  11
-       { "detach [-n]", 0 },
+       { "detach", 0 },
 #define ALISTCMDSW  12
        { "alist", 0 },
        { NULL, 0 }
@@ -92,8 +91,7 @@ static char *myprompt = "\nWhat now? ";
 /*
 ** static prototypes
 */
-static int editfile(char **, char **, char *, int, struct msgs *,
-       char *, char *);
+static int editfile(char **, char **, char *, int);
 static int sendfile(char **, char *);
 static int refile(char **, char *);
 static int removefile(char *);
@@ -103,17 +101,12 @@ static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
 static int system_in_dir(const char *dir, const char *cmd);
 
 
-#ifdef HAVE_LSTAT
-static int copyf(char *, char *);
-#endif
-
-
 int
 main(int argc, char **argv)
 {
        int use = 0;
        char *cp;
-       char *ed = NULL, *drft = NULL, *msgnam = NULL;
+       char *ed = NULL, *drft = NULL;
        char buf[BUFSIZ], prompt[BUFSIZ];
        char **argp, **arguments;
        struct stat st;
@@ -121,12 +114,8 @@ main(int argc, char **argv)
        char file[MAXPATHLEN + 1];  /* file name buffer */
        char shell[MAXPATHLEN + 1];  /* shell response buffer */
        FILE *f;  /* read pointer for bgnd proc */
-       int n;  /* set on -n to detach command */
 
-#ifdef LOCALE
        setlocale(LC_ALL, "");
-#endif
-
        invo_name = mhbasename(argv[0]);
 
        /* read user profile/context */
@@ -185,8 +174,6 @@ main(int argc, char **argv)
        if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
                drft = getcpy(m_draft(seq_cur));
 
-       msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
-
        if ((cp = getenv("mhuse")) && *cp)
                use = atoi(cp);
 
@@ -196,7 +183,11 @@ main(int argc, char **argv)
 
        /* start editing the draft, unless editor is the empty string */
        if (*ed) {
-               if (editfile(&ed, NULL, drft, use, NULL, msgnam, NULL) <0) {
+               if (editfile(&ed, NULL, drft, use) <0) {
+                       if (!use) {
+                               unlink(drft);
+                       }
+                       advise(NULL, "Try again.");
                        done(1);
                }
        }
@@ -204,15 +195,14 @@ main(int argc, char **argv)
        snprintf(prompt, sizeof(prompt), myprompt, invo_name);
        for (;;) {
                if (!(argp = getans(prompt, aleqs))) {
-                       unlink(altmsglink);
                        done(1);
                }
                switch (smatch(*argp, aleqs)) {
                case DISPSW:
                        /* display the msg being replied to or distributed */
-                       if (msgnam) {
+                       if ((cp = getenv("mhaltmsg")) && *cp) {
                                snprintf(buf, sizeof buf, "%s '%s'",
-                                               listproc, msgnam);
+                                               listproc, cp);
                                system(buf);
                        } else {
                                advise(NULL, "no alternate message to display");
@@ -223,9 +213,7 @@ main(int argc, char **argv)
                        /* Call an editor on the draft file */
                        if (*++argp)
                                ed = *argp++;
-                       if (editfile(&ed, argp, drft, NOUSE, NULL,
-                                       msgnam, NULL) == NOTOK)
-                               done(1);
+                       editfile(&ed, argp, drft, NOUSE);
                        break;
 
                case LISTSW:
@@ -309,8 +297,7 @@ main(int argc, char **argv)
                        ** List attachments on current draft.
                        */
                        snprintf(buf, sizeof buf, "anno -list -comp '%s' "
-                                       "-number all -text IGNORE '%s'",
-                                       attach_hdr, drft);
+                                       "-number '%s'", attach_hdr, drft);
                        if (system(buf) != 0) {
                                advise(NULL, "Could not list attachment headers.");
                        }
@@ -377,86 +364,39 @@ main(int argc, char **argv)
                        /*
                        ** Detach files from current draft.
                        **
-                       ** Scan the arguments for a -n.  Mixed file
-                       ** names and numbers aren't allowed, so this
-                       ** catches a -n anywhere in the argument list.
-                       */
-                       for (n = 0, arguments = argp + 1;
-                                       *arguments != NULL;
-                                       arguments++) {
-                               if (strcmp(*arguments, "-n") == 0) {
-                                               n = 1;
-                                               break;
-                               }
-                       }
-
-                       /*
-                       ** A -n was found so interpret the arguments as
+                       ** Interpret the arguments as
                        ** attachment numbers.  Decrement any remaining
                        ** argument number that is greater than the one
                        ** just processed after processing each one so
                        ** that the numbering stays correct.
                        */
-                       if (n == 1) {
-                               for (arguments=argp+1; *arguments;
-                                               arguments++) {
-                                       if (strcmp(*arguments, "-n") == 0)
-                                               continue;
-
-                                       if (**arguments != '\0') {
-                                               char buf[BUFSIZ];
-
-                                               n = atoi(*arguments);
-                                               snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
-                                               if (system(buf) != 0) {
-                                                       advise(NULL, "Could not delete attachment header.");
-                                               }
+                       for (arguments=argp+1; *arguments; arguments++) {
+                               char buf[BUFSIZ];
+                               int n;
 
-                                               for (argp=arguments+1; *argp;
-                                                               argp++) {
-                                                       if (atoi(*argp) > n) {
-                                                               if (atoi(*argp) == 1)
-                                                                       *argp = "";
-                                                               else
-                                                                       sprintf(*argp, "%d", atoi(*argp) - 1);
-                                                       }
-                                               }
-                                       }
+                               if (**arguments == '\0') {
+                                       continue;
                                }
-                               break;
-                       }
-                       /* else */
 
-                       /*
-                       ** The arguments are interpreted as file names.
-                       ** Run them through the user's shell for wildcard
-                       ** expansion and other goodies.  Do this from
-                       ** the current working directory if the argument
-                       ** is not an absolute path name (does not begin
-                       ** with a /).
-                       **
-                       ** We feed all the file names to the shell at
-                       ** once, otherwise you can't provide a file name
-                       ** with a space in it.
-                       */
-                       writelscmd(buf, sizeof(buf), argp);
-                       if ((f = popen_in_dir(cwd, buf, "r"))) {
-                               while (fgets(shell, sizeof (shell), f)) {
-                                       *(strchr(shell, '\n')) = '\0';
-                                       snprintf(buf, sizeof buf,
-                                                       "anno -delete -comp "
-                                                       "'%s' -text '%s' '%s'",
-                                                       attach_hdr, shell,
-                                                       drft);
-                                       if (system(buf) != 0) {
-                                               advise(NULL, "Could not delete attachment header.");
+                               n = atoi(*arguments);
+                               snprintf(buf, sizeof buf, "anno -delete "
+                                               "-comp '%s' -number '%d' "
+                                               "'%s'",
+                                               attach_hdr, n, drft);
+                               if (system(buf) != 0) {
+                                       advise(NULL, "Could not delete attachment header.");
+                               }
+
+                               for (argp=arguments+1; *argp; argp++) {
+                                       if (atoi(*argp) > n) {
+                                               if (atoi(*argp) == 1) {
+                                                       *argp = "";
+                                               } else {
+                                                       sprintf(*argp, "%d", atoi(*argp) - 1);
+                                               }
                                        }
                                }
-                               pclose(f);
-                       } else {
-                               advise("popen", "could not get file from shell");
                        }
-
                        break;
 
                default:
@@ -573,17 +513,10 @@ static char *edsave = NULL;  /* the editor we used previously */
 
 
 static int
-editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
-       char *altmsg, char *cwd)
+editfile(char **ed, char **arg, char *file, int use)
 {
        int pid, status, vecp;
-       char altpath[BUFSIZ], linkpath[BUFSIZ];
        char *cp, *vec[MAXARGS];
-       struct stat st;
-
-#ifdef HAVE_LSTAT
-       int slinked = 0;
-#endif /* HAVE_LSTAT */
 
        if (!*ed || !**ed) {
                /* We have no explicit editor. */
@@ -603,31 +536,7 @@ editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
                }
        }
 
-       if (altmsg) {
-               if (!mp || *altmsg == '/' || !cwd)
-                       strncpy(altpath, altmsg, sizeof(altpath));
-               else
-                       snprintf(altpath, sizeof(altpath), "%s/%s",
-                                       mp->foldpath, altmsg);
-               if (!cwd)
-                       strncpy(linkpath, altmsglink, sizeof(linkpath));
-               else
-                       snprintf(linkpath, sizeof(linkpath), "%s/%s",
-                                       cwd, altmsglink);
-               unlink(linkpath);
-#ifdef HAVE_LSTAT
-               if (link(altpath, linkpath) == NOTOK) {
-                       symlink(altpath, linkpath);
-                       slinked = 1;
-               } else {
-                       slinked = 0;
-               }
-#else /* not HAVE_LSTAT */
-               link(altpath, linkpath);
-#endif /* not HAVE_LSTAT */
-       }
-
-       context_save();  /* save the context file */
+       context_save();
        fflush(stdout);
 
        switch (pid = fork()) {
@@ -637,14 +546,6 @@ editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
                break;
 
        case OK:
-               if (cwd)
-                       chdir(cwd);
-               if (altmsg) {
-                       if (mp)
-                               m_putenv("mhfolder", mp->foldpath);
-                       m_putenv("editalt", altpath);
-               }
-
                vecp = 0;
                vec[vecp++] = mhbasename(*ed);
                if (arg)
@@ -654,84 +555,40 @@ editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
                vec[vecp] = NULL;
 
                execvp(*ed, vec);
-               fprintf(stderr, "unable to exec ");
+               fprintf(stderr, "%s: unable to exec ", invo_name);
                perror(*ed);
                _exit(-1);
 
        default:
                if ((status = pidwait(pid, NOTOK))) {
-                       if (((status & 0xff00) != 0xff00) &&
-                                       (!edsave || (status & 0x00ff))) {
-                               if (!use && (status & 0xff00) && (rename(file, cp = m_backup(file)) != NOTOK)) {
-                                       advise(NULL, "problems with edit--draft left in %s", cp);
-                               } else {
-                                       advise(NULL, "problems with edit--%s preserved", file);
-                               }
+                       if ((status & 0xff00) == 0xff00) {
+                               /* cmd not found or pidwait() failed */
+                               status = -1;
+                               break;
+                       }
+                       if (status & 0x00ff) {
+                               /* terminated by signal */
+                               advise(NULL, "%s terminated by signal %d",
+                                               *ed, status & 0x7f);
+                       } else {
+                               /* failure exit */
+                               advise(NULL, "%s exited with return code %d",
+                                               *ed, (status & 0xff00) >> 8);
                        }
-                       status = -2;  /* maybe "edsave ? -2 : -1"? */
+                       status = -1;
                        break;
                }
-
-#ifdef HAVE_LSTAT
-               if (altmsg && mp && !is_readonly(mp) && (slinked ?
-                               lstat (linkpath, &st) != NOTOK &&
-                               S_ISREG(st.st_mode) &&
-                               copyf(linkpath, altpath) == NOTOK :
-                               stat(linkpath, &st) != NOTOK &&
-                               st.st_nlink == 1 &&
-                               (unlink(altpath) == NOTOK ||
-                               link(linkpath, altpath) == NOTOK)))
-                       advise(linkpath, "unable to update %s from", altmsg);
-#else /* HAVE_LSTAT */
-               if (altmsg && mp && !is_readonly(mp) &&
-                               stat(linkpath, &st) != NOTOK &&
-                               st.st_nlink == 1 &&
-                               (unlink(altpath) == NOTOK ||
-                               link(linkpath, altpath) == NOTOK))
-                       advise(linkpath, "unable to update %s from", altmsg);
-#endif /* HAVE_LSTAT */
        }
 
        /* remember which editor we used */
        edsave = getcpy(*ed);
 
        *ed = NULL;
-       if (altmsg)
-               unlink(linkpath);
 
        return status;
 }
 
 
-#ifdef HAVE_LSTAT
-static int
-copyf(char *ifile, char *ofile)
-{
-       int i, in, out;
-       char buffer[BUFSIZ];
-
-       if ((in = open(ifile, O_RDONLY)) == NOTOK)
-               return NOTOK;
-       if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
-               admonish(ofile, "unable to open and truncate");
-               close(in);
-               return NOTOK;
-       }
-
-       while ((i = read(in, buffer, sizeof(buffer))) > OK)
-               if (write(out, buffer, i) != i) {
-                       advise(ofile, "may have damaged");
-                       i = NOTOK;
-                       break;
-               }
-
-       close(in);
-       close(out);
-       return i;
-}
-#endif /* HAVE_LSTAT */
-
-
 /*
 ** SEND
 */
@@ -760,7 +617,7 @@ sendfile(char **arg, char *file)
                vec[vecp] = NULL;
 
                execvp("send", vec);
-               fprintf(stderr, "unable to exec ");
+               fprintf(stderr, "%s: unable to exec ", invo_name);
                perror("send");
                _exit(-1);
 
@@ -803,7 +660,7 @@ refile(char **arg, char *file)
 
        case 0:
                execvp(*vec, vec);
-               fprintf(stderr, "unable to exec ");
+               fprintf(stderr, "%s: unable to exec ", invo_name);
                perror(*vec);
                _exit(-1);