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.
15 static struct swit switches[] = {
17 { "editor editor", 0 },
19 { "prompt string", 0 },
28 ** Options at the "whatnow" prompt
30 static struct swit aleqs[] = {
32 { "edit [editor [switches]]", 0 },
42 { "refile +folder", 0 },
48 { "cd [directory]", 0 },
55 #define ATTACHCMDSW 12
56 { "attach files", 0 },
57 #define DETACHCMDSW 13
58 { "detach numbers", 0 },
62 static char *myprompt = "\nWhat now? ";
67 static int editfile(char **, char **, char *, int);
68 static int sendfile(char **, char *);
69 static int refile(char **, char *);
70 static int removefile(char *);
71 static void writelscmd(char *, int, char **);
72 static void writesomecmd(char *, int, char *, char *, char **);
73 static FILE* popen_in_dir(const char *, const char *, const char *);
74 static int system_in_dir(const char *, const char *);
78 main(int argc, char **argv)
82 char *ed = NULL, *drft = NULL;
83 char buf[BUFSIZ], prompt[BUFSIZ];
84 char **argp, **arguments;
86 char cwd[MAXPATHLEN + 1]; /* current working directory */
87 char file[MAXPATHLEN + 1]; /* file name buffer */
88 char shell[MAXPATHLEN + 1]; /* shell response buffer */
89 FILE *f; /* read pointer for bgnd proc */
91 setlocale(LC_ALL, "");
92 invo_name = mhbasename(argv[0]);
94 /* read user profile/context */
97 arguments = getarguments(invo_name, argc, argv, 1);
101 ** Get the initial current working directory.
104 if (!getcwd(cwd, sizeof (cwd))) {
105 adios("getcwd", "could not get working directory");
108 while ((cp = *argp++)) {
110 switch (smatch(++cp, switches)) {
112 ambigsw(cp, switches);
115 adios(NULL, "-%s unknown", cp);
118 snprintf(buf, sizeof(buf),
119 "%s [switches] [file]",
121 print_help(buf, switches, 1);
124 print_version(invo_name);
128 if (!(ed = *argp++) || *ed == '-')
129 adios(NULL, "missing argument to %s",
134 if (!(myprompt = *argp++) || *myprompt == '-')
135 adios(NULL, "missing argument to %s",
142 adios(NULL, "only one draft at a time!");
147 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
148 drft = getcpy(m_draft(seq_cur));
150 if ((cp = getenv("mhuse")) && *cp)
153 if (!ed && !(ed = getenv("mheditor"))) {
154 ed = ""; /* Don't initially edit the draft */
157 /* start editing the draft, unless editor is the empty string */
159 if (editfile(&ed, NULL, drft, use) <0) {
163 advise(NULL, "Try again.");
168 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
170 if (!(argp = getans(prompt, aleqs))) {
173 switch (smatch(*argp, aleqs)) {
175 /* display the msg being replied to or distributed */
176 if ((cp = getenv("mhaltmsg")) && *cp) {
177 execprogl(listproc, listproc, "-file", cp,
180 advise(NULL, "no alternate message to display");
185 /* Call an editor on the draft file */
188 editfile(&ed, argp, drft, NOUSE);
192 /* display the draft file */
193 execprogl(listproc, listproc, "-file", drft,
199 if (stat(drft, &st) != NOTOK) {
200 advise(NULL, "draft left on %s", drft);
205 /* Delete draft and exit */
211 sendfile(++argp, drft);
215 /* Refile the draft */
216 if (refile(++argp, drft) == 0) {
223 ** Change the working directory for attachments
225 ** Run the directory through the user's shell
226 ** so that we can take advantage of any syntax
227 ** that the user is accustomed to. Read back
228 ** the absolute path.
231 if (*(argp+1) == NULL) {
232 sprintf(buf, "$SHELL -c \"cd;pwd\"");
234 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
236 if ((f = popen_in_dir(cwd, buf, "r"))) {
237 fgets(cwd, sizeof (cwd), f);
239 if (strchr(cwd, '\n'))
240 *strchr(cwd, '\n') = '\0';
244 advise("popen", "could not get directory");
250 /* Print the working directory for attachments */
256 ** List files in the current attachment working
259 ** Use the user's shell so that we can take
260 ** advantage of any syntax that the user is
263 writelscmd(buf, sizeof(buf), argp);
264 system_in_dir(cwd, buf);
269 ** List attachments on current draft.
271 if (execprogl("anno", "anno", "-list", "-comp",
272 attach_hdr, "-number", drft,
273 (char *)NULL) != 0) {
274 advise(NULL, "Could not list attachment headers.");
280 ** Attach files to current draft.
283 if (*(argp+1) == NULL) {
284 advise(NULL, "attach command requires file argument(s).");
289 ** Build a command line that causes the user's
290 ** shell to list the file name arguments.
291 ** This handles and wildcard expansion, tilde
294 writelscmd(buf, sizeof(buf), argp);
297 ** Read back the response from the shell,
298 ** which contains a number of lines with one
299 ** file name per line. Remove off the newline.
300 ** Determine whether we have an absolute or
301 ** relative path name. Prepend the current
302 ** working directory to relative path names.
303 ** Add the attachment annotation to the draft.
305 if (!(f = popen_in_dir(cwd, buf, "r"))) {
306 advise("popen", "could not get file from shell");
310 while (fgets(shell, sizeof(shell), f)) {
311 *(strchr(shell, '\n')) = '\0';
314 sprintf(file, "%s", shell);
316 sprintf(file, "%s/%s", cwd, shell);
318 if (execprogl("anno", "anno",
319 "-nodate", "-append",
322 drft, (char *)NULL) != 0) {
323 advise(NULL, "Could not add attachment header.");
331 ** Detach files from current draft.
333 ** Interpret the arguments as
334 ** attachment numbers. Decrement any remaining
335 ** argument number that is greater than the one
336 ** just processed after processing each one so
337 ** that the numbering stays correct.
339 for (arguments=argp+1; *arguments; arguments++) {
342 if (**arguments == '\0') {
346 if (execprogl("anno", "anno", "-delete",
348 "-number", *arguments, drft,
349 (char *)NULL) != 0) {
350 advise(NULL, "Could not delete attachment header.");
353 n = atoi(*arguments);
354 for (argp=arguments+1; *argp; argp++) {
355 if (atoi(*argp) > n) {
356 if (atoi(*argp) == 1) {
359 sprintf(*argp, "%d", atoi(*argp) - 1);
367 /* list recipients */
368 execprogl("whom", "whom", drft, (char *)NULL);
372 /* Unknown command */
373 advise(NULL, "say what?");
383 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
387 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
391 ** Note that we do not quote -- the argp from the user
392 ** is assumed to be quoted as they desire. (We can't treat
393 ** it as pure literal as that would prevent them using ~,
394 ** wildcards, etc.) The buffer produced by this function
395 ** should be given to popen_in_dir() or system_in_dir() so
396 ** that the current working directory is set correctly.
398 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
400 ** NB that some snprintf() return -1 on overflow rather than the
401 ** new C99 mandated 'number of chars that would have been written'
404 ** length checks here and inside the loop allow for the
405 ** trailing ';', trailcmd, '"' and NUL
407 int trailln = strlen(trailcmd) + 3;
408 if (ln < 0 || ln + trailln > bufsz)
409 adios(NULL, "arguments too long");
415 /* +1 for leading space */
416 if (ln + trailln + 1 > bufsz - (cp-buf))
417 adios(NULL, "arguments too long");
419 memcpy(cp, *argp, ln+1);
424 strcpy(cp, trailcmd);
432 ** Build a command line that causes the user's shell to list the file name
433 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
436 writelscmd(char *buf, int bufsz, char **argp)
438 writesomecmd(buf, bufsz, "ls", "", argp);
442 ** Like system(), but run the command in directory dir.
443 ** This assumes the program is single-threaded!
446 system_in_dir(const char *dir, const char *cmd)
450 if (getcwd(olddir, sizeof(olddir)) == 0)
451 adios("getcwd", "could not get working directory");
453 adios("chdir", "could not change working directory");
455 if (chdir(olddir) != 0)
456 adios("chdir", "could not change working directory");
460 /* ditto for popen() */
462 popen_in_dir(const char *dir, const char *cmd, const char *type)
466 if (getcwd(olddir, sizeof(olddir)) == 0)
467 adios("getcwd", "could not get working directory");
469 adios("chdir", "could not change working directory");
470 f = popen(cmd, type);
471 if (chdir(olddir) != 0)
472 adios("chdir", "could not change working directory");
481 static char *edsave = NULL; /* the editor we used previously */
485 editfile(char **ed, char **arg, char *file, int use)
487 int pid, status, vecp;
488 char *cp, *vec[MAXARGS];
491 /* We have no explicit editor. */
493 /* Use the previous editor ... */
495 if (!(cp = mhbasename(*ed)))
498 /* but prefer one specified via "editor-next" */
499 cp = concat(cp, "-next", NULL);
500 if ((cp = context_find(cp)))
503 /* set initial editor */
511 switch (pid = fork()) {
513 advise("fork", "unable to");
519 vec[vecp++] = mhbasename(*ed);
522 vec[vecp++] = *arg++;
527 fprintf(stderr, "%s: unable to exec ", invo_name);
532 if ((status = pidwait(pid, NOTOK))) {
533 if ((status & 0xff00) == 0xff00) {
534 /* cmd not found or pidwait() failed */
538 if (status & 0x00ff) {
539 /* terminated by signal */
540 advise(NULL, "%s terminated by signal %d",
544 advise(NULL, "%s exited with return code %d",
545 *ed, (status & 0xff00) >> 8);
552 /* remember which editor we used */
553 edsave = getcpy(*ed);
566 sendfile(char **arg, char *file)
574 vec[vecp++] = "send";
575 while (arg && *arg) {
576 vec[vecp++] = *arg++;
581 fprintf(stderr, "%s: unable to exec ", invo_name);
588 ** refile msg into another folder
591 refile(char **arg, char *file)
598 vec[vecp++] = "refile";
599 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
600 vec[vecp++] = "-file";
603 while (arg && *arg) {
604 vec[vecp++] = *arg++;
608 context_save(); /* save the context file */
611 switch (pid = fork()) {
613 advise("fork", "unable to");
618 fprintf(stderr, "%s: unable to exec ", invo_name);
623 return (pidwait(pid, -1));
629 ** Remove the draft file
633 removefile(char *drft)
635 if (unlink(drft) == NOTOK)
636 adios(drft, "unable to unlink");