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 [-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 },
73 { "send [<switches>]", 0 },
75 { "push [<switches>]", 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 int removefile(char *);
104 static void writelscmd(char *, int, char **);
105 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
106 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
107 static int system_in_dir(const char *dir, const char *cmd);
111 static int copyf(char *, char *);
116 main(int argc, char **argv)
118 int nedit = 0, use = 0;
120 char *ed = NULL, *drft = NULL, *msgnam = NULL;
121 char buf[BUFSIZ], prompt[BUFSIZ];
122 char **argp, **arguments;
124 char cwd[MAXPATHLEN + 1]; /* current working directory */
125 char file[MAXPATHLEN + 1]; /* file name buffer */
126 char shell[MAXPATHLEN + 1]; /* shell response buffer */
127 FILE *f; /* read pointer for bgnd proc */
128 char *l; /* set on -l to alist command */
129 int n; /* set on -n to alist command */
132 setlocale(LC_ALL, "");
135 invo_name = mhbasename(argv[0]);
137 /* read user profile/context */
140 arguments = getarguments(invo_name, argc, argv, 1);
144 ** Get the initial current working directory.
147 if (!getcwd(cwd, sizeof (cwd))) {
148 adios("getcwd", "could not get working directory");
151 while ((cp = *argp++)) {
153 switch (smatch(++cp, whatnowswitches)) {
155 ambigsw(cp, whatnowswitches);
158 adios(NULL, "-%s unknown", cp);
161 snprintf(buf, sizeof(buf),
162 "%s [switches] [file]",
164 print_help(buf, whatnowswitches, 1);
167 print_version(invo_name);
171 if (!(ed = *argp++) || *ed == '-')
172 adios(NULL, "missing argument to %s",
181 if (!(myprompt = *argp++) || *myprompt == '-')
182 adios(NULL, "missing argument to %s",
189 adios(NULL, "only one draft at a time!");
194 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
195 drft = getcpy(m_draft(seq_cur));
197 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
199 if ((cp = getenv("mhuse")) && *cp)
202 if (!ed && (!(ed = getenv("mheditor")) || !*ed)) {
207 /* start editing the draft, unless -noedit was given */
208 if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
212 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
214 if (!(argp = getans(prompt, aleqs))) {
218 switch (smatch(*argp, aleqs)) {
220 /* display the msg being replied to or distributed */
222 snprintf(buf, sizeof buf, "%s '%s'",
226 advise(NULL, "no alternate message to display");
231 /* Call an editor on the draft file */
234 if (editfile(&ed, argp, drft, NOUSE, NULL,
235 msgnam, NULL, 1) == NOTOK)
240 /* display the draft file */
241 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
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"))) {
290 fgets(cwd, sizeof (cwd), f);
292 if (strchr(cwd, '\n'))
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
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 snprintf(buf, sizeof buf,
353 "anno -list -comp '%s' "
356 n ? "-number all" : "",
357 l ? "-text IGNORE " : "",
359 if (system(buf) != 0) {
360 advise(NULL, "Could not list attachment headers.");
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.
392 if ((f = popen_in_dir(cwd, buf, "r"))) {
395 while (fgets(shell, sizeof(shell), f)) {
396 *(strchr(shell, '\n')) = '\0';
399 sprintf(file, "%s", shell);
401 sprintf(file, "%s/%s", cwd,
404 snprintf(buf, sizeof buf,
405 "anno -nodate -append "
406 "-comp '%s' -text '%s'"
410 if (system(buf) != 0) {
411 advise(NULL, "Could not add attachment header.");
417 advise("popen", "could not get file from shell");
424 ** Detach files from current draft.
426 ** Scan the arguments for a -n. Mixed file
427 ** names and numbers aren't allowed, so this
428 ** catches a -n anywhere in the argument list.
430 for (n = 0, arguments = argp + 1;
433 if (strcmp(*arguments, "-n") == 0) {
440 ** A -n was found so interpret the arguments as
441 ** attachment numbers. Decrement any remaining
442 ** argument number that is greater than the one
443 ** just processed after processing each one so
444 ** that the numbering stays correct.
447 for (arguments=argp+1; *arguments;
449 if (strcmp(*arguments, "-n") == 0)
452 if (**arguments != '\0') {
455 n = atoi(*arguments);
456 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
457 if (system(buf) != 0) {
458 advise(NULL, "Could not delete attachment header.");
461 for (argp=arguments+1; *argp;
463 if (atoi(*argp) > n) {
464 if (atoi(*argp) == 1)
467 sprintf(*argp, "%d", atoi(*argp) - 1);
477 ** The arguments are interpreted as file names.
478 ** Run them through the user's shell for wildcard
479 ** expansion and other goodies. Do this from
480 ** the current working directory if the argument
481 ** is not an absolute path name (does not begin
484 ** We feed all the file names to the shell at
485 ** once, otherwise you can't provide a file name
486 ** with a space in it.
488 writelscmd(buf, sizeof(buf), argp);
489 if ((f = popen_in_dir(cwd, buf, "r"))) {
490 while (fgets(shell, sizeof (shell), f)) {
491 *(strchr(shell, '\n')) = '\0';
492 snprintf(buf, sizeof buf,
493 "anno -delete -comp "
494 "'%s' -text '%s' '%s'",
497 if (system(buf) != 0) {
498 advise(NULL, "Could not delete attachment header.");
503 advise("popen", "could not get file from shell");
509 /* Unknown command */
510 advise(NULL, "say what?");
520 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
524 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
528 ** Note that we do not quote -- the argp from the user
529 ** is assumed to be quoted as they desire. (We can't treat
530 ** it as pure literal as that would prevent them using ~,
531 ** wildcards, etc.) The buffer produced by this function
532 ** should be given to popen_in_dir() or system_in_dir() so
533 ** that the current working directory is set correctly.
535 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
537 ** NB that some snprintf() return -1 on overflow rather than the
538 ** new C99 mandated 'number of chars that would have been written'
541 ** length checks here and inside the loop allow for the
542 ** trailing ';', trailcmd, '"' and NUL
544 int trailln = strlen(trailcmd) + 3;
545 if (ln < 0 || ln + trailln > bufsz)
546 adios(NULL, "arguments too long");
552 /* +1 for leading space */
553 if (ln + trailln + 1 > bufsz - (cp-buf))
554 adios(NULL, "arguments too long");
556 memcpy(cp, *argp, ln+1);
561 strcpy(cp, trailcmd);
569 ** Build a command line that causes the user's shell to list the file name
570 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
573 writelscmd(char *buf, int bufsz, char **argp)
575 writesomecmd(buf, bufsz, "ls", "", argp);
579 ** Like system(), but run the command in directory dir.
580 ** This assumes the program is single-threaded!
583 system_in_dir(const char *dir, const char *cmd)
587 if (getcwd(olddir, sizeof(olddir)) == 0)
588 adios("getcwd", "could not get working directory");
590 adios("chdir", "could not change working directory");
592 if (chdir(olddir) != 0)
593 adios("chdir", "could not change working directory");
597 /* ditto for popen() */
599 popen_in_dir(const char *dir, const char *cmd, const char *type)
603 if (getcwd(olddir, sizeof(olddir)) == 0)
604 adios("getcwd", "could not get working directory");
606 adios("chdir", "could not change working directory");
607 f = popen(cmd, type);
608 if (chdir(olddir) != 0)
609 adios("chdir", "could not change working directory");
618 static int reedit = 0; /* have we been here before? */
619 static char *edsave = NULL; /* the editor we used previously */
623 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
624 char *altmsg, char *cwd, int save_editor)
626 int pid, status, vecp;
627 char altpath[BUFSIZ], linkpath[BUFSIZ];
628 char *cp, *vec[MAXARGS];
633 #endif /* HAVE_LSTAT */
635 /* Was there a previous edit session? */
637 if (!*ed) { /* no explicit editor */
638 *ed = edsave; /* so use the previous one */
639 if (!(cp = mhbasename(*ed)))
642 /* unless we've specified it via "editor-next" */
643 cp = concat(cp, "-next", NULL);
644 if ((cp = context_find(cp)))
648 /* set initial editor */
653 if (!mp || *altmsg == '/' || !cwd)
654 strncpy(altpath, altmsg, sizeof(altpath));
656 snprintf(altpath, sizeof(altpath), "%s/%s",
657 mp->foldpath, altmsg);
659 strncpy(linkpath, altmsglink, sizeof(linkpath));
661 snprintf(linkpath, sizeof(linkpath), "%s/%s",
668 if (link(altpath, linkpath) == NOTOK) {
669 symlink(altpath, linkpath);
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))) {
712 if (((status & 0xff00) != 0xff00)
713 && (!reedit || (status & 0x00ff))) {
714 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
715 advise(NULL, "problems with edit--draft left in %s", cp);
717 advise(NULL, "problems with edit--%s preserved", file);
720 status = -2; /* maybe "reedit ? -2 : -1"? */
726 if (altmsg && mp && !is_readonly(mp) && (slinked ?
727 lstat (linkpath, &st) != NOTOK &&
728 S_ISREG(st.st_mode) &&
729 copyf(linkpath, altpath) == NOTOK :
730 stat(linkpath, &st) != NOTOK &&
732 (unlink(altpath) == NOTOK ||
733 link(linkpath, altpath) == NOTOK)))
734 advise(linkpath, "unable to update %s from", altmsg);
735 #else /* HAVE_LSTAT */
736 if (altmsg && mp && !is_readonly(mp) &&
737 stat(linkpath, &st) != NOTOK &&
739 (unlink(altpath) == NOTOK ||
740 link(linkpath, altpath) == NOTOK))
741 advise(linkpath, "unable to update %s from", altmsg);
742 #endif /* HAVE_LSTAT */
745 /* normally, we remember which editor we used */
747 edsave = getcpy(*ed);
759 copyf(char *ifile, char *ofile)
764 if ((in = open(ifile, O_RDONLY)) == NOTOK)
766 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
767 admonish(ofile, "unable to open and truncate");
772 while ((i = read(in, buffer, sizeof(buffer))) > OK)
773 if (write(out, buffer, i) != i) {
774 advise(ofile, "may have damaged");
783 #endif /* HAVE_LSTAT */
791 sendfile(char **arg, char *file, int pushsw)
797 context_save(); /* save the context file */
800 switch (child_id = fork()) {
802 advise(NULL, "unable to fork, so sending directly...");
806 vec[vecp++] = invo_name;
808 vec[vecp++] = "-push";
811 vec[vecp++] = *arg++;
816 fprintf(stderr, "unable to exec ");
821 if (pidwait(child_id, OK) == 0)
829 ** Remove the draft file
833 removefile(char *drft)
835 if (unlink(drft) == NOTOK)
836 adios(drft, "unable to unlink");