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 while (fgets(shell, sizeof(shell), f)) {
307 *(strchr(shell, '\n')) = '\0';
310 sprintf(file, "%s", shell);
312 sprintf(file, "%s/%s", cwd,
315 if (execprogl("anno", "anno",
322 advise(NULL, "Could not add attachment header.");
328 advise("popen", "could not get file from shell");
335 ** Detach files from current draft.
337 ** Interpret the arguments as
338 ** attachment numbers. Decrement any remaining
339 ** argument number that is greater than the one
340 ** just processed after processing each one so
341 ** that the numbering stays correct.
343 for (arguments=argp+1; *arguments; arguments++) {
346 if (**arguments == '\0') {
350 if (execprogl("anno", "anno", "-delete",
352 "-number", *arguments, drft,
353 (char *)NULL) != 0) {
354 advise(NULL, "Could not delete attachment header.");
357 n = atoi(*arguments);
358 for (argp=arguments+1; *argp; argp++) {
359 if (atoi(*argp) > n) {
360 if (atoi(*argp) == 1) {
363 sprintf(*argp, "%d", atoi(*argp) - 1);
371 /* list recipients */
372 execprogl("whom", "whom", drft, (char *)NULL);
376 /* Unknown command */
377 advise(NULL, "say what?");
387 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
391 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
395 ** Note that we do not quote -- the argp from the user
396 ** is assumed to be quoted as they desire. (We can't treat
397 ** it as pure literal as that would prevent them using ~,
398 ** wildcards, etc.) The buffer produced by this function
399 ** should be given to popen_in_dir() or system_in_dir() so
400 ** that the current working directory is set correctly.
402 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
404 ** NB that some snprintf() return -1 on overflow rather than the
405 ** new C99 mandated 'number of chars that would have been written'
408 ** length checks here and inside the loop allow for the
409 ** trailing ';', trailcmd, '"' and NUL
411 int trailln = strlen(trailcmd) + 3;
412 if (ln < 0 || ln + trailln > bufsz)
413 adios(NULL, "arguments too long");
419 /* +1 for leading space */
420 if (ln + trailln + 1 > bufsz - (cp-buf))
421 adios(NULL, "arguments too long");
423 memcpy(cp, *argp, ln+1);
428 strcpy(cp, trailcmd);
436 ** Build a command line that causes the user's shell to list the file name
437 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
440 writelscmd(char *buf, int bufsz, char **argp)
442 writesomecmd(buf, bufsz, "ls", "", argp);
446 ** Like system(), but run the command in directory dir.
447 ** This assumes the program is single-threaded!
450 system_in_dir(const char *dir, const char *cmd)
454 if (getcwd(olddir, sizeof(olddir)) == 0)
455 adios("getcwd", "could not get working directory");
457 adios("chdir", "could not change working directory");
459 if (chdir(olddir) != 0)
460 adios("chdir", "could not change working directory");
464 /* ditto for popen() */
466 popen_in_dir(const char *dir, const char *cmd, const char *type)
470 if (getcwd(olddir, sizeof(olddir)) == 0)
471 adios("getcwd", "could not get working directory");
473 adios("chdir", "could not change working directory");
474 f = popen(cmd, type);
475 if (chdir(olddir) != 0)
476 adios("chdir", "could not change working directory");
485 static char *edsave = NULL; /* the editor we used previously */
489 editfile(char **ed, char **arg, char *file, int use)
491 int pid, status, vecp;
492 char *cp, *vec[MAXARGS];
495 /* We have no explicit editor. */
497 /* Use the previous editor ... */
499 if (!(cp = mhbasename(*ed)))
502 /* but prefer one specified via "editor-next" */
503 cp = concat(cp, "-next", NULL);
504 if ((cp = context_find(cp)))
507 /* set initial editor */
515 switch (pid = fork()) {
517 advise("fork", "unable to");
523 vec[vecp++] = mhbasename(*ed);
526 vec[vecp++] = *arg++;
531 fprintf(stderr, "%s: unable to exec ", invo_name);
536 if ((status = pidwait(pid, NOTOK))) {
537 if ((status & 0xff00) == 0xff00) {
538 /* cmd not found or pidwait() failed */
542 if (status & 0x00ff) {
543 /* terminated by signal */
544 advise(NULL, "%s terminated by signal %d",
548 advise(NULL, "%s exited with return code %d",
549 *ed, (status & 0xff00) >> 8);
556 /* remember which editor we used */
557 edsave = getcpy(*ed);
570 sendfile(char **arg, char *file)
576 context_save(); /* save the context file */
579 switch (child_id = fork()) {
581 advise(NULL, "unable to fork, so sending directly...");
585 vec[vecp++] = "send";
588 vec[vecp++] = *arg++;
593 fprintf(stderr, "%s: unable to exec ", invo_name);
598 if (pidwait(child_id, OK) == 0)
606 ** refile msg into another folder
609 refile(char **arg, char *file)
616 vec[vecp++] = "refile";
617 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
618 vec[vecp++] = "-file";
621 while (arg && *arg) {
622 vec[vecp++] = *arg++;
626 context_save(); /* save the context file */
629 switch (pid = fork()) {
631 advise("fork", "unable to");
636 fprintf(stderr, "%s: unable to exec ", invo_name);
641 return (pidwait(pid, -1));
647 ** Remove the draft file
651 removefile(char *drft)
653 if (unlink(drft) == NOTOK)
654 adios(drft, "unable to unlink");