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 files This option removes attachments from the
35 ** detach -n numbers draft. This can be done by file name or
36 ** by attachment number.
45 static struct swit whatnowswitches[] = {
47 { "editor editor", 0 },
49 { "prompt string", 0 },
58 ** Options at the "whatnow" prompt
60 static struct swit aleqs[] = {
62 { "edit [<editor> <switches>]", 0 },
64 { "refile [<switches>] +folder", 0 },
70 { "send [<switches>]", 0 },
76 { "cd [directory]", 0 },
81 #define ATTACHCMDSW 10
83 #define DETACHCMDSW 11
90 static char *myprompt = "\nWhat now? ";
95 static int editfile(char **, char **, char *, int);
96 static int sendfile(char **, char *);
97 static int refile(char **, char *);
98 static int removefile(char *);
99 static void writelscmd(char *, int, char **);
100 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
101 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
102 static int system_in_dir(const char *dir, const char *cmd);
106 main(int argc, char **argv)
110 char *ed = NULL, *drft = NULL;
111 char buf[BUFSIZ], prompt[BUFSIZ];
112 char **argp, **arguments;
114 char cwd[MAXPATHLEN + 1]; /* current working directory */
115 char file[MAXPATHLEN + 1]; /* file name buffer */
116 char shell[MAXPATHLEN + 1]; /* shell response buffer */
117 FILE *f; /* read pointer for bgnd proc */
118 int n; /* set on -n to detach command */
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, whatnowswitches)) {
141 ambigsw(cp, whatnowswitches);
144 adios(NULL, "-%s unknown", cp);
147 snprintf(buf, sizeof(buf),
148 "%s [switches] [file]",
150 print_help(buf, whatnowswitches, 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 ** Scan the arguments for a -n. Mixed file
370 ** names and numbers aren't allowed, so this
371 ** catches a -n anywhere in the argument list.
373 for (n = 0, arguments = argp + 1;
376 if (strcmp(*arguments, "-n") == 0) {
383 ** A -n was found so interpret the arguments as
384 ** attachment numbers. Decrement any remaining
385 ** argument number that is greater than the one
386 ** just processed after processing each one so
387 ** that the numbering stays correct.
390 for (arguments=argp+1; *arguments;
392 if (strcmp(*arguments, "-n") == 0)
395 if (**arguments != '\0') {
398 n = atoi(*arguments);
399 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
400 if (system(buf) != 0) {
401 advise(NULL, "Could not delete attachment header.");
404 for (argp=arguments+1; *argp;
406 if (atoi(*argp) > n) {
407 if (atoi(*argp) == 1)
410 sprintf(*argp, "%d", atoi(*argp) - 1);
420 ** The arguments are interpreted as file names.
421 ** Run them through the user's shell for wildcard
422 ** expansion and other goodies. Do this from
423 ** the current working directory if the argument
424 ** is not an absolute path name (does not begin
427 ** We feed all the file names to the shell at
428 ** once, otherwise you can't provide a file name
429 ** with a space in it.
431 writelscmd(buf, sizeof(buf), argp);
432 if ((f = popen_in_dir(cwd, buf, "r"))) {
433 while (fgets(shell, sizeof (shell), f)) {
434 *(strchr(shell, '\n')) = '\0';
435 snprintf(buf, sizeof buf,
436 "anno -delete -comp "
437 "'%s' -text '%s' '%s'",
440 if (system(buf) != 0) {
441 advise(NULL, "Could not delete attachment header.");
446 advise("popen", "could not get file from shell");
452 /* Unknown command */
453 advise(NULL, "say what?");
463 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
467 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
471 ** Note that we do not quote -- the argp from the user
472 ** is assumed to be quoted as they desire. (We can't treat
473 ** it as pure literal as that would prevent them using ~,
474 ** wildcards, etc.) The buffer produced by this function
475 ** should be given to popen_in_dir() or system_in_dir() so
476 ** that the current working directory is set correctly.
478 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
480 ** NB that some snprintf() return -1 on overflow rather than the
481 ** new C99 mandated 'number of chars that would have been written'
484 ** length checks here and inside the loop allow for the
485 ** trailing ';', trailcmd, '"' and NUL
487 int trailln = strlen(trailcmd) + 3;
488 if (ln < 0 || ln + trailln > bufsz)
489 adios(NULL, "arguments too long");
495 /* +1 for leading space */
496 if (ln + trailln + 1 > bufsz - (cp-buf))
497 adios(NULL, "arguments too long");
499 memcpy(cp, *argp, ln+1);
504 strcpy(cp, trailcmd);
512 ** Build a command line that causes the user's shell to list the file name
513 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
516 writelscmd(char *buf, int bufsz, char **argp)
518 writesomecmd(buf, bufsz, "ls", "", argp);
522 ** Like system(), but run the command in directory dir.
523 ** This assumes the program is single-threaded!
526 system_in_dir(const char *dir, const char *cmd)
530 if (getcwd(olddir, sizeof(olddir)) == 0)
531 adios("getcwd", "could not get working directory");
533 adios("chdir", "could not change working directory");
535 if (chdir(olddir) != 0)
536 adios("chdir", "could not change working directory");
540 /* ditto for popen() */
542 popen_in_dir(const char *dir, const char *cmd, const char *type)
546 if (getcwd(olddir, sizeof(olddir)) == 0)
547 adios("getcwd", "could not get working directory");
549 adios("chdir", "could not change working directory");
550 f = popen(cmd, type);
551 if (chdir(olddir) != 0)
552 adios("chdir", "could not change working directory");
561 static char *edsave = NULL; /* the editor we used previously */
565 editfile(char **ed, char **arg, char *file, int use)
567 int pid, status, vecp;
568 char *cp, *vec[MAXARGS];
571 /* We have no explicit editor. */
573 /* Use the previous editor ... */
575 if (!(cp = mhbasename(*ed)))
578 /* but prefer one specified via "editor-next" */
579 cp = concat(cp, "-next", NULL);
580 if ((cp = context_find(cp)))
583 /* set initial editor */
591 switch (pid = fork()) {
593 advise("fork", "unable to");
599 vec[vecp++] = mhbasename(*ed);
602 vec[vecp++] = *arg++;
607 fprintf(stderr, "%s: unable to exec ", invo_name);
612 if ((status = pidwait(pid, NOTOK))) {
613 if ((status & 0xff00) == 0xff00) {
614 /* cmd not found or pidwait() failed */
618 if (status & 0x00ff) {
619 /* terminated by signal */
620 advise(NULL, "%s terminated by signal %d",
624 advise(NULL, "%s exited with return code %d",
625 *ed, (status & 0xff00) >> 8);
632 /* remember which editor we used */
633 edsave = getcpy(*ed);
646 sendfile(char **arg, char *file)
652 context_save(); /* save the context file */
655 switch (child_id = fork()) {
657 advise(NULL, "unable to fork, so sending directly...");
661 vec[vecp++] = invo_name;
664 vec[vecp++] = *arg++;
669 fprintf(stderr, "%s: unable to exec ", invo_name);
674 if (pidwait(child_id, OK) == 0)
682 ** refile msg into another folder
685 refile(char **arg, char *file)
692 vec[vecp++] = "refile";
693 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
694 vec[vecp++] = "-file";
697 while (arg && *arg) {
698 vec[vecp++] = *arg++;
702 context_save(); /* save the context file */
705 switch (pid = fork()) {
707 advise("fork", "unable to");
712 fprintf(stderr, "%s: unable to exec ", invo_name);
717 return (pidwait(pid, -1));
723 ** Remove the draft file
727 removefile(char *drft)
729 if (unlink(drft) == NOTOK)
730 adios(drft, "unable to unlink");