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.
19 #ifdef HAVE_SYS_PARAM_H
20 # include <sys/param.h>
23 static struct swit switches[] = {
25 { "editor editor", 0 },
27 { "prompt string", 0 },
36 ** Options at the "whatnow" prompt
38 static struct swit aleqs[] = {
40 { "edit [editor [switches]]", 0 },
50 { "refile +folder", 0 },
56 { "cd [directory]", 0 },
63 #define ATTACHCMDSW 12
64 { "attach files", 0 },
65 #define DETACHCMDSW 13
66 { "detach numbers", 0 },
70 static char *myprompt = "\nWhat now? ";
75 static int editfile(char **, char **, char *);
76 static int sendfile(char **, char *);
77 static int refile(char **, char *);
78 static int removefile(char *);
79 static void writelscmd(char *, int, char **);
80 static void writesomecmd(char *, int, char *, char *, char **);
81 static FILE* popen_in_dir(const char *, const char *, const char *);
82 static int system_in_dir(const char *, const char *);
86 main(int argc, char **argv)
90 char *ed = NULL, *drft = NULL;
91 char buf[BUFSIZ], prompt[BUFSIZ];
92 char **argp, **arguments;
94 char cwd[MAXPATHLEN + 1]; /* current working directory */
95 char file[MAXPATHLEN + 1]; /* file name buffer */
96 char shell[MAXPATHLEN + 1]; /* shell response buffer */
97 FILE *f; /* read pointer for bgnd proc */
99 setlocale(LC_ALL, "");
100 invo_name = mhbasename(argv[0]);
102 /* read user profile/context */
105 arguments = getarguments(invo_name, argc, argv, 1);
109 ** Get the initial current working directory.
112 if (!getcwd(cwd, sizeof (cwd))) {
113 adios(EX_USAGE, "getcwd", "could not get working directory");
116 while ((cp = *argp++)) {
118 switch (smatch(++cp, switches)) {
120 ambigsw(cp, switches);
123 adios(EX_USAGE, NULL, "-%s unknown", cp);
126 snprintf(buf, sizeof(buf),
127 "%s [switches] [file]",
129 print_help(buf, switches, 1);
130 exit(argc == 2 ? EX_OK : EX_USAGE);
132 print_version(invo_name);
133 exit(argc == 2 ? EX_OK : EX_USAGE);
136 if (!(ed = *argp++) || *ed == '-')
137 adios(EX_USAGE, NULL, "missing argument to %s",
142 if (!(myprompt = *argp++) || *myprompt == '-')
143 adios(EX_USAGE, NULL, "missing argument to %s",
150 adios(EX_USAGE, NULL, "only one draft at a time!");
155 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
156 drft = mh_xstrdup(m_draft(seq_cur));
158 if ((cp = getenv("mhuse")) && *cp)
161 if (!ed && !(ed = getenv("mheditor"))) {
162 ed = ""; /* Don't initially edit the draft */
165 /* start editing the draft, unless editor is the empty string */
167 if (editfile(&ed, NULL, drft) <0) {
171 adios(EX_SOFTWARE, NULL, "Try again.");
175 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
176 while ((argp = getans(prompt, aleqs))) {
177 switch (smatch(*argp, aleqs)) {
179 /* display the msg being replied to or distributed */
180 if ((cp = getenv("mhaltmsg")) && *cp) {
181 execprogl(listproc, listproc, "-file", cp,
184 advise(NULL, "no alternate message to display");
189 /* Call an editor on the draft file */
192 editfile(&ed, argp, drft);
196 /* display the draft file */
197 execprogl(listproc, listproc, "-file", drft,
203 if (stat(drft, &st) != NOTOK) {
204 advise(NULL, "draft left on %s", drft);
209 /* Delete draft and exit */
215 sendfile(++argp, drft);
219 /* Refile the draft */
220 if (refile(++argp, drft) == 0) {
227 ** Change the working directory for attachments
229 ** Run the directory through the user's shell
230 ** so that we can take advantage of any syntax
231 ** that the user is accustomed to. Read back
232 ** the absolute path.
235 if (*(argp+1) == NULL) {
236 sprintf(buf, "$SHELL -c \"cd;pwd\"");
238 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
240 if ((f = popen_in_dir(cwd, buf, "r"))) {
241 fgets(cwd, sizeof (cwd), f);
243 if (strchr(cwd, '\n'))
244 *strchr(cwd, '\n') = '\0';
248 advise("popen", "could not get directory");
254 /* Print the working directory for attachments */
260 ** List files in the current attachment working
263 ** Use the user's shell so that we can take
264 ** advantage of any syntax that the user is
267 writelscmd(buf, sizeof(buf), argp);
268 system_in_dir(cwd, buf);
273 ** List attachments on current draft.
275 if (execprogl("anno", "anno", "-list", "-comp",
276 attach_hdr, "-number", drft,
277 (char *)NULL) != 0) {
278 advise(NULL, "Could not list attachment headers.");
284 ** Attach files to current draft.
287 if (*(argp+1) == NULL) {
288 advise(NULL, "attach command requires file argument(s).");
293 ** Build a command line that causes the user's
294 ** shell to list the file name arguments.
295 ** This handles and wildcard expansion, tilde
298 writelscmd(buf, sizeof(buf), argp);
301 ** Read back the response from the shell,
302 ** which contains a number of lines with one
303 ** file name per line. Remove off the newline.
304 ** Determine whether we have an absolute or
305 ** relative path name. Prepend the current
306 ** working directory to relative path names.
307 ** Add the attachment annotation to the draft.
309 if (!(f = popen_in_dir(cwd, buf, "r"))) {
310 advise("popen", "could not get file from shell");
314 while (fgets(shell, sizeof(shell), f)) {
315 *(strchr(shell, '\n')) = '\0';
318 sprintf(file, "%s", shell);
320 sprintf(file, "%s/%s", cwd, shell);
322 if (execprogl("anno", "anno",
323 "-nodate", "-append",
326 drft, (char *)NULL) != 0) {
327 advise(NULL, "Could not add attachment header.");
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?");
388 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
392 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
396 ** Note that we do not quote -- the argp from the user
397 ** is assumed to be quoted as they desire. (We can't treat
398 ** it as pure literal as that would prevent them using ~,
399 ** wildcards, etc.) The buffer produced by this function
400 ** should be given to popen_in_dir() or system_in_dir() so
401 ** that the current working directory is set correctly.
403 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
405 ** NB that some snprintf() return -1 on overflow rather than the
406 ** new C99 mandated 'number of chars that would have been written'
409 ** length checks here and inside the loop allow for the
410 ** trailing ';', trailcmd, '"' and NUL
412 int trailln = strlen(trailcmd) + 3;
413 if (ln < 0 || ln + trailln > bufsz)
414 adios(EX_USAGE, NULL, "arguments too long");
420 /* +1 for leading space */
421 if (ln + trailln + 1 > bufsz - (cp-buf))
422 adios(EX_USAGE, NULL, "arguments too long");
424 memcpy(cp, *argp, ln+1);
429 strcpy(cp, trailcmd);
437 ** Build a command line that causes the user's shell to list the file name
438 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
441 writelscmd(char *buf, int bufsz, char **argp)
443 writesomecmd(buf, bufsz, "ls", "", argp);
447 ** Like system(), but run the command in directory dir.
448 ** This assumes the program is single-threaded!
451 system_in_dir(const char *dir, const char *cmd)
455 if (getcwd(olddir, sizeof(olddir)) == 0)
456 adios(EX_OSERR, "getcwd", "could not get working directory");
458 adios(EX_OSERR, "chdir", "could not change working directory");
460 if (chdir(olddir) != 0)
461 adios(EX_OSERR, "chdir", "could not change working directory");
465 /* ditto for popen() */
467 popen_in_dir(const char *dir, const char *cmd, const char *type)
471 if (getcwd(olddir, sizeof(olddir)) == 0)
472 adios(EX_OSERR, "getcwd", "could not get working directory");
474 adios(EX_OSERR, "chdir", "could not change working directory");
475 f = popen(cmd, type);
476 if (chdir(olddir) != 0)
477 adios(EX_OSERR, "chdir", "could not change working directory");
486 static char *edsave = NULL; /* the editor we used previously */
490 editfile(char **ed, char **arg, char *file)
492 int pid, status, vecp;
493 char *cp, *vec[MAXARGS];
496 /* We have no explicit editor. */
498 /* Use the previous editor ... */
500 if (!(cp = mhbasename(*ed)))
503 /* but prefer one specified via "editor-next" */
504 cp = concat(cp, "-next", NULL);
505 if ((cp = context_find(cp)))
508 /* set initial editor */
516 switch (pid = fork()) {
518 advise("fork", "unable to");
524 vec[vecp++] = mhbasename(*ed);
525 while (arg && *arg) {
526 vec[vecp++] = *arg++;
532 fprintf(stderr, "%s: unable to exec ", invo_name);
537 if ((status = pidwait(pid, NOTOK))) {
538 if ((status & 0xff00) == 0xff00) {
539 /* cmd not found or pidwait() failed */
540 status = EX_SOFTWARE;
543 if (status & 0x00ff) {
544 /* terminated by signal */
545 advise(NULL, "%s terminated by signal %d",
549 advise(NULL, "%s exited with return code %d",
550 *ed, (status & 0xff00) >> 8);
557 /* remember which editor we used */
558 edsave = mh_xstrdup(*ed);
571 sendfile(char **arg, char *file)
579 vec[vecp++] = "send";
580 while (arg && *arg) {
581 vec[vecp++] = *arg++;
586 fprintf(stderr, "%s: unable to exec ", invo_name);
593 ** refile msg into another folder
596 refile(char **arg, char *file)
601 vec[vecp++] = "refile";
602 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
603 vec[vecp++] = "-file";
606 while (arg && *arg) {
607 vec[vecp++] = *arg++;
614 return execprog(*vec, vec);
619 ** Remove the draft file
623 removefile(char *drft)
625 if (unlink(drft) == NOTOK)
626 adios(EX_IOERR, drft, "unable to unlink");