folder: Never display column headers (removed -header).
[mmh] / uip / whatnow.c
1 /*
2 ** whatnow.c -- the WhatNow shell
3 **
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.
7 **
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.
11 **
12 **  Several commands have been added at the whatnow prompt:
13 **
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.
19 **
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.
23 **
24 **        pwd                     This option works just like the normal
25 **                                pwd command and exists to allow the user
26 **                                to verify the directory.
27 **
28 **        attach files            This option attaches the named files to
29 **                                the draft.
30 **
31 **        alist                   This option lists the attachments on the
32 **                                draft.
33 **
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.
37 */
38
39 #include <h/mh.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <h/mime.h>
43 #include <h/utils.h>
44
45 static struct swit whatnowswitches[] = {
46 #define EDITRSW  0
47         { "editor editor", 0 },
48 #define PRMPTSW  1
49         { "prompt string", 4 },
50 #define VERSIONSW  2
51         { "version", 0 },
52 #define HELPSW  3
53         { "help", 0 },
54         { NULL, 0 }
55 };
56
57 /*
58 ** Options at the "whatnow" prompt
59 */
60 static struct swit aleqs[] = {
61 #define EDITSW  0
62         { "edit [<editor> <switches>]", 0 },
63 #define REFILEOPT  1
64         { "refile [<switches>] +folder", 0 },
65 #define DISPSW  2
66         { "display", 0 },
67 #define LISTSW  3
68         { "list", 0 },
69 #define SENDSW  4
70         { "send [<switches>]", 0 },
71 #define QUITSW  5
72         { "quit", 0 },
73 #define DELETESW  6
74         { "delete", 0 },
75 #define CDCMDSW  7
76         { "cd [directory]", 0 },
77 #define PWDCMDSW  8
78         { "pwd", 0 },
79 #define LSCMDSW  9
80         { "ls", 0 },
81 #define ATTACHCMDSW  10
82         { "attach", 0 },
83 #define DETACHCMDSW  11
84         { "detach [-n]", 0 },
85 #define ALISTCMDSW  12
86         { "alist", 0 },
87         { NULL, 0 }
88 };
89
90 static char *myprompt = "\nWhat now? ";
91
92 /*
93 ** static prototypes
94 */
95 static int editfile(char **, char **, char *, int, struct msgs *,
96         char *, char *);
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);
104
105
106 #ifdef HAVE_LSTAT
107 static int copyf(char *, char *);
108 #endif
109
110
111 int
112 main(int argc, char **argv)
113 {
114         int use = 0;
115         char *cp;
116         char *ed = NULL, *drft = NULL, *msgnam = NULL;
117         char buf[BUFSIZ], prompt[BUFSIZ];
118         char **argp, **arguments;
119         struct stat st;
120         char cwd[MAXPATHLEN + 1];  /* current working directory */
121         char file[MAXPATHLEN + 1];  /* file name buffer */
122         char shell[MAXPATHLEN + 1];  /* shell response buffer */
123         FILE *f;  /* read pointer for bgnd proc */
124         int n;  /* set on -n to detach command */
125
126 #ifdef LOCALE
127         setlocale(LC_ALL, "");
128 #endif
129
130         invo_name = mhbasename(argv[0]);
131
132         /* read user profile/context */
133         context_read();
134
135         arguments = getarguments(invo_name, argc, argv, 1);
136         argp = arguments;
137
138         /*
139         ** Get the initial current working directory.
140         */
141
142         if (!getcwd(cwd, sizeof (cwd))) {
143                 adios("getcwd", "could not get working directory");
144         }
145
146         while ((cp = *argp++)) {
147                 if (*cp == '-') {
148                         switch (smatch(++cp, whatnowswitches)) {
149                         case AMBIGSW:
150                                 ambigsw(cp, whatnowswitches);
151                                 done(1);
152                         case UNKWNSW:
153                                 adios(NULL, "-%s unknown", cp);
154
155                         case HELPSW:
156                                 snprintf(buf, sizeof(buf),
157                                                 "%s [switches] [file]",
158                                                 invo_name);
159                                 print_help(buf, whatnowswitches, 1);
160                                 done(1);
161                         case VERSIONSW:
162                                 print_version(invo_name);
163                                 done(1);
164
165                         case EDITRSW:
166                                 if (!(ed = *argp++) || *ed == '-')
167                                         adios(NULL, "missing argument to %s",
168                                                         argp[-2]);
169                                 continue;
170
171                         case PRMPTSW:
172                                 if (!(myprompt = *argp++) || *myprompt == '-')
173                                         adios(NULL, "missing argument to %s",
174                                                         argp[-2]);
175                                 continue;
176
177                         }
178                 }
179                 if (drft)
180                         adios(NULL, "only one draft at a time!");
181                 else
182                         drft = cp;
183         }
184
185         if ((!drft && !(drft = getenv("mhdraft"))) || !*drft)
186                 drft = getcpy(m_draft(seq_cur));
187
188         msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
189
190         if ((cp = getenv("mhuse")) && *cp)
191                 use = atoi(cp);
192
193         if (!ed && !(ed = getenv("mheditor"))) {
194                 ed = "";  /* Don't initially edit the draft */
195         }
196
197         /* start editing the draft, unless editor is the empty string */
198         if (*ed) {
199                 if (editfile(&ed, NULL, drft, use, NULL, msgnam, NULL) <0) {
200                         if (!use) {
201                                 unlink(drft);
202                         }
203                         advise(NULL, "Try again.");
204                         done(1);
205                 }
206         }
207
208         snprintf(prompt, sizeof(prompt), myprompt, invo_name);
209         for (;;) {
210                 if (!(argp = getans(prompt, aleqs))) {
211                         unlink(altmsglink);
212                         done(1);
213                 }
214                 switch (smatch(*argp, aleqs)) {
215                 case DISPSW:
216                         /* display the msg being replied to or distributed */
217                         if (msgnam) {
218                                 snprintf(buf, sizeof buf, "%s '%s'",
219                                                 listproc, msgnam);
220                                 system(buf);
221                         } else {
222                                 advise(NULL, "no alternate message to display");
223                         }
224                         break;
225
226                 case EDITSW:
227                         /* Call an editor on the draft file */
228                         if (*++argp)
229                                 ed = *argp++;
230                         editfile(&ed, argp, drft, NOUSE, NULL, msgnam, NULL);
231                         break;
232
233                 case LISTSW:
234                         /* display the draft file */
235                         snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
236                         system(buf);
237                         break;
238
239                 case QUITSW:
240                         /* quit */
241                         if (stat(drft, &st) != NOTOK) {
242                                 advise(NULL, "draft left on %s", drft);
243                         }
244                         done(1);
245
246                 case DELETESW:
247                         /* Delete draft and exit */
248                         removefile(drft);
249                         done(1);
250
251                 case SENDSW:
252                         /* Send draft */
253                         sendfile(++argp, drft);
254                         break;
255
256                 case REFILEOPT:
257                         /* Refile the draft */
258                         if (refile(++argp, drft) == 0) {
259                                 done(0);
260                         }
261                         break;
262
263                 case CDCMDSW:
264                         /*
265                         ** Change the working directory for attachments
266                         **
267                         ** Run the directory through the user's shell
268                         ** so that we can take advantage of any syntax
269                         ** that the user is accustomed to.  Read back
270                         ** the absolute path.
271                         */
272
273                         if (*(argp+1) == NULL) {
274                                 sprintf(buf, "$SHELL -c \"cd;pwd\"");
275                         } else {
276                                 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
277                         }
278                         if ((f = popen_in_dir(cwd, buf, "r"))) {
279                                 fgets(cwd, sizeof (cwd), f);
280
281                                 if (strchr(cwd, '\n'))
282                                         *strchr(cwd, '\n') = '\0';
283
284                                 pclose(f);
285                         } else {
286                                 advise("popen", "could not get directory");
287                         }
288
289                         break;
290
291                 case PWDCMDSW:
292                         /* Print the working directory for attachments */
293                         printf("%s\n", cwd);
294                         break;
295
296                 case LSCMDSW:
297                         /*
298                         ** List files in the current attachment working
299                         ** directory
300                         **
301                         ** Use the user's shell so that we can take
302                         ** advantage of any syntax that the user is
303                         ** accustomed to.
304                         */
305                         writelscmd(buf, sizeof(buf), argp);
306                         system_in_dir(cwd, buf);
307                         break;
308
309                 case ALISTCMDSW:
310                         /*
311                         ** List attachments on current draft.
312                         */
313                         snprintf(buf, sizeof buf, "anno -list -comp '%s' "
314                                         "-number all -text IGNORE '%s'",
315                                         attach_hdr, drft);
316                         if (system(buf) != 0) {
317                                 advise(NULL, "Could not list attachment headers.");
318                         }
319                         break;
320
321                 case ATTACHCMDSW:
322                         /*
323                         ** Attach files to current draft.
324                         */
325
326                         if (*(argp+1) == NULL) {
327                                 advise(NULL, "attach command requires file argument(s).");
328                                 break;
329                         }
330
331                         /*
332                         ** Build a command line that causes the user's
333                         ** shell to list the file name arguments.
334                         ** This handles and wildcard expansion, tilde
335                         ** expansion, etc.
336                         */
337                         writelscmd(buf, sizeof(buf), argp);
338
339                         /*
340                         ** Read back the response from the shell,
341                         ** which contains a number of lines with one
342                         ** file name per line.  Remove off the newline.
343                         ** Determine whether we have an absolute or
344                         ** relative path name.  Prepend the current
345                         ** working directory to relative path names.
346                         ** Add the attachment annotation to the draft.
347                         */
348                         if ((f = popen_in_dir(cwd, buf, "r"))) {
349                                 char buf[BUFSIZ];
350
351                                 while (fgets(shell, sizeof(shell), f)) {
352                                         *(strchr(shell, '\n')) = '\0';
353
354                                         if (*shell == '/')
355                                                 sprintf(file, "%s", shell);
356                                         else {
357                                                 sprintf(file, "%s/%s", cwd,
358                                                                 shell);
359                                         }
360                                         snprintf(buf, sizeof buf,
361                                                         "anno -nodate -append "
362                                                         "-comp '%s' -text '%s'"
363                                                         " '%s'",
364                                                         attach_hdr, file,
365                                                         drft);
366                                         if (system(buf) != 0) {
367                                                 advise(NULL, "Could not add attachment header.");
368                                         }
369                                 }
370
371                                 pclose(f);
372                         } else {
373                                 advise("popen", "could not get file from shell");
374                         }
375
376                         break;
377
378                 case DETACHCMDSW:
379                         /*
380                         ** Detach files from current draft.
381                         **
382                         ** Scan the arguments for a -n.  Mixed file
383                         ** names and numbers aren't allowed, so this
384                         ** catches a -n anywhere in the argument list.
385                         */
386                         for (n = 0, arguments = argp + 1;
387                                         *arguments != NULL;
388                                         arguments++) {
389                                 if (strcmp(*arguments, "-n") == 0) {
390                                                 n = 1;
391                                                 break;
392                                 }
393                         }
394
395                         /*
396                         ** A -n was found so interpret the arguments as
397                         ** attachment numbers.  Decrement any remaining
398                         ** argument number that is greater than the one
399                         ** just processed after processing each one so
400                         ** that the numbering stays correct.
401                         */
402                         if (n == 1) {
403                                 for (arguments=argp+1; *arguments;
404                                                 arguments++) {
405                                         if (strcmp(*arguments, "-n") == 0)
406                                                 continue;
407
408                                         if (**arguments != '\0') {
409                                                 char buf[BUFSIZ];
410
411                                                 n = atoi(*arguments);
412                                                 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
413                                                 if (system(buf) != 0) {
414                                                         advise(NULL, "Could not delete attachment header.");
415                                                 }
416
417                                                 for (argp=arguments+1; *argp;
418                                                                 argp++) {
419                                                         if (atoi(*argp) > n) {
420                                                                 if (atoi(*argp) == 1)
421                                                                         *argp = "";
422                                                                 else
423                                                                         sprintf(*argp, "%d", atoi(*argp) - 1);
424                                                         }
425                                                 }
426                                         }
427                                 }
428                                 break;
429                         }
430                         /* else */
431
432                         /*
433                         ** The arguments are interpreted as file names.
434                         ** Run them through the user's shell for wildcard
435                         ** expansion and other goodies.  Do this from
436                         ** the current working directory if the argument
437                         ** is not an absolute path name (does not begin
438                         ** with a /).
439                         **
440                         ** We feed all the file names to the shell at
441                         ** once, otherwise you can't provide a file name
442                         ** with a space in it.
443                         */
444                         writelscmd(buf, sizeof(buf), argp);
445                         if ((f = popen_in_dir(cwd, buf, "r"))) {
446                                 while (fgets(shell, sizeof (shell), f)) {
447                                         *(strchr(shell, '\n')) = '\0';
448                                         snprintf(buf, sizeof buf,
449                                                         "anno -delete -comp "
450                                                         "'%s' -text '%s' '%s'",
451                                                         attach_hdr, shell,
452                                                         drft);
453                                         if (system(buf) != 0) {
454                                                 advise(NULL, "Could not delete attachment header.");
455                                         }
456                                 }
457                                 pclose(f);
458                         } else {
459                                 advise("popen", "could not get file from shell");
460                         }
461
462                         break;
463
464                 default:
465                         /* Unknown command */
466                         advise(NULL, "say what?");
467                         break;
468                 }
469         }
470         /*NOTREACHED*/
471 }
472
473
474
475 /*
476 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
477 ** trailcmd".
478 */
479 static void
480 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
481 {
482         char *cp;
483         /*
484         ** Note that we do not quote -- the argp from the user
485         ** is assumed to be quoted as they desire. (We can't treat
486         ** it as pure literal as that would prevent them using ~,
487         ** wildcards, etc.) The buffer produced by this function
488         ** should be given to popen_in_dir() or system_in_dir() so
489         ** that the current working directory is set correctly.
490         */
491         int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
492         /*
493         ** NB that some snprintf() return -1 on overflow rather than the
494         ** new C99 mandated 'number of chars that would have been written'
495         */
496         /*
497         ** length checks here and inside the loop allow for the
498         ** trailing ';', trailcmd, '"' and NUL
499         */
500         int trailln = strlen(trailcmd) + 3;
501         if (ln < 0 || ln + trailln > bufsz)
502                 adios(NULL, "arguments too long");
503
504         cp = buf + ln;
505
506         while (*++argp) {
507                 ln = strlen(*argp);
508                 /* +1 for leading space */
509                 if (ln + trailln + 1 > bufsz - (cp-buf))
510                         adios(NULL, "arguments too long");
511                 *cp++ = ' ';
512                 memcpy(cp, *argp, ln+1);
513                 cp += ln;
514         }
515         if (*trailcmd) {
516                 *cp++ = ';';
517                 strcpy(cp, trailcmd);
518                 cp += trailln - 3;
519         }
520         *cp++ = '"';
521         *cp = 0;
522 }
523
524 /*
525 ** Build a command line that causes the user's shell to list the file name
526 ** arguments.  This handles and wildcard expansion, tilde expansion, etc.
527 */
528 static void
529 writelscmd(char *buf, int bufsz, char **argp)
530 {
531         writesomecmd(buf, bufsz, "ls", "", argp);
532 }
533
534 /*
535 ** Like system(), but run the command in directory dir.
536 ** This assumes the program is single-threaded!
537 */
538 static int
539 system_in_dir(const char *dir, const char *cmd)
540 {
541         char olddir[BUFSIZ];
542         int r;
543         if (getcwd(olddir, sizeof(olddir)) == 0)
544                 adios("getcwd", "could not get working directory");
545         if (chdir(dir) != 0)
546                 adios("chdir", "could not change working directory");
547         r = system(cmd);
548         if (chdir(olddir) != 0)
549                 adios("chdir", "could not change working directory");
550         return r;
551 }
552
553 /* ditto for popen() */
554 static FILE*
555 popen_in_dir(const char *dir, const char *cmd, const char *type)
556 {
557         char olddir[BUFSIZ];
558         FILE *f;
559         if (getcwd(olddir, sizeof(olddir)) == 0)
560                 adios("getcwd", "could not get working directory");
561         if (chdir(dir) != 0)
562                 adios("chdir", "could not change working directory");
563         f = popen(cmd, type);
564         if (chdir(olddir) != 0)
565                 adios("chdir", "could not change working directory");
566         return f;
567 }
568
569
570 /*
571 ** EDIT
572 */
573
574 static char *edsave = NULL;  /* the editor we used previously */
575
576
577 static int
578 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
579         char *altmsg, char *cwd)
580 {
581         int pid, status, vecp;
582         char altpath[BUFSIZ], linkpath[BUFSIZ];
583         char *cp, *vec[MAXARGS];
584         struct stat st;
585
586 #ifdef HAVE_LSTAT
587         int slinked = 0;
588 #endif /* HAVE_LSTAT */
589
590         if (!*ed || !**ed) {
591                 /* We have no explicit editor. */
592                 if (edsave) {
593                         /* Use the previous editor ... */
594                         *ed = edsave;
595                         if (!(cp = mhbasename(*ed)))
596                                 cp = *ed;
597
598                         /* but prefer one specified via "editor-next" */
599                         cp = concat(cp, "-next", NULL);
600                         if ((cp = context_find(cp)))
601                                 *ed = cp;
602                 } else {
603                         /* set initial editor */
604                         *ed = defaulteditor;
605                 }
606         }
607
608         if (altmsg) {
609                 if (!mp || *altmsg == '/' || !cwd)
610                         strncpy(altpath, altmsg, sizeof(altpath));
611                 else
612                         snprintf(altpath, sizeof(altpath), "%s/%s",
613                                         mp->foldpath, altmsg);
614                 if (!cwd)
615                         strncpy(linkpath, altmsglink, sizeof(linkpath));
616                 else
617                         snprintf(linkpath, sizeof(linkpath), "%s/%s",
618                                         cwd, altmsglink);
619                 unlink(linkpath);
620 #ifdef HAVE_LSTAT
621                 if (link(altpath, linkpath) == NOTOK) {
622                         symlink(altpath, linkpath);
623                         slinked = 1;
624                 } else {
625                         slinked = 0;
626                 }
627 #else /* not HAVE_LSTAT */
628                 link(altpath, linkpath);
629 #endif /* not HAVE_LSTAT */
630         }
631
632         context_save();  /* save the context file */
633         fflush(stdout);
634
635         switch (pid = fork()) {
636         case NOTOK:
637                 advise("fork", "unable to");
638                 status = NOTOK;
639                 break;
640
641         case OK:
642                 if (cwd)
643                         chdir(cwd);
644                 if (altmsg) {
645                         if (mp)
646                                 m_putenv("mhfolder", mp->foldpath);
647                         m_putenv("editalt", altpath);
648                 }
649
650                 vecp = 0;
651                 vec[vecp++] = mhbasename(*ed);
652                 if (arg)
653                         while (*arg)
654                                 vec[vecp++] = *arg++;
655                 vec[vecp++] = file;
656                 vec[vecp] = NULL;
657
658                 execvp(*ed, vec);
659                 fprintf(stderr, "%s: unable to exec ", invo_name);
660                 perror(*ed);
661                 _exit(-1);
662
663         default:
664                 if ((status = pidwait(pid, NOTOK))) {
665                         if ((status & 0xff00) == 0xff00) {
666                                 /* cmd not found or pidwait() failed */
667                                 status = -1;
668                                 break;
669                         }
670                         if (status & 0x00ff) {
671                                 /* terminated by signal */
672                                 advise(NULL, "%s terminated by signal %d",
673                                                 *ed, status & 0x7f);
674                         } else {
675                                 /* failure exit */
676                                 advise(NULL, "%s exited with return code %d",
677                                                 *ed, (status & 0xff00) >> 8);
678                         }
679                         status = -1;
680                         break;
681                 }
682
683 #ifdef HAVE_LSTAT
684                 if (altmsg && mp && !is_readonly(mp) && (slinked ?
685                                 lstat (linkpath, &st) != NOTOK &&
686                                 S_ISREG(st.st_mode) &&
687                                 copyf(linkpath, altpath) == NOTOK :
688                                 stat(linkpath, &st) != NOTOK &&
689                                 st.st_nlink == 1 &&
690                                 (unlink(altpath) == NOTOK ||
691                                 link(linkpath, altpath) == NOTOK)))
692                         advise(linkpath, "unable to update %s from", altmsg);
693 #else /* HAVE_LSTAT */
694                 if (altmsg && mp && !is_readonly(mp) &&
695                                 stat(linkpath, &st) != NOTOK &&
696                                 st.st_nlink == 1 &&
697                                 (unlink(altpath) == NOTOK ||
698                                 link(linkpath, altpath) == NOTOK))
699                         advise(linkpath, "unable to update %s from", altmsg);
700 #endif /* HAVE_LSTAT */
701         }
702
703         /* remember which editor we used */
704         edsave = getcpy(*ed);
705
706         *ed = NULL;
707         if (altmsg) {
708                 unlink(linkpath);
709         }
710
711         return status;
712 }
713
714
715 #ifdef HAVE_LSTAT
716 static int
717 copyf(char *ifile, char *ofile)
718 {
719         int i, in, out;
720         char buffer[BUFSIZ];
721
722         if ((in = open(ifile, O_RDONLY)) == NOTOK)
723                 return NOTOK;
724         if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
725                 admonish(ofile, "unable to open and truncate");
726                 close(in);
727                 return NOTOK;
728         }
729
730         while ((i = read(in, buffer, sizeof(buffer))) > OK)
731                 if (write(out, buffer, i) != i) {
732                         advise(ofile, "may have damaged");
733                         i = NOTOK;
734                         break;
735                 }
736
737         close(in);
738         close(out);
739         return i;
740 }
741 #endif /* HAVE_LSTAT */
742
743
744 /*
745 ** SEND
746 */
747
748 static int
749 sendfile(char **arg, char *file)
750 {
751         pid_t child_id;
752         int vecp;
753         char *vec[MAXARGS];
754
755         context_save();  /* save the context file */
756         fflush(stdout);
757
758         switch (child_id = fork()) {
759         case NOTOK:
760                 advise(NULL, "unable to fork, so sending directly...");
761                 /* fall */
762         case OK:
763                 vecp = 0;
764                 vec[vecp++] = invo_name;
765                 if (arg)
766                         while (*arg)
767                                 vec[vecp++] = *arg++;
768                 vec[vecp++] = file;
769                 vec[vecp] = NULL;
770
771                 execvp("send", vec);
772                 fprintf(stderr, "%s: unable to exec ", invo_name);
773                 perror("send");
774                 _exit(-1);
775
776         default:
777                 if (pidwait(child_id, OK) == 0)
778                         done(0);
779                 return 1;
780         }
781 }
782
783
784 /*
785 ** refile msg into another folder
786 */
787 static int
788 refile(char **arg, char *file)
789 {
790         pid_t pid;
791         register int vecp;
792         char *vec[MAXARGS];
793
794         vecp = 0;
795         vec[vecp++] = "refile";
796         vec[vecp++] = "-nolink";  /* override bad .mh_profile defaults */
797         vec[vecp++] = "-file";
798         vec[vecp++] = file;
799
800         while (arg && *arg) {
801                 vec[vecp++] = *arg++;
802         }
803         vec[vecp] = NULL;
804
805         context_save();  /* save the context file */
806         fflush(stdout);
807
808         switch (pid = fork()) {
809         case -1:
810                 advise("fork", "unable to");
811                 return -1;
812
813         case 0:
814                 execvp(*vec, vec);
815                 fprintf(stderr, "%s: unable to exec ", invo_name);
816                 perror(*vec);
817                 _exit(-1);
818
819         default:
820                 return (pidwait(pid, -1));
821         }
822 }
823
824
825 /*
826 ** Remove the draft file
827 */
828
829 static int
830 removefile(char *drft)
831 {
832         if (unlink(drft) == NOTOK)
833                 adios(drft, "unable to unlink");
834
835         return OK;
836 }