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 },
74 { "push [<switches>]", 0 },
80 { "cd [directory]", 0 },
85 #define ATTACHCMDSW 11
87 #define DETACHCMDSW 12
94 static char *myprompt = "\nWhat now? ";
99 static int editfile(char **, char **, char *, int, struct msgs *,
100 char *, char *, int);
101 static int sendfile(char **, char *, int);
102 static int refile(char **, char *);
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 int n; /* set on -n to detach command */
131 setlocale(LC_ALL, "");
134 invo_name = mhbasename(argv[0]);
136 /* read user profile/context */
139 arguments = getarguments(invo_name, argc, argv, 1);
143 ** Get the initial current working directory.
146 if (!getcwd(cwd, sizeof (cwd))) {
147 adios("getcwd", "could not get working directory");
150 while ((cp = *argp++)) {
152 switch (smatch(++cp, whatnowswitches)) {
154 ambigsw(cp, whatnowswitches);
157 adios(NULL, "-%s unknown", cp);
160 snprintf(buf, sizeof(buf),
161 "%s [switches] [file]",
163 print_help(buf, whatnowswitches, 1);
166 print_version(invo_name);
170 if (!(ed = *argp++) || *ed == '-')
171 adios(NULL, "missing argument to %s",
180 if (!(myprompt = *argp++) || *myprompt == '-')
181 adios(NULL, "missing argument to %s",
188 adios(NULL, "only one draft at a time!");
193 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
194 drft = getcpy(m_draft(seq_cur));
196 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
198 if ((cp = getenv("mhuse")) && *cp)
201 if (!ed && (!(ed = getenv("mheditor")) || !*ed)) {
206 /* start editing the draft, unless -noedit was given */
207 if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
211 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
213 if (!(argp = getans(prompt, aleqs))) {
217 switch (smatch(*argp, aleqs)) {
219 /* display the msg being replied to or distributed */
221 snprintf(buf, sizeof buf, "%s '%s'",
225 advise(NULL, "no alternate message to display");
230 /* Call an editor on the draft file */
233 if (editfile(&ed, argp, drft, NOUSE, NULL,
234 msgnam, NULL, 1) == NOTOK)
239 /* display the draft file */
240 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
246 if (stat(drft, &st) != NOTOK) {
247 advise(NULL, "draft left on %s", drft);
252 /* Delete draft and exit */
257 /* Send draft in background */
258 if (sendfile(++argp, drft, 1))
264 sendfile(++argp, drft, 0);
268 /* Refile the draft */
269 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.
324 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
325 "-number all -text IGNORE '%s'",
327 if (system(buf) != 0) {
328 advise(NULL, "Could not list attachment headers.");
334 ** Attach files to current draft.
337 if (*(argp+1) == NULL) {
338 advise(NULL, "attach command requires file argument(s).");
343 ** Build a command line that causes the user's
344 ** shell to list the file name arguments.
345 ** This handles and wildcard expansion, tilde
348 writelscmd(buf, sizeof(buf), argp);
351 ** Read back the response from the shell,
352 ** which contains a number of lines with one
353 ** file name per line. Remove off the newline.
354 ** Determine whether we have an absolute or
355 ** relative path name. Prepend the current
356 ** working directory to relative path names.
357 ** Add the attachment annotation to the draft.
359 if ((f = popen_in_dir(cwd, buf, "r"))) {
362 while (fgets(shell, sizeof(shell), f)) {
363 *(strchr(shell, '\n')) = '\0';
366 sprintf(file, "%s", shell);
368 sprintf(file, "%s/%s", cwd,
371 snprintf(buf, sizeof buf,
372 "anno -nodate -append "
373 "-comp '%s' -text '%s'"
377 if (system(buf) != 0) {
378 advise(NULL, "Could not add attachment header.");
384 advise("popen", "could not get file from shell");
391 ** Detach files from current draft.
393 ** Scan the arguments for a -n. Mixed file
394 ** names and numbers aren't allowed, so this
395 ** catches a -n anywhere in the argument list.
397 for (n = 0, arguments = argp + 1;
400 if (strcmp(*arguments, "-n") == 0) {
407 ** A -n was found so interpret the arguments as
408 ** attachment numbers. Decrement any remaining
409 ** argument number that is greater than the one
410 ** just processed after processing each one so
411 ** that the numbering stays correct.
414 for (arguments=argp+1; *arguments;
416 if (strcmp(*arguments, "-n") == 0)
419 if (**arguments != '\0') {
422 n = atoi(*arguments);
423 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
424 if (system(buf) != 0) {
425 advise(NULL, "Could not delete attachment header.");
428 for (argp=arguments+1; *argp;
430 if (atoi(*argp) > n) {
431 if (atoi(*argp) == 1)
434 sprintf(*argp, "%d", atoi(*argp) - 1);
444 ** The arguments are interpreted as file names.
445 ** Run them through the user's shell for wildcard
446 ** expansion and other goodies. Do this from
447 ** the current working directory if the argument
448 ** is not an absolute path name (does not begin
451 ** We feed all the file names to the shell at
452 ** once, otherwise you can't provide a file name
453 ** with a space in it.
455 writelscmd(buf, sizeof(buf), argp);
456 if ((f = popen_in_dir(cwd, buf, "r"))) {
457 while (fgets(shell, sizeof (shell), f)) {
458 *(strchr(shell, '\n')) = '\0';
459 snprintf(buf, sizeof buf,
460 "anno -delete -comp "
461 "'%s' -text '%s' '%s'",
464 if (system(buf) != 0) {
465 advise(NULL, "Could not delete attachment header.");
470 advise("popen", "could not get file from shell");
476 /* Unknown command */
477 advise(NULL, "say what?");
487 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
491 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
495 ** Note that we do not quote -- the argp from the user
496 ** is assumed to be quoted as they desire. (We can't treat
497 ** it as pure literal as that would prevent them using ~,
498 ** wildcards, etc.) The buffer produced by this function
499 ** should be given to popen_in_dir() or system_in_dir() so
500 ** that the current working directory is set correctly.
502 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
504 ** NB that some snprintf() return -1 on overflow rather than the
505 ** new C99 mandated 'number of chars that would have been written'
508 ** length checks here and inside the loop allow for the
509 ** trailing ';', trailcmd, '"' and NUL
511 int trailln = strlen(trailcmd) + 3;
512 if (ln < 0 || ln + trailln > bufsz)
513 adios(NULL, "arguments too long");
519 /* +1 for leading space */
520 if (ln + trailln + 1 > bufsz - (cp-buf))
521 adios(NULL, "arguments too long");
523 memcpy(cp, *argp, ln+1);
528 strcpy(cp, trailcmd);
536 ** Build a command line that causes the user's shell to list the file name
537 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
540 writelscmd(char *buf, int bufsz, char **argp)
542 writesomecmd(buf, bufsz, "ls", "", argp);
546 ** Like system(), but run the command in directory dir.
547 ** This assumes the program is single-threaded!
550 system_in_dir(const char *dir, const char *cmd)
554 if (getcwd(olddir, sizeof(olddir)) == 0)
555 adios("getcwd", "could not get working directory");
557 adios("chdir", "could not change working directory");
559 if (chdir(olddir) != 0)
560 adios("chdir", "could not change working directory");
564 /* ditto for popen() */
566 popen_in_dir(const char *dir, const char *cmd, const char *type)
570 if (getcwd(olddir, sizeof(olddir)) == 0)
571 adios("getcwd", "could not get working directory");
573 adios("chdir", "could not change working directory");
574 f = popen(cmd, type);
575 if (chdir(olddir) != 0)
576 adios("chdir", "could not change working directory");
585 static int reedit = 0; /* have we been here before? */
586 static char *edsave = NULL; /* the editor we used previously */
590 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
591 char *altmsg, char *cwd, int save_editor)
593 int pid, status, vecp;
594 char altpath[BUFSIZ], linkpath[BUFSIZ];
595 char *cp, *vec[MAXARGS];
600 #endif /* HAVE_LSTAT */
602 /* Was there a previous edit session? */
604 if (!*ed) { /* no explicit editor */
605 *ed = edsave; /* so use the previous one */
606 if (!(cp = mhbasename(*ed)))
609 /* unless we've specified it via "editor-next" */
610 cp = concat(cp, "-next", NULL);
611 if ((cp = context_find(cp)))
615 /* set initial editor */
620 if (!mp || *altmsg == '/' || !cwd)
621 strncpy(altpath, altmsg, sizeof(altpath));
623 snprintf(altpath, sizeof(altpath), "%s/%s",
624 mp->foldpath, altmsg);
626 strncpy(linkpath, altmsglink, sizeof(linkpath));
628 snprintf(linkpath, sizeof(linkpath), "%s/%s",
632 if (link(altpath, linkpath) == NOTOK) {
633 symlink(altpath, linkpath);
638 #else /* not HAVE_LSTAT */
639 link(altpath, linkpath);
640 #endif /* not HAVE_LSTAT */
643 context_save(); /* save the context file */
646 switch (pid = fork()) {
648 advise("fork", "unable to");
657 m_putenv("mhfolder", mp->foldpath);
658 m_putenv("editalt", altpath);
662 vec[vecp++] = mhbasename(*ed);
665 vec[vecp++] = *arg++;
670 fprintf(stderr, "unable to exec ");
675 if ((status = pidwait(pid, NOTOK))) {
676 if (((status & 0xff00) != 0xff00) &&
677 (!reedit || (status & 0x00ff))) {
678 if (!use && (status & 0xff00) && (rename(file, cp = m_backup(file)) != NOTOK)) {
679 advise(NULL, "problems with edit--draft left in %s", cp);
681 advise(NULL, "problems with edit--%s preserved", file);
684 status = -2; /* maybe "reedit ? -2 : -1"? */
690 if (altmsg && mp && !is_readonly(mp) && (slinked ?
691 lstat (linkpath, &st) != NOTOK &&
692 S_ISREG(st.st_mode) &&
693 copyf(linkpath, altpath) == NOTOK :
694 stat(linkpath, &st) != NOTOK &&
696 (unlink(altpath) == NOTOK ||
697 link(linkpath, altpath) == NOTOK)))
698 advise(linkpath, "unable to update %s from", altmsg);
699 #else /* HAVE_LSTAT */
700 if (altmsg && mp && !is_readonly(mp) &&
701 stat(linkpath, &st) != NOTOK &&
703 (unlink(altpath) == NOTOK ||
704 link(linkpath, altpath) == NOTOK))
705 advise(linkpath, "unable to update %s from", altmsg);
706 #endif /* HAVE_LSTAT */
709 /* normally, we remember which editor we used */
711 edsave = getcpy(*ed);
723 copyf(char *ifile, char *ofile)
728 if ((in = open(ifile, O_RDONLY)) == NOTOK)
730 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
731 admonish(ofile, "unable to open and truncate");
736 while ((i = read(in, buffer, sizeof(buffer))) > OK)
737 if (write(out, buffer, i) != i) {
738 advise(ofile, "may have damaged");
747 #endif /* HAVE_LSTAT */
755 sendfile(char **arg, char *file, int pushsw)
761 context_save(); /* save the context file */
764 switch (child_id = fork()) {
766 advise(NULL, "unable to fork, so sending directly...");
770 vec[vecp++] = invo_name;
772 vec[vecp++] = "-push";
775 vec[vecp++] = *arg++;
780 fprintf(stderr, "unable to exec ");
785 if (pidwait(child_id, OK) == 0)
793 ** refile msg into another folder
796 refile(char **arg, char *file)
803 vec[vecp++] = "refile";
804 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
805 vec[vecp++] = "-file";
808 while (arg && *arg) {
809 vec[vecp++] = *arg++;
813 context_save(); /* save the context file */
816 switch (pid = fork()) {
818 advise("fork", "unable to");
823 fprintf(stderr, "unable to exec ");
828 return (pidwait(pid, -1));
834 ** Remove the draft file
838 removefile(char *drft)
840 if (unlink(drft) == NOTOK)
841 adios(drft, "unable to unlink");