2 ** whatnow.c -- the WhatNow shell
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
8 ** The inclusion of attachments is eased by
9 ** using the header field name mechanism added to anno and send.
10 ** The header field name for attachments is predefined.
12 ** Several commands have been added at the whatnow prompt:
14 ** cd [ directory ] This option works just like the shell's
15 ** cd command and lets the user change the
16 ** directory from which attachments are
17 ** taken so that long path names are not
18 ** needed with every file.
20 ** ls [ ls-options ] This option works just like the normal
21 ** ls command and exists to allow the user
22 ** to verify file names in the directory.
24 ** pwd This option works just like the normal
25 ** pwd command and exists to allow the user
26 ** to verify the directory.
28 ** attach files This option attaches the named files to
31 ** alist [-ln] This option lists the attachments on the
32 ** draft. -l gets long listings, -n gets
35 ** detach files This option removes attachments from the
36 ** detach -n numbers draft. This can be done by file name or
37 ** by attachment number.
46 static struct swit whatnowswitches[] = {
48 { "editor editor", 0 },
52 { "prompt string", 4 },
61 ** Options at the "whatnow" prompt
63 static struct swit aleqs[] = {
65 { "edit [<editor> <switches>]", 0 },
67 { "refile [<switches>] +folder", 0 },
69 { "display [<switches>]", 0 },
71 { "list [<switches>]", 0 },
73 { "send [<switches>]", 0 },
75 { "push [<switches>]", 0 },
77 { "quit [-delete]", 0 },
81 { "cd [directory]", 0 },
86 #define ATTACHCMDSW 11
88 #define DETACHCMDSW 12
91 { "alist [-ln] ", 2 },
95 static char *myprompt = "\nWhat now? ";
100 static int editfile(char **, char **, char *, int, struct msgs *,
101 char *, char *, int);
102 static int sendfile(char **, char *, int);
103 static void sendit(char *, char **, char *, int);
104 static int removefile(char *);
105 static void writelscmd(char *, int, char **);
106 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
107 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
108 static int system_in_dir(const char *dir, const char *cmd);
112 static int copyf(char *, char *);
117 main(int argc, char **argv)
119 int nedit = 0, use = 0;
121 char *ed = NULL, *drft = NULL, *msgnam = NULL;
122 char buf[BUFSIZ], prompt[BUFSIZ];
123 char **argp, **arguments;
125 char cwd[MAXPATHLEN + 1]; /* current working directory */
126 char file[MAXPATHLEN + 1]; /* file name buffer */
127 char shell[MAXPATHLEN + 1]; /* shell response buffer */
128 FILE *f; /* read pointer for bgnd proc */
129 char *l; /* set on -l to alist command */
130 int n; /* set on -n to alist command */
133 setlocale(LC_ALL, "");
136 invo_name = mhbasename(argv[0]);
138 /* read user profile/context */
141 arguments = getarguments(invo_name, argc, argv, 1);
145 ** Get the initial current working directory.
148 if (getcwd(cwd, sizeof (cwd)) == NULL) {
149 adios("getcwd", "could not get working directory");
152 while ((cp = *argp++)) {
154 switch (smatch(++cp, whatnowswitches)) {
156 ambigsw(cp, whatnowswitches);
159 adios(NULL, "-%s unknown", cp);
162 snprintf(buf, sizeof(buf),
163 "%s [switches] [file]",
165 print_help(buf, whatnowswitches, 1);
168 print_version(invo_name);
172 if (!(ed = *argp++) || *ed == '-')
173 adios(NULL, "missing argument to %s",
182 if (!(myprompt = *argp++) || *myprompt == '-')
183 adios(NULL, "missing argument to %s",
190 adios(NULL, "only one draft at a time!");
195 if ((drft == NULL && (drft = getenv("mhdraft")) == NULL) || *drft == 0)
196 drft = getcpy(m_draft(seq_cur));
198 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
200 if ((cp = getenv("mhuse")) && *cp)
203 if (ed == NULL && ((ed = getenv("mheditor")) == NULL || *ed == 0)) {
208 /* start editing the draft, unless -noedit was given */
209 if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
213 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
215 if (!(argp = getans(prompt, aleqs))) {
219 switch (smatch(*argp, aleqs)) {
222 ** display the message being replied to,
226 showfile(++argp, msgnam);
228 advise(NULL, "no alternate message to display");
232 /* Call an editor on the draft file */
235 if (editfile(&ed, argp, drft, NOUSE, NULL,
236 msgnam, NULL, 1) == NOTOK)
241 /* display the draft file */
242 showfile(++argp, drft);
246 /* Quit, and possibly delete the draft */
247 if (*++argp && (*argp[0] == 'd' ||
248 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
251 if (stat(drft, &st) != NOTOK)
252 advise(NULL, "draft left on %s", drft);
257 /* Delete draft and exit */
262 /* Send draft in background */
263 if (sendfile(++argp, drft, 1))
269 sendfile(++argp, drft, 0);
273 /* Refile the draft */
274 if (refile(++argp, drft) == 0)
280 ** Change the working directory for attachments
282 ** Run the directory through the user's shell
283 ** so that we can take advantage of any syntax
284 ** that the user is accustomed to. Read back
285 ** the absolute path.
288 if (*(argp+1) == NULL) {
289 sprintf(buf, "$SHELL -c \"cd;pwd\"");
291 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
293 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
294 fgets(cwd, sizeof (cwd), f);
296 if (strchr(cwd, '\n') != NULL)
297 *strchr(cwd, '\n') = '\0';
301 advise("popen", "could not get directory");
307 /* Print the working directory for attachments */
313 ** List files in the current attachment working
316 ** Use the user's shell so that we can take
317 ** advantage of any syntax that the user is
320 writelscmd(buf, sizeof(buf), argp);
321 system_in_dir(cwd, buf);
326 ** List attachments on current draft. Options are:
328 ** -l long listing (full path names)
329 ** -n numbers listing
335 while (*++argp != NULL) {
336 if (strcmp(*argp, "-l") == 0)
339 else if (strcmp(*argp, "-n") == 0)
342 else if (strcmp(*argp, "-ln") == 0 ||
343 strcmp(*argp, "-nl") == 0) {
353 advise(NULL, "usage is alist [-ln].");
356 annolist(drft, attach_hdr, l, n);
362 ** Attach files to current draft.
365 if (*(argp+1) == NULL) {
366 advise(NULL, "attach command requires file argument(s).");
371 ** Build a command line that causes the user's
372 ** shell to list the file name arguments.
373 ** This handles and wildcard expansion, tilde
376 writelscmd(buf, sizeof(buf), argp);
379 ** Read back the response from the shell,
380 ** which contains a number of lines with one
381 ** file name per line. Remove off the newline.
382 ** Determine whether we have an absolute or
383 ** relative path name. Prepend the current
384 ** working directory to relative path names.
385 ** Add the attachment annotation to the draft.
387 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
388 while (fgets(shell, sizeof (shell), f)
390 *(strchr(shell, '\n')) = '\0';
393 annotate(drft, attach_hdr,
397 sprintf(file, "%s/%s", cwd,
399 annotate(drft, attach_hdr,
407 advise("popen", "could not get file from shell");
414 ** Detach files from current draft.
416 ** Scan the arguments for a -n. Mixed file
417 ** names and numbers aren't allowed, so this
418 ** catches a -n anywhere in the argument list.
421 for (n = 0, arguments = argp + 1;
424 if (strcmp(*arguments, "-n") == 0) {
431 ** A -n was found so interpret the arguments as
432 ** attachment numbers. Decrement any remaining
433 ** argument number that is greater than the one
434 ** just processed after processing each one so
435 ** that the numbering stays correct.
439 for (arguments = argp + 1;
442 if (strcmp(*arguments, "-n") == 0)
445 if (**arguments != '\0') {
446 n = atoi(*arguments);
447 annotate(drft, attach_hdr,
451 for (argp = arguments + 1; *argp != NULL; argp++) {
452 if (atoi(*argp) > n) {
453 if (atoi(*argp) == 1)
456 sprintf(*argp, "%d", atoi(*argp) - 1);
464 ** The arguments are interpreted as file names.
465 ** Run them through the user's shell for wildcard
466 ** expansion and other goodies. Do this from
467 ** the current working directory if the argument
468 ** is not an absolute path name (does not begin
471 ** We feed all the file names to the shell at
472 ** once, otherwise you can't provide a file name
473 ** with a space in it.
475 writelscmd(buf, sizeof(buf), argp);
476 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
477 while (fgets(shell, sizeof (shell), f)
479 *(strchr(shell, '\n')) = '\0';
480 annotate(drft, attach_hdr, shell,
485 advise("popen", "could not get file from shell");
491 /* Unknown command */
492 advise(NULL, "say what?");
502 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
506 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
510 ** Note that we do not quote -- the argp from the user
511 ** is assumed to be quoted as they desire. (We can't treat
512 ** it as pure literal as that would prevent them using ~,
513 ** wildcards, etc.) The buffer produced by this function
514 ** should be given to popen_in_dir() or system_in_dir() so
515 ** that the current working directory is set correctly.
517 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
519 ** NB that some snprintf() return -1 on overflow rather than the
520 ** new C99 mandated 'number of chars that would have been written'
523 ** length checks here and inside the loop allow for the
524 ** trailing ';', trailcmd, '"' and NUL
526 int trailln = strlen(trailcmd) + 3;
527 if (ln < 0 || ln + trailln > bufsz)
528 adios(NULL, "arguments too long");
532 while (*++argp != NULL) {
534 /* +1 for leading space */
535 if (ln + trailln + 1 > bufsz - (cp-buf))
536 adios(NULL, "arguments too long");
538 memcpy(cp, *argp, ln+1);
543 strcpy(cp, trailcmd);
551 ** Build a command line that causes the user's shell to list the file name
552 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
555 writelscmd(char *buf, int bufsz, char **argp)
557 writesomecmd(buf, bufsz, "ls", "", argp);
561 ** Like system(), but run the command in directory dir.
562 ** This assumes the program is single-threaded!
565 system_in_dir(const char *dir, const char *cmd)
569 if (getcwd(olddir, sizeof(olddir)) == 0)
570 adios("getcwd", "could not get working directory");
572 adios("chdir", "could not change working directory");
574 if (chdir(olddir) != 0)
575 adios("chdir", "could not change working directory");
579 /* ditto for popen() */
581 popen_in_dir(const char *dir, const char *cmd, const char *type)
585 if (getcwd(olddir, sizeof(olddir)) == 0)
586 adios("getcwd", "could not get working directory");
588 adios("chdir", "could not change working directory");
589 f = popen(cmd, type);
590 if (chdir(olddir) != 0)
591 adios("chdir", "could not change working directory");
600 static int reedit = 0; /* have we been here before? */
601 static char *edsave = NULL; /* the editor we used previously */
605 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
606 char *altmsg, char *cwd, int save_editor)
608 int pid, status, vecp;
609 char altpath[BUFSIZ], linkpath[BUFSIZ];
610 char *cp, *vec[MAXARGS];
615 #endif /* HAVE_LSTAT */
617 /* Was there a previous edit session? */
619 if (!*ed) { /* no explicit editor */
620 *ed = edsave; /* so use the previous one */
621 if ((cp = mhbasename(*ed)) == NULL)
624 /* unless we've specified it via "editor-next" */
625 cp = concat(cp, "-next", NULL);
626 if ((cp = context_find(cp)) != NULL)
630 /* set initial editor */
631 if (*ed == NULL && (*ed = context_find("editor")) == NULL)
636 if (mp == NULL || *altmsg == '/' || cwd == NULL)
637 strncpy(altpath, altmsg, sizeof(altpath));
639 snprintf(altpath, sizeof(altpath), "%s/%s",
640 mp->foldpath, altmsg);
642 strncpy(linkpath, altmsglink, sizeof(linkpath));
644 snprintf(linkpath, sizeof(linkpath), "%s/%s",
651 if (link(altpath, linkpath) == NOTOK) {
652 symlink(altpath, linkpath);
657 #else /* not HAVE_LSTAT */
658 link(altpath, linkpath);
659 #endif /* not HAVE_LSTAT */
662 context_save(); /* save the context file */
665 switch (pid = fork()) {
667 advise("fork", "unable to");
676 m_putenv("mhfolder", mp->foldpath);
677 m_putenv("editalt", altpath);
681 vec[vecp++] = mhbasename(*ed);
684 vec[vecp++] = *arg++;
689 fprintf(stderr, "unable to exec ");
694 if ((status = pidwait(pid, NOTOK))) {
695 if (((status & 0xff00) != 0xff00)
696 && (!reedit || (status & 0x00ff))) {
697 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
698 advise(NULL, "problems with edit--draft left in %s", cp);
700 advise(NULL, "problems with edit--%s preserved", file);
703 status = -2; /* maybe "reedit ? -2 : -1"? */
709 if (altmsg && mp && !is_readonly(mp) && (slinked ?
710 lstat (linkpath, &st) != NOTOK &&
711 S_ISREG(st.st_mode) &&
712 copyf(linkpath, altpath) == NOTOK :
713 stat(linkpath, &st) != NOTOK &&
715 (unlink(altpath) == NOTOK ||
716 link(linkpath, altpath) == NOTOK)))
717 advise(linkpath, "unable to update %s from", altmsg);
718 #else /* HAVE_LSTAT */
719 if (altmsg && mp && !is_readonly(mp) &&
720 stat(linkpath, &st) != NOTOK &&
722 (unlink(altpath) == NOTOK ||
723 link(linkpath, altpath) == NOTOK))
724 advise(linkpath, "unable to update %s from", altmsg);
725 #endif /* HAVE_LSTAT */
728 /* normally, we remember which editor we used */
730 edsave = getcpy(*ed);
742 copyf(char *ifile, char *ofile)
747 if ((in = open(ifile, O_RDONLY)) == NOTOK)
749 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
750 admonish(ofile, "unable to open and truncate");
755 while ((i = read(in, buffer, sizeof(buffer))) > OK)
756 if (write(out, buffer, i) != i) {
757 advise(ofile, "may have damaged");
766 #endif /* HAVE_LSTAT */
774 sendfile(char **arg, char *file, int pushsw)
778 char *cp, *sp, *vec[MAXARGS];
781 ** If the sendproc is the nmh command `send', then we call
782 ** those routines directly rather than exec'ing the command.
784 if (strcmp(sp = mhbasename(sendproc), "send") == 0) {
786 sendit(invo_name = sp, arg, file, pushsw);
791 /* some different sendproc */
793 context_save(); /* save the context file */
796 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
800 advise(NULL, "unable to fork, so sending directly...");
804 vec[vecp++] = invo_name;
806 vec[vecp++] = "-push";
809 vec[vecp++] = *arg++;
813 execvp(sendproc, vec);
814 fprintf(stderr, "unable to exec ");
819 if (pidwait(child_id, OK) == 0)
826 static struct swit sendswitches[] = {
828 { "alias aliasfile", 0 },
832 { "filter filterfile", 0 },
856 { "width columns", 0 },
857 #define SVERSIONSW 15
865 extern int debugsw; /* from sendsbr.c */
871 extern char *altmsg; /* .. */
872 extern char *annotext;
873 extern char *distfile;
877 sendit(char *sp, char **arg, char *file, int pushed)
880 char *cp, buf[BUFSIZ], **argp;
881 char **arguments, *vec[MAXARGS];
889 ** Make sure these are defined. In particular, we need
890 ** vec[1] to be NULL, in case "arg" is NULL below. It
891 ** doesn't matter what is the value of vec[0], but we
892 ** set it to NULL, to help catch "off-by-one" errors.
898 ** Temporarily copy arg to vec, since the brkstring() call in
899 ** getarguments() will wipe it out before it is merged in.
900 ** Also, we skip the first element of vec, since getarguments()
901 ** skips it. Then we count the number of arguments
902 ** copied. The value of "n" will be one greater than
903 ** this in order to simulate the standard argc/argv.
908 copyip(arg, vec+1, MAXARGS-1);
915 ** Merge any arguments from command line (now in vec)
916 ** and arguments from profile.
918 arguments = getarguments(sp, n, vec, 1);
929 vecp = 1; /* we'll get the zero'th element later */
930 vec[vecp++] = "-library";
931 vec[vecp++] = getcpy(toabsdir("+"));
933 while ((cp = *argp++)) {
935 switch (smatch(++cp, sendswitches)) {
937 ambigsw(cp, sendswitches);
940 advise(NULL, "-%s unknown\n", cp);
944 snprintf(buf, sizeof(buf),
945 "%s [switches]", sp);
946 print_help(buf, sendswitches, 1);
949 print_version(invo_name);
976 debugsw++; /* fall */
989 if (!(cp = *argp++) || *cp == '-') {
990 advise(NULL, "missing argument to %s",
999 advise(NULL, "usage: %s [switches]", sp);
1003 /* allow Aliasfile: profile entry */
1004 if ((cp = context_find("Aliasfile"))) {
1008 for (ap = brkstring(dp, " ", "\n"); ap && *ap; ap++) {
1009 vec[vecp++] = "-alias";
1010 vec[vecp++] = getcpy(etcpath(*ap));
1014 if (!(cp = getenv("SIGNATURE")) || !*cp)
1015 if ((cp = context_find("signature")) && *cp)
1016 m_putenv("SIGNATURE", cp);
1018 if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
1020 if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
1022 if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
1025 if ((cp = getenv("mhdist")) && *cp
1027 && (distsw = atoi (cp))
1028 #endif /* not lint */
1030 vec[vecp++] = "-dist";
1031 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
1032 if (link(altmsg, distfile) == NOTOK)
1033 adios(distfile, "unable to link %s to", altmsg);
1038 if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
1043 if ((pushsw = pushed))
1046 vec[0] = mhbasename(postproc);
1049 if (sendsbr(vec, vecp, file, &st, 1) == OK)
1055 ** Remove the draft file
1059 removefile(char *drft)
1061 if (unlink(drft) == NOTOK)
1062 adios(drft, "unable to unlink");