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 = getcpy(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);
177 if (!(argp = getans(prompt, aleqs))) {
180 switch (smatch(*argp, aleqs)) {
182 /* display the msg being replied to or distributed */
183 if ((cp = getenv("mhaltmsg")) && *cp) {
184 execprogl(listproc, listproc, "-file", cp,
187 advise(NULL, "no alternate message to display");
192 /* Call an editor on the draft file */
195 editfile(&ed, argp, drft);
199 /* display the draft file */
200 execprogl(listproc, listproc, "-file", drft,
206 if (stat(drft, &st) != NOTOK) {
207 advise(NULL, "draft left on %s", drft);
212 /* Delete draft and exit */
218 sendfile(++argp, drft);
222 /* Refile the draft */
223 if (refile(++argp, drft) == 0) {
230 ** Change the working directory for attachments
232 ** Run the directory through the user's shell
233 ** so that we can take advantage of any syntax
234 ** that the user is accustomed to. Read back
235 ** the absolute path.
238 if (*(argp+1) == NULL) {
239 sprintf(buf, "$SHELL -c \"cd;pwd\"");
241 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
243 if ((f = popen_in_dir(cwd, buf, "r"))) {
244 fgets(cwd, sizeof (cwd), f);
246 if (strchr(cwd, '\n'))
247 *strchr(cwd, '\n') = '\0';
251 advise("popen", "could not get directory");
257 /* Print the working directory for attachments */
263 ** List files in the current attachment working
266 ** Use the user's shell so that we can take
267 ** advantage of any syntax that the user is
270 writelscmd(buf, sizeof(buf), argp);
271 system_in_dir(cwd, buf);
276 ** List attachments on current draft.
278 if (execprogl("anno", "anno", "-list", "-comp",
279 attach_hdr, "-number", drft,
280 (char *)NULL) != 0) {
281 advise(NULL, "Could not list attachment headers.");
287 ** Attach files to current draft.
290 if (*(argp+1) == NULL) {
291 advise(NULL, "attach command requires file argument(s).");
296 ** Build a command line that causes the user's
297 ** shell to list the file name arguments.
298 ** This handles and wildcard expansion, tilde
301 writelscmd(buf, sizeof(buf), argp);
304 ** Read back the response from the shell,
305 ** which contains a number of lines with one
306 ** file name per line. Remove off the newline.
307 ** Determine whether we have an absolute or
308 ** relative path name. Prepend the current
309 ** working directory to relative path names.
310 ** Add the attachment annotation to the draft.
312 if (!(f = popen_in_dir(cwd, buf, "r"))) {
313 advise("popen", "could not get file from shell");
317 while (fgets(shell, sizeof(shell), f)) {
318 *(strchr(shell, '\n')) = '\0';
321 sprintf(file, "%s", shell);
323 sprintf(file, "%s/%s", cwd, shell);
325 if (execprogl("anno", "anno",
326 "-nodate", "-append",
329 drft, (char *)NULL) != 0) {
330 advise(NULL, "Could not add attachment header.");
338 ** Detach files from current draft.
340 ** Interpret the arguments as
341 ** attachment numbers. Decrement any remaining
342 ** argument number that is greater than the one
343 ** just processed after processing each one so
344 ** that the numbering stays correct.
346 for (arguments=argp+1; *arguments; arguments++) {
349 if (**arguments == '\0') {
353 if (execprogl("anno", "anno", "-delete",
355 "-number", *arguments, drft,
356 (char *)NULL) != 0) {
357 advise(NULL, "Could not delete attachment header.");
360 n = atoi(*arguments);
361 for (argp=arguments+1; *argp; argp++) {
362 if (atoi(*argp) > n) {
363 if (atoi(*argp) == 1) {
366 sprintf(*argp, "%d", atoi(*argp) - 1);
374 /* list recipients */
375 execprogl("whom", "whom", drft, (char *)NULL);
379 /* Unknown command */
380 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 = getcpy(*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");