+main(int argc, char **argv)
+{
+ int use = 0;
+ char *cp;
+ char *ed = NULL, *drft = NULL;
+ char buf[BUFSIZ], prompt[BUFSIZ];
+ char **argp, **arguments;
+ struct stat st;
+ char cwd[MAXPATHLEN + 1]; /* current working directory */
+ char file[MAXPATHLEN + 1]; /* file name buffer */
+ char shell[MAXPATHLEN + 1]; /* shell response buffer */
+ FILE *f; /* read pointer for bgnd proc */
+
+ setlocale(LC_ALL, "");
+ invo_name = mhbasename(argv[0]);
+
+ /* read user profile/context */
+ context_read();
+
+ arguments = getarguments(invo_name, argc, argv, 1);
+ argp = arguments;
+
+ /*
+ ** Get the initial current working directory.
+ */
+
+ if (!getcwd(cwd, sizeof (cwd))) {
+ adios("getcwd", "could not get working directory");
+ }
+
+ while ((cp = *argp++)) {
+ if (*cp == '-') {
+ switch (smatch(++cp, switches)) {
+ case AMBIGSW:
+ ambigsw(cp, switches);
+ done(1);
+ case UNKWNSW:
+ adios(NULL, "-%s unknown", cp);
+
+ case HELPSW:
+ snprintf(buf, sizeof(buf),
+ "%s [switches] [file]",
+ invo_name);
+ print_help(buf, switches, 1);
+ done(1);
+ case VERSIONSW:
+ print_version(invo_name);
+ done(1);
+
+ case EDITRSW:
+ if (!(ed = *argp++) || *ed == '-')
+ adios(NULL, "missing argument to %s",
+ argp[-2]);
+ continue;
+
+ case PRMPTSW:
+ if (!(myprompt = *argp++) || *myprompt == '-')
+ adios(NULL, "missing argument to %s",
+ argp[-2]);
+ continue;
+
+ }
+ }
+ if (drft)
+ adios(NULL, "only one draft at a time!");
+ else
+ drft = cp;
+ }
+
+ if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
+ drft = getcpy(m_draft(seq_cur));
+
+ if ((cp = getenv("mhuse")) && *cp)
+ use = atoi(cp);
+
+ if (!ed && !(ed = getenv("mheditor"))) {
+ ed = ""; /* Don't initially edit the draft */
+ }
+
+ /* start editing the draft, unless editor is the empty string */
+ if (*ed) {
+ if (editfile(&ed, NULL, drft, use) <0) {
+ if (!use) {
+ unlink(drft);
+ }
+ advise(NULL, "Try again.");
+ done(1);
+ }
+ }
+
+ snprintf(prompt, sizeof(prompt), myprompt, invo_name);
+ for (;;) {
+ if (!(argp = getans(prompt, aleqs))) {
+ done(1);
+ }
+ switch (smatch(*argp, aleqs)) {
+ case DISPSW:
+ /* display the msg being replied to or distributed */
+ if ((cp = getenv("mhaltmsg")) && *cp) {
+ snprintf(buf, sizeof buf, "%s '%s'",
+ listproc, cp);
+ system(buf);
+ } else {
+ advise(NULL, "no alternate message to display");
+ }
+ break;
+
+ case EDITSW:
+ /* Call an editor on the draft file */
+ if (*++argp)
+ ed = *argp++;
+ editfile(&ed, argp, drft, NOUSE);
+ break;
+
+ case LISTSW:
+ /* display the draft file */
+ snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
+ system(buf);
+ break;
+
+ case QUITSW:
+ /* quit */
+ if (stat(drft, &st) != NOTOK) {
+ advise(NULL, "draft left on %s", drft);
+ }
+ done(1);
+
+ case DELETESW:
+ /* Delete draft and exit */
+ removefile(drft);
+ done(1);
+
+ case SENDSW:
+ /* Send draft */
+ sendfile(++argp, drft);
+ break;
+
+ case REFILEOPT:
+ /* Refile the draft */
+ if (refile(++argp, drft) == 0) {
+ done(0);
+ }
+ break;
+
+ case CDCMDSW:
+ /*
+ ** Change the working directory for attachments
+ **
+ ** Run the directory through the user's shell
+ ** so that we can take advantage of any syntax
+ ** that the user is accustomed to. Read back
+ ** the absolute path.
+ */
+
+ if (*(argp+1) == NULL) {
+ sprintf(buf, "$SHELL -c \"cd;pwd\"");
+ } else {
+ writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
+ }
+ if ((f = popen_in_dir(cwd, buf, "r"))) {
+ fgets(cwd, sizeof (cwd), f);
+
+ if (strchr(cwd, '\n'))
+ *strchr(cwd, '\n') = '\0';
+
+ pclose(f);
+ } else {
+ advise("popen", "could not get directory");
+ }
+
+ break;
+
+ case PWDCMDSW:
+ /* Print the working directory for attachments */
+ printf("%s\n", cwd);
+ break;
+
+ case LSCMDSW:
+ /*
+ ** List files in the current attachment working
+ ** directory
+ **
+ ** Use the user's shell so that we can take
+ ** advantage of any syntax that the user is
+ ** accustomed to.
+ */
+ writelscmd(buf, sizeof(buf), argp);
+ system_in_dir(cwd, buf);
+ break;
+
+ case ALISTCMDSW:
+ /*
+ ** List attachments on current draft.
+ */
+ snprintf(buf, sizeof buf, "anno -list -comp '%s' "
+ "-number '%s'", attach_hdr, drft);
+ if (system(buf) != 0) {
+ advise(NULL, "Could not list attachment headers.");
+ }
+ break;
+
+ case ATTACHCMDSW:
+ /*
+ ** Attach files to current draft.
+ */
+
+ if (*(argp+1) == NULL) {
+ advise(NULL, "attach command requires file argument(s).");
+ break;
+ }
+
+ /*
+ ** Build a command line that causes the user's
+ ** shell to list the file name arguments.
+ ** This handles and wildcard expansion, tilde
+ ** expansion, etc.
+ */
+ writelscmd(buf, sizeof(buf), argp);
+
+ /*
+ ** Read back the response from the shell,
+ ** which contains a number of lines with one
+ ** file name per line. Remove off the newline.
+ ** Determine whether we have an absolute or
+ ** relative path name. Prepend the current
+ ** working directory to relative path names.
+ ** Add the attachment annotation to the draft.
+ */
+ if ((f = popen_in_dir(cwd, buf, "r"))) {
+ char buf[BUFSIZ];
+
+ while (fgets(shell, sizeof(shell), f)) {
+ *(strchr(shell, '\n')) = '\0';
+
+ if (*shell == '/')
+ sprintf(file, "%s", shell);
+ else {
+ sprintf(file, "%s/%s", cwd,
+ shell);
+ }
+ snprintf(buf, sizeof buf,
+ "anno -nodate -append "
+ "-comp '%s' -text '%s'"
+ " '%s'",
+ attach_hdr, file,
+ drft);
+ if (system(buf) != 0) {
+ advise(NULL, "Could not add attachment header.");
+ }
+ }
+
+ pclose(f);
+ } else {
+ advise("popen", "could not get file from shell");
+ }
+
+ break;
+
+ case DETACHCMDSW:
+ /*
+ ** Detach files from current draft.
+ **
+ ** Interpret the arguments as
+ ** attachment numbers. Decrement any remaining
+ ** argument number that is greater than the one
+ ** just processed after processing each one so
+ ** that the numbering stays correct.
+ */
+ for (arguments=argp+1; *arguments; arguments++) {
+ char buf[BUFSIZ];
+ int n;
+
+ if (**arguments == '\0') {
+ continue;
+ }
+
+ n = atoi(*arguments);
+ snprintf(buf, sizeof buf, "anno -delete "
+ "-comp '%s' -number '%d' "
+ "'%s'",
+ attach_hdr, n, drft);
+ if (system(buf) != 0) {
+ advise(NULL, "Could not delete attachment header.");
+ }
+
+ for (argp=arguments+1; *argp; argp++) {
+ if (atoi(*argp) > n) {
+ if (atoi(*argp) == 1) {
+ *argp = "";
+ } else {
+ sprintf(*argp, "%d", atoi(*argp) - 1);
+ }
+ }
+ }
+ }
+ break;
+
+ default:
+ /* Unknown command */
+ advise(NULL, "say what?");
+ break;
+ }
+ }
+ /*NOTREACHED*/
+}
+
+
+
+/*
+** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
+** trailcmd".
+*/
+static void
+writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
+{
+ char *cp;
+ /*
+ ** Note that we do not quote -- the argp from the user
+ ** is assumed to be quoted as they desire. (We can't treat
+ ** it as pure literal as that would prevent them using ~,
+ ** wildcards, etc.) The buffer produced by this function
+ ** should be given to popen_in_dir() or system_in_dir() so
+ ** that the current working directory is set correctly.
+ */
+ int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
+ /*
+ ** NB that some snprintf() return -1 on overflow rather than the
+ ** new C99 mandated 'number of chars that would have been written'
+ */
+ /*
+ ** length checks here and inside the loop allow for the
+ ** trailing ';', trailcmd, '"' and NUL
+ */
+ int trailln = strlen(trailcmd) + 3;
+ if (ln < 0 || ln + trailln > bufsz)
+ adios(NULL, "arguments too long");
+
+ cp = buf + ln;
+
+ while (*++argp) {
+ ln = strlen(*argp);
+ /* +1 for leading space */
+ if (ln + trailln + 1 > bufsz - (cp-buf))
+ adios(NULL, "arguments too long");
+ *cp++ = ' ';
+ memcpy(cp, *argp, ln+1);
+ cp += ln;
+ }
+ if (*trailcmd) {
+ *cp++ = ';';
+ strcpy(cp, trailcmd);
+ cp += trailln - 3;
+ }
+ *cp++ = '"';
+ *cp = 0;
+}
+
+/*
+** Build a command line that causes the user's shell to list the file name
+** arguments. This handles and wildcard expansion, tilde expansion, etc.
+*/
+static void
+writelscmd(char *buf, int bufsz, char **argp)
+{
+ writesomecmd(buf, bufsz, "ls", "", argp);
+}
+
+/*
+** Like system(), but run the command in directory dir.
+** This assumes the program is single-threaded!
+*/
+static int
+system_in_dir(const char *dir, const char *cmd)