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 removefile(char *);
103 static void writelscmd(char *, int, char **);
104 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
105 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
106 static int system_in_dir(const char *dir, const char *cmd);
110 static int copyf(char *, char *);
115 main(int argc, char **argv)
117 int nedit = 0, use = 0;
119 char *ed = NULL, *drft = NULL, *msgnam = NULL;
120 char buf[BUFSIZ], prompt[BUFSIZ];
121 char **argp, **arguments;
123 char cwd[MAXPATHLEN + 1]; /* current working directory */
124 char file[MAXPATHLEN + 1]; /* file name buffer */
125 char shell[MAXPATHLEN + 1]; /* shell response buffer */
126 FILE *f; /* read pointer for bgnd proc */
127 int n; /* set on -n to detach command */
130 setlocale(LC_ALL, "");
133 invo_name = mhbasename(argv[0]);
135 /* read user profile/context */
138 arguments = getarguments(invo_name, argc, argv, 1);
142 ** Get the initial current working directory.
145 if (!getcwd(cwd, sizeof (cwd))) {
146 adios("getcwd", "could not get working directory");
149 while ((cp = *argp++)) {
151 switch (smatch(++cp, whatnowswitches)) {
153 ambigsw(cp, whatnowswitches);
156 adios(NULL, "-%s unknown", cp);
159 snprintf(buf, sizeof(buf),
160 "%s [switches] [file]",
162 print_help(buf, whatnowswitches, 1);
165 print_version(invo_name);
169 if (!(ed = *argp++) || *ed == '-')
170 adios(NULL, "missing argument to %s",
179 if (!(myprompt = *argp++) || *myprompt == '-')
180 adios(NULL, "missing argument to %s",
187 adios(NULL, "only one draft at a time!");
192 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
193 drft = getcpy(m_draft(seq_cur));
195 msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
197 if ((cp = getenv("mhuse")) && *cp)
200 if (!ed && (!(ed = getenv("mheditor")) || !*ed)) {
205 /* start editing the draft, unless -noedit was given */
206 if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
210 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
212 if (!(argp = getans(prompt, aleqs))) {
216 switch (smatch(*argp, aleqs)) {
218 /* display the msg being replied to or distributed */
220 snprintf(buf, sizeof buf, "%s '%s'",
224 advise(NULL, "no alternate message to display");
229 /* Call an editor on the draft file */
232 if (editfile(&ed, argp, drft, NOUSE, NULL,
233 msgnam, NULL, 1) == NOTOK)
238 /* display the draft file */
239 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
245 if (stat(drft, &st) != NOTOK) {
246 advise(NULL, "draft left on %s", drft);
251 /* Delete draft and exit */
256 /* Send draft in background */
257 if (sendfile(++argp, drft, 1))
263 sendfile(++argp, drft, 0);
267 /* Refile the draft */
268 if (refile(++argp, drft) == 0)
274 ** Change the working directory for attachments
276 ** Run the directory through the user's shell
277 ** so that we can take advantage of any syntax
278 ** that the user is accustomed to. Read back
279 ** the absolute path.
282 if (*(argp+1) == NULL) {
283 sprintf(buf, "$SHELL -c \"cd;pwd\"");
285 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
287 if ((f = popen_in_dir(cwd, buf, "r"))) {
288 fgets(cwd, sizeof (cwd), f);
290 if (strchr(cwd, '\n'))
291 *strchr(cwd, '\n') = '\0';
295 advise("popen", "could not get directory");
301 /* Print the working directory for attachments */
307 ** List files in the current attachment working
310 ** Use the user's shell so that we can take
311 ** advantage of any syntax that the user is
314 writelscmd(buf, sizeof(buf), argp);
315 system_in_dir(cwd, buf);
320 ** List attachments on current draft.
322 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
323 "-number all -text IGNORE '%s'",
325 if (system(buf) != 0) {
326 advise(NULL, "Could not list attachment headers.");
332 ** Attach files to current draft.
335 if (*(argp+1) == NULL) {
336 advise(NULL, "attach command requires file argument(s).");
341 ** Build a command line that causes the user's
342 ** shell to list the file name arguments.
343 ** This handles and wildcard expansion, tilde
346 writelscmd(buf, sizeof(buf), argp);
349 ** Read back the response from the shell,
350 ** which contains a number of lines with one
351 ** file name per line. Remove off the newline.
352 ** Determine whether we have an absolute or
353 ** relative path name. Prepend the current
354 ** working directory to relative path names.
355 ** Add the attachment annotation to the draft.
357 if ((f = popen_in_dir(cwd, buf, "r"))) {
360 while (fgets(shell, sizeof(shell), f)) {
361 *(strchr(shell, '\n')) = '\0';
364 sprintf(file, "%s", shell);
366 sprintf(file, "%s/%s", cwd,
369 snprintf(buf, sizeof buf,
370 "anno -nodate -append "
371 "-comp '%s' -text '%s'"
375 if (system(buf) != 0) {
376 advise(NULL, "Could not add attachment header.");
382 advise("popen", "could not get file from shell");
389 ** Detach files from current draft.
391 ** Scan the arguments for a -n. Mixed file
392 ** names and numbers aren't allowed, so this
393 ** catches a -n anywhere in the argument list.
395 for (n = 0, arguments = argp + 1;
398 if (strcmp(*arguments, "-n") == 0) {
405 ** A -n was found so interpret the arguments as
406 ** attachment numbers. Decrement any remaining
407 ** argument number that is greater than the one
408 ** just processed after processing each one so
409 ** that the numbering stays correct.
412 for (arguments=argp+1; *arguments;
414 if (strcmp(*arguments, "-n") == 0)
417 if (**arguments != '\0') {
420 n = atoi(*arguments);
421 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
422 if (system(buf) != 0) {
423 advise(NULL, "Could not delete attachment header.");
426 for (argp=arguments+1; *argp;
428 if (atoi(*argp) > n) {
429 if (atoi(*argp) == 1)
432 sprintf(*argp, "%d", atoi(*argp) - 1);
442 ** The arguments are interpreted as file names.
443 ** Run them through the user's shell for wildcard
444 ** expansion and other goodies. Do this from
445 ** the current working directory if the argument
446 ** is not an absolute path name (does not begin
449 ** We feed all the file names to the shell at
450 ** once, otherwise you can't provide a file name
451 ** with a space in it.
453 writelscmd(buf, sizeof(buf), argp);
454 if ((f = popen_in_dir(cwd, buf, "r"))) {
455 while (fgets(shell, sizeof (shell), f)) {
456 *(strchr(shell, '\n')) = '\0';
457 snprintf(buf, sizeof buf,
458 "anno -delete -comp "
459 "'%s' -text '%s' '%s'",
462 if (system(buf) != 0) {
463 advise(NULL, "Could not delete attachment header.");
468 advise("popen", "could not get file from shell");
474 /* Unknown command */
475 advise(NULL, "say what?");
485 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
489 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
493 ** Note that we do not quote -- the argp from the user
494 ** is assumed to be quoted as they desire. (We can't treat
495 ** it as pure literal as that would prevent them using ~,
496 ** wildcards, etc.) The buffer produced by this function
497 ** should be given to popen_in_dir() or system_in_dir() so
498 ** that the current working directory is set correctly.
500 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
502 ** NB that some snprintf() return -1 on overflow rather than the
503 ** new C99 mandated 'number of chars that would have been written'
506 ** length checks here and inside the loop allow for the
507 ** trailing ';', trailcmd, '"' and NUL
509 int trailln = strlen(trailcmd) + 3;
510 if (ln < 0 || ln + trailln > bufsz)
511 adios(NULL, "arguments too long");
517 /* +1 for leading space */
518 if (ln + trailln + 1 > bufsz - (cp-buf))
519 adios(NULL, "arguments too long");
521 memcpy(cp, *argp, ln+1);
526 strcpy(cp, trailcmd);
534 ** Build a command line that causes the user's shell to list the file name
535 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
538 writelscmd(char *buf, int bufsz, char **argp)
540 writesomecmd(buf, bufsz, "ls", "", argp);
544 ** Like system(), but run the command in directory dir.
545 ** This assumes the program is single-threaded!
548 system_in_dir(const char *dir, const char *cmd)
552 if (getcwd(olddir, sizeof(olddir)) == 0)
553 adios("getcwd", "could not get working directory");
555 adios("chdir", "could not change working directory");
557 if (chdir(olddir) != 0)
558 adios("chdir", "could not change working directory");
562 /* ditto for popen() */
564 popen_in_dir(const char *dir, const char *cmd, const char *type)
568 if (getcwd(olddir, sizeof(olddir)) == 0)
569 adios("getcwd", "could not get working directory");
571 adios("chdir", "could not change working directory");
572 f = popen(cmd, type);
573 if (chdir(olddir) != 0)
574 adios("chdir", "could not change working directory");
583 static int reedit = 0; /* have we been here before? */
584 static char *edsave = NULL; /* the editor we used previously */
588 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
589 char *altmsg, char *cwd, int save_editor)
591 int pid, status, vecp;
592 char altpath[BUFSIZ], linkpath[BUFSIZ];
593 char *cp, *vec[MAXARGS];
598 #endif /* HAVE_LSTAT */
600 /* Was there a previous edit session? */
602 if (!*ed) { /* no explicit editor */
603 *ed = edsave; /* so use the previous one */
604 if (!(cp = mhbasename(*ed)))
607 /* unless we've specified it via "editor-next" */
608 cp = concat(cp, "-next", NULL);
609 if ((cp = context_find(cp)))
613 /* set initial editor */
618 if (!mp || *altmsg == '/' || !cwd)
619 strncpy(altpath, altmsg, sizeof(altpath));
621 snprintf(altpath, sizeof(altpath), "%s/%s",
622 mp->foldpath, altmsg);
624 strncpy(linkpath, altmsglink, sizeof(linkpath));
626 snprintf(linkpath, sizeof(linkpath), "%s/%s",
633 if (link(altpath, linkpath) == NOTOK) {
634 symlink(altpath, linkpath);
639 #else /* not HAVE_LSTAT */
640 link(altpath, linkpath);
641 #endif /* not HAVE_LSTAT */
644 context_save(); /* save the context file */
647 switch (pid = fork()) {
649 advise("fork", "unable to");
658 m_putenv("mhfolder", mp->foldpath);
659 m_putenv("editalt", altpath);
663 vec[vecp++] = mhbasename(*ed);
666 vec[vecp++] = *arg++;
671 fprintf(stderr, "unable to exec ");
676 if ((status = pidwait(pid, NOTOK))) {
677 if (((status & 0xff00) != 0xff00)
678 && (!reedit || (status & 0x00ff))) {
679 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
680 advise(NULL, "problems with edit--draft left in %s", cp);
682 advise(NULL, "problems with edit--%s preserved", file);
685 status = -2; /* maybe "reedit ? -2 : -1"? */
691 if (altmsg && mp && !is_readonly(mp) && (slinked ?
692 lstat (linkpath, &st) != NOTOK &&
693 S_ISREG(st.st_mode) &&
694 copyf(linkpath, altpath) == NOTOK :
695 stat(linkpath, &st) != NOTOK &&
697 (unlink(altpath) == NOTOK ||
698 link(linkpath, altpath) == NOTOK)))
699 advise(linkpath, "unable to update %s from", altmsg);
700 #else /* HAVE_LSTAT */
701 if (altmsg && mp && !is_readonly(mp) &&
702 stat(linkpath, &st) != NOTOK &&
704 (unlink(altpath) == NOTOK ||
705 link(linkpath, altpath) == NOTOK))
706 advise(linkpath, "unable to update %s from", altmsg);
707 #endif /* HAVE_LSTAT */
710 /* normally, we remember which editor we used */
712 edsave = getcpy(*ed);
724 copyf(char *ifile, char *ofile)
729 if ((in = open(ifile, O_RDONLY)) == NOTOK)
731 if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
732 admonish(ofile, "unable to open and truncate");
737 while ((i = read(in, buffer, sizeof(buffer))) > OK)
738 if (write(out, buffer, i) != i) {
739 advise(ofile, "may have damaged");
748 #endif /* HAVE_LSTAT */
756 sendfile(char **arg, char *file, int pushsw)
762 context_save(); /* save the context file */
765 switch (child_id = fork()) {
767 advise(NULL, "unable to fork, so sending directly...");
771 vec[vecp++] = invo_name;
773 vec[vecp++] = "-push";
776 vec[vecp++] = *arg++;
781 fprintf(stderr, "unable to exec ");
786 if (pidwait(child_id, OK) == 0)
794 ** Remove the draft file
798 removefile(char *drft)
800 if (unlink(drft) == NOTOK)
801 adios(drft, "unable to unlink");