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