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.
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
22 static struct swit switches[] = {
24 { "editor editor", 0 },
26 { "prompt string", 0 },
35 ** Options at the "whatnow" prompt
37 static struct swit aleqs[] = {
39 { "edit [editor [switches]]", 0 },
49 { "refile +folder", 0 },
55 { "cd [directory]", 0 },
62 #define ATTACHCMDSW 12
63 { "attach files", 0 },
64 #define DETACHCMDSW 13
65 { "detach numbers", 0 },
69 static char *myprompt = "\nWhat now? ";
74 static int editfile(char **, char **, char *, int);
75 static int sendfile(char **, char *);
76 static int refile(char **, char *);
77 static int removefile(char *);
78 static void writelscmd(char *, int, char **);
79 static void writesomecmd(char *, int, char *, char *, char **);
80 static FILE* popen_in_dir(const char *, const char *, const char *);
81 static int system_in_dir(const char *, const char *);
85 main(int argc, char **argv)
89 char *ed = NULL, *drft = NULL;
90 char buf[BUFSIZ], prompt[BUFSIZ];
91 char **argp, **arguments;
93 char cwd[MAXPATHLEN + 1]; /* current working directory */
94 char file[MAXPATHLEN + 1]; /* file name buffer */
95 char shell[MAXPATHLEN + 1]; /* shell response buffer */
96 FILE *f; /* read pointer for bgnd proc */
98 setlocale(LC_ALL, "");
99 invo_name = mhbasename(argv[0]);
101 /* read user profile/context */
104 arguments = getarguments(invo_name, argc, argv, 1);
108 ** Get the initial current working directory.
111 if (!getcwd(cwd, sizeof (cwd))) {
112 adios("getcwd", "could not get working directory");
115 while ((cp = *argp++)) {
117 switch (smatch(++cp, switches)) {
119 ambigsw(cp, switches);
122 adios(NULL, "-%s unknown", cp);
125 snprintf(buf, sizeof(buf),
126 "%s [switches] [file]",
128 print_help(buf, switches, 1);
131 print_version(invo_name);
135 if (!(ed = *argp++) || *ed == '-')
136 adios(NULL, "missing argument to %s",
141 if (!(myprompt = *argp++) || *myprompt == '-')
142 adios(NULL, "missing argument to %s",
149 adios(NULL, "only one draft at a time!");
154 if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
155 drft = getcpy(m_draft(seq_cur));
157 if ((cp = getenv("mhuse")) && *cp)
160 if (!ed && !(ed = getenv("mheditor"))) {
161 ed = ""; /* Don't initially edit the draft */
164 /* start editing the draft, unless editor is the empty string */
166 if (editfile(&ed, NULL, drft, use) <0) {
170 advise(NULL, "Try again.");
171 /* sysexits.h EX_SOFTWARE */
176 snprintf(prompt, sizeof(prompt), myprompt, invo_name);
178 if (!(argp = getans(prompt, aleqs))) {
181 switch (smatch(*argp, aleqs)) {
183 /* display the msg being replied to or distributed */
184 if ((cp = getenv("mhaltmsg")) && *cp) {
185 execprogl(listproc, listproc, "-file", cp,
188 advise(NULL, "no alternate message to display");
193 /* Call an editor on the draft file */
196 editfile(&ed, argp, drft, NOUSE);
200 /* display the draft file */
201 execprogl(listproc, listproc, "-file", drft,
207 if (stat(drft, &st) != NOTOK) {
208 advise(NULL, "draft left on %s", drft);
213 /* Delete draft and exit */
219 sendfile(++argp, drft);
223 /* Refile the draft */
224 if (refile(++argp, drft) == 0) {
231 ** Change the working directory for attachments
233 ** Run the directory through the user's shell
234 ** so that we can take advantage of any syntax
235 ** that the user is accustomed to. Read back
236 ** the absolute path.
239 if (*(argp+1) == NULL) {
240 sprintf(buf, "$SHELL -c \"cd;pwd\"");
242 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
244 if ((f = popen_in_dir(cwd, buf, "r"))) {
245 fgets(cwd, sizeof (cwd), f);
247 if (strchr(cwd, '\n'))
248 *strchr(cwd, '\n') = '\0';
252 advise("popen", "could not get directory");
258 /* Print the working directory for attachments */
264 ** List files in the current attachment working
267 ** Use the user's shell so that we can take
268 ** advantage of any syntax that the user is
271 writelscmd(buf, sizeof(buf), argp);
272 system_in_dir(cwd, buf);
277 ** List attachments on current draft.
279 if (execprogl("anno", "anno", "-list", "-comp",
280 attach_hdr, "-number", drft,
281 (char *)NULL) != 0) {
282 advise(NULL, "Could not list attachment headers.");
288 ** Attach files to current draft.
291 if (*(argp+1) == NULL) {
292 advise(NULL, "attach command requires file argument(s).");
297 ** Build a command line that causes the user's
298 ** shell to list the file name arguments.
299 ** This handles and wildcard expansion, tilde
302 writelscmd(buf, sizeof(buf), argp);
305 ** Read back the response from the shell,
306 ** which contains a number of lines with one
307 ** file name per line. Remove off the newline.
308 ** Determine whether we have an absolute or
309 ** relative path name. Prepend the current
310 ** working directory to relative path names.
311 ** Add the attachment annotation to the draft.
313 if (!(f = popen_in_dir(cwd, buf, "r"))) {
314 advise("popen", "could not get file from shell");
318 while (fgets(shell, sizeof(shell), f)) {
319 *(strchr(shell, '\n')) = '\0';
322 sprintf(file, "%s", shell);
324 sprintf(file, "%s/%s", cwd, shell);
326 if (execprogl("anno", "anno",
327 "-nodate", "-append",
330 drft, (char *)NULL) != 0) {
331 advise(NULL, "Could not add attachment header.");
339 ** Detach files from current draft.
341 ** Interpret the arguments as
342 ** attachment numbers. Decrement any remaining
343 ** argument number that is greater than the one
344 ** just processed after processing each one so
345 ** that the numbering stays correct.
347 for (arguments=argp+1; *arguments; arguments++) {
350 if (**arguments == '\0') {
354 if (execprogl("anno", "anno", "-delete",
356 "-number", *arguments, drft,
357 (char *)NULL) != 0) {
358 advise(NULL, "Could not delete attachment header.");
361 n = atoi(*arguments);
362 for (argp=arguments+1; *argp; argp++) {
363 if (atoi(*argp) > n) {
364 if (atoi(*argp) == 1) {
367 sprintf(*argp, "%d", atoi(*argp) - 1);
375 /* list recipients */
376 execprogl("whom", "whom", drft, (char *)NULL);
380 /* Unknown command */
381 advise(NULL, "say what?");
391 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
395 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
399 ** Note that we do not quote -- the argp from the user
400 ** is assumed to be quoted as they desire. (We can't treat
401 ** it as pure literal as that would prevent them using ~,
402 ** wildcards, etc.) The buffer produced by this function
403 ** should be given to popen_in_dir() or system_in_dir() so
404 ** that the current working directory is set correctly.
406 int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
408 ** NB that some snprintf() return -1 on overflow rather than the
409 ** new C99 mandated 'number of chars that would have been written'
412 ** length checks here and inside the loop allow for the
413 ** trailing ';', trailcmd, '"' and NUL
415 int trailln = strlen(trailcmd) + 3;
416 if (ln < 0 || ln + trailln > bufsz)
417 adios(NULL, "arguments too long");
423 /* +1 for leading space */
424 if (ln + trailln + 1 > bufsz - (cp-buf))
425 adios(NULL, "arguments too long");
427 memcpy(cp, *argp, ln+1);
432 strcpy(cp, trailcmd);
440 ** Build a command line that causes the user's shell to list the file name
441 ** arguments. This handles and wildcard expansion, tilde expansion, etc.
444 writelscmd(char *buf, int bufsz, char **argp)
446 writesomecmd(buf, bufsz, "ls", "", argp);
450 ** Like system(), but run the command in directory dir.
451 ** This assumes the program is single-threaded!
454 system_in_dir(const char *dir, const char *cmd)
458 if (getcwd(olddir, sizeof(olddir)) == 0)
459 adios("getcwd", "could not get working directory");
461 adios("chdir", "could not change working directory");
463 if (chdir(olddir) != 0)
464 adios("chdir", "could not change working directory");
468 /* ditto for popen() */
470 popen_in_dir(const char *dir, const char *cmd, const char *type)
474 if (getcwd(olddir, sizeof(olddir)) == 0)
475 adios("getcwd", "could not get working directory");
477 adios("chdir", "could not change working directory");
478 f = popen(cmd, type);
479 if (chdir(olddir) != 0)
480 adios("chdir", "could not change working directory");
489 static char *edsave = NULL; /* the editor we used previously */
493 editfile(char **ed, char **arg, char *file, int use)
495 int pid, status, vecp;
496 char *cp, *vec[MAXARGS];
499 /* We have no explicit editor. */
501 /* Use the previous editor ... */
503 if (!(cp = mhbasename(*ed)))
506 /* but prefer one specified via "editor-next" */
507 cp = concat(cp, "-next", NULL);
508 if ((cp = context_find(cp)))
511 /* set initial editor */
519 switch (pid = fork()) {
521 advise("fork", "unable to");
527 vec[vecp++] = mhbasename(*ed);
528 while (arg && *arg) {
529 vec[vecp++] = *arg++;
535 fprintf(stderr, "%s: unable to exec ", invo_name);
540 if ((status = pidwait(pid, NOTOK))) {
541 if ((status & 0xff00) == 0xff00) {
542 /* cmd not found or pidwait() failed */
546 if (status & 0x00ff) {
547 /* terminated by signal */
548 advise(NULL, "%s terminated by signal %d",
552 advise(NULL, "%s exited with return code %d",
553 *ed, (status & 0xff00) >> 8);
560 /* remember which editor we used */
561 edsave = getcpy(*ed);
574 sendfile(char **arg, char *file)
582 vec[vecp++] = "send";
583 while (arg && *arg) {
584 vec[vecp++] = *arg++;
589 fprintf(stderr, "%s: unable to exec ", invo_name);
596 ** refile msg into another folder
599 refile(char **arg, char *file)
604 vec[vecp++] = "refile";
605 vec[vecp++] = "-nolink"; /* override bad .mh_profile defaults */
606 vec[vecp++] = "-file";
609 while (arg && *arg) {
610 vec[vecp++] = *arg++;
617 return execprog(*vec, vec);
622 ** Remove the draft file
626 removefile(char *drft)
628 if (unlink(drft) == NOTOK)
629 adios(drft, "unable to unlink");