Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / mail / collect.c
index a89360f..ac1f105 100644 (file)
@@ -1,4 +1,23 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)collect.c  5.16 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -7,8 +26,6 @@
  * ~ escapes.
  */
 
  * ~ escapes.
  */
 
-static char *SccsId = "@(#)collect.c   2.8 %G%";
-
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
 
@@ -20,117 +37,115 @@ static char *SccsId = "@(#)collect.c      2.8 %G%";
 /*
  * The following hokiness with global variables is so that on
  * receipt of an interrupt signal, the partial message can be salted
 /*
  * The following hokiness with global variables is so that on
  * receipt of an interrupt signal, the partial message can be salted
- * away on dead.letter.  The output file must be available to flush,
- * and the input to read.  Several open files could be saved all through
- * Mail if stdio allowed simultaneous read/write access.
+ * away on dead.letter.
  */
 
  */
 
-static int     (*savesig)();           /* Previous SIGINT value */
+static int     (*saveint)();           /* Previous SIGINT value */
 static int     (*savehup)();           /* Previous SIGHUP value */
 static int     (*savehup)();           /* Previous SIGHUP value */
-# ifdef VMUNIX
 static int     (*savecont)();          /* Previous SIGCONT value */
 static int     (*savecont)();          /* Previous SIGCONT value */
-# endif VMUNIX
-static FILE    *newi;                  /* File for saving away */
-static FILE    *newo;                  /* Output side of same */
-static int     hf;                     /* Ignore interrups */
+static FILE    *collf;                 /* File for saving away */
 static int     hadintr;                /* Have seen one SIGINT so far */
 
 static jmp_buf coljmp;                 /* To get back to work */
 
 FILE *
 static int     hadintr;                /* Have seen one SIGINT so far */
 
 static jmp_buf coljmp;                 /* To get back to work */
 
 FILE *
-collect(hp)
+collect(hp, printheaders)
        struct header *hp;
 {
        struct header *hp;
 {
-       FILE *ibuf, *fbuf, *obuf;
-       int lc, cc, escape, collrub(), intack(), collhup, collcont(), eof;
+       FILE *fbuf;
+       int lc, cc, escape, eof;
+       int collrub(), intack(), collcont();
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
-       int notify();
-       extern collintsig(), collhupsig();
+       char getsub;
+       int omask;
 
        noreset++;
 
        noreset++;
-       ibuf = obuf = NULL;
-       if (value("ignore") != NOSTR)
-               hf = 1;
-       else
-               hf = 0;
-       hadintr = 0;
-# ifdef VMUNIX
-       if ((savesig = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
-               sigset(SIGINT, hf ? intack : collrub), sighold(SIGINT);
-       if ((savehup = sigset(SIGHUP, SIG_IGN)) != SIG_IGN)
-               sigset(SIGHUP, collrub), sighold(SIGHUP);
-       savecont = sigset(SIGCONT, collcont);
-# else VMUNIX
-       savesig = signal(SIGINT, SIG_IGN);
-       savehup = signal(SIGHUP, SIG_IGN);
-# endif VMUNIX
-       newi = NULL;
-       newo = NULL;
-       if ((obuf = fopen(tempMail, "w")) == NULL) {
-               perror(tempMail);
+       collf = NULL;
+
+       /*
+        * Start catching signals from here, but we're still die on interrupts
+        * until we're in the main loop.
+        */
+       omask = sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
+       if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
+               signal(SIGINT, value("ignore") != NOSTR ? intack : collrub);
+       if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
+               signal(SIGHUP, collrub);
+       savecont = signal(SIGCONT, SIG_DFL);
+       if (setjmp(coljmp)) {
+               remove(tempMail);
                goto err;
        }
                goto err;
        }
-       newo = obuf;
-       if ((ibuf = fopen(tempMail, "r")) == NULL) {
+       sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP)));
+
+       if ((collf = fopen(tempMail, "w+")) == NULL) {
                perror(tempMail);
                perror(tempMail);
-               newo = NULL;
-               fclose(obuf);
                goto err;
        }
                goto err;
        }
-       newi = ibuf;
-       remove(tempMail);
+       unlink(tempMail);
 
        /*
         * If we are going to prompt for a subject,
         * refrain from printing a newline after
         * the headers (since some people mind).
         */
 
        /*
         * If we are going to prompt for a subject,
         * refrain from printing a newline after
         * the headers (since some people mind).
         */
-
        t = GTO|GSUBJECT|GCC|GNL;
        t = GTO|GSUBJECT|GCC|GNL;
-       c = 0;
-       if (intty && sflag == NOSTR && hp->h_subject == NOSTR && value("ask"))
-               t &= ~GNL, c++;
-       if (hp->h_seq != 0) {
+       getsub = 0;
+       if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
+           (value("ask") != NOSTR || value("asksub") != NOSTR))
+               t &= ~GNL, getsub++;
+       if (printheaders) {
                puthead(hp, stdout, t);
                fflush(stdout);
        }
                puthead(hp, stdout, t);
                fflush(stdout);
        }
