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.
8 ** The inclusion of attachments is eased by
9 ** using the header field name mechanism added to anno and send.
10 ** The header field name for attachments is predefined.
12 ** Several commands have been added at the whatnow prompt:
14 ** cd [ directory ] This option works just like the shell's
15 ** cd command and lets the user change the
16 ** directory from which attachments are
17 ** taken so that long path names are not
18 ** needed with every file.
20 ** ls [ ls-options ] This option works just like the normal
21 ** ls command and exists to allow the user
22 ** to verify file names in the directory.
24 ** pwd This option works just like the normal
25 ** pwd command and exists to allow the user
26 ** to verify the directory.
28 ** attach files This option attaches the named files to
31 ** alist This option lists the attachments on the
34 ** detach numbers This option removes attachments by
35 ** attachment number from the draft.
44 static struct swit switches[] = {
46 { "editor editor", 0 },
48 { "prompt string", 0 },
57 ** Options at the "whatnow" prompt
59 static struct swit aleqs[] = {
61 { "edit [editor [switches]]", 0 },
71 { "refile +folder", 0 },
77 { "cd [directory]", 0 },
84 #define ATTACHCMDSW 12
85 { "attach files", 0 },
86 #define DETACHCMDSW 13
87 { "detach numbers", 0 },
91 static char *myprompt = "\nWhat now? ";
96 static int editfile(char **, char **, char *, int);
97 static int sendfile(char **, char *);
98 static int refile(char **, char *);
99 static int removefile(char *);
100 static void writelscmd(char *, int, char **);
101 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
102 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
103 static int system_in_dir(const char *dir, const char *cmd);
107 main(int argc, char **argv)
111 char *ed = NULL, *drft = NULL;
112 char buf[BUFSIZ], prompt[BUFSIZ];
113 char **argp, **arguments;
115 char cwd[MAXPATHLEN + 1]; /* current working directory */
116 char file[MAXPATHLEN + 1]; /* file name buffer */
117 char shell[MAXPATHLEN + 1]; /* shell response buffer */
118 FILE *f; /* read pointer for bgnd proc */
120 setlocale(LC_ALL, "");
121 invo_name = mhbasename(argv[0]);
123 /* read user profile/context */
126 arguments = getarguments(invo_name, argc, argv, 1);
130 ** Get the initial current working directory.
133 if (!getcwd(cwd, sizeof (cwd))) {
134 adios("getcwd", "could not get working directory");
137 while ((cp = *argp++)) {
139 switch (smatch(++cp, switches)) {
141 ambigsw(cp, switches);
144 adios(NULL, "-%s unknown", cp);
147 snprintf(buf, sizeof(buf),
148 "%s [switches] [file]",
150 print_help(buf, switches, 1);
153 print_version(invo_name);
157 if (!(ed = *argp++) || *ed == '-')
158 adios(NULL, "missing argument to %s",
163 if (!(myprompt = *argp++) || *myprompt == '-')
164 adios(NULL, "missing argument to %s",
171 adios(NULL, "only one draft at a time!");
176 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
177 drft = getcpy(m_draft(seq_cur));
179 if ((cp = getenv("mhuse")) && *cp)
182 if (!ed && !(ed = getenv("mheditor"))) {
183 ed = ""; /* Don't initially edit the draft */
186 /* start editing the draft, unless editor is the empty string */
188 if (editfile(&ed, NULL, drft, use) <0) {
192 advise(NULL, "Try again.");
197 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
199 if (!(argp = getans(prompt, aleqs))) {
202 switch (smatch(*argp, aleqs)) {
204 /* display the msg being replied to or distributed */
205 if ((cp = getenv("mhaltmsg")) && *cp) {
206 snprintf(buf, sizeof buf, "%s '%s'",
210 advise(NULL, "no alternate message to display");
215 /* Call an editor on the draft file */
218 editfile(&ed, argp, drft, NOUSE);
222 /* display the draft file */
223 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
229 if (stat(drft, &st) != NOTOK) {
230 advise(NULL, "draft left on %s", drft);
235 /* Delete draft and exit */
241 sendfile(++argp, drft);
245 /* Refile the draft */
246 if (refile(++argp, drft) == 0) {
253 ** Change the working directory for attachments
255 ** Run the directory through the user's shell
256 ** so that we can take advantage of any syntax
257 ** that the user is accustomed to. Read back
258 ** the absolute path.
261 if (*(argp+1) == NULL) {
262 sprintf(buf, "$SHELL -c \"cd;pwd\"");
264 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
266 if ((f = popen_in_dir(cwd, buf, "r"))) {
267 fgets(cwd, sizeof (cwd), f);
269 if (strchr(cwd, '\n'))
270 *strchr(cwd, '\n') = '\0';
274 advise("popen", "could not get directory");
280 /* Print the working directory for attachments */
286 ** List files in the current attachment working
289 ** Use the user's shell so that we can take
290 ** advantage of any syntax that the user is
293 writelscmd(buf, sizeof(buf), argp);
294 system_in_dir(cwd, buf);
299 ** List attachments on current draft.
301 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
302 "-number '%s'", attach_hdr, drft);
303 if (system(buf) != 0) {
304 advise(NULL, "Could not list attachment headers.");
310 ** Attach files to current draft.
313 if (*(argp+1) == NULL) {
314 advise(NULL, "attach command requires file argument(s).");
319 ** Build a command line that causes the user's
320 ** shell to list the file name arguments.
321 ** This handles and wildcard expansion, tilde
324 writelscmd(buf, sizeof(buf), argp);
327 ** Read back the response from the shell,
328 ** which contains a number of lines with one
329 ** file name per line. Remove off the newline.
330 ** Determine whether we have an absolute or
331 ** relative path name. Prepend the current
332 ** working directory to relative path names.
333 ** Add the attachment annotation to the draft.
335 if ((f = popen_in_dir(cwd, buf, "r"))) {
338 while (fgets(shell, sizeof(shell), f)) {
339 *(strchr(shell, '\n')) = '\0';
342 sprintf(file, "%s", shell);
344 sprintf(file, "%s/%s", cwd,
347 snprintf(buf, sizeof buf,
348 "anno -nodate -append "
349 "-comp '%s' -text '%s'"
353 if (system(buf) != 0) {
354 advise(NULL, "Could not add attachment header.");
360 advise("popen", "could not get file from shell");
367 ** Detach files from current draft.
369 ** Interpret the arguments as
370 ** attachment numbers. Decrement any remaining
371 ** argument number that is greater than the one
372 ** just processed after processing each one so
373 ** that the numbering stays correct.
375 for (arguments=argp+1; *arguments; arguments++) {
379 if (**arguments == '\0') {
383 n = atoi(*arguments);
384 snprintf(buf, sizeof buf, "anno -delete "
385 "-comp '%s' -number '%d' "
387 attach_hdr, n, drft);
388 if (system(buf) != 0) {
389 advise(NULL, "Could not delete attachment header.");
392 for (argp=arguments+1; *argp; argp++) {
393 if (atoi(*argp) > n) {
394 if (atoi(*argp) == 1) {
397 sprintf(*argp, "%d", atoi(*argp) - 1);
405 /* list recipients */
406 snprintf(buf, sizeof buf, "%s '%s'", "whom", drft);
411 /* Unknown command */
412 advise(NULL, "say what?");
422 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
426 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
430 ** Note that we do not quote -- the argp from the user
431 ** is assumed to be quoted as they desire. (We can't treat
432 ** it as pure literal as that would prevent them using ~,
433 ** wildcards, etc.) The buffer produced by this function
434 ** should be given to popen_in_dir() or system_in_dir() so
435 ** that the current working directory is set correctly.
437 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
439 ** NB that some snprintf() return -1 on overflow rather than the
440 ** new C99 mandated 'number of chars that would have been written'
443 ** length checks here and inside the loop allow for the
444 ** trailing ';', trailcmd, '"' and NUL
446 int trailln = strlen(trailcmd) + 3;
447 if (ln < 0 || ln + trailln > bufsz)
448 adios(NULL, "arguments too long");
454 /* +1 for leading space */
455 if (ln + trailln + 1 > bufsz - (cp-buf))
456 adios(NULL, "arguments too long");
458 memcpy(cp, *argp, ln+1);
463 strcpy(cp, trailcmd);
471 ** Build a command line that causes the user's shell to list the file name
472 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
475 writelscmd(char *buf, int bufsz, char **argp)
477 writesomecmd(buf, bufsz, "ls", "", argp);
481 ** Like system(), but run the command in directory dir.
482 ** This assumes the program is single-threaded!
485 system_in_dir(const char *dir, const char *cmd)
489 if (getcwd(olddir, sizeof(olddir)) == 0)
490 adios("getcwd", "could not get working directory");
492 adios("chdir", "could not change working directory");
494 if (chdir(olddir) != 0)
495 adios("chdir", "could not change working directory");
499 /* ditto for popen() */
501 popen_in_dir(const char *dir, const char *cmd, const char *type)
505 if (getcwd(olddir, sizeof(olddir)) == 0)
506 adios("getcwd", "could not get working directory");
508 adios("chdir", "could not change working directory");
509 f = popen(cmd, type);
510 if (chdir(olddir) != 0)
511 adios("chdir", "could not change working directory");
520 static char *edsave = NULL; /* the editor we used previously */
524 editfile(char **ed, char **arg, char *file, int use)
526 int pid, status, vecp;
527 char *cp, *vec[MAXARGS];
530 /* We have no explicit editor. */
532 /* Use the previous editor ... */
534 if (!(cp = mhbasename(*ed)))
537 /* but prefer one specified via "editor-next" */
538 cp = concat(cp, "-next", NULL);
539 if ((cp = context_find(cp)))
542 /* set initial editor */
550 switch (pid = fork()) {
552 advise("fork", "unable to");
558 vec[vecp++] = mhbasename(*ed);
561 vec[vecp++] = *arg++;
566 fprintf(stderr, "%s: unable to exec ", invo_name);
571 if ((status = pidwait(pid, NOTOK))) {
572 if ((status & 0xff00) == 0xff00) {
573 /* cmd not found or pidwait() failed */
577 if (status & 0x00ff) {
578 /* terminated by signal */
579 advise(NULL, "%s terminated by signal %d",
583 advise(NULL, "%s exited with return code %d",
584 *ed, (status & 0xff00) >> 8);
591 /* remember which editor we used */
592 edsave = getcpy(*ed);
605 sendfile(char **arg, char *file)
611 context_save(); /* save the context file */
614 switch (child_id = fork()) {
616 advise(NULL, "unable to fork, so sending directly...");
620 vec[vecp++] = "send";
623 vec[vecp++] = *arg++;
628 fprintf(stderr, "%s: unable to exec ", invo_name);
633 if (pidwait(child_id, OK) == 0)
641 ** refile msg into another folder
644 refile(char **arg, char *file)
651 vec[vecp++] = "refile";
652 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
653 vec[vecp++] = "-file";
656 while (arg && *arg) {
657 vec[vecp++] = *arg++;
661 context_save(); /* save the context file */
664 switch (pid = fork()) {
666 advise("fork", "unable to");
671 fprintf(stderr, "%s: unable to exec ", invo_name);
676 return (pidwait(pid, -1));
682 ** Remove the draft file
686 removefile(char *drft)
688 if (unlink(drft) == NOTOK)
689 adios(drft, "unable to unlink");