2 ** whatnowsbr.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 WhatNow(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 */
132 invo_name = mhbasename(argv[0]);
134 /* read user profile/context */
137 arguments = getarguments(invo_name, argc, argv, 1);
141 ** Get the initial current working directory.
144 if (getcwd(cwd, sizeof (cwd)) == NULL) {
145 adios("getcwd", "could not get working directory");
148 while ((cp = *argp++)) {
150 switch (smatch(++cp, whatnowswitches)) {
152 ambigsw(cp, whatnowswitches);
155 adios(NULL, "-%s unknown", cp);
158 snprintf(buf, sizeof(buf),
159 "%s [switches] [file]",
161 print_help(buf, whatnowswitches, 1);
164 print_version(invo_name);
168 if (!(ed = *argp++) || *ed == '-')
169 adios(NULL, "missing argument to %s",
178 if (!(myprompt = *argp++) || *myprompt == '-')
179 adios(NULL, "missing argument to %s",
186 adios(NULL, "only one draft at a time!");
191 if ((drft == NULL && (drft = getenv("mhdraft")) == NULL) || *drft == 0)
192 drft = getcpy(m_draft(seq_cur));
194 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
196 if ((cp = getenv("mhuse")) && *cp)
199 if (ed == NULL && ((ed = getenv("mheditor")) == NULL || *ed == 0)) {
204 /* start editing the draft, unless -noedit was given */
205 if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
209 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
211 if (!(argp = getans(prompt, aleqs))) {
215 switch (smatch(*argp, aleqs)) {
218 ** display the message being replied to,
222 showfile(++argp, msgnam);
224 advise(NULL, "no alternate message to display");
228 /* Call an editor on the draft file */
231 if (editfile(&ed, argp, drft, NOUSE, NULL,
232 msgnam, NULL, 1) == NOTOK)
237 /* display the draft file */
238 showfile(++argp, drft);
242 /* Quit, and possibly delete the draft */
243 if (*++argp && (*argp[0] == 'd' ||
244 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
247 if (stat(drft, &st) != NOTOK)
248 advise(NULL, "draft left on %s", drft);
253 /* Delete draft and exit */
258 /* Send draft in background */
259 if (sendfile(++argp, drft, 1))
265 sendfile(++argp, drft, 0);
269 /* Refile the draft */
270 if (refile(++argp, drft) == 0)
276 ** Change the working directory for attachments
278 ** Run the directory through the user's shell
279 ** so that we can take advantage of any syntax
280 ** that the user is accustomed to. Read back
281 ** the absolute path.
284 if (*(argp+1) == NULL) {
285 sprintf(buf, "$SHELL -c \"cd;pwd\"");
287 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
289 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
290 fgets(cwd, sizeof (cwd), f);
292 if (strchr(cwd, '\n') != NULL)
293 *strchr(cwd, '\n') = '\0';
297 advise("popen", "could not get directory");
303 /* Print the working directory for attachments */
309 ** List files in the current attachment working
312 ** Use the user's shell so that we can take
313 ** advantage of any syntax that the user is
316 writelscmd(buf, sizeof(buf), argp);
317 system_in_dir(cwd, buf);
322 ** List attachments on current draft. Options are:
324 ** -l long listing (full path names)
325 ** -n numbers listing
331 while (*++argp != NULL) {
332 if (strcmp(*argp, "-l") == 0)
335 else if (strcmp(*argp, "-n") == 0)
338 else if (strcmp(*argp, "-ln") == 0 ||
339 strcmp(*argp, "-nl") == 0) {
349 advise(NULL, "usage is alist [-ln].");
352 annolist(drft, attach_hdr, l, n);
358 ** Attach files to current draft.
361 if (*(argp+1) == NULL) {
362 advise(NULL, "attach command requires file argument(s).");
367 ** Build a command line that causes the user's
368 ** shell to list the file name arguments.
369 ** This handles and wildcard expansion, tilde
372 writelscmd(buf, sizeof(buf), argp);
375 ** Read back the response from the shell,
376 ** which contains a number of lines with one
377 ** file name per line. Remove off the newline.
378 ** Determine whether we have an absolute or
379 ** relative path name. Prepend the current
380 ** working directory to relative path names.
381 ** Add the attachment annotation to the draft.
383 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
384 while (fgets(shell, sizeof (shell), f)
386 *(strchr(shell, '\n')) = '\0';
389 annotate(drft, attach_hdr,
393 sprintf(file, "%s/%s", cwd,
395 annotate(drft, attach_hdr,
403 advise("popen", "could not get file from shell");
410 ** Detach files from current draft.
412 ** Scan the arguments for a -n. Mixed file
413 ** names and numbers aren't allowed, so this
414 ** catches a -n anywhere in the argument list.
417 for (n = 0, arguments = argp + 1;
420 if (strcmp(*arguments, "-n") == 0) {
427 ** A -n was found so interpret the arguments as
428 ** attachment numbers. Decrement any remaining
429 ** argument number that is greater than the one
430 ** just processed after processing each one so
431 ** that the numbering stays correct.
435 for (arguments = argp + 1;
438 if (strcmp(*arguments, "-n") == 0)
441 if (**arguments != '\0') {
442 n = atoi(*arguments);
443 annotate(drft, attach_hdr,
447 for (argp = arguments + 1; *argp != NULL; argp++) {
448 if (atoi(*argp) > n) {
449 if (atoi(*argp) == 1)
452 sprintf(*argp, "%d", atoi(*argp) - 1);
460 ** The arguments are interpreted as file names.
461 ** Run them through the user's shell for wildcard
462 ** expansion and other goodies. Do this from
463 ** the current working directory if the argument
464 ** is not an absolute path name (does not begin
467 ** We feed all the file names to the shell at
468 ** once, otherwise you can't provide a file name
469 ** with a space in it.
471 writelscmd(buf, sizeof(buf), argp);
472 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
473 while (fgets(shell, sizeof (shell), f)
475 *(strchr(shell, '\n')) = '\0';
476 annotate(drft, attach_hdr, shell,
481 advise("popen", "could not get file from shell");
487 /* Unknown command */
488 advise(NULL, "say what?");
498 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
502 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
506 ** Note that we do not quote -- the argp from the user
507 ** is assumed to be quoted as they desire. (We can't treat
508 ** it as pure literal as that would prevent them using ~,
509 ** wildcards, etc.) The buffer produced by this function
510 ** should be given to popen_in_dir() or system_in_dir() so
511 ** that the current working directory is set correctly.
513 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
515 ** NB that some snprintf() return -1 on overflow rather than the
516 ** new C99 mandated 'number of chars that would have been written'
519 ** length checks here and inside the loop allow for the
520 ** trailing ';', trailcmd, '"' and NUL
522 int trailln = strlen(trailcmd) + 3;
523 if (ln < 0 || ln + trailln > bufsz)
524 adios(NULL, "arguments too long");
528 while (*++argp != NULL) {
530 /* +1 for leading space */
531 if (ln + trailln + 1 > bufsz - (cp-buf))
532 adios(NULL, "arguments too long");
534 memcpy(cp, *argp, ln+1);
539 strcpy(cp, trailcmd);
547 ** Build a command line that causes the user's shell to list the file name
548 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
551 writelscmd(char *buf, int bufsz, char **argp)
553 writesomecmd(buf, bufsz, "ls", "", argp);
557 ** Like system(), but run the command in directory dir.
558 ** This assumes the program is single-threaded!
561 system_in_dir(const char *dir, const char *cmd)
565 if (getcwd(olddir, sizeof(olddir)) == 0)
566 adios("getcwd", "could not get working directory");
568 adios("chdir", "could not change working directory");
570 if (chdir(olddir) != 0)
571 adios("chdir", "could not change working directory");
575 /* ditto for popen() */
577 popen_in_dir(const char *dir, const char *cmd, const char *type)
581 if (getcwd(olddir, sizeof(olddir)) == 0)
582 adios("getcwd", "could not get working directory");
584 adios("chdir", "could not change working directory");
585 f = popen(cmd, type);
586 if (chdir(olddir) != 0)
587 adios("chdir", "could not change working directory");
596 static int reedit = 0; /* have we been here before? */
597 static char *edsave = NULL; /* the editor we used previously */
601 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
602 char *altmsg, char *cwd, int save_editor)
604 int pid, status, vecp;
605 char altpath[BUFSIZ], linkpath[BUFSIZ];
606 char *cp, *vec[MAXARGS];
612 int oumask; /* PJS: for setting permissions on symlinks. */
614 #endif /* HAVE_LSTAT */
616 /* Was there a previous edit session? */
618 if (!*ed) { /* no explicit editor */
619 *ed = edsave; /* so use the previous one */
620 if ((cp = mhbasename(*ed)) == NULL)
623 /* unless we've specified it via "editor-next" */
624 cp = concat(cp, "-next", NULL);
625 if ((cp = context_find(cp)) != NULL)
629 /* set initial editor */
630 if (*ed == NULL && (*ed = context_find("editor")) == NULL)
635 if (mp == NULL || *altmsg == '/' || cwd == NULL)
636 strncpy(altpath, altmsg, sizeof(altpath));
638 snprintf(altpath, sizeof(altpath), "%s/%s",
639 mp->foldpath, altmsg);
641 strncpy(linkpath, altmsglink, sizeof(linkpath));
643 snprintf(linkpath, sizeof(linkpath), "%s/%s",
650 if (link(altpath, linkpath) == NOTOK) {
652 /* I don't think permission on symlinks matters /JLR */
653 /* PJS: else symlinks are world 'r' */
654 oumask = umask(0044);
656 symlink(altpath, linkpath);
658 umask(oumask); /* PJS: else symlinks are world 'r' */
664 #else /* not HAVE_LSTAT */
665 link(altpath, linkpath);
666 #endif /* not HAVE_LSTAT */
669 context_save(); /* save the context file */
672 switch (pid = fork()) {
674 advise("fork", "unable to");
683 m_putenv("mhfolder", mp->foldpath);
684 m_putenv("editalt", altpath);
688 vec[vecp++] = mhbasename(*ed);
691 vec[vecp++] = *arg++;
696 fprintf(stderr, "unable to exec ");
701 if ((status = pidwait(pid, NOTOK))) {
703 if ((cp = mhbasename(*ed)) && strcmp(cp, "vi") == 0 &&
704 (status & 0x00ff) == 0)
708 if (((status & 0xff00) != 0xff00)
709 && (!reedit || (status & 0x00ff))) {
710 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
711 advise(NULL, "problems with edit--draft left in %s", cp);
713 advise(NULL, "problems with edit--%s preserved", file);
716 status = -2; /* maybe "reedit ? -2 : -1"? */
725 if (altmsg && mp && !is_readonly(mp) && (slinked ?
726 lstat (linkpath, &st) != NOTOK &&
727 S_ISREG(st.st_mode) &&
728 copyf(linkpath, altpath) == NOTOK :
729 stat(linkpath, &st) != NOTOK &&
731 (unlink(altpath) == NOTOK ||
732 link(linkpath, altpath) == NOTOK)))
733 advise(linkpath, "unable to update %s from", altmsg);
734 #else /* HAVE_LSTAT */
735 if (altmsg && mp && !is_readonly(mp) &&
736 stat(linkpath, &st) != NOTOK &&
738 (unlink(altpath) == NOTOK ||
739 link(linkpath, altpath) == NOTOK))
740 advise(linkpath, "unable to update %s from", altmsg);
741 #endif /* HAVE_LSTAT */
744 /* normally, we remember which editor we used */
746 edsave = getcpy(*ed);
758 copyf(char *ifile, char *ofile)
763 if ((in = open(ifile, O_RDONLY)) == NOTOK)
765 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
766 admonish(ofile, "unable to open and truncate");
771 while ((i = read(in, buffer, sizeof(buffer))) > OK)
772 if (write(out, buffer, i) != i) {
773 advise(ofile, "may have damaged");
782 #endif /* HAVE_LSTAT */
790 sendfile(char **arg, char *file, int pushsw)
794 char *cp, *sp, *vec[MAXARGS];
797 ** If the sendproc is the nmh command `send', then we call
798 ** those routines directly rather than exec'ing the command.
800 if (strcmp(sp = mhbasename(sendproc), "send") == 0) {
802 sendit(invo_name = sp, arg, file, pushsw);
807 context_save(); /* save the context file */
810 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
814 advise(NULL, "unable to fork, so sending directly...");
817 vec[vecp++] = invo_name;
819 vec[vecp++] = "-push";
822 vec[vecp++] = *arg++;
826 execvp(sendproc, vec);
827 fprintf(stderr, "unable to exec ");
832 if (pidwait(child_id, OK) == 0)
839 static struct swit sendswitches[] = {
841 { "alias aliasfile", 0 },
845 { "filter filterfile", 0 },
881 { "width columns", 0 },
882 #define SVERSIONSW 21
886 #define BITSTUFFSW 23
887 { "dashstuffing", -12 },
888 #define NBITSTUFFSW 24
889 { "nodashstuffing", -14 },
899 { "client host", -6 },
901 { "server host", 6 },
905 { "port server-port-name/number", 4 },
910 extern int debugsw; /* from sendsbr.c */
917 extern char *altmsg; /* .. */
918 extern char *annotext;
919 extern char *distfile;
923 sendit(char *sp, char **arg, char *file, int pushed)
926 char *cp, buf[BUFSIZ], **argp;
927 char **arguments, *vec[MAXARGS];
938 ** Make sure these are defined. In particular, we need
939 ** vec[1] to be NULL, in case "arg" is NULL below. It
940 ** doesn't matter what is the value of vec[0], but we
941 ** set it to NULL, to help catch "off-by-one" errors.
947 ** Temporarily copy arg to vec, since the brkstring() call in
948 ** getarguments() will wipe it out before it is merged in.
949 ** Also, we skip the first element of vec, since getarguments()
950 ** skips it. Then we count the number of arguments
951 ** copied. The value of "n" will be one greater than
952 ** this in order to simulate the standard argc/argv.
957 copyip(arg, vec+1, MAXARGS-1);
964 ** Merge any arguments from command line (now in vec)
965 ** and arguments from profile.
967 arguments = getarguments (sp, n, vec, 1);
979 vecp = 1; /* we'll get the zero'th element later */
980 vec[vecp++] = "-library";
981 vec[vecp++] = getcpy(toabsdir("+"));
983 while ((cp = *argp++)) {
985 switch (smatch(++cp, sendswitches)) {
987 ambigsw(cp, sendswitches);
990 advise(NULL, "-%s unknown\n", cp);
994 snprintf(buf, sizeof(buf),
995 "%s [switches]", sp);
996 print_help(buf, sendswitches, 1);
999 print_version(invo_name);
1032 debugsw++; /* fall */
1059 if (!(cp = *argp++) || *cp == '-') {
1060 advise(NULL, "missing argument to %s",
1069 advise(NULL, "usage: %s [switches]", sp);
1073 /* allow Aliasfile: profile entry */
1074 if ((cp = context_find("Aliasfile"))) {
1078 for (ap = brkstring(dp, " ", "\n"); ap && *ap; ap++) {
1079 vec[vecp++] = "-alias";
1084 if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
1085 if ((cp = context_find("signature")) && *cp)
1086 m_putenv("SIGNATURE", cp);
1089 snprintf(buf, sizeof(buf), "%s/.signature", mypath);
1090 if ((fp = fopen(buf, "r")) != NULL &&
1091 fgets(buf, sizeof(buf), fp) != NULL) {
1093 if (cp = strchr(buf, '\n'))
1095 m_putenv("SIGNATURE", buf);
1100 if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
1102 if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
1104 if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
1107 if ((cp = getenv("mhdist")) && *cp
1109 && (distsw = atoi (cp))
1110 #endif /* not lint */
1112 vec[vecp++] = "-dist";
1113 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
1114 if (link(altmsg, distfile) == NOTOK)
1115 adios(distfile, "unable to link %s to", altmsg);
1120 if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
1125 if ((pushsw = pushed))
1128 vec[0] = mhbasename(postproc);
1131 if (sendsbr(vec, vecp, file, &st, 1) == OK)
1137 ** Remove the draft file
1141 removefile(char *drft)
1143 if (unlink(drft) == NOTOK)
1144 adios(drft, "unable to unlink");