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) {
204 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
206 if (!(argp = getans(prompt, aleqs))) {
210 switch (smatch(*argp, aleqs)) {
212 /* display the msg being replied to or distributed */
214 snprintf(buf, sizeof buf, "%s '%s'",
218 advise(NULL, "no alternate message to display");
223 /* Call an editor on the draft file */
226 if (editfile(&ed, argp, drft, NOUSE, NULL,
227 msgnam, NULL) == NOTOK)
232 /* display the draft file */
233 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
239 if (stat(drft, &st) != NOTOK) {
240 advise(NULL, "draft left on %s", drft);
245 /* Delete draft and exit */
251 sendfile(++argp, drft);
255 /* Refile the draft */
256 if (refile(++argp, drft) == 0) {
263 ** Change the working directory for attachments
265 ** Run the directory through the user's shell
266 ** so that we can take advantage of any syntax
267 ** that the user is accustomed to. Read back
268 ** the absolute path.
271 if (*(argp+1) == NULL) {
272 sprintf(buf, "$SHELL -c \"cd;pwd\"");
274 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
276 if ((f = popen_in_dir(cwd, buf, "r"))) {
277 fgets(cwd, sizeof (cwd), f);
279 if (strchr(cwd, '\n'))
280 *strchr(cwd, '\n') = '\0';
284 advise("popen", "could not get directory");
290 /* Print the working directory for attachments */
296 ** List files in the current attachment working
299 ** Use the user's shell so that we can take
300 ** advantage of any syntax that the user is
303 writelscmd(buf, sizeof(buf), argp);
304 system_in_dir(cwd, buf);
309 ** List attachments on current draft.
311 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
312 "-number all -text IGNORE '%s'",
314 if (system(buf) != 0) {
315 advise(NULL, "Could not list attachment headers.");
321 ** Attach files to current draft.
324 if (*(argp+1) == NULL) {
325 advise(NULL, "attach command requires file argument(s).");
330 ** Build a command line that causes the user's
331 ** shell to list the file name arguments.
332 ** This handles and wildcard expansion, tilde
335 writelscmd(buf, sizeof(buf), argp);
338 ** Read back the response from the shell,
339 ** which contains a number of lines with one
340 ** file name per line. Remove off the newline.
341 ** Determine whether we have an absolute or
342 ** relative path name. Prepend the current
343 ** working directory to relative path names.
344 ** Add the attachment annotation to the draft.
346 if ((f = popen_in_dir(cwd, buf, "r"))) {
349 while (fgets(shell, sizeof(shell), f)) {
350 *(strchr(shell, '\n')) = '\0';
353 sprintf(file, "%s", shell);
355 sprintf(file, "%s/%s", cwd,
358 snprintf(buf, sizeof buf,
359 "anno -nodate -append "
360 "-comp '%s' -text '%s'"
364 if (system(buf) != 0) {
365 advise(NULL, "Could not add attachment header.");
371 advise("popen", "could not get file from shell");
378 ** Detach files from current draft.
380 ** Scan the arguments for a -n. Mixed file
381 ** names and numbers aren't allowed, so this
382 ** catches a -n anywhere in the argument list.
384 for (n = 0, arguments = argp + 1;
387 if (strcmp(*arguments, "-n") == 0) {
394 ** A -n was found so interpret the arguments as
395 ** attachment numbers. Decrement any remaining
396 ** argument number that is greater than the one
397 ** just processed after processing each one so
398 ** that the numbering stays correct.
401 for (arguments=argp+1; *arguments;
403 if (strcmp(*arguments, "-n") == 0)
406 if (**arguments != '\0') {
409 n = atoi(*arguments);
410 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
411 if (system(buf) != 0) {
412 advise(NULL, "Could not delete attachment header.");
415 for (argp=arguments+1; *argp;
417 if (atoi(*argp) > n) {
418 if (atoi(*argp) == 1)
421 sprintf(*argp, "%d", atoi(*argp) - 1);
431 ** The arguments are interpreted as file names.
432 ** Run them through the user's shell for wildcard
433 ** expansion and other goodies. Do this from
434 ** the current working directory if the argument
435 ** is not an absolute path name (does not begin
438 ** We feed all the file names to the shell at
439 ** once, otherwise you can't provide a file name
440 ** with a space in it.
442 writelscmd(buf, sizeof(buf), argp);
443 if ((f = popen_in_dir(cwd, buf, "r"))) {
444 while (fgets(shell, sizeof (shell), f)) {
445 *(strchr(shell, '\n')) = '\0';
446 snprintf(buf, sizeof buf,
447 "anno -delete -comp "
448 "'%s' -text '%s' '%s'",
451 if (system(buf) != 0) {
452 advise(NULL, "Could not delete attachment header.");
457 advise("popen", "could not get file from shell");
463 /* Unknown command */
464 advise(NULL, "say what?");
474 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
478 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
482 ** Note that we do not quote -- the argp from the user
483 ** is assumed to be quoted as they desire. (We can't treat
484 ** it as pure literal as that would prevent them using ~,
485 ** wildcards, etc.) The buffer produced by this function
486 ** should be given to popen_in_dir() or system_in_dir() so
487 ** that the current working directory is set correctly.
489 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
491 ** NB that some snprintf() return -1 on overflow rather than the
492 ** new C99 mandated 'number of chars that would have been written'
495 ** length checks here and inside the loop allow for the
496 ** trailing ';', trailcmd, '"' and NUL
498 int trailln = strlen(trailcmd) + 3;
499 if (ln < 0 || ln + trailln > bufsz)
500 adios(NULL, "arguments too long");
506 /* +1 for leading space */
507 if (ln + trailln + 1 > bufsz - (cp-buf))
508 adios(NULL, "arguments too long");
510 memcpy(cp, *argp, ln+1);
515 strcpy(cp, trailcmd);
523 ** Build a command line that causes the user's shell to list the file name
524 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
527 writelscmd(char *buf, int bufsz, char **argp)
529 writesomecmd(buf, bufsz, "ls", "", argp);
533 ** Like system(), but run the command in directory dir.
534 ** This assumes the program is single-threaded!
537 system_in_dir(const char *dir, const char *cmd)
541 if (getcwd(olddir, sizeof(olddir)) == 0)
542 adios("getcwd", "could not get working directory");
544 adios("chdir", "could not change working directory");
546 if (chdir(olddir) != 0)
547 adios("chdir", "could not change working directory");
551 /* ditto for popen() */
553 popen_in_dir(const char *dir, const char *cmd, const char *type)
557 if (getcwd(olddir, sizeof(olddir)) == 0)
558 adios("getcwd", "could not get working directory");
560 adios("chdir", "could not change working directory");
561 f = popen(cmd, type);
562 if (chdir(olddir) != 0)
563 adios("chdir", "could not change working directory");
572 static char *edsave = NULL; /* the editor we used previously */
576 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
577 char *altmsg, char *cwd)
579 int pid, status, vecp;
580 char altpath[BUFSIZ], linkpath[BUFSIZ];
581 char *cp, *vec[MAXARGS];
586 #endif /* HAVE_LSTAT */
589 /* We have no explicit editor. */
591 /* Use the previous editor ... */
593 if (!(cp = mhbasename(*ed)))
596 /* but prefer one specified via "editor-next" */
597 cp = concat(cp, "-next", NULL);
598 if ((cp = context_find(cp)))
601 /* set initial editor */
607 if (!mp || *altmsg == '/' || !cwd)
608 strncpy(altpath, altmsg, sizeof(altpath));
610 snprintf(altpath, sizeof(altpath), "%s/%s",
611 mp->foldpath, altmsg);
613 strncpy(linkpath, altmsglink, sizeof(linkpath));
615 snprintf(linkpath, sizeof(linkpath), "%s/%s",
619 if (link(altpath, linkpath) == NOTOK) {
620 symlink(altpath, linkpath);
625 #else /* not HAVE_LSTAT */
626 link(altpath, linkpath);
627 #endif /* not HAVE_LSTAT */
630 context_save(); /* save the context file */
633 switch (pid = fork()) {
635 advise("fork", "unable to");
644 m_putenv("mhfolder", mp->foldpath);
645 m_putenv("editalt", altpath);
649 vec[vecp++] = mhbasename(*ed);
652 vec[vecp++] = *arg++;
657 fprintf(stderr, "unable to exec ");
662 if ((status = pidwait(pid, NOTOK))) {
663 if (((status & 0xff00) != 0xff00) &&
664 (!edsave || (status & 0x00ff))) {
665 if (!use && (status & 0xff00) && (rename(file, cp = m_backup(file)) != NOTOK)) {
666 advise(NULL, "problems with edit--draft left in %s", cp);
668 advise(NULL, "problems with edit--%s preserved", file);
671 status = -2; /* maybe "edsave ? -2 : -1"? */
676 if (altmsg && mp && !is_readonly(mp) && (slinked ?
677 lstat (linkpath, &st) != NOTOK &&
678 S_ISREG(st.st_mode) &&
679 copyf(linkpath, altpath) == NOTOK :
680 stat(linkpath, &st) != NOTOK &&
682 (unlink(altpath) == NOTOK ||
683 link(linkpath, altpath) == NOTOK)))
684 advise(linkpath, "unable to update %s from", altmsg);
685 #else /* HAVE_LSTAT */
686 if (altmsg && mp && !is_readonly(mp) &&
687 stat(linkpath, &st) != NOTOK &&
689 (unlink(altpath) == NOTOK ||
690 link(linkpath, altpath) == NOTOK))
691 advise(linkpath, "unable to update %s from", altmsg);
692 #endif /* HAVE_LSTAT */
695 /* remember which editor we used */
696 edsave = getcpy(*ed);
708 copyf(char *ifile, char *ofile)
713 if ((in = open(ifile, O_RDONLY)) == NOTOK)
715 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
716 admonish(ofile, "unable to open and truncate");
721 while ((i = read(in, buffer, sizeof(buffer))) > OK)
722 if (write(out, buffer, i) != i) {
723 advise(ofile, "may have damaged");
732 #endif /* HAVE_LSTAT */
740 sendfile(char **arg, char *file)
746 context_save(); /* save the context file */
749 switch (child_id = fork()) {
751 advise(NULL, "unable to fork, so sending directly...");
755 vec[vecp++] = invo_name;
758 vec[vecp++] = *arg++;
763 fprintf(stderr, "unable to exec ");
768 if (pidwait(child_id, OK) == 0)
776 ** refile msg into another folder
779 refile(char **arg, char *file)
786 vec[vecp++] = "refile";
787 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
788 vec[vecp++] = "-file";
791 while (arg && *arg) {
792 vec[vecp++] = *arg++;
796 context_save(); /* save the context file */
799 switch (pid = fork()) {
801 advise("fork", "unable to");
806 fprintf(stderr, "unable to exec ");
811 return (pidwait(pid, -1));
817 ** Remove the draft file
821 removefile(char *drft)
823 if (unlink(drft) == NOTOK)
824 adios(drft, "unable to unlink");