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 This option lists the attachments on the
34 ** detach files This option removes attachments from the
35 ** detach -n numbers draft. This can be done by file name or
36 ** by attachment number.
45 static struct swit whatnowswitches[] = {
47 { "editor editor", 0 },
49 { "prompt string", 4 },
58 ** Options at the "whatnow" prompt
60 static struct swit aleqs[] = {
62 { "edit [<editor> <switches>]", 0 },
64 { "refile [<switches>] +folder", 0 },
70 { "send [<switches>]", 0 },
76 { "cd [directory]", 0 },
81 #define ATTACHCMDSW 10
83 #define DETACHCMDSW 11
90 static char *myprompt = "\nWhat now? ";
95 static int editfile(char **, char **, char *, int, struct msgs *,
97 static int sendfile(char **, char *);
98 static int refile(char **, char *);
99 static int removefile(char *);
100 static void writelscmd(char *, int, char **);
101 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
102 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
103 static int system_in_dir(const char *dir, const char *cmd);
107 static int copyf(char *, char *);
112 main(int argc, char **argv)
116 char *ed = NULL, *drft = NULL, *msgnam = NULL;
117 char buf[BUFSIZ], prompt[BUFSIZ];
118 char **argp, **arguments;
120 char cwd[MAXPATHLEN + 1]; /* current working directory */
121 char file[MAXPATHLEN + 1]; /* file name buffer */
122 char shell[MAXPATHLEN + 1]; /* shell response buffer */
123 FILE *f; /* read pointer for bgnd proc */
124 int n; /* set on -n to detach command */
127 setlocale(LC_ALL, "");
130 invo_name = mhbasename(argv[0]);
132 /* read user profile/context */
135 arguments = getarguments(invo_name, argc, argv, 1);
139 ** Get the initial current working directory.
142 if (!getcwd(cwd, sizeof (cwd))) {
143 adios("getcwd", "could not get working directory");
146 while ((cp = *argp++)) {
148 switch (smatch(++cp, whatnowswitches)) {
150 ambigsw(cp, whatnowswitches);
153 adios(NULL, "-%s unknown", cp);
156 snprintf(buf, sizeof(buf),
157 "%s [switches] [file]",
159 print_help(buf, whatnowswitches, 1);
162 print_version(invo_name);
166 if (!(ed = *argp++) || *ed == '-')
167 adios(NULL, "missing argument to %s",
172 if (!(myprompt = *argp++) || *myprompt == '-')
173 adios(NULL, "missing argument to %s",
180 adios(NULL, "only one draft at a time!");
185 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
186 drft = getcpy(m_draft(seq_cur));
188 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
190 if ((cp = getenv("mhuse")) && *cp)
193 if (!ed && !(ed = getenv("mheditor"))) {
194 ed = ""; /* Don't initially edit the draft */
197 /* start editing the draft, unless editor is the empty string */
199 if (editfile(&ed, NULL, drft, use, NULL, msgnam, NULL) <0) {
203 advise(NULL, "Try again.");
208 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
210 if (!(argp = getans(prompt, aleqs))) {
214 switch (smatch(*argp, aleqs)) {
216 /* display the msg being replied to or distributed */
218 snprintf(buf, sizeof buf, "%s '%s'",
222 advise(NULL, "no alternate message to display");
227 /* Call an editor on the draft file */
230 editfile(&ed, argp, drft, NOUSE, NULL, msgnam, NULL);
234 /* display the draft file */
235 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
241 if (stat(drft, &st) != NOTOK) {
242 advise(NULL, "draft left on %s", drft);
247 /* Delete draft and exit */
253 sendfile(++argp, drft);
257 /* Refile the draft */
258 if (refile(++argp, drft) == 0) {
265 ** Change the working directory for attachments
267 ** Run the directory through the user's shell
268 ** so that we can take advantage of any syntax
269 ** that the user is accustomed to. Read back
270 ** the absolute path.
273 if (*(argp+1) == NULL) {
274 sprintf(buf, "$SHELL -c \"cd;pwd\"");
276 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
278 if ((f = popen_in_dir(cwd, buf, "r"))) {
279 fgets(cwd, sizeof (cwd), f);
281 if (strchr(cwd, '\n'))
282 *strchr(cwd, '\n') = '\0';
286 advise("popen", "could not get directory");
292 /* Print the working directory for attachments */
298 ** List files in the current attachment working
301 ** Use the user's shell so that we can take
302 ** advantage of any syntax that the user is
305 writelscmd(buf, sizeof(buf), argp);
306 system_in_dir(cwd, buf);
311 ** List attachments on current draft.
313 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
314 "-number all -text IGNORE '%s'",
316 if (system(buf) != 0) {
317 advise(NULL, "Could not list attachment headers.");
323 ** Attach files to current draft.
326 if (*(argp+1) == NULL) {
327 advise(NULL, "attach command requires file argument(s).");
332 ** Build a command line that causes the user's
333 ** shell to list the file name arguments.
334 ** This handles and wildcard expansion, tilde
337 writelscmd(buf, sizeof(buf), argp);
340 ** Read back the response from the shell,
341 ** which contains a number of lines with one
342 ** file name per line. Remove off the newline.
343 ** Determine whether we have an absolute or
344 ** relative path name. Prepend the current
345 ** working directory to relative path names.
346 ** Add the attachment annotation to the draft.
348 if ((f = popen_in_dir(cwd, buf, "r"))) {
351 while (fgets(shell, sizeof(shell), f)) {
352 *(strchr(shell, '\n')) = '\0';
355 sprintf(file, "%s", shell);
357 sprintf(file, "%s/%s", cwd,
360 snprintf(buf, sizeof buf,
361 "anno -nodate -append "
362 "-comp '%s' -text '%s'"
366 if (system(buf) != 0) {
367 advise(NULL, "Could not add attachment header.");
373 advise("popen", "could not get file from shell");
380 ** Detach files from current draft.
382 ** Scan the arguments for a -n. Mixed file
383 ** names and numbers aren't allowed, so this
384 ** catches a -n anywhere in the argument list.
386 for (n = 0, arguments = argp + 1;
389 if (strcmp(*arguments, "-n") == 0) {
396 ** A -n was found so interpret the arguments as
397 ** attachment numbers. Decrement any remaining
398 ** argument number that is greater than the one
399 ** just processed after processing each one so
400 ** that the numbering stays correct.
403 for (arguments=argp+1; *arguments;
405 if (strcmp(*arguments, "-n") == 0)
408 if (**arguments != '\0') {
411 n = atoi(*arguments);
412 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
413 if (system(buf) != 0) {
414 advise(NULL, "Could not delete attachment header.");
417 for (argp=arguments+1; *argp;
419 if (atoi(*argp) > n) {
420 if (atoi(*argp) == 1)
423 sprintf(*argp, "%d", atoi(*argp) - 1);
433 ** The arguments are interpreted as file names.
434 ** Run them through the user's shell for wildcard
435 ** expansion and other goodies. Do this from
436 ** the current working directory if the argument
437 ** is not an absolute path name (does not begin
440 ** We feed all the file names to the shell at
441 ** once, otherwise you can't provide a file name
442 ** with a space in it.
444 writelscmd(buf, sizeof(buf), argp);
445 if ((f = popen_in_dir(cwd, buf, "r"))) {
446 while (fgets(shell, sizeof (shell), f)) {
447 *(strchr(shell, '\n')) = '\0';
448 snprintf(buf, sizeof buf,
449 "anno -delete -comp "
450 "'%s' -text '%s' '%s'",
453 if (system(buf) != 0) {
454 advise(NULL, "Could not delete attachment header.");
459 advise("popen", "could not get file from shell");
465 /* Unknown command */
466 advise(NULL, "say what?");
476 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
480 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
484 ** Note that we do not quote -- the argp from the user
485 ** is assumed to be quoted as they desire. (We can't treat
486 ** it as pure literal as that would prevent them using ~,
487 ** wildcards, etc.) The buffer produced by this function
488 ** should be given to popen_in_dir() or system_in_dir() so
489 ** that the current working directory is set correctly.
491 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
493 ** NB that some snprintf() return -1 on overflow rather than the
494 ** new C99 mandated 'number of chars that would have been written'
497 ** length checks here and inside the loop allow for the
498 ** trailing ';', trailcmd, '"' and NUL
500 int trailln = strlen(trailcmd) + 3;
501 if (ln < 0 || ln + trailln > bufsz)
502 adios(NULL, "arguments too long");
508 /* +1 for leading space */
509 if (ln + trailln + 1 > bufsz - (cp-buf))
510 adios(NULL, "arguments too long");
512 memcpy(cp, *argp, ln+1);
517 strcpy(cp, trailcmd);
525 ** Build a command line that causes the user's shell to list the file name
526 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
529 writelscmd(char *buf, int bufsz, char **argp)
531 writesomecmd(buf, bufsz, "ls", "", argp);
535 ** Like system(), but run the command in directory dir.
536 ** This assumes the program is single-threaded!
539 system_in_dir(const char *dir, const char *cmd)
543 if (getcwd(olddir, sizeof(olddir)) == 0)
544 adios("getcwd", "could not get working directory");
546 adios("chdir", "could not change working directory");
548 if (chdir(olddir) != 0)
549 adios("chdir", "could not change working directory");
553 /* ditto for popen() */
555 popen_in_dir(const char *dir, const char *cmd, const char *type)
559 if (getcwd(olddir, sizeof(olddir)) == 0)
560 adios("getcwd", "could not get working directory");
562 adios("chdir", "could not change working directory");
563 f = popen(cmd, type);
564 if (chdir(olddir) != 0)
565 adios("chdir", "could not change working directory");
574 static char *edsave = NULL; /* the editor we used previously */
578 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
579 char *altmsg, char *cwd)
581 int pid, status, vecp;
582 char altpath[BUFSIZ], linkpath[BUFSIZ];
583 char *cp, *vec[MAXARGS];
588 #endif /* HAVE_LSTAT */
591 /* We have no explicit editor. */
593 /* Use the previous editor ... */
595 if (!(cp = mhbasename(*ed)))
598 /* but prefer one specified via "editor-next" */
599 cp = concat(cp, "-next", NULL);
600 if ((cp = context_find(cp)))
603 /* set initial editor */
609 if (!mp || *altmsg == '/' || !cwd)
610 strncpy(altpath, altmsg, sizeof(altpath));
612 snprintf(altpath, sizeof(altpath), "%s/%s",
613 mp->foldpath, altmsg);
615 strncpy(linkpath, altmsglink, sizeof(linkpath));
617 snprintf(linkpath, sizeof(linkpath), "%s/%s",
621 if (link(altpath, linkpath) == NOTOK) {
622 symlink(altpath, linkpath);
627 #else /* not HAVE_LSTAT */
628 link(altpath, linkpath);
629 #endif /* not HAVE_LSTAT */
632 context_save(); /* save the context file */
635 switch (pid = fork()) {
637 advise("fork", "unable to");
646 m_putenv("mhfolder", mp->foldpath);
647 m_putenv("editalt", altpath);
651 vec[vecp++] = mhbasename(*ed);
654 vec[vecp++] = *arg++;
659 fprintf(stderr, "%s: unable to exec ", invo_name);
664 if ((status = pidwait(pid, NOTOK))) {
665 if ((status & 0xff00) == 0xff00) {
666 /* cmd not found or pidwait() failed */
670 if (status & 0x00ff) {
671 /* terminated by signal */
672 advise(NULL, "%s terminated by signal %d",
676 advise(NULL, "%s exited with return code %d",
677 *ed, (status & 0xff00) >> 8);
684 if (altmsg && mp && !is_readonly(mp) && (slinked ?
685 lstat (linkpath, &st) != NOTOK &&
686 S_ISREG(st.st_mode) &&
687 copyf(linkpath, altpath) == NOTOK :
688 stat(linkpath, &st) != NOTOK &&
690 (unlink(altpath) == NOTOK ||
691 link(linkpath, altpath) == NOTOK)))
692 advise(linkpath, "unable to update %s from", altmsg);
693 #else /* HAVE_LSTAT */
694 if (altmsg && mp && !is_readonly(mp) &&
695 stat(linkpath, &st) != NOTOK &&
697 (unlink(altpath) == NOTOK ||
698 link(linkpath, altpath) == NOTOK))
699 advise(linkpath, "unable to update %s from", altmsg);
700 #endif /* HAVE_LSTAT */
703 /* remember which editor we used */
704 edsave = getcpy(*ed);
717 copyf(char *ifile, char *ofile)
722 if ((in = open(ifile, O_RDONLY)) == NOTOK)
724 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
725 admonish(ofile, "unable to open and truncate");
730 while ((i = read(in, buffer, sizeof(buffer))) > OK)
731 if (write(out, buffer, i) != i) {
732 advise(ofile, "may have damaged");
741 #endif /* HAVE_LSTAT */
749 sendfile(char **arg, char *file)
755 context_save(); /* save the context file */
758 switch (child_id = fork()) {
760 advise(NULL, "unable to fork, so sending directly...");
764 vec[vecp++] = invo_name;
767 vec[vecp++] = *arg++;
772 fprintf(stderr, "%s: unable to exec ", invo_name);
777 if (pidwait(child_id, OK) == 0)
785 ** refile msg into another folder
788 refile(char **arg, char *file)
795 vec[vecp++] = "refile";
796 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
797 vec[vecp++] = "-file";
800 while (arg && *arg) {
801 vec[vecp++] = *arg++;
805 context_save(); /* save the context file */
808 switch (pid = fork()) {
810 advise("fork", "unable to");
815 fprintf(stderr, "%s: unable to exec ", invo_name);
820 return (pidwait(pid, -1));
826 ** Remove the draft file
830 removefile(char *drft)
832 if (unlink(drft) == NOTOK)
833 adios(drft, "unable to unlink");