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 },
51 { "prompt string", 4 },
60 ** Options at the "whatnow" prompt
62 static struct swit aleqs[] = {
64 { "edit [<editor> <switches>]", 0 },
66 { "refile [<switches>] +folder", 0 },
72 { "send [<switches>]", 0 },
78 { "cd [directory]", 0 },
83 #define ATTACHCMDSW 10
85 #define DETACHCMDSW 11
92 static char *myprompt = "\nWhat now? ";
97 static int editfile(char **, char **, char *, int, struct msgs *,
99 static int sendfile(char **, char *);
100 static int refile(char **, char *);
101 static int removefile(char *);
102 static void writelscmd(char *, int, char **);
103 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
104 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
105 static int system_in_dir(const char *dir, const char *cmd);
109 static int copyf(char *, char *);
114 main(int argc, char **argv)
116 int nedit = 0, use = 0;
118 char *ed = NULL, *drft = NULL, *msgnam = NULL;
119 char buf[BUFSIZ], prompt[BUFSIZ];
120 char **argp, **arguments;
122 char cwd[MAXPATHLEN + 1]; /* current working directory */
123 char file[MAXPATHLEN + 1]; /* file name buffer */
124 char shell[MAXPATHLEN + 1]; /* shell response buffer */
125 FILE *f; /* read pointer for bgnd proc */
126 int n; /* set on -n to detach command */
129 setlocale(LC_ALL, "");
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))) {
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 && !(drft = getenv("mhdraft"))) || !*drft)
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 && (!(ed = getenv("mheditor")) || !*ed)) {
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)) {
217 /* display the msg being replied to or distributed */
219 snprintf(buf, sizeof buf, "%s '%s'",
223 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 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
244 if (stat(drft, &st) != NOTOK) {
245 advise(NULL, "draft left on %s", drft);
250 /* Delete draft and exit */
256 sendfile(++argp, drft);
260 /* Refile the draft */
261 if (refile(++argp, drft) == 0) {
268 ** Change the working directory for attachments
270 ** Run the directory through the user's shell
271 ** so that we can take advantage of any syntax
272 ** that the user is accustomed to. Read back
273 ** the absolute path.
276 if (*(argp+1) == NULL) {
277 sprintf(buf, "$SHELL -c \"cd;pwd\"");
279 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
281 if ((f = popen_in_dir(cwd, buf, "r"))) {
282 fgets(cwd, sizeof (cwd), f);
284 if (strchr(cwd, '\n'))
285 *strchr(cwd, '\n') = '\0';
289 advise("popen", "could not get directory");
295 /* Print the working directory for attachments */
301 ** List files in the current attachment working
304 ** Use the user's shell so that we can take
305 ** advantage of any syntax that the user is
308 writelscmd(buf, sizeof(buf), argp);
309 system_in_dir(cwd, buf);
314 ** List attachments on current draft.
316 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
317 "-number all -text IGNORE '%s'",
319 if (system(buf) != 0) {
320 advise(NULL, "Could not list attachment headers.");
326 ** Attach files to current draft.
329 if (*(argp+1) == NULL) {
330 advise(NULL, "attach command requires file argument(s).");
335 ** Build a command line that causes the user's
336 ** shell to list the file name arguments.
337 ** This handles and wildcard expansion, tilde
340 writelscmd(buf, sizeof(buf), argp);
343 ** Read back the response from the shell,
344 ** which contains a number of lines with one
345 ** file name per line. Remove off the newline.
346 ** Determine whether we have an absolute or
347 ** relative path name. Prepend the current
348 ** working directory to relative path names.
349 ** Add the attachment annotation to the draft.
351 if ((f = popen_in_dir(cwd, buf, "r"))) {
354 while (fgets(shell, sizeof(shell), f)) {
355 *(strchr(shell, '\n')) = '\0';
358 sprintf(file, "%s", shell);
360 sprintf(file, "%s/%s", cwd,
363 snprintf(buf, sizeof buf,
364 "anno -nodate -append "
365 "-comp '%s' -text '%s'"
369 if (system(buf) != 0) {
370 advise(NULL, "Could not add attachment header.");
376 advise("popen", "could not get file from shell");
383 ** Detach files from current draft.
385 ** Scan the arguments for a -n. Mixed file
386 ** names and numbers aren't allowed, so this
387 ** catches a -n anywhere in the argument list.
389 for (n = 0, arguments = argp + 1;
392 if (strcmp(*arguments, "-n") == 0) {
399 ** A -n was found so interpret the arguments as
400 ** attachment numbers. Decrement any remaining
401 ** argument number that is greater than the one
402 ** just processed after processing each one so
403 ** that the numbering stays correct.
406 for (arguments=argp+1; *arguments;
408 if (strcmp(*arguments, "-n") == 0)
411 if (**arguments != '\0') {
414 n = atoi(*arguments);
415 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
416 if (system(buf) != 0) {
417 advise(NULL, "Could not delete attachment header.");
420 for (argp=arguments+1; *argp;
422 if (atoi(*argp) > n) {
423 if (atoi(*argp) == 1)
426 sprintf(*argp, "%d", atoi(*argp) - 1);
436 ** The arguments are interpreted as file names.
437 ** Run them through the user's shell for wildcard
438 ** expansion and other goodies. Do this from
439 ** the current working directory if the argument
440 ** is not an absolute path name (does not begin
443 ** We feed all the file names to the shell at
444 ** once, otherwise you can't provide a file name
445 ** with a space in it.
447 writelscmd(buf, sizeof(buf), argp);
448 if ((f = popen_in_dir(cwd, buf, "r"))) {
449 while (fgets(shell, sizeof (shell), f)) {
450 *(strchr(shell, '\n')) = '\0';
451 snprintf(buf, sizeof buf,
452 "anno -delete -comp "
453 "'%s' -text '%s' '%s'",
456 if (system(buf) != 0) {
457 advise(NULL, "Could not delete attachment header.");
462 advise("popen", "could not get file from shell");
468 /* Unknown command */
469 advise(NULL, "say what?");
479 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
483 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
487 ** Note that we do not quote -- the argp from the user
488 ** is assumed to be quoted as they desire. (We can't treat
489 ** it as pure literal as that would prevent them using ~,
490 ** wildcards, etc.) The buffer produced by this function
491 ** should be given to popen_in_dir() or system_in_dir() so
492 ** that the current working directory is set correctly.
494 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
496 ** NB that some snprintf() return -1 on overflow rather than the
497 ** new C99 mandated 'number of chars that would have been written'
500 ** length checks here and inside the loop allow for the
501 ** trailing ';', trailcmd, '"' and NUL
503 int trailln = strlen(trailcmd) + 3;
504 if (ln < 0 || ln + trailln > bufsz)
505 adios(NULL, "arguments too long");
511 /* +1 for leading space */
512 if (ln + trailln + 1 > bufsz - (cp-buf))
513 adios(NULL, "arguments too long");
515 memcpy(cp, *argp, ln+1);
520 strcpy(cp, trailcmd);
528 ** Build a command line that causes the user's shell to list the file name
529 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
532 writelscmd(char *buf, int bufsz, char **argp)
534 writesomecmd(buf, bufsz, "ls", "", argp);
538 ** Like system(), but run the command in directory dir.
539 ** This assumes the program is single-threaded!
542 system_in_dir(const char *dir, const char *cmd)
546 if (getcwd(olddir, sizeof(olddir)) == 0)
547 adios("getcwd", "could not get working directory");
549 adios("chdir", "could not change working directory");
551 if (chdir(olddir) != 0)
552 adios("chdir", "could not change working directory");
556 /* ditto for popen() */
558 popen_in_dir(const char *dir, const char *cmd, const char *type)
562 if (getcwd(olddir, sizeof(olddir)) == 0)
563 adios("getcwd", "could not get working directory");
565 adios("chdir", "could not change working directory");
566 f = popen(cmd, type);
567 if (chdir(olddir) != 0)
568 adios("chdir", "could not change working directory");
577 static int reedit = 0; /* have we been here before? */
578 static char *edsave = NULL; /* the editor we used previously */
582 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
583 char *altmsg, char *cwd, int save_editor)
585 int pid, status, vecp;
586 char altpath[BUFSIZ], linkpath[BUFSIZ];
587 char *cp, *vec[MAXARGS];
592 #endif /* HAVE_LSTAT */
594 /* Was there a previous edit session? */
596 if (!*ed) { /* no explicit editor */
597 *ed = edsave; /* so use the previous one */
598 if (!(cp = mhbasename(*ed)))
601 /* unless we've specified it via "editor-next" */
602 cp = concat(cp, "-next", NULL);
603 if ((cp = context_find(cp)))
607 /* set initial editor */
612 if (!mp || *altmsg == '/' || !cwd)
613 strncpy(altpath, altmsg, sizeof(altpath));
615 snprintf(altpath, sizeof(altpath), "%s/%s",
616 mp->foldpath, altmsg);
618 strncpy(linkpath, altmsglink, sizeof(linkpath));
620 snprintf(linkpath, sizeof(linkpath), "%s/%s",
624 if (link(altpath, linkpath) == NOTOK) {
625 symlink(altpath, linkpath);
630 #else /* not HAVE_LSTAT */
631 link(altpath, linkpath);
632 #endif /* not HAVE_LSTAT */
635 context_save(); /* save the context file */
638 switch (pid = fork()) {
640 advise("fork", "unable to");
649 m_putenv("mhfolder", mp->foldpath);
650 m_putenv("editalt", altpath);
654 vec[vecp++] = mhbasename(*ed);
657 vec[vecp++] = *arg++;
662 fprintf(stderr, "unable to exec ");
667 if ((status = pidwait(pid, NOTOK))) {
668 if (((status & 0xff00) != 0xff00) &&
669 (!reedit || (status & 0x00ff))) {
670 if (!use && (status & 0xff00) && (rename(file, cp = m_backup(file)) != NOTOK)) {
671 advise(NULL, "problems with edit--draft left in %s", cp);
673 advise(NULL, "problems with edit--%s preserved", file);
676 status = -2; /* maybe "reedit ? -2 : -1"? */
682 if (altmsg && mp && !is_readonly(mp) && (slinked ?
683 lstat (linkpath, &st) != NOTOK &&
684 S_ISREG(st.st_mode) &&
685 copyf(linkpath, altpath) == NOTOK :
686 stat(linkpath, &st) != NOTOK &&
688 (unlink(altpath) == NOTOK ||
689 link(linkpath, altpath) == NOTOK)))
690 advise(linkpath, "unable to update %s from", altmsg);
691 #else /* HAVE_LSTAT */
692 if (altmsg && mp && !is_readonly(mp) &&
693 stat(linkpath, &st) != NOTOK &&
695 (unlink(altpath) == NOTOK ||
696 link(linkpath, altpath) == NOTOK))
697 advise(linkpath, "unable to update %s from", altmsg);
698 #endif /* HAVE_LSTAT */
701 /* normally, we remember which editor we used */
703 edsave = getcpy(*ed);
715 copyf(char *ifile, char *ofile)
720 if ((in = open(ifile, O_RDONLY)) == NOTOK)
722 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
723 admonish(ofile, "unable to open and truncate");
728 while ((i = read(in, buffer, sizeof(buffer))) > OK)
729 if (write(out, buffer, i) != i) {
730 advise(ofile, "may have damaged");
739 #endif /* HAVE_LSTAT */
747 sendfile(char **arg, char *file)
753 context_save(); /* save the context file */
756 switch (child_id = fork()) {
758 advise(NULL, "unable to fork, so sending directly...");
762 vec[vecp++] = invo_name;
765 vec[vecp++] = *arg++;
770 fprintf(stderr, "unable to exec ");
775 if (pidwait(child_id, OK) == 0)
783 ** refile msg into another folder
786 refile(char **arg, char *file)
793 vec[vecp++] = "refile";
794 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
795 vec[vecp++] = "-file";
798 while (arg && *arg) {
799 vec[vecp++] = *arg++;
803 context_save(); /* save the context file */
806 switch (pid = fork()) {
808 advise("fork", "unable to");
813 fprintf(stderr, "unable to exec ");
818 return (pidwait(pid, -1));
824 ** Remove the draft file
828 removefile(char *drft)
830 if (unlink(drft) == NOTOK)
831 adios(drft, "unable to unlink");