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 arguments = getarguments(invo_name, argc, argv, 1);
111 ** Get the initial current working directory.
114 if (!getcwd(cwd, sizeof (cwd))) {
115 adios(EX_USAGE, "getcwd", "could not get working directory");
118 while ((cp = *argp++)) {
120 switch (smatch(++cp, switches)) {
122 ambigsw(cp, switches);
125 adios(EX_USAGE, NULL, "-%s unknown", cp);
128 snprintf(buf, sizeof(buf),
129 "%s [switches] [file]",
131 print_help(buf, switches, 1);
132 exit(argc == 2 ? EX_OK : EX_USAGE);
134 print_version(invo_name);
135 exit(argc == 2 ? EX_OK : EX_USAGE);
138 if (!(ed = *argp++) || *ed == '-')
139 adios(EX_USAGE, NULL, "missing argument to %s",
144 if (!(myprompt = *argp++) || *myprompt == '-')
145 adios(EX_USAGE, NULL, "missing argument to %s",
152 adios(EX_USAGE, NULL, "only one draft at a time!");
157 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
158 drft = mh_xstrdup(m_draft(seq_cur));
160 if ((cp = getenv("mhuse")) && *cp)
163 if (!ed && !(ed = getenv("mheditor"))) {
164 ed = ""; /* Don't initially edit the draft */
167 /* start editing the draft, unless editor is the empty string */
169 if (editfile(&ed, NULL, drft) <0) {
173 adios(EX_SOFTWARE, NULL, "Try again.");
177 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
178 while ((argp = getans(prompt, aleqs))) {
179 switch (smatch(*argp, aleqs)) {
181 /* display the msg being replied to or distributed */
182 if ((cp = getenv("mhaltmsg")) && *cp) {
183 execprogl(listproc, listproc, "-file", cp,
186 advise(NULL, "no alternate message to display");
191 /* Call an editor on the draft file */
194 editfile(&ed, argp, drft);
198 /* display the draft file */
199 execprogl(listproc, listproc, "-file", drft,
205 if (stat(drft, &st) != NOTOK) {
206 advise(NULL, "draft left on %s", drft);
211 /* Delete draft and exit */
217 sendfile(++argp, drft);
221 /* Refile the draft */
222 if (refile(++argp, drft) == 0) {
229 ** Change the working directory for attachments
231 ** Run the directory through the user's shell
232 ** so that we can take advantage of any syntax
233 ** that the user is accustomed to. Read back
234 ** the absolute path.
237 if (*(argp+1) == NULL) {
238 sprintf(buf, "$SHELL -c \"cd;pwd\"");
240 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
242 if ((f = popen_in_dir(cwd, buf, "r"))) {
243 fgets(cwd, sizeof (cwd), f);
245 if (strchr(cwd, '\n'))
246 *strchr(cwd, '\n') = '\0';
250 advise("popen", "could not get directory");
256 /* Print the working directory for attachments */
262 ** List files in the current attachment working
265 ** Use the user's shell so that we can take
266 ** advantage of any syntax that the user is
269 writelscmd(buf, sizeof(buf), argp);
270 system_in_dir(cwd, buf);
275 ** List attachments on current draft.
277 if (execprogl("anno", "anno", "-list", "-comp",
278 attach_hdr, "-number", drft,
279 (char *)NULL) != 0) {
280 advise(NULL, "Could not list attachment headers.");
286 ** Attach files to current draft.
289 if (*(argp+1) == NULL) {
290 advise(NULL, "attach command requires file argument(s).");
295 ** Build a command line that causes the user's
296 ** shell to list the file name arguments.
297 ** This handles and wildcard expansion, tilde
300 writelscmd(buf, sizeof(buf), argp);
303 ** Read back the response from the shell,
304 ** which contains a number of lines with one
305 ** file name per line. Remove off the newline.
306 ** Determine whether we have an absolute or
307 ** relative path name. Prepend the current
308 ** working directory to relative path names.
309 ** Add the attachment annotation to the draft.
311 if (!(f = popen_in_dir(cwd, buf, "r"))) {
312 advise("popen", "could not get file from shell");
316 while (fgets(shell, sizeof(shell), f)) {
317 *(strchr(shell, '\n')) = '\0';
320 sprintf(file, "%s", shell);
322 sprintf(file, "%s/%s", cwd, shell);
324 if (execprogl("anno", "anno",
325 "-nodate", "-append",
328 drft, (char *)NULL) != 0) {
329 advise(NULL, "Could not add attachment header.");
337 ** Detach files from current draft.
339 ** Interpret the arguments as
340 ** attachment numbers. Decrement any remaining
341 ** argument number that is greater than the one
342 ** just processed after processing each one so
343 ** that the numbering stays correct.
345 for (arguments=argp+1; *arguments; arguments++) {
348 if (**arguments == '\0') {
352 if (execprogl("anno", "anno", "-delete",
354 "-number", *arguments, drft,
355 (char *)NULL) != 0) {
356 advise(NULL, "Could not delete attachment header.");
359 n = atoi(*arguments);
360 for (argp=arguments+1; *argp; argp++) {
361 if (atoi(*argp) > n) {
362 if (atoi(*argp) == 1) {
365 sprintf(*argp, "%d", atoi(*argp) - 1);
373 /* list recipients */
374 execprogl("whom", "whom", drft, (char *)NULL);
378 /* Unknown command */
379 advise(NULL, "say what?");
390 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
394 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
398 ** Note that we do not quote -- the argp from the user
399 ** is assumed to be quoted as they desire. (We can't treat
400 ** it as pure literal as that would prevent them using ~,
401 ** wildcards, etc.) The buffer produced by this function
402 ** should be given to popen_in_dir() or system_in_dir() so
403 ** that the current working directory is set correctly.
405 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
407 ** NB that some snprintf() return -1 on overflow rather than the
408 ** new C99 mandated 'number of chars that would have been written'
411 ** length checks here and inside the loop allow for the
412 ** trailing ';', trailcmd, '"' and NUL
414 int trailln = strlen(trailcmd) + 3;
415 if (ln < 0 || ln + trailln > bufsz)
416 adios(EX_USAGE, NULL, "arguments too long");
422 /* +1 for leading space */
423 if (ln + trailln + 1 > bufsz - (cp-buf))
424 adios(EX_USAGE, NULL, "arguments too long");
426 memcpy(cp, *argp, ln+1);
431 strcpy(cp, trailcmd);
439 ** Build a command line that causes the user's shell to list the file name
440 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
443 writelscmd(char *buf, int bufsz, char **argp)
445 writesomecmd(buf, bufsz, "ls", "", argp);
449 ** Like system(), but run the command in directory dir.
450 ** This assumes the program is single-threaded!
453 system_in_dir(const char *dir, const char *cmd)
457 if (getcwd(olddir, sizeof(olddir)) == 0)
458 adios(EX_OSERR, "getcwd", "could not get working directory");
460 adios(EX_OSERR, "chdir", "could not change working directory");
462 if (chdir(olddir) != 0)
463 adios(EX_OSERR, "chdir", "could not change working directory");
467 /* ditto for popen() */
469 popen_in_dir(const char *dir, const char *cmd, const char *type)
473 if (getcwd(olddir, sizeof(olddir)) == 0)
474 adios(EX_OSERR, "getcwd", "could not get working directory");
476 adios(EX_OSERR, "chdir", "could not change working directory");
477 f = popen(cmd, type);
478 if (chdir(olddir) != 0)
479 adios(EX_OSERR, "chdir", "could not change working directory");
488 static char *edsave = NULL; /* the editor we used previously */
492 editfile(char **ed, char **arg, char *file)
494 int pid, status, vecp;
495 char *cp, *vec[MAXARGS];
498 /* We have no explicit editor. */
500 /* Use the previous editor ... */
502 if (!(cp = mhbasename(*ed)))
505 /* but prefer one specified via "editor-next" */
506 cp = concat(cp, "-next", NULL);
507 if ((cp = context_find(cp)))
510 /* set initial editor */
518 switch (pid = fork()) {
520 advise("fork", "unable to");
526 vec[vecp++] = mhbasename(*ed);
527 while (arg && *arg) {
528 vec[vecp++] = *arg++;
534 fprintf(stderr, "%s: unable to exec ", invo_name);
539 if ((status = pidwait(pid, NOTOK))) {
540 if ((status & 0xff00) == 0xff00) {
541 /* cmd not found or pidwait() failed */
542 status = EX_SOFTWARE;
545 if (status & 0x00ff) {
546 /* terminated by signal */
547 advise(NULL, "%s terminated by signal %d",
551 advise(NULL, "%s exited with return code %d",
552 *ed, (status & 0xff00) >> 8);
559 /* remember which editor we used */
560 edsave = mh_xstrdup(*ed);
573 sendfile(char **arg, char *file)
581 vec[vecp++] = "send";
582 while (arg && *arg) {
583 vec[vecp++] = *arg++;
588 fprintf(stderr, "%s: unable to exec ", invo_name);
595 ** refile msg into another folder
598 refile(char **arg, char *file)
603 vec[vecp++] = "refile";
604 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
605 vec[vecp++] = "-file";
608 while (arg && *arg) {
609 vec[vecp++] = *arg++;
616 return execprog(*vec, vec);
621 ** Remove the draft file
625 removefile(char *drft)
627 if (unlink(drft) == NOTOK)
628 adios(EX_IOERR, drft, "unable to unlink");