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 whatnowswitches[] = {
46 { "editor editor", 0 },
48 { "prompt string", 0 },
57 ** Options at the "whatnow" prompt
59 static struct swit aleqs[] = {
61 { "edit [<editor> <switches>]", 0 },
63 { "refile [<switches>] +folder", 0 },
69 { "send [<switches>]", 0 },
75 { "cd [directory]", 0 },
80 #define ATTACHCMDSW 10
82 #define DETACHCMDSW 11
89 static char *myprompt = "\nWhat now? ";
94 static int editfile(char **, char **, char *, int);
95 static int sendfile(char **, char *);
96 static int refile(char **, char *);
97 static int removefile(char *);
98 static void writelscmd(char *, int, char **);
99 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
100 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
101 static int system_in_dir(const char *dir, const char *cmd);
105 main(int argc, char **argv)
109 char *ed = NULL, *drft = NULL;
110 char buf[BUFSIZ], prompt[BUFSIZ];
111 char **argp, **arguments;
113 char cwd[MAXPATHLEN + 1]; /* current working directory */
114 char file[MAXPATHLEN + 1]; /* file name buffer */
115 char shell[MAXPATHLEN + 1]; /* shell response buffer */
116 FILE *f; /* read pointer for bgnd proc */
118 setlocale(LC_ALL, "");
119 invo_name = mhbasename(argv[0]);
121 /* read user profile/context */
124 arguments = getarguments(invo_name, argc, argv, 1);
128 ** Get the initial current working directory.
131 if (!getcwd(cwd, sizeof (cwd))) {
132 adios("getcwd", "could not get working directory");
135 while ((cp = *argp++)) {
137 switch (smatch(++cp, whatnowswitches)) {
139 ambigsw(cp, whatnowswitches);
142 adios(NULL, "-%s unknown", cp);
145 snprintf(buf, sizeof(buf),
146 "%s [switches] [file]",
148 print_help(buf, whatnowswitches, 1);
151 print_version(invo_name);
155 if (!(ed = *argp++) || *ed == '-')
156 adios(NULL, "missing argument to %s",
161 if (!(myprompt = *argp++) || *myprompt == '-')
162 adios(NULL, "missing argument to %s",
169 adios(NULL, "only one draft at a time!");
174 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
175 drft = getcpy(m_draft(seq_cur));
177 if ((cp = getenv("mhuse")) && *cp)
180 if (!ed && !(ed = getenv("mheditor"))) {
181 ed = ""; /* Don't initially edit the draft */
184 /* start editing the draft, unless editor is the empty string */
186 if (editfile(&ed, NULL, drft, use) <0) {
190 advise(NULL, "Try again.");
195 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
197 if (!(argp = getans(prompt, aleqs))) {
200 switch (smatch(*argp, aleqs)) {
202 /* display the msg being replied to or distributed */
203 if ((cp = getenv("mhaltmsg")) && *cp) {
204 snprintf(buf, sizeof buf, "%s '%s'",
208 advise(NULL, "no alternate message to display");
213 /* Call an editor on the draft file */
216 editfile(&ed, argp, drft, NOUSE);
220 /* display the draft file */
221 snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
227 if (stat(drft, &st) != NOTOK) {
228 advise(NULL, "draft left on %s", drft);
233 /* Delete draft and exit */
239 sendfile(++argp, drft);
243 /* Refile the draft */
244 if (refile(++argp, drft) == 0) {
251 ** Change the working directory for attachments
253 ** Run the directory through the user's shell
254 ** so that we can take advantage of any syntax
255 ** that the user is accustomed to. Read back
256 ** the absolute path.
259 if (*(argp+1) == NULL) {
260 sprintf(buf, "$SHELL -c \"cd;pwd\"");
262 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
264 if ((f = popen_in_dir(cwd, buf, "r"))) {
265 fgets(cwd, sizeof (cwd), f);
267 if (strchr(cwd, '\n'))
268 *strchr(cwd, '\n') = '\0';
272 advise("popen", "could not get directory");
278 /* Print the working directory for attachments */
284 ** List files in the current attachment working
287 ** Use the user's shell so that we can take
288 ** advantage of any syntax that the user is
291 writelscmd(buf, sizeof(buf), argp);
292 system_in_dir(cwd, buf);
297 ** List attachments on current draft.
299 snprintf(buf, sizeof buf, "anno -list -comp '%s' "
300 "-number '%s'", attach_hdr, drft);
301 if (system(buf) != 0) {
302 advise(NULL, "Could not list attachment headers.");
308 ** Attach files to current draft.
311 if (*(argp+1) == NULL) {
312 advise(NULL, "attach command requires file argument(s).");
317 ** Build a command line that causes the user's
318 ** shell to list the file name arguments.
319 ** This handles and wildcard expansion, tilde
322 writelscmd(buf, sizeof(buf), argp);
325 ** Read back the response from the shell,
326 ** which contains a number of lines with one
327 ** file name per line. Remove off the newline.
328 ** Determine whether we have an absolute or
329 ** relative path name. Prepend the current
330 ** working directory to relative path names.
331 ** Add the attachment annotation to the draft.
333 if ((f = popen_in_dir(cwd, buf, "r"))) {
336 while (fgets(shell, sizeof(shell), f)) {
337 *(strchr(shell, '\n')) = '\0';
340 sprintf(file, "%s", shell);
342 sprintf(file, "%s/%s", cwd,
345 snprintf(buf, sizeof buf,
346 "anno -nodate -append "
347 "-comp '%s' -text '%s'"
351 if (system(buf) != 0) {
352 advise(NULL, "Could not add attachment header.");
358 advise("popen", "could not get file from shell");
365 ** Detach files from current draft.
367 ** Interpret the arguments as
368 ** attachment numbers. Decrement any remaining
369 ** argument number that is greater than the one
370 ** just processed after processing each one so
371 ** that the numbering stays correct.
373 for (arguments=argp+1; *arguments; arguments++) {
377 if (**arguments == '\0') {
381 n = atoi(*arguments);
382 snprintf(buf, sizeof buf, "anno -delete "
383 "-comp '%s' -number '%d' "
385 attach_hdr, n, drft);
386 if (system(buf) != 0) {
387 advise(NULL, "Could not delete attachment header.");
390 for (argp=arguments+1; *argp; argp++) {
391 if (atoi(*argp) > n) {
392 if (atoi(*argp) == 1) {
395 sprintf(*argp, "%d", atoi(*argp) - 1);
403 /* Unknown command */
404 advise(NULL, "say what?");
414 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
418 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
422 ** Note that we do not quote -- the argp from the user
423 ** is assumed to be quoted as they desire. (We can't treat
424 ** it as pure literal as that would prevent them using ~,
425 ** wildcards, etc.) The buffer produced by this function
426 ** should be given to popen_in_dir() or system_in_dir() so
427 ** that the current working directory is set correctly.
429 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
431 ** NB that some snprintf() return -1 on overflow rather than the
432 ** new C99 mandated 'number of chars that would have been written'
435 ** length checks here and inside the loop allow for the
436 ** trailing ';', trailcmd, '"' and NUL
438 int trailln = strlen(trailcmd) + 3;
439 if (ln < 0 || ln + trailln > bufsz)
440 adios(NULL, "arguments too long");
446 /* +1 for leading space */
447 if (ln + trailln + 1 > bufsz - (cp-buf))
448 adios(NULL, "arguments too long");
450 memcpy(cp, *argp, ln+1);
455 strcpy(cp, trailcmd);
463 ** Build a command line that causes the user's shell to list the file name
464 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
467 writelscmd(char *buf, int bufsz, char **argp)
469 writesomecmd(buf, bufsz, "ls", "", argp);
473 ** Like system(), but run the command in directory dir.
474 ** This assumes the program is single-threaded!
477 system_in_dir(const char *dir, const char *cmd)
481 if (getcwd(olddir, sizeof(olddir)) == 0)
482 adios("getcwd", "could not get working directory");
484 adios("chdir", "could not change working directory");
486 if (chdir(olddir) != 0)
487 adios("chdir", "could not change working directory");
491 /* ditto for popen() */
493 popen_in_dir(const char *dir, const char *cmd, const char *type)
497 if (getcwd(olddir, sizeof(olddir)) == 0)
498 adios("getcwd", "could not get working directory");
500 adios("chdir", "could not change working directory");
501 f = popen(cmd, type);
502 if (chdir(olddir) != 0)
503 adios("chdir", "could not change working directory");
512 static char *edsave = NULL; /* the editor we used previously */
516 editfile(char **ed, char **arg, char *file, int use)
518 int pid, status, vecp;
519 char *cp, *vec[MAXARGS];
522 /* We have no explicit editor. */
524 /* Use the previous editor ... */
526 if (!(cp = mhbasename(*ed)))
529 /* but prefer one specified via "editor-next" */
530 cp = concat(cp, "-next", NULL);
531 if ((cp = context_find(cp)))
534 /* set initial editor */
542 switch (pid = fork()) {
544 advise("fork", "unable to");
550 vec[vecp++] = mhbasename(*ed);
553 vec[vecp++] = *arg++;
558 fprintf(stderr, "%s: unable to exec ", invo_name);
563 if ((status = pidwait(pid, NOTOK))) {
564 if ((status & 0xff00) == 0xff00) {
565 /* cmd not found or pidwait() failed */
569 if (status & 0x00ff) {
570 /* terminated by signal */
571 advise(NULL, "%s terminated by signal %d",
575 advise(NULL, "%s exited with return code %d",
576 *ed, (status & 0xff00) >> 8);
583 /* remember which editor we used */
584 edsave = getcpy(*ed);
597 sendfile(char **arg, char *file)
603 context_save(); /* save the context file */
606 switch (child_id = fork()) {
608 advise(NULL, "unable to fork, so sending directly...");
612 vec[vecp++] = invo_name;
615 vec[vecp++] = *arg++;
620 fprintf(stderr, "%s: unable to exec ", invo_name);
625 if (pidwait(child_id, OK) == 0)
633 ** refile msg into another folder
636 refile(char **arg, char *file)
643 vec[vecp++] = "refile";
644 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
645 vec[vecp++] = "-file";
648 while (arg && *arg) {
649 vec[vecp++] = *arg++;
653 context_save(); /* save the context file */
656 switch (pid = fork()) {
658 advise("fork", "unable to");
663 fprintf(stderr, "%s: unable to exec ", invo_name);
668 return (pidwait(pid, -1));
674 ** Remove the draft file
678 removefile(char *drft)
680 if (unlink(drft) == NOTOK)
681 adios(drft, "unable to unlink");