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 { "mime [<switches>]", 0 },
71 { "display [<switches>]", 0 },
73 { "list [<switches>]", 0 },
75 { "send [<switches>]", 0 },
77 { "push [<switches>]", 0 },
79 { "quit [-delete]", 0 },
83 { "cd [directory]", 0 },
88 #define ATTACHCMDSW 12
90 #define DETACHCMDSW 13
93 { "alist [-ln] ", 2 },
97 static char *myprompt = "\nWhat now? ";
102 static int editfile(char **, char **, char *, int, struct msgs *,
103 char *, char *, int);
104 static int sendfile(char **, char *, int);
105 static void sendit(char *, char **, char *, int);
106 static int buildfile(char **, char *);
107 static int check_draft(char *);
108 static int removefile(char *);
109 static void writelscmd(char *, int, char **);
110 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
111 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
112 static int system_in_dir(const char *dir, const char *cmd);
116 static int copyf(char *, char *);
121 WhatNow(int argc, char **argv)
123 int nedit = 0, use = 0;
125 char *ed = NULL, *drft = NULL, *msgnam = NULL;
126 char buf[BUFSIZ], prompt[BUFSIZ];
127 char **argp, **arguments;
129 char cwd[MAXPATHLEN + 1]; /* current working directory */
130 char file[MAXPATHLEN + 1]; /* file name buffer */
131 char shell[MAXPATHLEN + 1]; /* shell response buffer */
132 FILE *f; /* read pointer for bgnd proc */
133 char *l; /* set on -l to alist command */
134 int n; /* set on -n to alist command */
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 /* Translate MIME composition file */
233 buildfile(++argp, drft);
237 /* Call an editor on the draft file */
240 if (editfile(&ed, argp, drft, NOUSE, NULL,
241 msgnam, NULL, 1) == NOTOK)
246 /* display the draft file */
247 showfile(++argp, drft);
251 /* Quit, and possibly delete the draft */
252 if (*++argp && (*argp[0] == 'd' ||
253 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
256 if (stat(drft, &st) != NOTOK)
257 advise(NULL, "draft left on %s", drft);
262 /* Delete draft and exit */
267 /* Send draft in background */
268 if (sendfile(++argp, drft, 1))
274 sendfile(++argp, drft, 0);
278 /* Refile the draft */
279 if (refile(++argp, drft) == 0)
285 ** Change the working directory for attachments
287 ** Run the directory through the user's shell
288 ** so that we can take advantage of any syntax
289 ** that the user is accustomed to. Read back
290 ** the absolute path.
293 if (*(argp+1) == NULL) {
294 sprintf(buf, "$SHELL -c \"cd;pwd\"");
296 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
298 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
299 fgets(cwd, sizeof (cwd), f);
301 if (strchr(cwd, '\n') != NULL)
302 *strchr(cwd, '\n') = '\0';
306 advise("popen", "could not get directory");
312 /* Print the working directory for attachments */
318 ** List files in the current attachment working
321 ** Use the user's shell so that we can take
322 ** advantage of any syntax that the user is
325 writelscmd(buf, sizeof(buf), argp);
326 system_in_dir(cwd, buf);
331 ** List attachments on current draft. Options are:
333 ** -l long listing (full path names)
334 ** -n numbers listing
340 while (*++argp != NULL) {
341 if (strcmp(*argp, "-l") == 0)
344 else if (strcmp(*argp, "-n") == 0)
347 else if (strcmp(*argp, "-ln") == 0 ||
348 strcmp(*argp, "-nl") == 0) {
358 advise(NULL, "usage is alist [-ln].");
361 annolist(drft, attach_hdr, l, n);
367 ** Attach files to current draft.
370 if (*(argp+1) == NULL) {
371 advise(NULL, "attach command requires file argument(s).");
376 ** Build a command line that causes the user's
377 ** shell to list the file name arguments.
378 ** This handles and wildcard expansion, tilde
381 writelscmd(buf, sizeof(buf), argp);
384 ** Read back the response from the shell,
385 ** which contains a number of lines with one
386 ** file name per line. Remove off the newline.
387 ** Determine whether we have an absolute or
388 ** relative path name. Prepend the current
389 ** working directory to relative path names.
390 ** Add the attachment annotation to the draft.
393 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
394 while (fgets(shell, sizeof (shell), f)
396 *(strchr(shell, '\n')) = '\0';
399 annotate(drft, attach_hdr,
403 sprintf(file, "%s/%s", cwd,
405 annotate(drft, attach_hdr,
413 advise("popen", "could not get file from shell");
420 ** Detach files from current draft.
422 ** Scan the arguments for a -n. Mixed file
423 ** names and numbers aren't allowed, so this
424 ** catches a -n anywhere in the argument list.
427 for (n = 0, arguments = argp + 1;
430 if (strcmp(*arguments, "-n") == 0) {
437 ** A -n was found so interpret the arguments as
438 ** attachment numbers. Decrement any remaining
439 ** argument number that is greater than the one
440 ** just processed after processing each one so
441 ** that the numbering stays correct.
445 for (arguments = argp + 1;
448 if (strcmp(*arguments, "-n") == 0)
451 if (**arguments != '\0') {
452 n = atoi(*arguments);
453 annotate(drft, attach_hdr,
457 for (argp = arguments + 1; *argp != NULL; argp++) {
458 if (atoi(*argp) > n) {
459 if (atoi(*argp) == 1)
462 sprintf(*argp, "%d", atoi(*argp) - 1);
470 ** The arguments are interpreted as file names.
471 ** Run them through the user's shell for wildcard
472 ** expansion and other goodies. Do this from
473 ** the current working directory if the argument
474 ** is not an absolute path name (does not begin
477 ** We feed all the file names to the shell at
478 ** once, otherwise you can't provide a file name
479 ** with a space in it.
481 writelscmd(buf, sizeof(buf), argp);
482 if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
483 while (fgets(shell, sizeof (shell), f)
485 *(strchr(shell, '\n')) = '\0';
486 annotate(drft, attach_hdr, shell,
491 advise("popen", "could not get file from shell");
497 /* Unknown command */
498 advise(NULL, "say what?");
508 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
512 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
516 ** Note that we do not quote -- the argp from the user
517 ** is assumed to be quoted as they desire. (We can't treat
518 ** it as pure literal as that would prevent them using ~,
519 ** wildcards, etc.) The buffer produced by this function
520 ** should be given to popen_in_dir() or system_in_dir() so
521 ** that the current working directory is set correctly.
523 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
525 ** NB that some snprintf() return -1 on overflow rather than the
526 ** new C99 mandated 'number of chars that would have been written'
529 ** length checks here and inside the loop allow for the
530 ** trailing ';', trailcmd, '"' and NUL
532 int trailln = strlen(trailcmd) + 3;
533 if (ln < 0 || ln + trailln > bufsz)
534 adios(NULL, "arguments too long");
538 while (*++argp != NULL) {
540 /* +1 for leading space */
541 if (ln + trailln + 1 > bufsz - (cp-buf))
542 adios(NULL, "arguments too long");
544 memcpy(cp, *argp, ln+1);
549 strcpy(cp, trailcmd);
557 ** Build a command line that causes the user's shell to list the file name
558 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
561 writelscmd(char *buf, int bufsz, char **argp)
563 writesomecmd(buf, bufsz, "ls", "", argp);
567 ** Like system(), but run the command in directory dir.
568 ** This assumes the program is single-threaded!
571 system_in_dir(const char *dir, const char *cmd)
575 if (getcwd(olddir, sizeof(olddir)) == 0)
576 adios("getcwd", "could not get working directory");
578 adios("chdir", "could not change working directory");
580 if (chdir(olddir) != 0)
581 adios("chdir", "could not change working directory");
585 /* ditto for popen() */
587 popen_in_dir(const char *dir, const char *cmd, const char *type)
591 if (getcwd(olddir, sizeof(olddir)) == 0)
592 adios("getcwd", "could not get working directory");
594 adios("chdir", "could not change working directory");
595 f = popen(cmd, type);
596 if (chdir(olddir) != 0)
597 adios("chdir", "could not change working directory");
606 static int reedit = 0; /* have we been here before? */
607 static char *edsave = NULL; /* the editor we used previously */
611 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
612 char *altmsg, char *cwd, int save_editor)
614 int pid, status, vecp;
615 char altpath[BUFSIZ], linkpath[BUFSIZ];
616 char *cp, *vec[MAXARGS];
622 int oumask; /* PJS: for setting permissions on symlinks. */
624 #endif /* HAVE_LSTAT */
626 /* Was there a previous edit session? */
628 if (!*ed) { /* no explicit editor */
629 *ed = edsave; /* so use the previous one */
630 if ((cp = mhbasename(*ed)) == NULL)
633 /* unless we've specified it via "editor-next" */
634 cp = concat(cp, "-next", NULL);
635 if ((cp = context_find(cp)) != NULL)
639 /* set initial editor */
640 if (*ed == NULL && (*ed = context_find("editor")) == NULL)
645 if (mp == NULL || *altmsg == '/' || cwd == NULL)
646 strncpy(altpath, altmsg, sizeof(altpath));
648 snprintf(altpath, sizeof(altpath), "%s/%s",
649 mp->foldpath, altmsg);
651 strncpy(linkpath, altmsglink, sizeof(linkpath));
653 snprintf(linkpath, sizeof(linkpath), "%s/%s",
660 if (link(altpath, linkpath) == NOTOK) {
662 /* I don't think permission on symlinks matters /JLR */
663 /* PJS: else symlinks are world 'r' */
664 oumask = umask(0044);
666 symlink(altpath, linkpath);
668 umask(oumask); /* PJS: else symlinks are world 'r' */
674 #else /* not HAVE_LSTAT */
675 link(altpath, linkpath);
676 #endif /* not HAVE_LSTAT */
679 context_save(); /* save the context file */
682 switch (pid = fork()) {
684 advise("fork", "unable to");
693 m_putenv("mhfolder", mp->foldpath);
694 m_putenv("editalt", altpath);
698 vec[vecp++] = mhbasename(*ed);
701 vec[vecp++] = *arg++;
706 fprintf(stderr, "unable to exec ");
711 if ((status = pidwait(pid, NOTOK))) {
713 if ((cp = mhbasename(*ed)) && strcmp(cp, "vi") == 0 &&
714 (status & 0x00ff) == 0)
718 if (((status & 0xff00) != 0xff00)
719 && (!reedit || (status & 0x00ff))) {
720 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
721 advise(NULL, "problems with edit--draft left in %s", cp);
723 advise(NULL, "problems with edit--%s preserved", file);
726 status = -2; /* maybe "reedit ? -2 : -1"? */
735 if (altmsg && mp && !is_readonly(mp) && (slinked ?
736 lstat (linkpath, &st) != NOTOK &&
737 S_ISREG(st.st_mode) &&
738 copyf(linkpath, altpath) == NOTOK :
739 stat(linkpath, &st) != NOTOK &&
741 (unlink(altpath) == NOTOK ||
742 link(linkpath, altpath) == NOTOK)))
743 advise(linkpath, "unable to update %s from", altmsg);
744 #else /* HAVE_LSTAT */
745 if (altmsg && mp && !is_readonly(mp) &&
746 stat(linkpath, &st) != NOTOK &&
748 (unlink(altpath) == NOTOK ||
749 link(linkpath, altpath) == NOTOK))
750 advise(linkpath, "unable to update %s from", altmsg);
751 #endif /* HAVE_LSTAT */
754 /* normally, we remember which editor we used */
756 edsave = getcpy(*ed);
768 copyf(char *ifile, char *ofile)
773 if ((in = open(ifile, O_RDONLY)) == NOTOK)
775 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
776 admonish(ofile, "unable to open and truncate");
781 while ((i = read(in, buffer, sizeof(buffer))) > OK)
782 if (write(out, buffer, i) != i) {
783 advise(ofile, "may have damaged");
792 #endif /* HAVE_LSTAT */
800 sendfile(char **arg, char *file, int pushsw)
804 char *cp, *sp, *vec[MAXARGS];
806 /* Translate MIME composition file, if necessary */
807 if ((cp = context_find("automimeproc")) && (strcmp(cp, "1")==0) &&
808 !getenv("NOMHNPROC") && check_draft(file) &&
809 (buildfile(NULL, file) == NOTOK))
812 /* For backwards compatibility */
813 if ((cp = context_find("automhnproc")) && !getenv("NOMHNPROC")
814 && check_draft(file) && (i = editfile(&cp, NULL,
815 file, NOUSE, NULL, NULL, NULL, 0)))
819 ** If the sendproc is the nmh command `send', then we call
820 ** those routines directly rather than exec'ing the command.
822 if (strcmp(sp = mhbasename(sendproc), "send") == 0) {
824 sendit(invo_name = sp, arg, file, pushsw);
829 context_save(); /* save the context file */
832 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
836 advise(NULL, "unable to fork, so sending directly...");
839 vec[vecp++] = invo_name;
841 vec[vecp++] = "-push";
844 vec[vecp++] = *arg++;
848 execvp(sendproc, vec);
849 fprintf(stderr, "unable to exec ");
854 if (pidwait(child_id, OK) == 0)
862 ** Translate MIME composition file (call buildmimeproc)
866 buildfile(char **argp, char *file)
873 /* allocate space for arguments */
879 args = (char **) mh_xmalloc((i + 2) * sizeof(char *));
882 ** For backward compatibility, we need to add -build
883 ** if we are using mhn as buildmimeproc
886 if (strcmp(mhbasename(ed), "mhn") == 0)
887 args[i++] = "-build";
889 /* copy any other arguments */
890 while (argp && *argp)
894 i = editfile(&ed, args, file, NOUSE, NULL, NULL, NULL, 0);
897 return (i ? NOTOK : OK);
902 ** Check if draft is a mhbuild composition file
906 check_draft(char *msgnam)
909 char buf[BUFSIZ], name[NAMESZ];
912 if ((fp = fopen(msgnam, "r")) == NULL)
915 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
920 ** If draft already contains any of the
921 ** Content-XXX fields, then assume it already
924 if (uprf(name, XXX_FIELD_PRF)) {
928 while (state == FLDPLUS)
929 state = m_getfld(state, name, buf,
937 for (bp = buf; *bp; bp++)
938 if (*bp != ' ' && *bp != '\t' &&
944 state = m_getfld(state, name, buf,
946 } while (state == BODY);
956 static struct swit sendswitches[] = {
958 { "alias aliasfile", 0 },
962 { "filter filterfile", 0 },
998 { "width columns", 0 },
999 #define SVERSIONSW 21
1003 #define BITSTUFFSW 23
1004 { "dashstuffing", -12 },
1005 #define NBITSTUFFSW 24
1006 { "nodashstuffing", -14 },
1016 { "client host", -6 },
1018 { "server host", 6 },
1022 { "port server-port-name/number", 4 },
1027 extern int debugsw; /* from sendsbr.c */
1034 extern char *altmsg; /* .. */
1035 extern char *annotext;
1036 extern char *distfile;
1040 sendit(char *sp, char **arg, char *file, int pushed)
1043 char *cp, buf[BUFSIZ], **argp;
1044 char **arguments, *vec[MAXARGS];
1055 ** Make sure these are defined. In particular, we need
1056 ** vec[1] to be NULL, in case "arg" is NULL below. It
1057 ** doesn't matter what is the value of vec[0], but we
1058 ** set it to NULL, to help catch "off-by-one" errors.
1064 ** Temporarily copy arg to vec, since the brkstring() call in
1065 ** getarguments() will wipe it out before it is merged in.
1066 ** Also, we skip the first element of vec, since getarguments()
1067 ** skips it. Then we count the number of arguments
1068 ** copied. The value of "n" will be one greater than
1069 ** this in order to simulate the standard argc/argv.
1074 copyip(arg, vec+1, MAXARGS-1);
1081 ** Merge any arguments from command line (now in vec)
1082 ** and arguments from profile.
1084 arguments = getarguments (sp, n, vec, 1);
1096 vecp = 1; /* we'll get the zero'th element later */
1097 vec[vecp++] = "-library";
1098 vec[vecp++] = getcpy(toabsdir("+"));
1100 while ((cp = *argp++)) {
1102 switch (smatch(++cp, sendswitches)) {
1104 ambigsw(cp, sendswitches);
1107 advise(NULL, "-%s unknown\n", cp);
1111 snprintf(buf, sizeof(buf),
1112 "%s [switches]", sp);
1113 print_help(buf, sendswitches, 1);
1116 print_version(invo_name);
1149 debugsw++; /* fall */
1176 if (!(cp = *argp++) || *cp == '-') {
1177 advise(NULL, "missing argument to %s",
1186 advise(NULL, "usage: %s [switches]", sp);
1190 /* allow Aliasfile: profile entry */
1191 if ((cp = context_find("Aliasfile"))) {
1195 for (ap = brkstring(dp, " ", "\n"); ap && *ap; ap++) {
1196 vec[vecp++] = "-alias";
1201 if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
1202 if ((cp = context_find("signature")) && *cp)
1203 m_putenv("SIGNATURE", cp);
1206 snprintf(buf, sizeof(buf), "%s/.signature", mypath);
1207 if ((fp = fopen(buf, "r")) != NULL &&
1208 fgets(buf, sizeof(buf), fp) != NULL) {
1210 if (cp = strchr(buf, '\n'))
1212 m_putenv("SIGNATURE", buf);
1217 if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
1219 if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
1221 if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
1224 if ((cp = getenv("mhdist")) && *cp
1226 && (distsw = atoi (cp))
1227 #endif /* not lint */
1229 vec[vecp++] = "-dist";
1230 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
1231 if (link(altmsg, distfile) == NOTOK)
1232 adios(distfile, "unable to link %s to", altmsg);
1237 if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
1242 if ((pushsw = pushed))
1245 vec[0] = mhbasename(postproc);
1248 if (sendsbr(vec, vecp, file, &st, 1) == OK)
1254 ** Remove the draft file
1258 removefile(char *drft)
1260 if (unlink(drft) == NOTOK)
1261 adios(drft, "unable to unlink");