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 },
35 char *version=VERSION;
38 ** Options at the "whatnow" prompt
40 static struct swit aleqs[] = {
42 { "edit [editor [switches]]", 0 },
52 { "refile +folder", 0 },
58 { "cd [directory]", 0 },
65 #define ATTACHCMDSW 12
66 { "attach files", 0 },
67 #define DETACHCMDSW 13
68 { "detach numbers", 0 },
72 static char *myprompt = "\nWhat now? ";
77 static int editfile(char **, char **, char *);
78 static int sendfile(char **, char *);
79 static int refile(char **, char *);
80 static int removefile(char *);
81 static void writelscmd(char *, int, char **);
82 static void writesomecmd(char *, int, char *, char *, char **);
83 static FILE* popen_in_dir(const char *, const char *, const char *);
84 static int system_in_dir(const char *, const char *);
88 main(int argc, char **argv)
92 char *ed = NULL, *drft = NULL;
93 char buf[BUFSIZ], prompt[BUFSIZ];
94 char **argp, **arguments;
96 char cwd[MAXPATHLEN + 1]; /* current working directory */
97 char file[MAXPATHLEN + 1]; /* file name buffer */
98 char shell[MAXPATHLEN + 1]; /* shell response buffer */
99 FILE *f; /* read pointer for bgnd proc */
101 setlocale(LC_ALL, "");
102 invo_name = mhbasename(argv[0]);
104 /* read user profile/context */
107 advise(NULL, "whatnow is deprecated. Consider switching to whatnow2.");
109 arguments = getarguments(invo_name, argc, argv, 1);
113 ** Get the initial current working directory.
116 if (!getcwd(cwd, sizeof (cwd))) {
117 adios(EX_USAGE, "getcwd", "could not get working directory");
120 while ((cp = *argp++)) {
122 switch (smatch(++cp, switches)) {
124 ambigsw(cp, switches);
127 adios(EX_USAGE, NULL, "-%s unknown", cp);
130 snprintf(buf, sizeof(buf),
131 "%s [switches] [file]",
133 print_help(buf, switches, 1);
134 exit(argc == 2 ? EX_OK : EX_USAGE);
136 print_version(invo_name);
137 exit(argc == 2 ? EX_OK : EX_USAGE);
140 if (!(ed = *argp++) || *ed == '-')
141 adios(EX_USAGE, NULL, "missing argument to %s",
146 if (!(myprompt = *argp++) || *myprompt == '-')
147 adios(EX_USAGE, NULL, "missing argument to %s",
154 adios(EX_USAGE, NULL, "only one draft at a time!");
159 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
160 drft = mh_xstrdup(m_draft(seq_cur));
162 if ((cp = getenv("mhuse")) && *cp)
165 if (!ed && !(ed = getenv("mheditor"))) {
166 ed = ""; /* Don't initially edit the draft */
169 /* start editing the draft, unless editor is the empty string */
171 if (editfile(&ed, NULL, drft) <0) {
175 adios(EX_SOFTWARE, NULL, "Try again.");
179 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
180 while ((argp = getans(prompt, aleqs))) {
181 switch (smatch(*argp, aleqs)) {
183 /* display the msg being replied to or distributed */
184 if ((cp = getenv("mhaltmsg")) && *cp) {
185 execprogl(listproc, listproc, "-file", cp,
188 advise(NULL, "no alternate message to display");
193 /* Call an editor on the draft file */
196 editfile(&ed, argp, drft);
200 /* display the draft file */
201 execprogl(listproc, listproc, "-file", drft,
207 if (stat(drft, &st) != NOTOK) {
208 advise(NULL, "draft left on %s", drft);
213 /* Delete draft and exit */
219 sendfile(++argp, drft);
223 /* Refile the draft */
224 if (refile(++argp, drft) == 0) {
231 ** Change the working directory for attachments
233 ** Run the directory through the user's shell
234 ** so that we can take advantage of any syntax
235 ** that the user is accustomed to. Read back
236 ** the absolute path.
239 if (*(argp+1) == NULL) {
240 sprintf(buf, "$SHELL -c \"cd;pwd\"");
242 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
244 if ((f = popen_in_dir(cwd, buf, "r"))) {
245 fgets(cwd, sizeof (cwd), f);
247 if (strchr(cwd, '\n'))
248 *strchr(cwd, '\n') = '\0';
252 advise("popen", "could not get directory");
258 /* Print the working directory for attachments */
264 ** List files in the current attachment working
267 ** Use the user's shell so that we can take
268 ** advantage of any syntax that the user is
271 writelscmd(buf, sizeof(buf), argp);
272 system_in_dir(cwd, buf);
277 ** List attachments on current draft.
279 if (execprogl("anno", "anno", "-list", "-comp",
280 attach_hdr, "-number", drft,
281 (char *)NULL) != 0) {
282 advise(NULL, "Could not list attachment headers.");
288 ** Attach files to current draft.
291 if (*(argp+1) == NULL) {
292 advise(NULL, "attach command requires file argument(s).");
297 ** Build a command line that causes the user's
298 ** shell to list the file name arguments.
299 ** This handles and wildcard expansion, tilde
302 writelscmd(buf, sizeof(buf), argp);
305 ** Read back the response from the shell,
306 ** which contains a number of lines with one
307 ** file name per line. Remove off the newline.
308 ** Determine whether we have an absolute or
309 ** relative path name. Prepend the current
310 ** working directory to relative path names.
311 ** Add the attachment annotation to the draft.
313 if (!(f = popen_in_dir(cwd, buf, "r"))) {
314 advise("popen", "could not get file from shell");
318 while (fgets(shell, sizeof(shell), f)) {
319 *(strchr(shell, '\n')) = '\0';
322 sprintf(file, "%s", shell);
324 sprintf(file, "%s/%s", cwd, shell);
326 if (execprogl("anno", "anno",
327 "-nodate", "-append",
330 drft, (char *)NULL) != 0) {
331 advise(NULL, "Could not add attachment header.");
339 ** Detach files from current draft.
341 ** Interpret the arguments as
342 ** attachment numbers. Decrement any remaining
343 ** argument number that is greater than the one
344 ** just processed after processing each one so
345 ** that the numbering stays correct.
347 for (arguments=argp+1; *arguments; arguments++) {
350 if (**arguments == '\0') {
354 if (execprogl("anno", "anno", "-delete",
356 "-number", *arguments, drft,
357 (char *)NULL) != 0) {
358 advise(NULL, "Could not delete attachment header.");
361 n = atoi(*arguments);
362 for (argp=arguments+1; *argp; argp++) {
363 if (atoi(*argp) > n) {
364 if (atoi(*argp) == 1) {
367 sprintf(*argp, "%d", atoi(*argp) - 1);
375 /* list recipients */
376 execprogl("whom", "whom", drft, (char *)NULL);
380 /* Unknown command */
381 advise(NULL, "say what?");
392 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
396 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
400 ** Note that we do not quote -- the argp from the user
401 ** is assumed to be quoted as they desire. (We can't treat
402 ** it as pure literal as that would prevent them using ~,
403 ** wildcards, etc.) The buffer produced by this function
404 ** should be given to popen_in_dir() or system_in_dir() so
405 ** that the current working directory is set correctly.
407 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
409 ** NB that some snprintf() return -1 on overflow rather than the
410 ** new C99 mandated 'number of chars that would have been written'
413 ** length checks here and inside the loop allow for the
414 ** trailing ';', trailcmd, '"' and NUL
416 int trailln = strlen(trailcmd) + 3;
417 if (ln < 0 || ln + trailln > bufsz)
418 adios(EX_USAGE, NULL, "arguments too long");
424 /* +1 for leading space */
425 if (ln + trailln + 1 > bufsz - (cp-buf))
426 adios(EX_USAGE, NULL, "arguments too long");
428 memcpy(cp, *argp, ln+1);
433 strcpy(cp, trailcmd);
441 ** Build a command line that causes the user's shell to list the file name
442 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
445 writelscmd(char *buf, int bufsz, char **argp)
447 writesomecmd(buf, bufsz, "ls", "", argp);
451 ** Like system(), but run the command in directory dir.
452 ** This assumes the program is single-threaded!
455 system_in_dir(const char *dir, const char *cmd)
459 if (getcwd(olddir, sizeof(olddir)) == 0)
460 adios(EX_OSERR, "getcwd", "could not get working directory");
462 adios(EX_OSERR, "chdir", "could not change working directory");
464 if (chdir(olddir) != 0)
465 adios(EX_OSERR, "chdir", "could not change working directory");
469 /* ditto for popen() */
471 popen_in_dir(const char *dir, const char *cmd, const char *type)
475 if (getcwd(olddir, sizeof(olddir)) == 0)
476 adios(EX_OSERR, "getcwd", "could not get working directory");
478 adios(EX_OSERR, "chdir", "could not change working directory");
479 f = popen(cmd, type);
480 if (chdir(olddir) != 0)
481 adios(EX_OSERR, "chdir", "could not change working directory");
490 static char *edsave = NULL; /* the editor we used previously */
494 editfile(char **ed, char **arg, char *file)
496 int pid, status, vecp;
497 char *cp, *vec[MAXARGS];
500 /* We have no explicit editor. */
502 /* Use the previous editor ... */
504 if (!(cp = mhbasename(*ed)))
507 /* but prefer one specified via "editor-next" */
508 cp = concat(cp, "-next", NULL);
509 if ((cp = context_find(cp)))
512 /* set initial editor */
520 switch (pid = fork()) {
522 advise("fork", "unable to");
528 vec[vecp++] = mhbasename(*ed);
529 while (arg && *arg) {
530 vec[vecp++] = *arg++;
536 fprintf(stderr, "%s: unable to exec ", invo_name);
541 if ((status = pidwait(pid, NOTOK))) {
542 if ((status & 0xff00) == 0xff00) {
543 /* cmd not found or pidwait() failed */
544 status = EX_SOFTWARE;
547 if (status & 0x00ff) {
548 /* terminated by signal */
549 advise(NULL, "%s terminated by signal %d",
553 advise(NULL, "%s exited with return code %d",
554 *ed, (status & 0xff00) >> 8);
561 /* remember which editor we used */
562 edsave = mh_xstrdup(*ed);
575 sendfile(char **arg, char *file)
583 vec[vecp++] = "send";
584 while (arg && *arg) {
585 vec[vecp++] = *arg++;
590 fprintf(stderr, "%s: unable to exec ", invo_name);
597 ** refile msg into another folder
600 refile(char **arg, char *file)
605 vec[vecp++] = "refile";
606 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
607 vec[vecp++] = "-file";
610 while (arg && *arg) {
611 vec[vecp++] = *arg++;
618 return execprog(*vec, vec);
623 ** Remove the draft file
627 removefile(char *drft)
629 if (unlink(drft) == NOTOK)
630 adios(EX_IOERR, drft, "unable to unlink");