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", 0 },
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);
96 static int sendfile(char **, char *);
97 static int refile(char **, char *);
98 static int removefile(char *);
99 static void writelscmd(char *, int, char **);
100 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
101 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
102 static int system_in_dir(const char *dir, const char *cmd);
106 main(int argc, char **argv)
110 char *ed = NULL, *drft = NULL;
111 char buf[BUFSIZ], prompt[BUFSIZ];
112 char **argp, **arguments;
114 char cwd[MAXPATHLEN + 1]; /* current working directory */
115 char file[MAXPATHLEN + 1]; /* file name buffer */
116 char shell[MAXPATHLEN + 1]; /* shell response buffer */
117 FILE *f; /* read pointer for bgnd proc */
118 int n; /* set on -n to detach command */
121 setlocale(LC_ALL, "");
124 invo_name = mhbasename(argv[0]);
126 /* read user profile/context */
129 arguments = getarguments(invo_name, argc, argv, 1);
133 ** Get the initial current working directory.
136 if (!getcwd(cwd, sizeof (cwd))) {
137 adios("getcwd", "could not get working directory");
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, whatnowswitches)) {
144 ambigsw(cp, whatnowswitches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf),
151 "%s [switches] [file]",
153 print_help(buf, whatnowswitches, 1);
156 print_version(invo_name);
160 if (!(ed = *argp++) || *ed == '-')
161 adios(NULL, "missing argument to %s",
166 if (!(myprompt = *argp++) || *myprompt == '-')
167 adios(NULL, "missing argument to %s",
174 adios(NULL, "only one draft at a time!");
179 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
180 drft = getcpy(m_draft(seq_cur));
182 if ((cp = getenv("mhuse")) && *cp)
185 if (!ed && !(ed = getenv("mheditor"))) {
186 ed = ""; /* Don't initially edit the draft */
189 /* start editing the draft, unless editor is the empty string */
191 if (editfile(&ed, NULL, drft, use) <0) {
195 advise(NULL, "Try again.");
200 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
202 if (!(argp = getans(prompt, aleqs))) {
205 switch (smatch(*argp, aleqs)) {
207 /* display the msg being replied to or distributed */
208 if ((cp = getenv("mhaltmsg")) && *cp) {
209 snprintf(buf, sizeof buf, "%s '%s'",
213 advise(NULL, "no alternate message to display");
218 /* Call an editor on the draft file */
221 editfile(&ed, argp, drft, NOUSE);
225 /* display the draft file */
226 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
232 if (stat(drft, &st) != NOTOK) {
233 advise(NULL, "draft left on %s", drft);
238 /* Delete draft and exit */
244 sendfile(++argp, drft);
248 /* Refile the draft */
249 if (refile(++argp, drft) == 0) {
256 ** Change the working directory for attachments
258 ** Run the directory through the user's shell
259 ** so that we can take advantage of any syntax
260 ** that the user is accustomed to. Read back
261 ** the absolute path.
264 if (*(argp+1) == NULL) {
265 sprintf(buf, "$SHELL -c \"cd;pwd\"");
267 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
269 if ((f = popen_in_dir(cwd, buf, "r"))) {
270 fgets(cwd, sizeof (cwd), f);
272 if (strchr(cwd, '\n'))
273 *strchr(cwd, '\n') = '\0';
277 advise("popen", "could not get directory");
283 /* Print the working directory for attachments */
289 ** List files in the current attachment working
292 ** Use the user's shell so that we can take
293 ** advantage of any syntax that the user is
296 writelscmd(buf, sizeof(buf), argp);
297 system_in_dir(cwd, buf);
302 ** List attachments on current draft.
304 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
305 "-number all -text IGNORE '%s'",
307 if (system(buf) != 0) {
308 advise(NULL, "Could not list attachment headers.");
314 ** Attach files to current draft.
317 if (*(argp+1) == NULL) {
318 advise(NULL, "attach command requires file argument(s).");
323 ** Build a command line that causes the user's
324 ** shell to list the file name arguments.
325 ** This handles and wildcard expansion, tilde
328 writelscmd(buf, sizeof(buf), argp);
331 ** Read back the response from the shell,
332 ** which contains a number of lines with one
333 ** file name per line. Remove off the newline.
334 ** Determine whether we have an absolute or
335 ** relative path name. Prepend the current
336 ** working directory to relative path names.
337 ** Add the attachment annotation to the draft.
339 if ((f = popen_in_dir(cwd, buf, "r"))) {
342 while (fgets(shell, sizeof(shell), f)) {
343 *(strchr(shell, '\n')) = '\0';
346 sprintf(file, "%s", shell);
348 sprintf(file, "%s/%s", cwd,
351 snprintf(buf, sizeof buf,
352 "anno -nodate -append "
353 "-comp '%s' -text '%s'"
357 if (system(buf) != 0) {
358 advise(NULL, "Could not add attachment header.");
364 advise("popen", "could not get file from shell");
371 ** Detach files from current draft.
373 ** Scan the arguments for a -n. Mixed file
374 ** names and numbers aren't allowed, so this
375 ** catches a -n anywhere in the argument list.
377 for (n = 0, arguments = argp + 1;
380 if (strcmp(*arguments, "-n") == 0) {
387 ** A -n was found so interpret the arguments as
388 ** attachment numbers. Decrement any remaining
389 ** argument number that is greater than the one
390 ** just processed after processing each one so
391 ** that the numbering stays correct.
394 for (arguments=argp+1; *arguments;
396 if (strcmp(*arguments, "-n") == 0)
399 if (**arguments != '\0') {
402 n = atoi(*arguments);
403 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
404 if (system(buf) != 0) {
405 advise(NULL, "Could not delete attachment header.");
408 for (argp=arguments+1; *argp;
410 if (atoi(*argp) > n) {
411 if (atoi(*argp) == 1)
414 sprintf(*argp, "%d", atoi(*argp) - 1);
424 ** The arguments are interpreted as file names.
425 ** Run them through the user's shell for wildcard
426 ** expansion and other goodies. Do this from
427 ** the current working directory if the argument
428 ** is not an absolute path name (does not begin
431 ** We feed all the file names to the shell at
432 ** once, otherwise you can't provide a file name
433 ** with a space in it.
435 writelscmd(buf, sizeof(buf), argp);
436 if ((f = popen_in_dir(cwd, buf, "r"))) {
437 while (fgets(shell, sizeof (shell), f)) {
438 *(strchr(shell, '\n')) = '\0';
439 snprintf(buf, sizeof buf,
440 "anno -delete -comp "
441 "'%s' -text '%s' '%s'",
444 if (system(buf) != 0) {
445 advise(NULL, "Could not delete attachment header.");
450 advise("popen", "could not get file from shell");
456 /* Unknown command */
457 advise(NULL, "say what?");
467 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
471 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
475 ** Note that we do not quote -- the argp from the user
476 ** is assumed to be quoted as they desire. (We can't treat
477 ** it as pure literal as that would prevent them using ~,
478 ** wildcards, etc.) The buffer produced by this function
479 ** should be given to popen_in_dir() or system_in_dir() so
480 ** that the current working directory is set correctly.
482 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
484 ** NB that some snprintf() return -1 on overflow rather than the
485 ** new C99 mandated 'number of chars that would have been written'
488 ** length checks here and inside the loop allow for the
489 ** trailing ';', trailcmd, '"' and NUL
491 int trailln = strlen(trailcmd) + 3;
492 if (ln < 0 || ln + trailln > bufsz)
493 adios(NULL, "arguments too long");
499 /* +1 for leading space */
500 if (ln + trailln + 1 > bufsz - (cp-buf))
501 adios(NULL, "arguments too long");
503 memcpy(cp, *argp, ln+1);
508 strcpy(cp, trailcmd);
516 ** Build a command line that causes the user's shell to list the file name
517 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
520 writelscmd(char *buf, int bufsz, char **argp)
522 writesomecmd(buf, bufsz, "ls", "", argp);
526 ** Like system(), but run the command in directory dir.
527 ** This assumes the program is single-threaded!
530 system_in_dir(const char *dir, const char *cmd)
534 if (getcwd(olddir, sizeof(olddir)) == 0)
535 adios("getcwd", "could not get working directory");
537 adios("chdir", "could not change working directory");
539 if (chdir(olddir) != 0)
540 adios("chdir", "could not change working directory");
544 /* ditto for popen() */
546 popen_in_dir(const char *dir, const char *cmd, const char *type)
550 if (getcwd(olddir, sizeof(olddir)) == 0)
551 adios("getcwd", "could not get working directory");
553 adios("chdir", "could not change working directory");
554 f = popen(cmd, type);
555 if (chdir(olddir) != 0)
556 adios("chdir", "could not change working directory");
565 static char *edsave = NULL; /* the editor we used previously */
569 editfile(char **ed, char **arg, char *file, int use)
571 int pid, status, vecp;
572 char *cp, *vec[MAXARGS];
575 /* We have no explicit editor. */
577 /* Use the previous editor ... */
579 if (!(cp = mhbasename(*ed)))
582 /* but prefer one specified via "editor-next" */
583 cp = concat(cp, "-next", NULL);
584 if ((cp = context_find(cp)))
587 /* set initial editor */
595 switch (pid = fork()) {
597 advise("fork", "unable to");
603 vec[vecp++] = mhbasename(*ed);
606 vec[vecp++] = *arg++;
611 fprintf(stderr, "%s: unable to exec ", invo_name);
616 if ((status = pidwait(pid, NOTOK))) {
617 if ((status & 0xff00) == 0xff00) {
618 /* cmd not found or pidwait() failed */
622 if (status & 0x00ff) {
623 /* terminated by signal */
624 advise(NULL, "%s terminated by signal %d",
628 advise(NULL, "%s exited with return code %d",
629 *ed, (status & 0xff00) >> 8);
636 /* remember which editor we used */
637 edsave = getcpy(*ed);
650 sendfile(char **arg, char *file)
656 context_save(); /* save the context file */
659 switch (child_id = fork()) {
661 advise(NULL, "unable to fork, so sending directly...");
665 vec[vecp++] = invo_name;
668 vec[vecp++] = *arg++;
673 fprintf(stderr, "%s: unable to exec ", invo_name);
678 if (pidwait(child_id, OK) == 0)
686 ** refile msg into another folder
689 refile(char **arg, char *file)
696 vec[vecp++] = "refile";
697 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
698 vec[vecp++] = "-file";
701 while (arg && *arg) {
702 vec[vecp++] = *arg++;
706 context_save(); /* save the context file */
709 switch (pid = fork()) {
711 advise("fork", "unable to");
716 fprintf(stderr, "%s: unable to exec ", invo_name);
721 return (pidwait(pid, -1));
727 ** Remove the draft file
731 removefile(char *drft)
733 if (unlink(drft) == NOTOK)
734 adios(drft, "unable to unlink");