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 snprintf(buf, sizeof buf, "%s '%s'",
181 advise(NULL, "no alternate message to display");
186 /* Call an editor on the draft file */
189 editfile(&ed, argp, drft, NOUSE);
193 /* display the draft file */
194 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
200 if (stat(drft, &st) != NOTOK) {
201 advise(NULL, "draft left on %s", drft);
206 /* Delete draft and exit */
212 sendfile(++argp, drft);
216 /* Refile the draft */
217 if (refile(++argp, drft) == 0) {
224 ** Change the working directory for attachments
226 ** Run the directory through the user's shell
227 ** so that we can take advantage of any syntax
228 ** that the user is accustomed to. Read back
229 ** the absolute path.
232 if (*(argp+1) == NULL) {
233 sprintf(buf, "$SHELL -c \"cd;pwd\"");
235 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
237 if ((f = popen_in_dir(cwd, buf, "r"))) {
238 fgets(cwd, sizeof (cwd), f);
240 if (strchr(cwd, '\n'))
241 *strchr(cwd, '\n') = '\0';
245 advise("popen", "could not get directory");
251 /* Print the working directory for attachments */
257 ** List files in the current attachment working
260 ** Use the user's shell so that we can take
261 ** advantage of any syntax that the user is
264 writelscmd(buf, sizeof(buf), argp);
265 system_in_dir(cwd, buf);
270 ** List attachments on current draft.
272 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
273 "-number '%s'", attach_hdr, drft);
274 if (system(buf) != 0) {
275 advise(NULL, "Could not list attachment headers.");
281 ** Attach files to current draft.
284 if (*(argp+1) == NULL) {
285 advise(NULL, "attach command requires file argument(s).");
290 ** Build a command line that causes the user's
291 ** shell to list the file name arguments.
292 ** This handles and wildcard expansion, tilde
295 writelscmd(buf, sizeof(buf), argp);
298 ** Read back the response from the shell,
299 ** which contains a number of lines with one
300 ** file name per line. Remove off the newline.
301 ** Determine whether we have an absolute or
302 ** relative path name. Prepend the current
303 ** working directory to relative path names.
304 ** Add the attachment annotation to the draft.
306 if ((f = popen_in_dir(cwd, buf, "r"))) {
309 while (fgets(shell, sizeof(shell), f)) {
310 *(strchr(shell, '\n')) = '\0';
313 sprintf(file, "%s", shell);
315 sprintf(file, "%s/%s", cwd,
318 snprintf(buf, sizeof buf,
319 "anno -nodate -append "
320 "-comp '%s' -text '%s'"
324 if (system(buf) != 0) {
325 advise(NULL, "Could not add attachment header.");
331 advise("popen", "could not get file from shell");
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++) {
350 if (**arguments == '\0') {
354 n = atoi(*arguments);
355 snprintf(buf, sizeof buf, "anno -delete "
356 "-comp '%s' -number '%d' "
358 attach_hdr, n, drft);
359 if (system(buf) != 0) {
360 advise(NULL, "Could not delete attachment header.");
363 for (argp=arguments+1; *argp; argp++) {
364 if (atoi(*argp) > n) {
365 if (atoi(*argp) == 1) {
368 sprintf(*argp, "%d", atoi(*argp) - 1);
376 /* list recipients */
377 snprintf(buf, sizeof buf, "%s '%s'", "whom", drft);
382 /* Unknown command */
383 advise(NULL, "say what?");
393 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
397 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
401 ** Note that we do not quote -- the argp from the user
402 ** is assumed to be quoted as they desire. (We can't treat
403 ** it as pure literal as that would prevent them using ~,
404 ** wildcards, etc.) The buffer produced by this function
405 ** should be given to popen_in_dir() or system_in_dir() so
406 ** that the current working directory is set correctly.
408 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
410 ** NB that some snprintf() return -1 on overflow rather than the
411 ** new C99 mandated 'number of chars that would have been written'
414 ** length checks here and inside the loop allow for the
415 ** trailing ';', trailcmd, '"' and NUL
417 int trailln = strlen(trailcmd) + 3;
418 if (ln < 0 || ln + trailln > bufsz)
419 adios(NULL, "arguments too long");
425 /* +1 for leading space */
426 if (ln + trailln + 1 > bufsz - (cp-buf))
427 adios(NULL, "arguments too long");
429 memcpy(cp, *argp, ln+1);
434 strcpy(cp, trailcmd);
442 ** Build a command line that causes the user's shell to list the file name
443 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
446 writelscmd(char *buf, int bufsz, char **argp)
448 writesomecmd(buf, bufsz, "ls", "", argp);
452 ** Like system(), but run the command in directory dir.
453 ** This assumes the program is single-threaded!
456 system_in_dir(const char *dir, const char *cmd)
460 if (getcwd(olddir, sizeof(olddir)) == 0)
461 adios("getcwd", "could not get working directory");
463 adios("chdir", "could not change working directory");
465 if (chdir(olddir) != 0)
466 adios("chdir", "could not change working directory");
470 /* ditto for popen() */
472 popen_in_dir(const char *dir, const char *cmd, const char *type)
476 if (getcwd(olddir, sizeof(olddir)) == 0)
477 adios("getcwd", "could not get working directory");
479 adios("chdir", "could not change working directory");
480 f = popen(cmd, type);
481 if (chdir(olddir) != 0)
482 adios("chdir", "could not change working directory");
491 static char *edsave = NULL; /* the editor we used previously */
495 editfile(char **ed, char **arg, char *file, int use)
497 int pid, status, vecp;
498 char *cp, *vec[MAXARGS];
501 /* We have no explicit editor. */
503 /* Use the previous editor ... */
505 if (!(cp = mhbasename(*ed)))
508 /* but prefer one specified via "editor-next" */
509 cp = concat(cp, "-next", NULL);
510 if ((cp = context_find(cp)))
513 /* set initial editor */
521 switch (pid = fork()) {
523 advise("fork", "unable to");
529 vec[vecp++] = mhbasename(*ed);
532 vec[vecp++] = *arg++;
537 fprintf(stderr, "%s: unable to exec ", invo_name);
542 if ((status = pidwait(pid, NOTOK))) {
543 if ((status & 0xff00) == 0xff00) {
544 /* cmd not found or pidwait() failed */
548 if (status & 0x00ff) {
549 /* terminated by signal */
550 advise(NULL, "%s terminated by signal %d",
554 advise(NULL, "%s exited with return code %d",
555 *ed, (status & 0xff00) >> 8);
562 /* remember which editor we used */
563 edsave = getcpy(*ed);
576 sendfile(char **arg, char *file)
582 context_save(); /* save the context file */
585 switch (child_id = fork()) {
587 advise(NULL, "unable to fork, so sending directly...");
591 vec[vecp++] = "send";
594 vec[vecp++] = *arg++;
599 fprintf(stderr, "%s: unable to exec ", invo_name);
604 if (pidwait(child_id, OK) == 0)
612 ** refile msg into another folder
615 refile(char **arg, char *file)
622 vec[vecp++] = "refile";
623 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
624 vec[vecp++] = "-file";
627 while (arg && *arg) {
628 vec[vecp++] = *arg++;
632 context_save(); /* save the context file */
635 switch (pid = fork()) {
637 advise("fork", "unable to");
642 fprintf(stderr, "%s: unable to exec ", invo_name);
647 return (pidwait(pid, -1));
653 ** Remove the draft file
657 removefile(char *drft)
659 if (unlink(drft) == NOTOK)
660 adios(drft, "unable to unlink");