08e3be84738f005fb22716e0e41aaf22f035b150
[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                         done(1);
201                 }
202         }
203
204         snprintf(prompt, sizeof(prompt), myprompt, invo_name);
205         for (;;) {
206                 if (!(argp = getans(prompt, aleqs))) {
207                         unlink(altmsglink);
208                         done(1);
209                 }
210                 switch (smatch(*argp, aleqs)) {
211                 case DISPSW:
212                         /* display the msg being replied to or distributed */
213                         if (msgnam) {
214                                 snprintf(buf, sizeof buf, "%s '%s'",
215                                                 listproc, msgnam);
216                                 system(buf);
217                         } else {
218                                 advise(NULL, "no alternate message to display");
219                         }
220                         break;
221
222                 case EDITSW:
223                         /* Call an editor on the draft file */
224                         if (*++argp)
225                                 ed = *argp++;
226                         if (editfile(&ed, argp, drft, NOUSE, NULL,
227                                         msgnam, NULL) == NOTOK)
228                                 done(1);
229                         break;
230
231                 case LISTSW:
232                         /* display the draft file */
233                         snprintf(buf, sizeof buf, "%s '%s'", listproc, drft);
234                         system(buf);
235                         break;
236
237                 case QUITSW:
238                         /* quit */
239                         if (stat(drft, &st) != NOTOK) {
240                                 advise(NULL, "draft left on %s", drft);
241                         }
242                         done(1);
243
244                 case DELETESW:
245                         /* Delete draft and exit */
246                         removefile(drft);
247                         done(1);
248
249                 case SENDSW:
250                         /* Send draft */
251                         sendfile(++argp, drft);
252                         break;
253
254                 case REFILEOPT:
255                         /* Refile the draft */
256                         if (refile(++argp, drft) == 0) {
257                                 done(0);
258                         }
259                         break;
260
261                 case CDCMDSW:
262                         /*
263                         ** Change the working directory for attachments
264                         **
265                         ** Run the directory through the user's shell
266                         ** so that we can take advantage of any syntax
267                         ** that the user is accustomed to.  Read back
268                         ** the absolute path.
269                         */
270
271                         if (*(argp+1) == NULL) {
272                                 sprintf(buf, "$SHELL -c \"cd;pwd\"");
273                         } else {
274                                 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
275                         }
276                         if ((f = popen_in_dir(cwd, buf, "r"))) {
277                                 fgets(cwd, sizeof (cwd), f);
278
279                                 if (strchr(cwd, '\n'))
280                                         *strchr(cwd, '\n') = '\0';
281
282                                 pclose(f);
283                         } else {
284                                 advise("popen", "could not get directory");
285                         }
286
287                         break;
288
289                 case PWDCMDSW:
290                         /* Print the working directory for attachments */
291                         printf("%s\n", cwd);
292                         break;
293
294                 case LSCMDSW:
295                         /*
296                         ** List files in the current attachment working
297                         ** directory
298                         **
299                         ** Use the user's shell so that we can take
300                         ** advantage of any syntax that the user is
301                         ** accustomed to.
302                         */
303                         writelscmd(buf, sizeof(buf), argp);
304                         system_in_dir(cwd, buf);
305                         break;
306
307                 case ALISTCMDSW:
308                         /*
309                         ** List attachments on current draft.
310                         */
311                         snprintf(buf, sizeof buf, "anno -list -comp '%s' "
312                                         "-number all -text IGNORE '%s'",
313                                         attach_hdr, drft);
314                         if (system(buf) != 0) {
315                                 advise(NULL, "Could not list attachment headers.");
316                         }
317                         break;
318
319                 case ATTACHCMDSW:
320                         /*
321                         ** Attach files to current draft.
322                         */
323
324                         if (*(argp+1) == NULL) {
325                                 advise(NULL, "attach command requires file argument(s).");
326                                 break;
327                         }
328
329                         /*
330                         ** Build a command line that causes the user's
331                         ** shell to list the file name arguments.
332                         ** This handles and wildcard expansion, tilde
333                         ** expansion, etc.
334                         */
335                         writelscmd(buf, sizeof(buf), argp);
336
337                         /*
338                         ** Read back the response from the shell,
339                         ** which contains a number of lines with one
340                         ** file name per line.  Remove off the newline.
341                         ** Determine whether we have an absolute or
342                         ** relative path name.  Prepend the current
343                         ** working directory to relative path names.
344                         ** Add the attachment annotation to the draft.
345                         */
346                         if ((f = popen_in_dir(cwd, buf, "r"))) {
347                                 char buf[BUFSIZ];
348
349                                 while (fgets(shell, sizeof(shell), f)) {
350                                         *(strchr(shell, '\n')) = '\0';
351
352                                         if (*shell == '/')
353                                                 sprintf(file, "%s", shell);
354                                         else {
355                                                 sprintf(file, "%s/%s", cwd,
356                                                                 shell);
357                                         }
358                                         snprintf(buf, sizeof buf,
359                                                         "anno -nodate -append "
360                                                         "-comp '%s' -text '%s'"
361                                                         " '%s'",
362                                                         attach_hdr, file,
363                                                         drft);
364                                         if (system(buf) != 0) {
365                                                 advise(NULL, "Could not add attachment header.");
366                                         }
367                                 }
368
369                                 pclose(f);
370                         } else {
371                                 advise("popen", "could not get file from shell");
372                         }
373
374                         break;
375
376                 case DETACHCMDSW:
377                         /*
378                         ** Detach files from current draft.
379                         **
380                         ** Scan the arguments for a -n.  Mixed file
381                         ** names and numbers aren't allowed, so this
382                         ** catches a -n anywhere in the argument list.
383                         */
384                         for (n = 0, arguments = argp + 1;
385                                         *arguments != NULL;
386                                         arguments++) {
387                                 if (strcmp(*arguments, "-n") == 0) {
388                                                 n = 1;
389                                                 break;
390                                 }
391                         }
392
393                         /*
394                         ** A -n was found so interpret the arguments as
395                         ** attachment numbers.  Decrement any remaining
396                         ** argument number that is greater than the one
397                         ** just processed after processing each one so
398                         ** that the numbering stays correct.
399                         */
400                         if (n == 1) {
401                                 for (arguments=argp+1; *arguments;
402                                                 arguments++) {
403                                         if (strcmp(*arguments, "-n") == 0)
404                                                 continue;
405
406                                         if (**arguments != '\0') {
407                                                 char buf[BUFSIZ];
408
409                                                 n = atoi(*arguments);
410                                                 snprintf(buf, sizeof buf, "anno -delete -comp '%s' -number '%d' '%s'", attach_hdr, n, drft);
411                                                 if (system(buf) != 0) {
412                                                         advise(NULL, "Could not delete attachment header.");
413                                                 }
414
415                                                 for (argp=arguments+1; *argp;
416                                                                 argp++) {
417                                                         if (atoi(*argp) > n) {
418                                                                 if (atoi(*argp) == 1)
419                                                                         *argp = "";
420                                                                 else
421                                                                         sprintf(*argp, "%d", atoi(*argp) - 1);
422                                                         }
423                                                 }
424                                         }
425                                 }
426                                 break;
427                         }
428                         /* else */
429
430                         /*
431                         ** The arguments are interpreted as file names.
432                         ** Run them through the user's shell for wildcard
433                         ** expansion and other goodies.  Do this from
434                         ** the current working directory if the argument
435                         ** is not an absolute path name (does not begin
436                         ** with a /).
437                         **
438                         ** We feed all the file names to the shell at
439                         ** once, otherwise you can't provide a file name
440                         ** with a space in it.
441                         */
442                         writelscmd(buf, sizeof(buf), argp);
443                         if ((f = popen_in_dir(cwd, buf, "r"))) {
444                                 while (fgets(shell, sizeof (shell), f)) {
445                                         *(strchr(shell, '\n')) = '\0';
446                                         snprintf(buf, sizeof buf,
447                                                         "anno -delete -comp "
448                                                         "'%s' -text '%s' '%s'",
449                                                         attach_hdr, shell,
450                                                         drft);
451                                         if (system(buf) != 0) {
452                                                 advise(NULL, "Could not delete attachment header.");
453                                         }
454                                 }
455                                 pclose(f);
456                         } else {
457                                 advise("popen", "could not get file from shell");
458                         }
459
460                         break;
461
462                 default:
463                         /* Unknown command */
464                         advise(NULL, "say what?");
465                         break;
466                 }
467         }
468         /*NOTREACHED*/
469 }
470
471
472
473 /*
474 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
475 ** trailcmd".
476 */
477 static void
478 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
479 {
480         char *cp;
481         /*
482         ** Note that we do not quote -- the argp from the user
483         ** is assumed to be quoted as they desire. (We can't treat
484         ** it as pure literal as that would prevent them using ~,
485         ** wildcards, etc.) The buffer produced by this function
486         ** should be given to popen_in_dir() or system_in_dir() so
487         ** that the current working directory is set correctly.
488         */
489         int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
490         /*
491         ** NB that some snprintf() return -1 on overflow rather than the
492         ** new C99 mandated 'number of chars that would have been written'
493         */
494         /*
495         ** length checks here and inside the loop allow for the
496         ** trailing ';', trailcmd, '"' and NUL
497         */
498         int trailln = strlen(trailcmd) + 3;
499         if (ln < 0 || ln + trailln > bufsz)
500                 adios(NULL, "arguments too long");
501
502         cp = buf + ln;
503
504         while (*++argp) {
505                 ln = strlen(*argp);
506                 /* +1 for leading space */
507                 if (ln + trailln + 1 > bufsz - (cp-buf))
508                         adios(NULL, "arguments too long");
509                 *cp++ = ' ';
510                 memcpy(cp, *argp, ln+1);
511                 cp += ln;
512         }
513         if (*trailcmd) {
514                 *cp++ = ';';
515                 strcpy(cp, trailcmd);
516                 cp += trailln - 3;
517         }
518         *cp++ = '"';
519         *cp = 0;
520 }
521
522 /*
523 ** Build a command line that causes the user's shell to list the file name
524 ** arguments.  This handles and wildcard expansion, tilde expansion, etc.
525 */
526 static void
527 writelscmd(char *buf, int bufsz, char **argp)
528 {
529         writesomecmd(buf, bufsz, "ls", "", argp);
530 }
531
532 /*
533 ** Like system(), but run the command in directory dir.
534 ** This assumes the program is single-threaded!
535 */
536 static int
537 system_in_dir(const char *dir, const char *cmd)
538 {
539         char olddir[BUFSIZ];
540         int r;
541         if (getcwd(olddir, sizeof(olddir)) == 0)
542                 adios("getcwd", "could not get working directory");
543         if (chdir(dir) != 0)
544                 adios("chdir", "could not change working directory");
545         r = system(cmd);
546         if (chdir(olddir) != 0)
547                 adios("chdir", "could not change working directory");
548         return r;
549 }
550
551 /* ditto for popen() */
552 static FILE*
553 popen_in_dir(const char *dir, const char *cmd, const char *type)
554 {
555         char olddir[BUFSIZ];
556         FILE *f;
557         if (getcwd(olddir, sizeof(olddir)) == 0)
558                 adios("getcwd", "could not get working directory");
559         if (chdir(dir) != 0)
560                 adios("chdir", "could not change working directory");
561         f = popen(cmd, type);
562         if (chdir(olddir) != 0)
563                 adios("chdir", "could not change working directory");
564         return f;
565 }
566
567
568 /*
569 ** EDIT
570 */
571
572 static char *edsave = NULL;  /* the editor we used previously */
573
574
575 static int
576 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
577         char *altmsg, char *cwd)
578 {
579         int pid, status, vecp;
580         char altpath[BUFSIZ], linkpath[BUFSIZ];
581         char *cp, *vec[MAXARGS];
582         struct stat st;
583
584 #ifdef HAVE_LSTAT
585         int slinked = 0;
586 #endif /* HAVE_LSTAT */
587
588         if (!*ed || !**ed) {
589                 /* We have no explicit editor. */
590                 if (edsave) {
591                         /* Use the previous editor ... */
592                         *ed = edsave;
593                         if (!(cp = mhbasename(*ed)))
594                                 cp = *ed;
595
596                         /* but prefer one specified via "editor-next" */
597                         cp = concat(cp, "-next", NULL);
598                         if ((cp = context_find(cp)))
599                                 *ed = cp;
600                 } else {
601                         /* set initial editor */
602                         *ed = defaulteditor;
603                 }
604         }
605
606         if (altmsg) {
607                 if (!mp || *altmsg == '/' || !cwd)
608                         strncpy(altpath, altmsg, sizeof(altpath));
609                 else
610                         snprintf(altpath, sizeof(altpath), "%s/%s",
611                                         mp->foldpath, altmsg);
612                 if (!cwd)
613                         strncpy(linkpath, altmsglink, sizeof(linkpath));
614                 else
615                         snprintf(linkpath, sizeof(linkpath), "%s/%s",
616                                         cwd, altmsglink);
617                 unlink(linkpath);
618 #ifdef HAVE_LSTAT
619                 if (link(altpath, linkpath) == NOTOK) {
620                         symlink(altpath, linkpath);
621                         slinked = 1;
622                 } else {
623                         slinked = 0;
624                 }
625 #else /* not HAVE_LSTAT */
626                 link(altpath, linkpath);
627 #endif /* not HAVE_LSTAT */
628         }
629
630         context_save();  /* save the context file */
631         fflush(stdout);
632
633         switch (pid = fork()) {
634         case NOTOK:
635                 advise("fork", "unable to");
636                 status = NOTOK;
637                 break;
638
639         case OK:
640                 if (cwd)
641                         chdir(cwd);
642                 if (altmsg) {
643                         if (mp)
644                                 m_putenv("mhfolder", mp->foldpath);
645                         m_putenv("editalt", altpath);
646                 }
647
648                 vecp = 0;
649                 vec[vecp++] = mhbasename(*ed);
650                 if (arg)
651                         while (*arg)
652                                 vec[vecp++] = *arg++;
653                 vec[vecp++] = file;
654                 vec[vecp] = NULL;
655
656                 execvp(*ed, vec);
657                 fprintf(stderr, "unable to exec ");
658                 perror(*ed);
659                 _exit(-1);
660
661         default:
662                 if ((status = pidwait(pid, NOTOK))) {
663                         if (((status & 0xff00) != 0xff00) &&
664                                         (!edsave || (status & 0x00ff))) {
665                                 if (!use && (status & 0xff00) && (rename(file, cp = m_backup(file)) != NOTOK)) {
666                                         advise(NULL, "problems with edit--draft left in %s", cp);
667                                 } else {
668                                         advise(NULL, "problems with edit--%s preserved", file);
669                                 }
670                         }
671                         status = -2;  /* maybe "edsave ? -2 : -1"? */
672                         break;
673                 }
674
675 #ifdef HAVE_LSTAT
676                 if (altmsg && mp && !is_readonly(mp) && (slinked ?
677                                 lstat (linkpath, &st) != NOTOK &&
678                                 S_ISREG(st.st_mode) &&
679                                 copyf(linkpath, altpath) == NOTOK :
680                                 stat(linkpath, &st) != NOTOK &&
681                                 st.st_nlink == 1 &&
682                                 (unlink(altpath) == NOTOK ||
683                                 link(linkpath, altpath) == NOTOK)))
684                         advise(linkpath, "unable to update %s from", altmsg);
685 #else /* HAVE_LSTAT */
686                 if (altmsg && mp && !is_readonly(mp) &&
687                                 stat(linkpath, &st) != NOTOK &&
688                                 st.st_nlink == 1 &&
689                                 (unlink(altpath) == NOTOK ||
690                                 link(linkpath, altpath) == NOTOK))
691                         advise(linkpath, "unable to update %s from", altmsg);
692 #endif /* HAVE_LSTAT */
693         }
694
695         /* remember which editor we used */
696         edsave = getcpy(*ed);
697
698         *ed = NULL;
699         if (altmsg)
700                 unlink(linkpath);
701
702         return status;
703 }
704
705
706 #ifdef HAVE_LSTAT
707 static int
708 copyf(char *ifile, char *ofile)
709 {
710         int i, in, out;
711         char buffer[BUFSIZ];
712
713         if ((in = open(ifile, O_RDONLY)) == NOTOK)
714                 return NOTOK;
715         if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
716                 admonish(ofile, "unable to open and truncate");
717                 close(in);
718                 return NOTOK;
719         }
720
721         while ((i = read(in, buffer, sizeof(buffer))) > OK)
722                 if (write(out, buffer, i) != i) {
723                         advise(ofile, "may have damaged");
724                         i = NOTOK;
725                         break;
726                 }
727
728         close(in);
729         close(out);
730         return i;
731 }
732 #endif /* HAVE_LSTAT */
733
734
735 /*
736 ** SEND
737 */
738
739 static int
740 sendfile(char **arg, char *file)
741 {
742         pid_t child_id;
743         int vecp;
744         char *vec[MAXARGS];
745
746         context_save();  /* save the context file */
747         fflush(stdout);
748
749         switch (child_id = fork()) {
750         case NOTOK:
751                 advise(NULL, "unable to fork, so sending directly...");
752                 /* fall */
753         case OK:
754                 vecp = 0;
755                 vec[vecp++] = invo_name;
756                 if (arg)
757                         while (*arg)
758                                 vec[vecp++] = *arg++;
759                 vec[vecp++] = file;
760                 vec[vecp] = NULL;
761
762                 execvp("send", vec);
763                 fprintf(stderr, "unable to exec ");
764                 perror("send");
765                 _exit(-1);
766
767         default:
768                 if (pidwait(child_id, OK) == 0)
769                         done(0);
770                 return 1;
771         }
772 }
773
774
775 /*
776 ** refile msg into another folder
777 */
778 static int
779 refile(char **arg, char *file)
780 {
781         pid_t pid;
782         register int vecp;
783         char *vec[MAXARGS];
784
785         vecp = 0;
786         vec[vecp++] = "refile";
787         vec[vecp++] = "-nolink";  /* override bad .mh_profile defaults */
788         vec[vecp++] = "-file";
789         vec[vecp++] = file;
790
791         while (arg && *arg) {
792                 vec[vecp++] = *arg++;
793         }
794         vec[vecp] = NULL;
795
796         context_save();  /* save the context file */
797         fflush(stdout);
798
799         switch (pid = fork()) {
800         case -1:
801                 advise("fork", "unable to");
802                 return -1;
803
804         case 0:
805                 execvp(*vec, vec);
806                 fprintf(stderr, "unable to exec ");
807                 perror(*vec);
808                 _exit(-1);
809
810         default:
811                 return (pidwait(pid, -1));
812         }
813 }
814
815
816 /*
817 ** Remove the draft file
818 */
819
820 static int
821 removefile(char *drft)
822 {
823         if (unlink(drft) == NOTOK)
824                 adios(drft, "unable to unlink");
825
826         return OK;
827 }