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.");
164 /* sysexits.h EX_SOFTWARE */
169 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
171 if (!(argp = getans(prompt, aleqs))) {
174 switch (smatch(*argp, aleqs)) {
176 /* display the msg being replied to or distributed */
177 if ((cp = getenv("mhaltmsg")) && *cp) {
178 execprogl(listproc, listproc, "-file", cp,
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 execprogl(listproc, listproc, "-file", 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 if (execprogl("anno", "anno", "-list", "-comp",
273 attach_hdr, "-number", drft,
274 (char *)NULL) != 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"))) {
307 advise("popen", "could not get file from shell");
311 while (fgets(shell, sizeof(shell), f)) {
312 *(strchr(shell, '\n')) = '\0';
315 sprintf(file, "%s", shell);
317 sprintf(file, "%s/%s", cwd, shell);
319 if (execprogl("anno", "anno",
320 "-nodate", "-append",
323 drft, (char *)NULL) != 0) {
324 advise(NULL, "Could not add attachment header.");
332 ** Detach files from current draft.
334 ** Interpret the arguments as
335 ** attachment numbers. Decrement any remaining
336 ** argument number that is greater than the one
337 ** just processed after processing each one so
338 ** that the numbering stays correct.
340 for (arguments=argp+1; *arguments; arguments++) {
343 if (**arguments == '\0') {
347 if (execprogl("anno", "anno", "-delete",
349 "-number", *arguments, drft,
350 (char *)NULL) != 0) {
351 advise(NULL, "Could not delete attachment header.");
354 n = atoi(*arguments);
355 for (argp=arguments+1; *argp; argp++) {
356 if (atoi(*argp) > n) {
357 if (atoi(*argp) == 1) {
360 sprintf(*argp, "%d", atoi(*argp) - 1);
368 /* list recipients */
369 execprogl("whom", "whom", drft, (char *)NULL);
373 /* Unknown command */
374 advise(NULL, "say what?");
384 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
388 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
392 ** Note that we do not quote -- the argp from the user
393 ** is assumed to be quoted as they desire. (We can't treat
394 ** it as pure literal as that would prevent them using ~,
395 ** wildcards, etc.) The buffer produced by this function
396 ** should be given to popen_in_dir() or system_in_dir() so
397 ** that the current working directory is set correctly.
399 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
401 ** NB that some snprintf() return -1 on overflow rather than the
402 ** new C99 mandated 'number of chars that would have been written'
405 ** length checks here and inside the loop allow for the
406 ** trailing ';', trailcmd, '"' and NUL
408 int trailln = strlen(trailcmd) + 3;
409 if (ln < 0 || ln + trailln > bufsz)
410 adios(NULL, "arguments too long");
416 /* +1 for leading space */
417 if (ln + trailln + 1 > bufsz - (cp-buf))
418 adios(NULL, "arguments too long");
420 memcpy(cp, *argp, ln+1);
425 strcpy(cp, trailcmd);
433 ** Build a command line that causes the user's shell to list the file name
434 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
437 writelscmd(char *buf, int bufsz, char **argp)
439 writesomecmd(buf, bufsz, "ls", "", argp);
443 ** Like system(), but run the command in directory dir.
444 ** This assumes the program is single-threaded!
447 system_in_dir(const char *dir, const char *cmd)
451 if (getcwd(olddir, sizeof(olddir)) == 0)
452 adios("getcwd", "could not get working directory");
454 adios("chdir", "could not change working directory");
456 if (chdir(olddir) != 0)
457 adios("chdir", "could not change working directory");
461 /* ditto for popen() */
463 popen_in_dir(const char *dir, const char *cmd, const char *type)
467 if (getcwd(olddir, sizeof(olddir)) == 0)
468 adios("getcwd", "could not get working directory");
470 adios("chdir", "could not change working directory");
471 f = popen(cmd, type);
472 if (chdir(olddir) != 0)
473 adios("chdir", "could not change working directory");
482 static char *edsave = NULL; /* the editor we used previously */
486 editfile(char **ed, char **arg, char *file, int use)
488 int pid, status, vecp;
489 char *cp, *vec[MAXARGS];
492 /* We have no explicit editor. */
494 /* Use the previous editor ... */
496 if (!(cp = mhbasename(*ed)))
499 /* but prefer one specified via "editor-next" */
500 cp = concat(cp, "-next", NULL);
501 if ((cp = context_find(cp)))
504 /* set initial editor */
512 switch (pid = fork()) {
514 advise("fork", "unable to");
520 vec[vecp++] = mhbasename(*ed);
523 vec[vecp++] = *arg++;
528 fprintf(stderr, "%s: unable to exec ", invo_name);
533 if ((status = pidwait(pid, NOTOK))) {
534 if ((status & 0xff00) == 0xff00) {
535 /* cmd not found or pidwait() failed */
539 if (status & 0x00ff) {
540 /* terminated by signal */
541 advise(NULL, "%s terminated by signal %d",
545 advise(NULL, "%s exited with return code %d",
546 *ed, (status & 0xff00) >> 8);
553 /* remember which editor we used */
554 edsave = getcpy(*ed);
567 sendfile(char **arg, char *file)
573 context_save(); /* save the context file */
576 switch (child_id = fork()) {
578 advise(NULL, "unable to fork, so sending directly...");
582 vec[vecp++] = "send";
585 vec[vecp++] = *arg++;
590 fprintf(stderr, "%s: unable to exec ", invo_name);
595 if (pidwait(child_id, OK) == 0)
603 ** refile msg into another folder
606 refile(char **arg, char *file)
613 vec[vecp++] = "refile";
614 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
615 vec[vecp++] = "-file";
618 while (arg && *arg) {
619 vec[vecp++] = *arg++;
623 context_save(); /* save the context file */
626 switch (pid = fork()) {
628 advise("fork", "unable to");
633 fprintf(stderr, "%s: unable to exec ", invo_name);
638 return (pidwait(pid, -1));
644 ** Remove the draft file
648 removefile(char *drft)
650 if (unlink(drft) == NOTOK)
651 adios(drft, "unable to unlink");