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 */
120 setlocale(LC_ALL, "");
121 invo_name = mhbasename(argv[0]);
123 /* read user profile/context */
126 arguments = getarguments(invo_name, argc, argv, 1);
130 ** Get the initial current working directory.
133 if (!getcwd(cwd, sizeof (cwd))) {
134 adios("getcwd", "could not get working directory");
137 while ((cp = *argp++)) {
139 switch (smatch(++cp, whatnowswitches)) {
141 ambigsw(cp, whatnowswitches);
144 adios(NULL, "-%s unknown", cp);
147 snprintf(buf, sizeof(buf),
148 "%s [switches] [file]",
150 print_help(buf, whatnowswitches, 1);
153 print_version(invo_name);
157 if (!(ed = *argp++) || *ed == '-')
158 adios(NULL, "missing argument to %s",
163 if (!(myprompt = *argp++) || *myprompt == '-')
164 adios(NULL, "missing argument to %s",
171 adios(NULL, "only one draft at a time!");
176 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
177 drft = getcpy(m_draft(seq_cur));
179 if ((cp = getenv("mhuse")) && *cp)
182 if (!ed && !(ed = getenv("mheditor"))) {
183 ed = ""; /* Don't initially edit the draft */
186 /* start editing the draft, unless editor is the empty string */
188 if (editfile(&ed, NULL, drft, use) <0) {
192 advise(NULL, "Try again.");
197 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
199 if (!(argp = getans(prompt, aleqs))) {
202 switch (smatch(*argp, aleqs)) {
204 /* display the msg being replied to or distributed */
205 if ((cp = getenv("mhaltmsg")) && *cp) {
206 snprintf(buf, sizeof buf, "%s '%s'",
210 advise(NULL, "no alternate message to display");
215 /* Call an editor on the draft file */
218 editfile(&ed, argp, drft, NOUSE);
222 /* display the draft file */
223 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
229 if (stat(drft, &st) != NOTOK) {
230 advise(NULL, "draft left on %s", drft);
235 /* Delete draft and exit */
241 sendfile(++argp, drft);
245 /* Refile the draft */
246 if (refile(++argp, drft) == 0) {
253 ** Change the working directory for attachments
255 ** Run the directory through the user's shell
256 ** so that we can take advantage of any syntax
257 ** that the user is accustomed to. Read back
258 ** the absolute path.
261 if (*(argp+1) == NULL) {
262 sprintf(buf, "$SHELL -c \"cd;pwd\"");
264 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
266 if ((f = popen_in_dir(cwd, buf, "r"))) {
267 fgets(cwd, sizeof (cwd), f);
269 if (strchr(cwd, '\n'))
270 *strchr(cwd, '\n') = '\0';
274 advise("popen", "could not get directory");
280 /* Print the working directory for attachments */
286 ** List files in the current attachment working
289 ** Use the user's shell so that we can take
290 ** advantage of any syntax that the user is
293 writelscmd(buf, sizeof(buf), argp);
294 system_in_dir(cwd, buf);
299 ** List attachments on current draft.
301 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
302 "-number all -text IGNORE '%s'",
304 if (system(buf) != 0) {
305 advise(NULL, "Could not list attachment headers.");
311 ** Attach files to current draft.
314 if (*(argp+1) == NULL) {
315 advise(NULL, "attach command requires file argument(s).");
320 ** Build a command line that causes the user's
321 ** shell to list the file name arguments.
322 ** This handles and wildcard expansion, tilde
325 writelscmd(buf, sizeof(buf), argp);
328 ** Read back the response from the shell,
329 ** which contains a number of lines with one
330 ** file name per line. Remove off the newline.
331 ** Determine whether we have an absolute or
332 ** relative path name. Prepend the current
333 ** working directory to relative path names.
334 ** Add the attachment annotation to the draft.
336 if ((f = popen_in_dir(cwd, buf, "r"))) {
339 while (fgets(shell, sizeof(shell), f)) {
340 *(strchr(shell, '\n')) = '\0';
343 sprintf(file, "%s", shell);
345 sprintf(file, "%s/%s", cwd,
348 snprintf(buf, sizeof buf,
349 "anno -nodate -append "
350 "-comp '%s' -text '%s'"
354 if (system(buf) != 0) {
355 advise(NULL, "Could not add attachment header.");
361 advise("popen", "could not get file from shell");
368 ** Detach files from current draft.
370 ** Scan the arguments for a -n. Mixed file
371 ** names and numbers aren't allowed, so this
372 ** catches a -n anywhere in the argument list.
374 for (n = 0, arguments = argp + 1;
377 if (strcmp(*arguments, "-n") == 0) {
384 ** A -n was found so interpret the arguments as
385 ** attachment numbers. Decrement any remaining
386 ** argument number that is greater than the one
387 ** just processed after processing each one so
388 ** that the numbering stays correct.
391 for (arguments=argp+1; *arguments;
393 if (strcmp(*arguments, "-n") == 0)
396 if (**arguments != '\0') {
399 n = atoi(*arguments);
400 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
401 if (system(buf) != 0) {
402 advise(NULL, "Could not delete attachment header.");
405 for (argp=arguments+1; *argp;
407 if (atoi(*argp) > n) {
408 if (atoi(*argp) == 1)
411 sprintf(*argp, "%d", atoi(*argp) - 1);
421 ** The arguments are interpreted as file names.
422 ** Run them through the user's shell for wildcard
423 ** expansion and other goodies. Do this from
424 ** the current working directory if the argument
425 ** is not an absolute path name (does not begin
428 ** We feed all the file names to the shell at
429 ** once, otherwise you can't provide a file name
430 ** with a space in it.
432 writelscmd(buf, sizeof(buf), argp);
433 if ((f = popen_in_dir(cwd, buf, "r"))) {
434 while (fgets(shell, sizeof (shell), f)) {
435 *(strchr(shell, '\n')) = '\0';
436 snprintf(buf, sizeof buf,
437 "anno -delete -comp "
438 "'%s' -text '%s' '%s'",
441 if (system(buf) != 0) {
442 advise(NULL, "Could not delete attachment header.");
447 advise("popen", "could not get file from shell");
453 /* Unknown command */
454 advise(NULL, "say what?");
464 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
468 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
472 ** Note that we do not quote -- the argp from the user
473 ** is assumed to be quoted as they desire. (We can't treat
474 ** it as pure literal as that would prevent them using ~,
475 ** wildcards, etc.) The buffer produced by this function
476 ** should be given to popen_in_dir() or system_in_dir() so
477 ** that the current working directory is set correctly.
479 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
481 ** NB that some snprintf() return -1 on overflow rather than the
482 ** new C99 mandated 'number of chars that would have been written'
485 ** length checks here and inside the loop allow for the
486 ** trailing ';', trailcmd, '"' and NUL
488 int trailln = strlen(trailcmd) + 3;
489 if (ln < 0 || ln + trailln > bufsz)
490 adios(NULL, "arguments too long");
496 /* +1 for leading space */
497 if (ln + trailln + 1 > bufsz - (cp-buf))
498 adios(NULL, "arguments too long");
500 memcpy(cp, *argp, ln+1);
505 strcpy(cp, trailcmd);
513 ** Build a command line that causes the user's shell to list the file name
514 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
517 writelscmd(char *buf, int bufsz, char **argp)
519 writesomecmd(buf, bufsz, "ls", "", argp);
523 ** Like system(), but run the command in directory dir.
524 ** This assumes the program is single-threaded!
527 system_in_dir(const char *dir, const char *cmd)
531 if (getcwd(olddir, sizeof(olddir)) == 0)
532 adios("getcwd", "could not get working directory");
534 adios("chdir", "could not change working directory");
536 if (chdir(olddir) != 0)
537 adios("chdir", "could not change working directory");
541 /* ditto for popen() */
543 popen_in_dir(const char *dir, const char *cmd, const char *type)
547 if (getcwd(olddir, sizeof(olddir)) == 0)
548 adios("getcwd", "could not get working directory");
550 adios("chdir", "could not change working directory");
551 f = popen(cmd, type);
552 if (chdir(olddir) != 0)
553 adios("chdir", "could not change working directory");
562 static char *edsave = NULL; /* the editor we used previously */
566 editfile(char **ed, char **arg, char *file, int use)
568 int pid, status, vecp;
569 char *cp, *vec[MAXARGS];
572 /* We have no explicit editor. */
574 /* Use the previous editor ... */
576 if (!(cp = mhbasename(*ed)))
579 /* but prefer one specified via "editor-next" */
580 cp = concat(cp, "-next", NULL);
581 if ((cp = context_find(cp)))
584 /* set initial editor */
592 switch (pid = fork()) {
594 advise("fork", "unable to");
600 vec[vecp++] = mhbasename(*ed);
603 vec[vecp++] = *arg++;
608 fprintf(stderr, "%s: unable to exec ", invo_name);
613 if ((status = pidwait(pid, NOTOK))) {
614 if ((status & 0xff00) == 0xff00) {
615 /* cmd not found or pidwait() failed */
619 if (status & 0x00ff) {
620 /* terminated by signal */
621 advise(NULL, "%s terminated by signal %d",
625 advise(NULL, "%s exited with return code %d",
626 *ed, (status & 0xff00) >> 8);
633 /* remember which editor we used */
634 edsave = getcpy(*ed);
647 sendfile(char **arg, char *file)
653 context_save(); /* save the context file */
656 switch (child_id = fork()) {
658 advise(NULL, "unable to fork, so sending directly...");
662 vec[vecp++] = invo_name;
665 vec[vecp++] = *arg++;
670 fprintf(stderr, "%s: unable to exec ", invo_name);
675 if (pidwait(child_id, OK) == 0)
683 ** refile msg into another folder
686 refile(char **arg, char *file)
693 vec[vecp++] = "refile";
694 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
695 vec[vecp++] = "-file";
698 while (arg && *arg) {
699 vec[vecp++] = *arg++;
703 context_save(); /* save the context file */
706 switch (pid = fork()) {
708 advise("fork", "unable to");
713 fprintf(stderr, "%s: unable to exec ", invo_name);
718 return (pidwait(pid, -1));
724 ** Remove the draft file
728 removefile(char *drft)
730 if (unlink(drft) == NOTOK)
731 adios(drft, "unable to unlink");