Removed the mime command from whatnow and automimeproc (automhnproc).
[mmh] / uip / whatnowsbr.c
1 /*
2 ** whatnowsbr.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 void sendit(char *, char **, char *, int);
104 static int removefile(char *);
105 static void writelscmd(char *, int, char **);
106 static void writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp);
107 static FILE* popen_in_dir(const char *dir, const char *cmd, const char *type);
108 static int system_in_dir(const char *dir, const char *cmd);
109
110
111 #ifdef HAVE_LSTAT
112 static int copyf(char *, char *);
113 #endif
114
115
116 int
117 WhatNow(int argc, char **argv)
118 {
119         int nedit = 0, use = 0;
120         char *cp;
121         char *ed = NULL, *drft = NULL, *msgnam = NULL;
122         char buf[BUFSIZ], prompt[BUFSIZ];
123         char **argp, **arguments;
124         struct stat st;
125         char cwd[MAXPATHLEN + 1];  /* current working directory */
126         char file[MAXPATHLEN + 1];  /* file name buffer */
127         char shell[MAXPATHLEN + 1];  /* shell response buffer */
128         FILE *f;  /* read pointer for bgnd proc */
129         char *l;  /* set on -l to alist  command */
130         int n;  /* set on -n to alist command */
131
132         invo_name = mhbasename(argv[0]);
133
134         /* read user profile/context */
135         context_read();
136
137         arguments = getarguments(invo_name, argc, argv, 1);
138         argp = arguments;
139
140         /*
141         ** Get the initial current working directory.
142         */
143
144         if (getcwd(cwd, sizeof (cwd)) == NULL) {
145                 adios("getcwd", "could not get working directory");
146         }
147
148         while ((cp = *argp++)) {
149                 if (*cp == '-') {
150                         switch (smatch(++cp, whatnowswitches)) {
151                         case AMBIGSW:
152                                 ambigsw(cp, whatnowswitches);
153                                 done(1);
154                         case UNKWNSW:
155                                 adios(NULL, "-%s unknown", cp);
156
157                         case HELPSW:
158                                 snprintf(buf, sizeof(buf),
159                                                 "%s [switches] [file]",
160                                                 invo_name);
161                                 print_help(buf, whatnowswitches, 1);
162                                 done(1);
163                         case VERSIONSW:
164                                 print_version(invo_name);
165                                 done(1);
166
167                         case EDITRSW:
168                                 if (!(ed = *argp++) || *ed == '-')
169                                         adios(NULL, "missing argument to %s",
170                                                         argp[-2]);
171                                 nedit = 0;
172                                 continue;
173                         case NEDITSW:
174                                 nedit++;
175                                 continue;
176
177                         case PRMPTSW:
178                                 if (!(myprompt = *argp++) || *myprompt == '-')
179                                         adios(NULL, "missing argument to %s",
180                                                         argp[-2]);
181                                 continue;
182
183                         }
184                 }
185                 if (drft)
186                         adios(NULL, "only one draft at a time!");
187                 else
188                         drft = cp;
189         }
190
191         if ((drft == NULL && (drft = getenv("mhdraft")) == NULL) || *drft == 0)
192                 drft = getcpy(m_draft(seq_cur));
193
194         msgnam = (cp = getenv("mhaltmsg")) && *cp ? getcpy(cp) : NULL;
195
196         if ((cp = getenv("mhuse")) && *cp)
197                 use = atoi(cp);
198
199         if (ed == NULL && ((ed = getenv("mheditor")) == NULL || *ed == 0)) {
200                 ed = NULL;
201                 nedit++;
202         }
203
204         /* start editing the draft, unless -noedit was given */
205         if (!nedit && editfile(&ed, NULL, drft, use, NULL, msgnam, NULL, 1)
206                         < 0)
207                 done(1);
208
209         snprintf(prompt, sizeof(prompt), myprompt, invo_name);
210         for (;;) {
211                 if (!(argp = getans(prompt, aleqs))) {
212                         unlink(altmsglink);
213                         done(1);
214                 }
215                 switch (smatch(*argp, aleqs)) {
216                 case DISPSW:
217                         /*
218                         ** display the message being replied to,
219                         ** or distributed
220                         */
221                         if (msgnam)
222                                 showfile(++argp, msgnam);
223                         else
224                                 advise(NULL, "no alternate message to display");
225                         break;
226
227                 case EDITSW:
228                         /* Call an editor on the draft file */
229                         if (*++argp)
230                                 ed = *argp++;
231                         if (editfile(&ed, argp, drft, NOUSE, NULL,
232                                         msgnam, NULL, 1) == NOTOK)
233                                 done(1);
234                         break;
235
236                 case LISTSW:
237                         /* display the draft file */
238                         showfile(++argp, drft);
239                         break;
240
241                 case QUITSW:
242                         /* Quit, and possibly delete the draft */
243                         if (*++argp && (*argp[0] == 'd' ||
244                                 ((*argp)[0] == '-' && (*argp)[1] == 'd'))) {
245                                 removefile(drft);
246                         } else {
247                                 if (stat(drft, &st) != NOTOK)
248                                         advise(NULL, "draft left on %s", drft);
249                         }
250                         done(1);
251
252                 case DELETESW:
253                         /* Delete draft and exit */
254                         removefile(drft);
255                         done(1);
256
257                 case PUSHSW:
258                         /* Send draft in background */
259                         if (sendfile(++argp, drft, 1))
260                                 done(1);
261                         break;
262
263                 case SENDSW:
264                         /* Send draft */
265                         sendfile(++argp, drft, 0);
266                         break;
267
268                 case REFILEOPT:
269                         /* Refile the draft */
270                         if (refile(++argp, drft) == 0)
271                                 done(0);
272                         break;
273
274                 case CDCMDSW:
275                         /*
276                         ** Change the working directory for attachments
277                         **
278                         ** Run the directory through the user's shell
279                         ** so that we can take advantage of any syntax
280                         ** that the user is accustomed to.  Read back
281                         ** the absolute path.
282                         */
283
284                         if (*(argp+1) == NULL) {
285                                 sprintf(buf, "$SHELL -c \"cd;pwd\"");
286                         } else {
287                                 writesomecmd(buf, BUFSIZ, "cd", "pwd", argp);
288                         }
289                         if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
290                                 fgets(cwd, sizeof (cwd), f);
291
292                                 if (strchr(cwd, '\n') != NULL)
293                                                 *strchr(cwd, '\n') = '\0';
294
295                                 pclose(f);
296                         } else {
297                                 advise("popen", "could not get directory");
298                         }
299
300                         break;
301
302                 case PWDCMDSW:
303                         /* Print the working directory for attachments */
304                         printf("%s\n", cwd);
305                         break;
306
307                 case LSCMDSW:
308                         /*
309                         ** List files in the current attachment working
310                         ** directory
311                         **
312                         ** Use the user's shell so that we can take
313                         ** advantage of any syntax that the user is
314                         ** accustomed to.
315                         */
316                         writelscmd(buf, sizeof(buf), argp);
317                         system_in_dir(cwd, buf);
318                         break;
319
320                 case ALISTCMDSW:
321                         /*
322                         ** List attachments on current draft.  Options are:
323                         **
324                         ** -l    long listing (full path names)
325                         ** -n    numbers listing
326                         */
327
328                         l = NULL;
329                         n = 0;
330
331                         while (*++argp != NULL) {
332                                 if (strcmp(*argp, "-l") == 0)
333                                         l = "/";
334
335                                 else if (strcmp(*argp, "-n") == 0)
336                                         n = 1;
337
338                                 else if (strcmp(*argp, "-ln") == 0 ||
339                                                 strcmp(*argp, "-nl") == 0) {
340                                         l = "/";
341                                         n = 1;
342                                 } else {
343                                         n = -1;
344                                         break;
345                                 }
346                         }
347
348                         if (n == -1)
349                                 advise(NULL, "usage is alist [-ln].");
350
351                         else
352                                 annolist(drft, attach_hdr, l, n);
353
354                         break;
355
356                 case ATTACHCMDSW:
357                         /*
358                         ** Attach files to current draft.
359                         */
360
361                         if (*(argp+1) == NULL) {
362                                 advise(NULL, "attach command requires file argument(s).");
363                                 break;
364                         }
365
366                         /*
367                         ** Build a command line that causes the user's
368                         ** shell to list the file name arguments.
369                         ** This handles and wildcard expansion, tilde
370                         ** expansion, etc.
371                         */
372                         writelscmd(buf, sizeof(buf), argp);
373
374                         /*
375                         ** Read back the response from the shell,
376                         ** which contains a number of lines with one
377                         ** file name per line.  Remove off the newline.
378                         ** Determine whether we have an absolute or
379                         ** relative path name.  Prepend the current
380                         ** working directory to relative path names.
381                         ** Add the attachment annotation to the draft.
382                         */
383                         if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
384                                 while (fgets(shell, sizeof (shell), f)
385                                                 != NULL) {
386                                         *(strchr(shell, '\n')) = '\0';
387
388                                         if (*shell == '/')
389                                                 annotate(drft, attach_hdr,
390                                                                 shell, 1, 0,
391                                                                 -2, 1);
392                                         else {
393                                                 sprintf(file, "%s/%s", cwd,
394                                                                 shell);
395                                                 annotate(drft, attach_hdr,
396                                                                 file, 1, 0,
397                                                                 -2, 1);
398                                         }
399                                 }
400
401                                 pclose(f);
402                         } else {
403                                 advise("popen", "could not get file from shell");
404                         }
405
406                         break;
407
408                 case DETACHCMDSW:
409                         /*
410                         ** Detach files from current draft.
411                         **
412                         ** Scan the arguments for a -n.  Mixed file
413                         ** names and numbers aren't allowed, so this
414                         ** catches a -n anywhere in the argument list.
415                         */
416
417                         for (n = 0, arguments = argp + 1;
418                                         *arguments != NULL;
419                                         arguments++) {
420                                 if (strcmp(*arguments, "-n") == 0) {
421                                                 n = 1;
422                                                 break;
423                                 }
424                         }
425
426                         /*
427                         ** A -n was found so interpret the arguments as
428                         ** attachment numbers.  Decrement any remaining
429                         ** argument number that is greater than the one
430                         ** just processed after processing each one so
431                         ** that the numbering stays correct.
432                         */
433
434                         if (n == 1) {
435                                 for (arguments = argp + 1;
436                                                 *arguments != NULL;
437                                                 arguments++) {
438                                         if (strcmp(*arguments, "-n") == 0)
439                                                 continue;
440
441                                         if (**arguments != '\0') {
442                                                 n = atoi(*arguments);
443                                                 annotate(drft, attach_hdr,
444                                                                 NULL, 1, 0,
445                                                                 n, 1);
446
447                                                 for (argp = arguments + 1; *argp != NULL; argp++) {
448                                                         if (atoi(*argp) > n) {
449                                                                 if (atoi(*argp) == 1)
450                                                                         *argp = "";
451                                                                 else
452                                                                         sprintf(*argp, "%d", atoi(*argp) - 1);
453                                                         }
454                                                 }
455                                         }
456                                 }
457                         }
458
459                         /*
460                         ** The arguments are interpreted as file names.
461                         ** Run them through the user's shell for wildcard
462                         ** expansion and other goodies.  Do this from
463                         ** the current working directory if the argument
464                         ** is not an absolute path name (does not begin
465                         ** with a /).
466                         **
467                         ** We feed all the file names to the shell at
468                         ** once, otherwise you can't provide a file name
469                         ** with a space in it.
470                         */
471                         writelscmd(buf, sizeof(buf), argp);
472                         if ((f = popen_in_dir(cwd, buf, "r")) != (FILE *)0) {
473                                 while (fgets(shell, sizeof (shell), f)
474                                                 != NULL) {
475                                         *(strchr(shell, '\n')) = '\0';
476                                         annotate(drft, attach_hdr, shell,
477                                                         1, 0, 0, 1);
478                                 }
479                                 pclose(f);
480                         } else {
481                                 advise("popen", "could not get file from shell");
482                         }
483
484                         break;
485
486                 default:
487                         /* Unknown command */
488                         advise(NULL, "say what?");
489                         break;
490                 }
491         }
492         /*NOTREACHED*/
493 }
494
495
496
497 /*
498 ** Build a command line of the form $SHELL -c "cd 'cwd'; cmd argp ... ;
499 ** trailcmd".
500 */
501 static void
502 writesomecmd(char *buf, int bufsz, char *cmd, char *trailcmd, char **argp)
503 {
504         char *cp;
505         /*
506         ** Note that we do not quote -- the argp from the user
507         ** is assumed to be quoted as they desire. (We can't treat
508         ** it as pure literal as that would prevent them using ~,
509         ** wildcards, etc.) The buffer produced by this function
510         ** should be given to popen_in_dir() or system_in_dir() so
511         ** that the current working directory is set correctly.
512         */
513         int ln = snprintf(buf, bufsz, "$SHELL -c \"%s", cmd);
514         /*
515         ** NB that some snprintf() return -1 on overflow rather than the
516         ** new C99 mandated 'number of chars that would have been written'
517         */
518         /*
519         ** length checks here and inside the loop allow for the
520         ** trailing ';', trailcmd, '"' and NUL
521         */
522         int trailln = strlen(trailcmd) + 3;
523         if (ln < 0 || ln + trailln > bufsz)
524                 adios(NULL, "arguments too long");
525
526         cp = buf + ln;
527
528         while (*++argp != NULL) {
529                 ln = strlen(*argp);
530                 /* +1 for leading space */
531                 if (ln + trailln + 1 > bufsz - (cp-buf))
532                         adios(NULL, "arguments too long");
533                 *cp++ = ' ';
534                 memcpy(cp, *argp, ln+1);
535                 cp += ln;
536         }
537         if (*trailcmd) {
538                 *cp++ = ';';
539                 strcpy(cp, trailcmd);
540                 cp += trailln - 3;
541         }
542         *cp++ = '"';
543         *cp = 0;
544 }
545
546 /*
547 ** Build a command line that causes the user's shell to list the file name
548 ** arguments.  This handles and wildcard expansion, tilde expansion, etc.
549 */
550 static void
551 writelscmd(char *buf, int bufsz, char **argp)
552 {
553         writesomecmd(buf, bufsz, "ls", "", argp);
554 }
555
556 /*
557 ** Like system(), but run the command in directory dir.
558 ** This assumes the program is single-threaded!
559 */
560 static int
561 system_in_dir(const char *dir, const char *cmd)
562 {
563         char olddir[BUFSIZ];
564         int r;
565         if (getcwd(olddir, sizeof(olddir)) == 0)
566                 adios("getcwd", "could not get working directory");
567         if (chdir(dir) != 0)
568                 adios("chdir", "could not change working directory");
569         r = system(cmd);
570         if (chdir(olddir) != 0)
571                 adios("chdir", "could not change working directory");
572         return r;
573 }
574
575 /* ditto for popen() */
576 static FILE*
577 popen_in_dir(const char *dir, const char *cmd, const char *type)
578 {
579         char olddir[BUFSIZ];
580         FILE *f;
581         if (getcwd(olddir, sizeof(olddir)) == 0)
582                 adios("getcwd", "could not get working directory");
583         if (chdir(dir) != 0)
584                 adios("chdir", "could not change working directory");
585         f = popen(cmd, type);
586         if (chdir(olddir) != 0)
587                 adios("chdir", "could not change working directory");
588         return f;
589 }
590
591
592 /*
593 ** EDIT
594 */
595
596 static int  reedit = 0;  /* have we been here before? */
597 static char *edsave = NULL;  /* the editor we used previously */
598
599
600 static int
601 editfile(char **ed, char **arg, char *file, int use, struct msgs *mp,
602         char *altmsg, char *cwd, int save_editor)
603 {
604         int pid, status, vecp;
605         char altpath[BUFSIZ], linkpath[BUFSIZ];
606         char *cp, *vec[MAXARGS];
607         struct stat st;
608
609 #ifdef HAVE_LSTAT
610         int slinked = 0;
611 #if 0
612         int oumask;  /* PJS: for setting permissions on symlinks. */
613 #endif
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 #if 0
652                         /* I don't think permission on symlinks matters /JLR */
653                         /* PJS: else symlinks are world 'r' */
654                         oumask = umask(0044);
655 #endif
656                         symlink(altpath, linkpath);
657 #if 0
658                         umask(oumask);  /* PJS: else symlinks are world 'r' */
659 #endif
660                         slinked = 1;
661                 } else {
662                         slinked = 0;
663                 }
664 #else /* not HAVE_LSTAT */
665                 link(altpath, linkpath);
666 #endif /* not HAVE_LSTAT */
667         }
668
669         context_save();  /* save the context file */
670         fflush(stdout);
671
672         switch (pid = fork()) {
673         case NOTOK:
674                 advise("fork", "unable to");
675                 status = NOTOK;
676                 break;
677
678         case OK:
679                 if (cwd)
680                         chdir(cwd);
681                 if (altmsg) {
682                         if (mp)
683                                 m_putenv("mhfolder", mp->foldpath);
684                         m_putenv("editalt", altpath);
685                 }
686
687                 vecp = 0;
688                 vec[vecp++] = mhbasename(*ed);
689                 if (arg)
690                         while (*arg)
691                                 vec[vecp++] = *arg++;
692                 vec[vecp++] = file;
693                 vec[vecp] = NULL;
694
695                 execvp(*ed, vec);
696                 fprintf(stderr, "unable to exec ");
697                 perror(*ed);
698                 _exit(-1);
699
700         default:
701                 if ((status = pidwait(pid, NOTOK))) {
702 #ifdef ATTVIBUG
703                         if ((cp = mhbasename(*ed)) && strcmp(cp, "vi") == 0 &&
704                                         (status & 0x00ff) == 0)
705                                 status = 0;
706                         else {
707 #endif
708                         if (((status & 0xff00) != 0xff00)
709                                 && (!reedit || (status & 0x00ff))) {
710                                 if (!use && (status & 0xff00) && (rename(file, cp = m_backup (file)) != NOTOK)) {
711                                         advise(NULL, "problems with edit--draft left in %s", cp);
712                                 } else {
713                                         advise(NULL, "problems with edit--%s preserved", file);
714                                 }
715                         }
716                         status = -2;  /* maybe "reedit ? -2 : -1"? */
717                         break;
718 #ifdef ATTVIBUG
719                         }
720 #endif
721                 }
722
723                 reedit++;
724 #ifdef HAVE_LSTAT
725                 if (altmsg && mp && !is_readonly(mp) && (slinked ?
726                                 lstat (linkpath, &st) != NOTOK &&
727                                 S_ISREG(st.st_mode) &&
728                                 copyf(linkpath, altpath) == NOTOK :
729                                 stat(linkpath, &st) != NOTOK &&
730                                 st.st_nlink == 1 &&
731                                 (unlink(altpath) == NOTOK ||
732                                 link(linkpath, altpath) == NOTOK)))
733                         advise(linkpath, "unable to update %s from", altmsg);
734 #else /* HAVE_LSTAT */
735                 if (altmsg && mp && !is_readonly(mp) &&
736                                 stat(linkpath, &st) != NOTOK &&
737                                 st.st_nlink == 1 &&
738                                 (unlink(altpath) == NOTOK ||
739                                 link(linkpath, altpath) == NOTOK))
740                         advise(linkpath, "unable to update %s from", altmsg);
741 #endif /* HAVE_LSTAT */
742         }
743
744         /* normally, we remember which editor we used */
745         if (save_editor)
746                 edsave = getcpy(*ed);
747
748         *ed = NULL;
749         if (altmsg)
750                 unlink(linkpath);
751
752         return status;
753 }
754
755
756 #ifdef HAVE_LSTAT
757 static int
758 copyf(char *ifile, char *ofile)
759 {
760         int i, in, out;
761         char buffer[BUFSIZ];
762
763         if ((in = open(ifile, O_RDONLY)) == NOTOK)
764                 return NOTOK;
765         if ((out = open(ofile, O_WRONLY | O_TRUNC)) == NOTOK) {
766                 admonish(ofile, "unable to open and truncate");
767                 close(in);
768                 return NOTOK;
769         }
770
771         while ((i = read(in, buffer, sizeof(buffer))) > OK)
772                 if (write(out, buffer, i) != i) {
773                         advise(ofile, "may have damaged");
774                         i = NOTOK;
775                         break;
776                 }
777
778         close(in);
779         close(out);
780         return i;
781 }
782 #endif /* HAVE_LSTAT */
783
784
785 /*
786 ** SEND
787 */
788
789 static int
790 sendfile(char **arg, char *file, int pushsw)
791 {
792         pid_t child_id;
793         int i, vecp;
794         char *cp, *sp, *vec[MAXARGS];
795
796         /*
797         ** If the sendproc is the nmh command `send', then we call
798         ** those routines directly rather than exec'ing the command.
799         */
800         if (strcmp(sp = mhbasename(sendproc), "send") == 0) {
801                 cp = invo_name;
802                 sendit(invo_name = sp, arg, file, pushsw);
803                 invo_name = cp;
804                 return 1;
805         }
806
807         context_save();  /* save the context file */
808         fflush(stdout);
809
810         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
811                 sleep(5);
812         switch (child_id) {
813         case NOTOK:
814                 advise(NULL, "unable to fork, so sending directly...");
815         case OK:
816                 vecp = 0;
817                 vec[vecp++] = invo_name;
818                 if (pushsw)
819                         vec[vecp++] = "-push";
820                 if (arg)
821                         while (*arg)
822                                 vec[vecp++] = *arg++;
823                 vec[vecp++] = file;
824                 vec[vecp] = NULL;
825
826                 execvp(sendproc, vec);
827                 fprintf(stderr, "unable to exec ");
828                 perror(sendproc);
829                 _exit(-1);
830
831         default:
832                 if (pidwait(child_id, OK) == 0)
833                         done(0);
834                 return 1;
835         }
836 }
837
838
839 static struct swit  sendswitches[] = {
840 #define ALIASW  0
841         { "alias aliasfile", 0 },
842 #define DEBUGSW  1
843         { "debug", -5 },
844 #define FILTSW  2
845         { "filter filterfile", 0 },
846 #define NFILTSW  3
847         { "nofilter", 0 },
848 #define FRMTSW  4
849         { "format", 0 },
850 #define NFRMTSW  5
851         { "noformat", 0 },
852 #define FORWSW  6
853         { "forward", 0 },
854 #define NFORWSW  7
855         { "noforward", 0 },
856 #define MIMESW  8
857         { "mime", 0 },
858 #define NMIMESW  9
859         { "nomime", 0 },
860 #define MSGDSW  10
861         { "msgid", 0 },
862 #define NMSGDSW  11
863         { "nomsgid", 0 },
864 #define SPSHSW  12
865         { "push", 0 },
866 #define NSPSHSW  13
867         { "nopush", 0 },
868 #define UNIQSW  14
869         { "unique", -6 },
870 #define NUNIQSW  15
871         { "nounique", -8 },
872 #define VERBSW  16
873         { "verbose", 0 },
874 #define NVERBSW  17
875         { "noverbose", 0 },
876 #define WATCSW  18
877         { "watch", 0 },
878 #define NWATCSW  19
879         { "nowatch", 0 },
880 #define WIDTHSW  20
881         { "width columns", 0 },
882 #define SVERSIONSW  21
883         { "version", 0 },
884 #define SHELPSW  22
885         { "help", 0 },
886 #define BITSTUFFSW  23
887         { "dashstuffing", -12 },
888 #define NBITSTUFFSW  24
889         { "nodashstuffing", -14 },
890 #define MAILSW  25
891         { "mail", -4 },
892 #define SAMLSW  26
893         { "saml", -4 },
894 #define SSNDSW  27
895         { "send", -4 },
896 #define SOMLSW  28
897         { "soml", -4 },
898 #define CLIESW  29
899         { "client host", -6 },
900 #define SERVSW  30
901         { "server host", 6 },
902 #define SNOOPSW  31
903         { "snoop", -5 },
904 #define PORTSW  32
905         { "port server-port-name/number", 4 },
906         { NULL, 0 }
907 };
908
909
910 extern int debugsw;  /* from sendsbr.c */
911 extern int forwsw;
912 extern int inplace;
913 extern int pushsw;
914 extern int unique;
915 extern int verbsw;
916
917 extern char *altmsg;  /*  .. */
918 extern char *annotext;
919 extern char *distfile;
920
921
922 static void
923 sendit(char *sp, char **arg, char *file, int pushed)
924 {
925         int vecp, n = 1;
926         char *cp, buf[BUFSIZ], **argp;
927         char **arguments, *vec[MAXARGS];
928         struct stat st;
929
930 #ifndef lint
931         int distsw = 0;
932 #endif
933 #ifdef UCI
934         FILE *fp;
935 #endif
936
937         /*
938         ** Make sure these are defined.  In particular, we need
939         ** vec[1] to be NULL, in case "arg" is NULL below.  It
940         ** doesn't matter what is the value of vec[0], but we
941         ** set it to NULL, to help catch "off-by-one" errors.
942         */
943         vec[0] = NULL;
944         vec[1] = NULL;
945
946         /*
947         ** Temporarily copy arg to vec, since the brkstring() call in
948         ** getarguments() will wipe it out before it is merged in.
949         ** Also, we skip the first element of vec, since getarguments()
950         ** skips it.  Then we count the number of arguments
951         ** copied.  The value of "n" will be one greater than
952         ** this in order to simulate the standard argc/argv.
953         */
954         if (arg) {
955                 char **bp;
956
957                 copyip(arg, vec+1, MAXARGS-1);
958                 bp = vec+1;
959                 while (*bp++)
960                         n++;
961         }
962
963         /*
964         ** Merge any arguments from command line (now in vec)
965         ** and arguments from profile.
966         */
967         arguments = getarguments (sp, n, vec, 1);
968         argp = arguments;
969
970         debugsw = 0;
971         forwsw = 1;
972         inplace = 1;
973         unique = 0;
974
975         altmsg = NULL;
976         annotext = NULL;
977         distfile = NULL;
978
979         vecp = 1;  /* we'll get the zero'th element later */
980         vec[vecp++] = "-library";
981         vec[vecp++] = getcpy(toabsdir("+"));
982
983         while ((cp = *argp++)) {
984                 if (*cp == '-') {
985                         switch (smatch(++cp, sendswitches)) {
986                         case AMBIGSW:
987                                 ambigsw(cp, sendswitches);
988                                 return;
989                         case UNKWNSW:
990                                 advise(NULL, "-%s unknown\n", cp);
991                                 return;
992
993                         case SHELPSW:
994                                 snprintf(buf, sizeof(buf),
995                                                 "%s [switches]", sp);
996                                 print_help(buf, sendswitches, 1);
997                                 return;
998                         case SVERSIONSW:
999                                 print_version(invo_name);
1000                                 return;
1001
1002                         case SPSHSW:
1003                                 pushed++;
1004                                 continue;
1005                         case NSPSHSW:
1006                                 pushed = 0;
1007                                 continue;
1008
1009                         case UNIQSW:
1010                                 unique++;
1011                                 continue;
1012                         case NUNIQSW:
1013                                 unique = 0;
1014                                 continue;
1015                         case FORWSW:
1016                                 forwsw++;
1017                                 continue;
1018                         case NFORWSW:
1019                                 forwsw = 0;
1020                                 continue;
1021
1022                         case VERBSW:
1023                                 verbsw++;
1024                                 vec[vecp++] = --cp;
1025                                 continue;
1026                         case NVERBSW:
1027                                 verbsw = 0;
1028                                 vec[vecp++] = --cp;
1029                                 continue;
1030
1031                         case DEBUGSW:
1032                                 debugsw++;  /* fall */
1033                         case NFILTSW:
1034                         case FRMTSW:
1035                         case NFRMTSW:
1036                         case BITSTUFFSW:
1037                         case NBITSTUFFSW:
1038                         case MIMESW:
1039                         case NMIMESW:
1040                         case MSGDSW:
1041                         case NMSGDSW:
1042                         case WATCSW:
1043                         case NWATCSW:
1044                         case MAILSW:
1045                         case SAMLSW:
1046                         case SSNDSW:
1047                         case SOMLSW:
1048                         case SNOOPSW:
1049                                 vec[vecp++] = --cp;
1050                                 continue;
1051
1052                         case ALIASW:
1053                         case FILTSW:
1054                         case WIDTHSW:
1055                         case CLIESW:
1056                         case SERVSW:
1057                         case PORTSW:
1058                                 vec[vecp++] = --cp;
1059                                 if (!(cp = *argp++) || *cp == '-') {
1060                                         advise(NULL, "missing argument to %s",
1061                                                         argp[-2]);
1062                                         return;
1063                                 }
1064                                 vec[vecp++] = cp;
1065                                 continue;
1066
1067                         }
1068                 }
1069                 advise(NULL, "usage: %s [switches]", sp);
1070                 return;
1071         }
1072
1073         /* allow Aliasfile: profile entry */
1074         if ((cp = context_find("Aliasfile"))) {
1075                 char **ap, *dp;
1076
1077                 dp = getcpy(cp);
1078                 for (ap = brkstring(dp, " ", "\n"); ap && *ap; ap++) {
1079                         vec[vecp++] = "-alias";
1080                         vec[vecp++] = *ap;
1081                 }
1082         }
1083
1084         if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
1085                 if ((cp = context_find("signature")) && *cp)
1086                         m_putenv("SIGNATURE", cp);
1087 #ifdef UCI
1088                 else {
1089                         snprintf(buf, sizeof(buf), "%s/.signature", mypath);
1090                         if ((fp = fopen(buf, "r")) != NULL &&
1091                                         fgets(buf, sizeof(buf), fp) != NULL) {
1092                                 fclose(fp);
1093                                 if (cp = strchr(buf, '\n'))
1094                                         *cp = 0;
1095                                 m_putenv("SIGNATURE", buf);
1096                         }
1097                 }
1098 #endif /* UCI */
1099
1100         if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
1101                 annotext = NULL;
1102         if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
1103                 altmsg = NULL;
1104         if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
1105                 inplace = atoi(cp);
1106
1107         if ((cp = getenv("mhdist")) && *cp
1108 #ifndef lint
1109                         && (distsw = atoi (cp))
1110 #endif /* not lint */
1111                         && altmsg) {
1112                 vec[vecp++] = "-dist";
1113                 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
1114                 if (link(altmsg, distfile) == NOTOK)
1115                         adios(distfile, "unable to link %s to", altmsg);
1116         } else {
1117                 distfile = NULL;
1118         }
1119
1120         if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
1121                 st.st_mtime = 0;
1122                 st.st_dev = 0;
1123                 st.st_ino = 0;
1124         }
1125         if ((pushsw = pushed))
1126                 push();
1127
1128         vec[0] = mhbasename(postproc);
1129         closefds(3);
1130
1131         if (sendsbr(vec, vecp, file, &st, 1) == OK)
1132                 done(0);
1133 }
1134
1135
1136 /*
1137 ** Remove the draft file
1138 */
1139
1140 static int
1141 removefile(char *drft)
1142 {
1143         if (unlink(drft) == NOTOK)
1144                 adios(drft, "unable to unlink");
1145
1146         return OK;
1147 }