-       if (c)
-               grabh(hp, GSUBJECT);
-       escape = ESCAPE;
        if ((cp = value("escape")) != NOSTR)
                escape = *cp;
        if ((cp = value("escape")) != NOSTR)
                escape = *cp;
+       else
+               escape = ESCAPE;
        eof = 0;
        eof = 0;
+       hadintr = 0;
+
+       if (!setjmp(coljmp)) {
+               signal(SIGCONT, collcont);
+               if (getsub)
+                       grabh(hp, GSUBJECT);
+       } else {
+               /*
+                * Come here for printing the after-signal message.
+                * Duplicate messages won't be printed because
+                * the write is aborted if we get a SIGTTOU.
+                */
+cont:
+               if (hadintr) {
+                       fflush(stdout);
+                       fprintf(stderr,
+                       "\n(Interrupt -- one more to kill letter)\n");
+               } else {
+                       printf("(continue)\n");
+                       fflush(stdout);
+               }
+       }
        for (;;) {
        for (;;) {
-               setjmp(coljmp);
-# ifdef VMUNIX
-               sigrelse(SIGINT);
-               sigrelse(SIGHUP);
-# else VMUNIX
-               if (savesig != SIG_IGN)
-                       signal(SIGINT, hf ? intack : collintsig);
-               if (savehup != SIG_IGN)
-                       signal(SIGHUP, collhupsig);
-# endif VMUNIX
-               flush();
-               if (readline(stdin, linebuf) <= 0) {
-                       if (intty && value("ignoreeof") != NOSTR) {
+               if (readline(stdin, linebuf) < 0) {
+                       if (value("interactive") != NOSTR &&
+                           value("ignoreeof") != NOSTR) {
                                if (++eof > 35)
                                        break;
                                if (++eof > 35)
                                        break;
-                               printf("Use \".\" to terminate letter\n",
-                                   escape);
+                               printf("Use \".\" to terminate letter\n");
                                continue;
                        }
                        break;
                }
                eof = 0;
                hadintr = 0;
                                continue;
                        }
                        break;
                }
                eof = 0;
                hadintr = 0;
-               if (intty && equal(".", linebuf) &&
+               if (linebuf[0] == '.' && linebuf[1] == '\0' &&
+                   value("interactive") != NOSTR &&
                    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
                        break;
                    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
                        break;
-               if (linebuf[0] != escape || rflag != NOSTR) {
-                       if ((t = putline(obuf, linebuf)) < 0)
+               if (linebuf[0] != escape || value("interactive") == NOSTR) {
+                       if (putline(collf, linebuf) < 0)
                                goto err;
                        continue;
                }
                                goto err;
                        continue;
                }
@@ -141,122 +156,90 @@ collect(hp)
                         * On double escape, just send the single one.
                         * Otherwise, it's an error.
                         */
                         * On double escape, just send the single one.
                         * Otherwise, it's an error.
                         */
-
                        if (c == escape) {
                        if (c == escape) {
-                               if (putline(obuf, &linebuf[1]) < 0)
+                               if (putline(collf, &linebuf[1]) < 0)
                                        goto err;
                                else
                                        break;
                        }
                        printf("Unknown tilde escape.\n");
                        break;
                                        goto err;
                                else
                                        break;
                        }
                        printf("Unknown tilde escape.\n");
                        break;
-
                case 'C':
                        /*
                         * Dump core.
                         */
                case 'C':
                        /*
                         * Dump core.
                         */
-
                        core();
                        break;
                        core();
                        break;
-
                case '!':
                        /*
                         * Shell escape, send the balance of the
                         * line to sh -c.
                         */
                case '!':
                        /*
                         * Shell escape, send the balance of the
                         * line to sh -c.
                         */
-
                        shell(&linebuf[2]);
                        break;
                        shell(&linebuf[2]);
                        break;
-
                case ':':
                case ':':
-               case '_':
                        /*
                         * Escape to command mode, but be nice!
                         */
                        /*
                         * Escape to command mode, but be nice!
                         */
-
                        execute(&linebuf[2], 1);
                        execute(&linebuf[2], 1);
-                       break;
-
+                       goto cont;
                case '.':
                        /*
                         * Simulate end of file on input.
                         */
                case '.':
                        /*
                         * Simulate end of file on input.
                         */
-                       goto eofl;
-
+                       goto out;
                case 'q':
                case 'q':
-               case 'Q':
                        /*
                         * Force a quit of sending mail.
                         * Act like an interrupt happened.
                         */
                        /*
                         * Force a quit of sending mail.
                         * Act like an interrupt happened.
                         */
-
                        hadintr++;
                        collrub(SIGINT);
                        exit(1);
                        hadintr++;
                        collrub(SIGINT);
                        exit(1);
-
                case 'h':
                        /*
                         * Grab a bunch of headers.
                         */
                case 'h':
                        /*
                         * Grab a bunch of headers.
                         */
-                       if (!intty || !outtty) {
-                               printf("~h: no can do!?\n");
-                               break;
-                       }
                        grabh(hp, GTO|GSUBJECT|GCC|GBCC);
                        grabh(hp, GTO|GSUBJECT|GCC|GBCC);
-                       printf("(continue)\n");
-                       break;
-
+                       goto cont;
                case 't':
                        /*
                         * Add to the To list.
                         */
                case 't':
                        /*
                         * Add to the To list.
                         */
-
-                       hp->h_to = addto(hp->h_to, &linebuf[2]);
-                       hp->h_seq++;
+                       hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
                        break;
                        break;
-
                case 's':
                        /*
                         * Set the Subject list.
                         */
                case 's':
                        /*
                         * Set the Subject list.
                         */
-
                        cp = &linebuf[2];
                        cp = &linebuf[2];
-                       while (any(*cp, " \t"))
+                       while (isspace(*cp))
                                cp++;
                        hp->h_subject = savestr(cp);
                                cp++;
                        hp->h_subject = savestr(cp);
-                       hp->h_seq++;
                        break;
                        break;
-
                case 'c':
                        /*
                         * Add to the CC list.
                         */
                case 'c':
                        /*
                         * Add to the CC list.
                         */
-
-                       hp->h_cc = addto(hp->h_cc, &linebuf[2]);
-                       hp->h_seq++;
+                       hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
                        break;
                        break;
-
                case 'b':
                        /*
                         * Add stuff to blind carbon copies list.
                         */
                case 'b':
                        /*
                         * Add stuff to blind carbon copies list.
                         */
-                       hp->h_bcc = addto(hp->h_bcc, &linebuf[2]);
-                       hp->h_seq++;
+                       hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
                        break;
                        break;
-
                case 'd':
                case 'd':
-                       copy(deadletter, &linebuf[2]);
+                       strcpy(linebuf + 2, getdeadletter());
                        /* fall into . . . */
                        /* fall into . . . */
-
                case 'r':
                        /*
                         * Invoke a file:
                         * Search for the file name,
                case 'r':
                        /*
                         * Invoke a file:
                         * Search for the file name,
-                        * then open it and copy the contents to obuf.
+                        * then open it and copy the contents to collf.
                         */
                         */
-
                        cp = &linebuf[2];
                        cp = &linebuf[2];
-                       while (any(*cp, " \t"))
+                       while (isspace(*cp))
                                cp++;
                        if (*cp == '\0') {
                                printf("Interpolate what file?\n");
                                cp++;
                        if (*cp == '\0') {
                                printf("Interpolate what file?\n");
@@ -266,7 +249,7 @@ collect(hp)
                        if (cp == NOSTR)
                                break;
                        if (isdir(cp)) {
                        if (cp == NOSTR)
                                break;
                        if (isdir(cp)) {
-                               printf("%s: directory\n");
+                               printf("%s: Directory\n", cp);
                                break;
                        }
                        if ((fbuf = fopen(cp, "r")) == NULL) {
                                break;
                        }
                        if ((fbuf = fopen(cp, "r")) == NULL) {
@@ -274,12 +257,12 @@ collect(hp)
                                break;
                        }
                        printf("\"%s\" ", cp);
                                break;
                        }
                        printf("\"%s\" ", cp);
-                       flush();
+                       fflush(stdout);
                        lc = 0;
                        cc = 0;
                        lc = 0;
                        cc = 0;
-                       while (readline(fbuf, linebuf) > 0) {
+                       while (readline(fbuf, linebuf) >= 0) {
                                lc++;
                                lc++;
-                               if ((t = putline(obuf, linebuf)) < 0) {
+                               if ((t = putline(collf, linebuf)) < 0) {
                                        fclose(fbuf);
                                        goto err;
                                }
                                        fclose(fbuf);
                                        goto err;
                                }
@@ -288,14 +271,12 @@ collect(hp)
                        fclose(fbuf);
                        printf("%d/%d\n", lc, cc);
                        break;
                        fclose(fbuf);
                        printf("%d/%d\n", lc, cc);
                        break;
-
                case 'w':
                        /*
                         * Write the message on a file.
                         */
                case 'w':
                        /*
                         * Write the message on a file.
                         */
-
                        cp = &linebuf[2];
                        cp = &linebuf[2];
-                       while (any(*cp, " \t"))
+                       while (*cp == ' ' || *cp == '\t')
                                cp++;
                        if (*cp == '\0') {
                                fprintf(stderr, "Write what file!?\n");
                                cp++;
                        if (*cp == '\0') {
                                fprintf(stderr, "Write what file!?\n");
@@ -303,77 +284,54 @@ collect(hp)
                        }
                        if ((cp = expand(cp)) == NOSTR)
                                break;
                        }
                        if ((cp = expand(cp)) == NOSTR)
                                break;
-                       fflush(obuf);
-                       rewind(ibuf);
-                       exwrite(cp, ibuf, 1);
+                       rewind(collf);
+                       exwrite(cp, collf, 1);
                        break;
                        break;
-
                case 'm':
                case 'm':
+               case 'M':
                case 'f':
                case 'f':
+               case 'F':
                        /*
                         * Interpolate the named messages, if we
                         * are in receiving mail mode.  Does the
                         * standard list processing garbage.
                         * If ~f is given, we don't shift over.
                         */
                        /*
                         * Interpolate the named messages, if we
                         * are in receiving mail mode.  Does the
                         * standard list processing garbage.
                         * If ~f is given, we don't shift over.
                         */
-
                        if (!rcvmode) {
                                printf("No messages to send from!?!\n");
                                break;
                        }
                        if (!rcvmode) {
                                printf("No messages to send from!?!\n");
                                break;
                        }
-                       cp = &linebuf[2];
-                       while (any(*cp, " \t"))
-                               cp++;
-                       if (forward(cp, obuf, c) < 0)
+                       if (forward(linebuf + 2, collf, c) < 0)
                                goto err;
                                goto err;
-                       printf("(continue)\n");
-                       break;
-
+                       goto cont;
                case '?':
                        if ((fbuf = fopen(THELPFILE, "r")) == NULL) {
                case '?':
                        if ((fbuf = fopen(THELPFILE, "r")) == NULL) {
-                               printf("No help just now.\n");
+                               perror(THELPFILE);
                                break;
                        }
                                break;
                        }
-                       t = getc(fbuf);
-                       while (t != -1) {
+                       while ((t = getc(fbuf)) != EOF)
                                putchar(t);
                                putchar(t);
-                               t = getc(fbuf);
-                       }
                        fclose(fbuf);
                        break;
                        fclose(fbuf);
                        break;
-
                case 'p':
                        /*
                         * Print out the current state of the
                         * message without altering anything.
                         */
                case 'p':
                        /*
                         * Print out the current state of the
                         * message without altering anything.
                         */
-
-                       fflush(obuf);
-                       rewind(ibuf);
+                       rewind(collf);
                        printf("-------\nMessage contains:\n");
                        puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
                        printf("-------\nMessage contains:\n");
                        puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
-                       t = getc(ibuf);
-                       while (t != EOF) {
+                       while ((t = getc(collf)) != EOF)
                                putchar(t);
                                putchar(t);
-                               t = getc(ibuf);
-                       }
-                       printf("(continue)\n");
-                       break;
-
-               case '^':
+                       goto cont;
                case '|':
                        /*
                         * Pipe message through command.
                         * Collect output as new message.
                         */
                case '|':
                        /*
                         * Pipe message through command.
                         * Collect output as new message.
                         */
-
-                       obuf = mespipe(ibuf, obuf, &linebuf[2]);
-                       newo = obuf;
-                       ibuf = newi;
-                       newi = ibuf;
-                       printf("(continue)\n");
-                       break;
-
+                       rewind(collf);
+                       mespipe(collf, &linebuf[2]);
+                       goto cont;
                case 'v':
                case 'e':
                        /*
                case 'v':
                case 'e':
                        /*
@@ -381,61 +339,35 @@ collect(hp)
                         * 'e' means to use EDITOR
                         * 'v' means to use VISUAL
                         */
                         * 'e' means to use EDITOR
                         * 'v' means to use VISUAL
                         */
-
-                       if ((obuf = mesedit(ibuf, obuf, c)) == NULL)
-                               goto err;
-                       newo = obuf;
-                       ibuf = newi;
-                       printf("(continue)\n");
-                       break;
-                       break;
+                       rewind(collf);
+                       mesedit(collf, c);
+                       goto cont;
                }
        }
                }
        }
-eofl:
-       fclose(obuf);
-       rewind(ibuf);
-       sigset(SIGINT, savesig);
-       sigset(SIGHUP, savehup);
-# ifdef VMUNIX
-       sigset(SIGCONT, savecont);
-# endif VMUNIX
-       noreset = 0;
-       return(ibuf);
-
+       goto out;
 err:
 err:
-       if (ibuf != NULL)
-               fclose(ibuf);
-       if (obuf != NULL)
-               fclose(obuf);
-       sigset(SIGINT, savesig);
-       sigset(SIGHUP, savehup);
-# ifdef VMUNIX
-       sigset(SIGCONT, savecont);
-# endif VMUNIX
-       noreset = 0;
-       return(NULL);
-}
-
-/*
- * Non destructively interrogate the value of the given signal.
- */
-
-psig(n)
-{
-       register (*wassig)();
-
-       wassig = sigset(n, SIG_IGN);
-       sigset(n, wassig);
-       return((int) wassig);
+       if (collf != NULL) {
+               fclose(collf);
+               collf = NULL;
+       }
+out:
+       if (collf != NULL)
+               rewind(collf);
+       signal(SIGINT, saveint);
+       signal(SIGHUP, savehup);
+       signal(SIGCONT, savecont);
+       sigsetmask(omask);
+       noreset--;
+       return collf;
 }
 
 /*
  * Write a file, ex-like if f set.
  */
 
 }
 
 /*
  * Write a file, ex-like if f set.
  */
 
-exwrite(name, ibuf, f)
+exwrite(name, fp, f)
        char name[];
        char name[];
-       FILE *ibuf;
+       FILE *fp;
 {
        register FILE *of;
        register int c;
 {
        register FILE *of;
        register int c;
@@ -450,7 +382,7 @@ exwrite(name, ibuf, f)
        if (stat(name, &junk) >= 0 && (junk.st_mode & S_IFMT) == S_IFREG) {
                if (!f)
                        fprintf(stderr, "%s: ", name);
        if (stat(name, &junk) >= 0 && (junk.st_mode & S_IFMT) == S_IFREG) {
                if (!f)
                        fprintf(stderr, "%s: ", name);
-               fprintf(stderr, "File exists\n", name);
+               fprintf(stderr, "File exists\n");
                return(-1);
        }
        if ((of = fopen(name, "w")) == NULL) {
                return(-1);
        }
        if ((of = fopen(name, "w")) == NULL) {
@@ -459,7 +391,7 @@ exwrite(name, ibuf, f)
        }
        lc = 0;
        cc = 0;
        }
        lc = 0;
        cc = 0;
-       while ((c = getc(ibuf)) != EOF) {
+       while ((c = getc(fp)) != EOF) {
                cc++;
                if (c == '\n')
                        lc++;
                cc++;
                if (c == '\n')
                        lc++;
@@ -477,193 +409,67 @@ exwrite(name, ibuf, f)
 }
 
 /*
 }
 
 /*
- * Edit the message being collected on ibuf and obuf.
- * Write the message out onto some poorly-named temp file
- * and point an editor at it.
- *
+ * Edit the message being collected on fp.
  * On return, make the edit file the new temp file.
  */
  * On return, make the edit file the new temp file.
  */
-
-FILE *
-mesedit(ibuf, obuf, c)
-       FILE *ibuf, *obuf;
+mesedit(fp, c)
+       FILE *fp;
 {
 {
-       int pid, s;
-       FILE *fbuf;
-       register int t;
-       int (*sig)(), (*scont)(), foonly();
-       struct stat sbuf;
-       extern char tempMail[], tempEdit[];
-       register char *edit;
-
-       sig = sigset(SIGINT, SIG_IGN);
-# ifdef VMUNIX
-       scont = sigset(SIGCONT, foonly);
-# endif VMUNIX
-       if (stat(tempEdit, &sbuf) >= 0) {
-               printf("%s: file exists\n", tempEdit);
-               goto out;
-       }
-       close(creat(tempEdit, 0600));
-       if ((fbuf = fopen(tempEdit, "w")) == NULL) {
-               perror(tempEdit);
-               goto out;
+       int (*sigint)() = signal(SIGINT, SIG_IGN);
+       int (*sigcont)() = signal(SIGCONT, SIG_DFL);
+       FILE *nf = run_editor(fp, (off_t)-1, c, 0);
+
+       if (nf != NULL) {
+               fseek(nf, (off_t)0, 2);
+               collf = nf;
+               fclose(fp);
        }
        }
-       fflush(obuf);
-       rewind(ibuf);
-       t = getc(ibuf);
-       while (t != EOF) {
-               putc(t, fbuf);
-               t = getc(ibuf);
-       }
-       fflush(fbuf);
-       if (ferror(fbuf)) {
-               perror(tempEdit);
-               remove(tempEdit);
-               goto fix;
-       }
-       fclose(fbuf);
-       if ((edit = value(c == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
-               edit = c == 'e' ? EDITOR : VISUAL;
-       pid = vfork();
-       if (pid == 0) {
-               if (sig != SIG_IGN)
-                       sigsys(SIGINT, SIG_DFL);
-               execl(edit, edit, tempEdit, 0);
-               perror(edit);
-               _exit(1);
-       }
-       if (pid == -1) {
-               perror("fork");
-               remove(tempEdit);
-               goto out;
-       }
-       while (wait(&s) != pid)
-               ;
-       if ((s & 0377) != 0) {
-               printf("Fatal error in \"%s\"\n", edit);
-               remove(tempEdit);
-               goto out;
-       }
-
-       /*
-        * Now switch to new file.
-        */
-
-       if ((fbuf = fopen(tempEdit, "a")) == NULL) {
-               perror(tempEdit);
-               remove(tempEdit);
-               goto out;
-       }
-       if ((ibuf = fopen(tempEdit, "r")) == NULL) {
-               perror(tempEdit);
-               fclose(fbuf);
-               remove(tempEdit);
-               goto out;
-       }
-       remove(tempEdit);
-       fclose(obuf);
-       fclose(newi);
-       obuf = fbuf;
-       goto out;
-fix:
-       perror(tempEdit);
-out:
-# ifdef VMUNIX
-       sigset(SIGCONT, scont);
-# endif VMUNIX
-       sigset(SIGINT, sig);
-       newi = ibuf;
-       return(obuf);
+       (void) signal(SIGINT, sigint);
+       (void) signal(SIGCONT, sigcont);
 }
 
 }
 
-/*
- * Currently, Berkeley virtual VAX/UNIX will not let you change the
- * disposition of SIGCONT, except to trap it somewhere new.
- * Hence, sigset(SIGCONT, foonly) is used to ignore continue signals.
- */
-foonly() {}
-
 /*
  * Pipe the message through the command.
  * Old message is on stdin of command;
  * New message collected from stdout.
  * Sh -c must return 0 to accept the new message.
  */
 /*
  * Pipe the message through the command.
  * Old message is on stdin of command;
  * New message collected from stdout.
  * Sh -c must return 0 to accept the new message.
  */
-
-FILE *
-mespipe(ibuf, obuf, cmd)
-       FILE *ibuf, *obuf;
+mespipe(fp, cmd)
+       FILE *fp;
        char cmd[];
 {
        char cmd[];
 {
-       register FILE *ni, *no;
-       int pid, s;
-       int (*savesig)();
-       char *Shell;
+       FILE *nf;
+       int (*sigint)() = signal(SIGINT, SIG_IGN);
+       int (*sigcont)() = signal(SIGCONT, SIG_DFL);
+       extern char tempEdit[];
 
 
-       newi = ibuf;
-       if ((no = fopen(tempEdit, "w")) == NULL) {
-               perror(tempEdit);
-               return(obuf);
-       }
-       if ((ni = fopen(tempEdit, "r")) == NULL) {
+       if ((nf = fopen(tempEdit, "w+")) == NULL) {
                perror(tempEdit);
                perror(tempEdit);
-               fclose(no);
-               remove(tempEdit);
-               return(obuf);
-       }
-       remove(tempEdit);
-       savesig = sigset(SIGINT, SIG_IGN);
-       fflush(obuf);
-       rewind(ibuf);
-       if ((Shell = value("SHELL")) == NULL)
-               Shell = "/bin/sh";
-       if ((pid = vfork()) == -1) {
-               perror("fork");
-               goto err;
-       }
-       if (pid == 0) {
-               /*
-                * stdin = current message.
-                * stdout = new message.
-                */
-
-               close(0);
-               dup(fileno(ibuf));
-               close(1);
-               dup(fileno(no));
-               for (s = 4; s < 15; s++)
-                       close(s);
-               execl(Shell, Shell, "-c", cmd, 0);
-               perror(Shell);
-               _exit(1);
+               goto out;
        }
        }
-       while (wait(&s) != pid)
-               ;
-       if (s != 0 || pid == -1) {
-               fprintf(stderr, "\"%s\" failed!?\n", cmd);
-               goto err;
+       (void) unlink(tempEdit);
+       /*
+        * stdin = current message.
+        * stdout = new message.
+        */
+       if (run_command(cmd, 0, fileno(fp), fileno(nf), NOSTR) < 0) {
+               (void) fclose(nf);
+               goto out;
        }
        }
-       if (fsize(ni) == 0) {
+       if (fsize(nf) == 0) {
                fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
                fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
-               goto err;
+               (void) fclose(nf);
+               goto out;
        }
        }
-
        /*
         * Take new files.
         */
        /*
         * Take new files.
         */
-
-       newi = ni;
-       fclose(ibuf);
-       fclose(obuf);
-       sigset(SIGINT, savesig);
-       return(no);
-
-err:
-       fclose(no);
-       fclose(ni);
-       sigset(SIGINT, savesig);
-       return(obuf);
+       (void) fseek(nf, 0L, 2);
+       collf = nf;
+       (void) fclose(fp);
+out:
+       (void) signal(SIGINT, sigint);
+       (void) signal(SIGCONT, sigcont);
 }
 
 /*
 }
 
 /*
@@ -674,20 +480,21 @@ err:
  * the message temporary.  The flag argument is 'm' if we
  * should shift over and 'f' if not.
  */
  * the message temporary.  The flag argument is 'm' if we
  * should shift over and 'f' if not.
  */
-
-forward(ms, obuf, f)
+forward(ms, fp, f)
        char ms[];
        char ms[];
-       FILE *obuf;
+       FILE *fp;
 {
 {
-       register int *msgvec, *ip;
+       register int *msgvec;
        extern char tempMail[];
        extern char tempMail[];
+       struct ignoretab *ig;
+       char *tabst;
 
        msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
        if (msgvec == (int *) NOSTR)
                return(0);
        if (getmsglist(ms, msgvec, 0) < 0)
                return(0);
 
        msgvec = (int *) salloc((msgCount+1) * sizeof *msgvec);
        if (msgvec == (int *) NOSTR)
                return(0);
        if (getmsglist(ms, msgvec, 0) < 0)
                return(0);
-       if (*msgvec == NULL) {
+       if (*msgvec == 0) {
                *msgvec = first(0, MMNORM);
                if (*msgvec == NULL) {
                        printf("No appropriate messages\n");
                *msgvec = first(0, MMNORM);
                if (*msgvec == NULL) {
                        printf("No appropriate messages\n");
@@ -695,76 +502,35 @@ forward(ms, obuf, f)
                }
                msgvec[1] = NULL;
        }
                }
                msgvec[1] = NULL;
        }
+       if (f == 'f' || f == 'F')
+               tabst = NOSTR;
+       else if ((tabst = value("tabstr")) == NOSTR)
+               tabst = "\t";
+       ig = isupper(f) ? NULL : ignore;
        printf("Interpolating:");
        printf("Interpolating:");
-       for (ip = msgvec; *ip != NULL; ip++) {
-               touch(*ip);
-               printf(" %d", *ip);
-               if (f == 'm') {
-                       if (transmit(&message[*ip-1], obuf) < 0) {
-                               perror(tempMail);
-                               return(-1);
-                       }
-               } else
-                       if (send(&message[*ip-1], obuf) < 0) {
-                               perror(tempMail);
-                               return(-1);
-                       }
-       }
-       printf("\n");
-       return(0);
-}
-
-/*
- * Send message described by the passed pointer to the
- * passed output buffer.  Insert a tab in front of each
- * line.  Return a count of the characters sent, or -1
- * on error.
- */
-
-transmit(mailp, obuf)
-       struct message *mailp;
-       FILE *obuf;
-{
-       register struct message *mp;
-       register int c, ch;
-       int n, bol;
-       FILE *ibuf;
+       for (; *msgvec != 0; msgvec++) {
+               struct message *mp = message + *msgvec - 1;
 
 
-       mp = mailp;
-       ibuf = setinput(mp);
-       c = msize(mp);
-       n = c;
-       bol = 1;
-       while (c-- > 0) {
-               if (bol) {
-                       bol = 0;
-                       putc('\t', obuf);
-                       n++;
-                       if (ferror(obuf)) {
-                               perror("/tmp");
-                               return(-1);
-                       }
-               }
-               ch = getc(ibuf);
-               if (ch == '\n')
-                       bol++;
-               putc(ch, obuf);
-               if (ferror(obuf)) {
-                       perror("/tmp");
+               touch(mp);
+               printf(" %d", *msgvec);
+               if (send(mp, fp, ig, tabst) < 0) {
+                       perror(tempMail);
                        return(-1);
                }
        }
                        return(-1);
                }
        }
-       return(n);
+       printf("\n");
+       return(0);
 }
 
 /*
  * Print (continue) when continued after ^Z.
  */
 }
 
 /*
  * Print (continue) when continued after ^Z.
  */
+/*ARGSUSED*/
 collcont(s)
 {
 
 collcont(s)
 {
 
-       printf("(continue)\n");
-       fflush(stdout);
+       hadintr = 0;
+       longjmp(coljmp, 1);
 }
 
 /*
 }
 
 /*
@@ -774,99 +540,58 @@ collcont(s)
  * signal routine.  We only come here if signals
  * were previously set anyway.
  */
  * signal routine.  We only come here if signals
  * were previously set anyway.
  */
-
-# ifndef VMUNIX
-collintsig()
-{
-       signal(SIGINT, SIG_IGN);
-       collrub(SIGINT);
-}
-
-collhupsig()
-{
-       signal(SIGHUP, SIG_IGN);
-       collrub(SIGHUP);
-}
-# endif VMUNIX
-
 collrub(s)
 {
 collrub(s)
 {
-       register FILE *dbuf;
-       register int c;
 
        if (s == SIGINT && hadintr == 0) {
 
        if (s == SIGINT && hadintr == 0) {
-               hadintr++;
-               clrbuf(stdout);
-               printf("\n(Interrupt -- one more to kill letter)\n");
-# ifdef VMUNIX
-               sigrelse(s);
-# endif VMUNIX
+               hadintr = 1;
                longjmp(coljmp, 1);
        }
                longjmp(coljmp, 1);
        }
-       fclose(newo);
-       rewind(newi);
-       if (s == SIGINT && value("nosave") != NOSTR || fsize(newi) == 0)
-               goto done;
-       if ((dbuf = fopen(deadletter, "w")) == NULL)
-               goto done;
-       chmod(deadletter, 0600);
-       while ((c = getc(newi)) != EOF)
-               putc(c, dbuf);
-       fclose(dbuf);
-
-done:
-       fclose(newi);
-       sigset(SIGINT, savesig);
-       sigset(SIGHUP, savehup);
-# ifdef VMUNIX
-       sigset(SIGCONT, savecont);
-# endif VMUNIX
+       rewind(collf);
+       if (s != SIGINT || value("nosave") == NOSTR)
+               savedeadletter(collf);
+       fclose(collf);
+       signal(SIGINT, saveint);
+       signal(SIGHUP, savehup);
+       signal(SIGCONT, savecont);
        if (rcvmode) {
                if (s == SIGHUP)
                        hangup(SIGHUP);
                else
                        stop(s);
        if (rcvmode) {
                if (s == SIGHUP)
                        hangup(SIGHUP);
                else
                        stop(s);
-       }
-       else
+       } else
                exit(1);
 }
 
                exit(1);
 }
 
+savedeadletter(fp)
+       register FILE *fp;
+{
+       register FILE *dbuf;
+       register int c;
+       char *cp;
+
+       if (fsize(fp) == 0)
+               return;
+       cp = getdeadletter();
+       c = umask(077);
+       dbuf = fopen(cp, "a");
+       umask(c);
+       if (dbuf == NULL)
+               return;
+       while ((c = getc(fp)) != EOF)
+               putc(c, dbuf);
+       fclose(dbuf);
+       rewind(fp);
+}
+
 /*
  * Acknowledge an interrupt signal from the tty by typing an @
  */
 /*
  * Acknowledge an interrupt signal from the tty by typing an @
  */
-
+/*ARGSUSED*/
 intack(s)
 {
 intack(s)
 {
-       
+
        puts("@");
        fflush(stdout);
        clearerr(stdin);
 }
        puts("@");
        fflush(stdout);
        clearerr(stdin);
 }
-
-/*
- * Add a string to the end of a header entry field.
- */
-
-char *
-addto(hf, news)
-       char hf[], news[];
-{
-       register char *cp, *cp2, *linebuf;
-
-       if (hf == NOSTR)
-               hf = "";
-       if (*news == '\0')
-               return(hf);
-       linebuf = salloc(strlen(hf) + strlen(news) + 2);
-       for (cp = hf; any(*cp, " \t"); cp++)
-               ;
-       for (cp2 = linebuf; *cp;)
-               *cp2++ = *cp++;
-       *cp2++ = ' ';
-       for (cp = news; any(*cp, " \t"); cp++)
-               ;
-       while (*cp != '\0')
-               *cp2++ = *cp++;
-       *cp2 = '\0';
-       return(linebuf);
-}