quad's broke it -- prototyped it rather than figure out the problem
[unix-history] / usr / src / usr.bin / mail / collect.c
index 8eb1c40..e1c7160 100644 (file)
@@ -2,17 +2,12 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at 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'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)collect.c  5.9 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)collect.c  5.25 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -22,8 +17,7 @@ static char sccsid[] = "@(#)collect.c 5.9 (Berkeley) %G%";
  */
 
 #include "rcv.h"
  */
 
 #include "rcv.h"
-#include <sys/stat.h>
-#include <sys/wait.h>
+#include "extern.h"
 
 /*
  * Read a message from standard output and return a read file to it
 
 /*
  * Read a message from standard output and return a read file to it
@@ -36,54 +30,57 @@ static char sccsid[] = "@(#)collect.c       5.9 (Berkeley) %G%";
  * away on dead.letter.
  */
 
  * away on dead.letter.
  */
 
-static int     (*saveint)();           /* Previous SIGINT value */
-static int     (*savehup)();           /* Previous SIGHUP value */
-static int     (*savecont)();          /* Previous SIGCONT value */
+static sig_t   saveint;                /* Previous SIGINT value */
+static sig_t   savehup;                /* Previous SIGHUP value */
+static sig_t   savetstp;               /* Previous SIGTSTP value */
+static sig_t   savettou;               /* Previous SIGTTOU value */
+static sig_t   savettin;               /* Previous SIGTTIN value */
 static FILE    *collf;                 /* File for saving away */
 static int     hadintr;                /* Have seen one SIGINT so far */
 
 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 */
+static jmp_buf colljmp;                /* To get back to work */
+static int     colljmp_p;              /* whether to long jump */
+static jmp_buf collabort;              /* To end collection with error */
 
 FILE *
 collect(hp, printheaders)
        struct header *hp;
 
 FILE *
 collect(hp, printheaders)
        struct header *hp;
+       int printheaders;
 {
 {
-       FILE *fp, *fbuf;
-       int lc, cc, escape, eof;
-       int collrub(), intack(), collcont();
+       FILE *fbuf;
+       int lc, cc, escape, eofcount;
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
-       int notify();
        char getsub;
        int omask;
        char getsub;
        int omask;
+       void collint(), collhup(), collstop();
 
 
-       noreset++;
-       fp = NULL;
        collf = NULL;
        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)
        /*
         * 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);
+               signal(SIGINT, collint);
        if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
        if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
-               signal(SIGHUP, collrub);
-       savecont = signal(SIGCONT, SIG_DFL);
-       if (setjmp(coljmp)) {
-               remove(tempMail);
+               signal(SIGHUP, collhup);
+       savetstp = signal(SIGTSTP, collstop);
+       savettou = signal(SIGTTOU, collstop);
+       savettin = signal(SIGTTIN, collstop);
+       if (setjmp(collabort) || setjmp(colljmp)) {
+               rm(tempMail);
                goto err;
        }
        sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP)));
 
                goto err;
        }
        sigsetmask(omask & ~(sigmask(SIGINT) | sigmask(SIGHUP)));
 
-       if ((fp = fopen(tempMail, "w+")) == NULL) {
+       noreset++;
+       if ((collf = Fopen(tempMail, "w+")) == NULL) {
                perror(tempMail);
                goto err;
        }
                perror(tempMail);
                goto err;
        }
-       collf = fp;
-       remove(tempMail);
+       unlink(tempMail);
 
        /*
         * If we are going to prompt for a subject,
 
        /*
         * If we are going to prompt for a subject,
@@ -93,7 +90,7 @@ collect(hp, printheaders)
        t = GTO|GSUBJECT|GCC|GNL;
        getsub = 0;
        if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
        t = GTO|GSUBJECT|GCC|GNL;
        getsub = 0;
        if (hp->h_subject == NOSTR && value("interactive") != NOSTR &&
-           value("ask"))
+           (value("ask") != NOSTR || value("asksub") != NOSTR))
                t &= ~GNL, getsub++;
        if (printheaders) {
                puthead(hp, stdout, t);
                t &= ~GNL, getsub++;
        if (printheaders) {
                puthead(hp, stdout, t);
@@ -103,15 +100,10 @@ collect(hp, printheaders)
                escape = *cp;
        else
                escape = ESCAPE;
                escape = *cp;
        else
                escape = ESCAPE;
-       eof = 0;
+       eofcount = 0;
        hadintr = 0;
 
        hadintr = 0;
 
-       /*
-        * We can put the setjmp here because register variable
-        * needs to be saved in the loop.
-        */
-       if (!setjmp(coljmp)) {
-               signal(SIGCONT, collcont);
+       if (!setjmp(colljmp)) {
                if (getsub)
                        grabh(hp, GSUBJECT);
        } else {
                if (getsub)
                        grabh(hp, GSUBJECT);
        } else {
@@ -131,24 +123,25 @@ cont:
                }
        }
        for (;;) {
                }
        }
        for (;;) {
-               if (readline(stdin, linebuf) < 0) {
+               colljmp_p = 1;
+               c = readline(stdin, linebuf, LINESIZE);
+               colljmp_p = 0;
+               if (c < 0) {
                        if (value("interactive") != NOSTR &&
                        if (value("interactive") != NOSTR &&
-                           value("ignoreeof") != NOSTR) {
-                               if (++eof > 35)
-                                       break;
+                           value("ignoreeof") != NOSTR && ++eofcount < 25) {
                                printf("Use \".\" to terminate letter\n");
                                continue;
                        }
                        break;
                }
                                printf("Use \".\" to terminate letter\n");
                                continue;
                        }
                        break;
                }
-               eof = 0;
+               eofcount = 0;
                hadintr = 0;
                if (linebuf[0] == '.' && linebuf[1] == '\0' &&
                    value("interactive") != NOSTR &&
                    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
                        break;
                if (linebuf[0] != escape || value("interactive") == NOSTR) {
                hadintr = 0;
                if (linebuf[0] == '.' && linebuf[1] == '\0' &&
                    value("interactive") != NOSTR &&
                    (value("dot") != NOSTR || value("ignoreeof") != NOSTR))
                        break;
                if (linebuf[0] != escape || value("interactive") == NOSTR) {
-                       if (putline(fp, linebuf) < 0)
+                       if (putline(collf, linebuf) < 0)
                                goto err;
                        continue;
                }
                                goto err;
                        continue;
                }
@@ -160,7 +153,7 @@ cont:
                         * Otherwise, it's an error.
                         */
                        if (c == escape) {
                         * Otherwise, it's an error.
                         */
                        if (c == escape) {
-                               if (putline(fp, &linebuf[1]) < 0)
+                               if (putline(collf, &linebuf[1]) < 0)
                                        goto err;
                                else
                                        break;
                                        goto err;
                                else
                                        break;
@@ -197,7 +190,7 @@ cont:
                         * Act like an interrupt happened.
                         */
                        hadintr++;
                         * Act like an interrupt happened.
                         */
                        hadintr++;
-                       collrub(SIGINT);
+                       collint(SIGINT);
                        exit(1);
                case 'h':
                        /*
                        exit(1);
                case 'h':
                        /*
@@ -233,13 +226,13 @@ cont:
                        hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
                        break;
                case 'd':
                        hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
                        break;
                case 'd':
-                       strcpy(linebuf + 2, deadletter);
+                       strcpy(linebuf + 2, getdeadletter());
                        /* fall into . . . */
                case 'r':
                        /*
                         * Invoke a file:
                         * Search for the file name,
                        /* fall into . . . */
                case 'r':
                        /*
                         * Invoke a file:
                         * Search for the file name,
-                        * then open it and copy the contents to fp.
+                        * then open it and copy the contents to collf.
                         */
                        cp = &linebuf[2];
                        while (isspace(*cp))
                         */
                        cp = &linebuf[2];
                        while (isspace(*cp))
@@ -255,7 +248,7 @@ cont:
                                printf("%s: Directory\n", cp);
                                break;
                        }
                                printf("%s: Directory\n", cp);
                                break;
                        }
-                       if ((fbuf = fopen(cp, "r")) == NULL) {
+                       if ((fbuf = Fopen(cp, "r")) == NULL) {
                                perror(cp);
                                break;
                        }
                                perror(cp);
                                break;
                        }
@@ -263,15 +256,15 @@ cont:
                        fflush(stdout);
                        lc = 0;
                        cc = 0;
                        fflush(stdout);
                        lc = 0;
                        cc = 0;
-                       while (readline(fbuf, linebuf) >= 0) {
+                       while (readline(fbuf, linebuf, LINESIZE) >= 0) {
                                lc++;
                                lc++;
-                               if ((t = putline(fp, linebuf)) < 0) {
-                                       fclose(fbuf);
+                               if ((t = putline(collf, linebuf)) < 0) {
+                                       Fclose(fbuf);
                                        goto err;
                                }
                                cc += t;
                        }
                                        goto err;
                                }
                                cc += t;
                        }
-                       fclose(fbuf);
+                       Fclose(fbuf);
                        printf("%d/%d\n", lc, cc);
                        break;
                case 'w':
                        printf("%d/%d\n", lc, cc);
                        break;
                case 'w':
@@ -279,7 +272,7 @@ cont:
                         * Write the message on a file.
                         */
                        cp = &linebuf[2];
                         * Write the message on a file.
                         */
                        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");
@@ -287,54 +280,49 @@ cont:
                        }
                        if ((cp = expand(cp)) == NOSTR)
                                break;
                        }
                        if ((cp = expand(cp)) == NOSTR)
                                break;
-                       rewind(fp);
-                       exwrite(cp, fp, 1);
+                       rewind(collf);
+                       exwrite(cp, collf, 1);
                        break;
                case 'm':
                        break;
                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;
-                       }
-                       cp = &linebuf[2];
-                       while (any(*cp, " \t"))
-                               cp++;
-                       if (forward(cp, fp, c) < 0)
+                       if (forward(linebuf + 2, collf, c) < 0)
                                goto err;
                        goto cont;
                case '?':
                                goto err;
                        goto cont;
                case '?':
-                       if ((fbuf = fopen(THELPFILE, "r")) == NULL) {
-                               perror(THELPFILE);
+                       if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
+                               perror(_PATH_TILDE);
                                break;
                        }
                        while ((t = getc(fbuf)) != EOF)
                                break;
                        }
                        while ((t = getc(fbuf)) != EOF)
-                               putchar(t);
-                       fclose(fbuf);
+                               (void) putchar(t);
+                       Fclose(fbuf);
                        break;
                case 'p':
                        /*
                         * Print out the current state of the
                         * message without altering anything.
                         */
                        break;
                case 'p':
                        /*
                         * Print out the current state of the
                         * message without altering anything.
                         */
-                       rewind(fp);
+                       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);
-                       while ((t = getc(fp)) != EOF)
-                               putchar(t);
+                       while ((t = getc(collf)) != EOF)
+                               (void) putchar(t);
                        goto cont;
                case '|':
                        /*
                         * Pipe message through command.
                         * Collect output as new message.
                         */
                        goto cont;
                case '|':
                        /*
                         * Pipe message through command.
                         * Collect output as new message.
                         */
-                       rewind(fp);
-                       fp = mespipe(fp, &linebuf[2]);
+                       rewind(collf);
+                       mespipe(collf, &linebuf[2]);
                        goto cont;
                case 'v':
                case 'e':
                        goto cont;
                case 'v':
                case 'e':
@@ -343,36 +331,39 @@ cont:
                         * 'e' means to use EDITOR
                         * 'v' means to use VISUAL
                         */
                         * 'e' means to use EDITOR
                         * 'v' means to use VISUAL
                         */
-                       rewind(fp);
-                       if ((fp = mesedit(fp, c)) == NULL)
-                               goto err;
+                       rewind(collf);
+                       mesedit(collf, c);
                        goto cont;
                }
        }
        goto out;
 err:
                        goto cont;
                }
        }
        goto out;
 err:
-       if (fp != NULL) {
-               fclose(fp);
-               fp = NULL;
+       if (collf != NULL) {
+               Fclose(collf);
+               collf = NULL;
        }
 out:
        }
 out:
-       if (fp != NULL)
-               rewind(fp);
+       if (collf != NULL)
+               rewind(collf);
+       noreset--;
+       sigblock(sigmask(SIGINT) | sigmask(SIGHUP));
        signal(SIGINT, saveint);
        signal(SIGHUP, savehup);
        signal(SIGINT, saveint);
        signal(SIGHUP, savehup);
-       signal(SIGCONT, savecont);
+       signal(SIGTSTP, savetstp);
+       signal(SIGTTOU, savettou);
+       signal(SIGTTIN, savettin);
        sigsetmask(omask);
        sigsetmask(omask);
-       noreset = 0;
-       return(fp);
+       return collf;
 }
 
 /*
  * Write a file, ex-like if f set.
  */
 }
 
 /*
  * Write a file, ex-like if f set.
  */
-
+int
 exwrite(name, fp, f)
        char name[];
        FILE *fp;
 exwrite(name, fp, f)
        char name[];
        FILE *fp;
+       int f;
 {
        register FILE *of;
        register int c;
 {
        register FILE *of;
        register int c;
@@ -390,7 +381,7 @@ exwrite(name, fp, f)
                fprintf(stderr, "File exists\n");
                return(-1);
        }
                fprintf(stderr, "File exists\n");
                return(-1);
        }
-       if ((of = fopen(name, "w")) == NULL) {
+       if ((of = Fopen(name, "w")) == NULL) {
                perror(NOSTR);
                return(-1);
        }
                perror(NOSTR);
                return(-1);
        }
@@ -400,14 +391,14 @@ exwrite(name, fp, f)
                cc++;
                if (c == '\n')
                        lc++;
                cc++;
                if (c == '\n')
                        lc++;
-               putc(c, of);
+               (void) putc(c, of);
                if (ferror(of)) {
                        perror(name);
                if (ferror(of)) {
                        perror(name);
-                       fclose(of);
+                       Fclose(of);
                        return(-1);
                }
        }
                        return(-1);
                }
        }
-       fclose(of);
+       Fclose(of);
        printf("%d/%ld\n", lc, cc);
        fflush(stdout);
        return(0);
        printf("%d/%ld\n", lc, cc);
        fflush(stdout);
        return(0);
@@ -415,88 +406,22 @@ exwrite(name, fp, f)
 
 /*
  * Edit the message being collected on fp.
 
 /*
  * Edit the message being collected on fp.
- * Write the message out onto some poorly-named temp file
- * and point an editor at it.
- *
  * On return, make the edit file the new temp file.
  */
  * On return, make the edit file the new temp file.
  */
-
-FILE *
+void
 mesedit(fp, c)
        FILE *fp;
 mesedit(fp, c)
        FILE *fp;
+       int c;
 {
 {
-       int pid;
-       union wait s;
-       FILE *fbuf;
-       register int t;
-       int (*sigint)(), (*sigcont)();
-       struct stat sbuf;
-       extern char tempEdit[];
-       register char *edit;
-
-       sigint = signal(SIGINT, SIG_IGN);
-       sigcont = signal(SIGCONT, SIG_DFL);
-       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;
-       }
-       while ((t = getc(fp)) != EOF)
-               putc(t, fbuf);
-       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 (sigint != SIG_IGN)
-                       signal(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.w_status != 0) {
-               printf("Fatal error in \"%s\"\n", edit);
-               remove(tempEdit);
-               goto out;
-       }
+       sig_t sigint = signal(SIGINT, SIG_IGN);
+       FILE *nf = run_editor(fp, (off_t)-1, c, 0);
 
 
-       /*
-        * Now switch to new file.
-        */
-
-       if ((fbuf = fopen(tempEdit, "a+")) == NULL) {
-               perror(tempEdit);
-               remove(tempEdit);
-               goto out;
+       if (nf != NULL) {
+               fseek(nf, 0L, 2);
+               collf = nf;
+               Fclose(fp);
        }
        }
-       remove(tempEdit);
-       collf = fbuf;
-       fclose(fp);
-       fp = fbuf;
-       goto out;
-fix:
-       perror(tempEdit);
-out:
-       signal(SIGCONT, sigcont);
-       signal(SIGINT, sigint);
-       return(fp);
+       (void) signal(SIGINT, sigint);
 }
 
 /*
 }
 
 /*
@@ -505,73 +430,42 @@ out:
  * New message collected from stdout.
  * Sh -c must return 0 to accept the new message.
  */
  * New message collected from stdout.
  * Sh -c must return 0 to accept the new message.
  */
-
-FILE *
+void
 mespipe(fp, cmd)
        FILE *fp;
        char cmd[];
 {
 mespipe(fp, cmd)
        FILE *fp;
        char cmd[];
 {
+       FILE *nf;
+       sig_t sigint = signal(SIGINT, SIG_IGN);
        extern char tempEdit[];
        extern char tempEdit[];
-       register FILE *nf;
-       int pid;
-       union wait s;
-       int (*saveint)();
-       char *Shell;
 
 
-       if ((nf = fopen(tempEdit, "w+")) == NULL) {
+       if ((nf = Fopen(tempEdit, "w+")) == NULL) {
                perror(tempEdit);
                perror(tempEdit);
-               return(fp);
-       }
-       remove(tempEdit);
-       saveint = signal(SIGINT, SIG_IGN);
-       if ((Shell = value("SHELL")) == NULL)
-               Shell = "/bin/sh";
-       if ((pid = vfork()) == -1) {
-               perror("fork");
-               goto err;
-       }
-       if (pid == 0) {
-               int fd;
-               /*
-                * stdin = current message.
-                * stdout = new message.
-                */
-
-               close(0);
-               dup(fileno(fp));
-               close(1);
-               dup(fileno(nf));
-               for (fd = getdtablesize(); --fd > 2;)
-                       close(fd);
-               execl(Shell, Shell, "-c", cmd, 0);
-               perror(Shell);
-               _exit(1);
+               goto out;
        }
        }
-       while (wait(&s) != pid)
-               ;
-       if (s.w_status != 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, NOSTR, NOSTR) < 0) {
+               (void) Fclose(nf);
+               goto out;
        }
        if (fsize(nf) == 0) {
                fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
        }
        if (fsize(nf) == 0) {
                fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
-               goto err;
+               (void) Fclose(nf);
+               goto out;
        }
        }
-
        /*
         * Take new files.
         */
        /*
         * Take new files.
         */
-
-       fseek(nf, 0L, 2);
+       (void) fseek(nf, 0L, 2);
        collf = nf;
        collf = nf;
-       fclose(fp);
-       signal(SIGINT, saveint);
-       return(nf);
-
-err:
-       fclose(nf);
-       signal(SIGINT, saveint);
-       return(fp);
+       (void) Fclose(fp);
+out:
+       (void) signal(SIGINT, sigint);
 }
 
 /*
 }
 
 /*
@@ -582,19 +476,23 @@ 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.
  */
+int
 forward(ms, fp, f)
        char ms[];
        FILE *fp;
 forward(ms, fp, f)
        char ms[];
        FILE *fp;
+       int f;
 {
 {
-       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");
@@ -602,126 +500,107 @@ forward(ms, fp, f)
                }
                msgvec[1] = NULL;
        }
                }
                msgvec[1] = NULL;
        }
+       if (f == 'f' || f == 'F')
+               tabst = NOSTR;
+       else if ((tabst = value("indentprefix")) == 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], fp) < 0L) {
-                               perror(tempMail);
-                               return(-1);
-                       }
-               } else
-                       if (send(&message[*ip-1], fp, 0) < 0) {
-                               perror(tempMail);
-                               return(-1);
-                       }
-       }
-       printf("\n");
-       return(0);
-}
+       for (; *msgvec != 0; msgvec++) {
+               struct message *mp = message + *msgvec - 1;
 
 
-/*
- * 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.
- */
-long
-transmit(mailp, fp)
-       struct message *mailp;
-       FILE *fp;
-{
-       register struct message *mp;
-       register int ch;
-       long c, n;
-       int bol;
-       FILE *ibuf;
-       char *tabst;
-
-       mp = mailp;
-       ibuf = setinput(mp);
-       c = mp->m_size;
-       n = c;
-       bol = 1;
-       if ((tabst = value("tabstr")) == NOSTR)
-               tabst = "\t";
-       while (c-- > 0L) {
-               ch = getc(ibuf);
-               if (ch == '\n')
-                       bol = 1;
-               else if (bol) {
-                       bol = 0;
-                       fputs(tabst, fp);
-                       n++;
-               }
-               putc(ch, fp);
-               if (ferror(fp)) {
-                       perror("/tmp");
-                       return(-1L);
+               touch(mp);
+               printf(" %d", *msgvec);
+               if (send(mp, fp, ig, tabst) < 0) {
+                       perror(tempMail);
+                       return(-1);
                }
        }
                }
        }
-       return(n);
+       printf("\n");
+       return(0);
 }
 
 /*
  * Print (continue) when continued after ^Z.
  */
 /*ARGSUSED*/
 }
 
 /*
  * Print (continue) when continued after ^Z.
  */
 /*ARGSUSED*/
-collcont(s)
+void
+collstop(s)
+       int s;
 {
 {
-
-       hadintr = 0;
-       longjmp(coljmp, 1);
+       sig_t old_action = signal(s, SIG_DFL);
+
+       sigsetmask(sigblock(0) & ~sigmask(s));
+       kill(0, s);
+       sigblock(sigmask(s));
+       signal(s, old_action);
+       if (colljmp_p) {
+               colljmp_p = 0;
+               hadintr = 0;
+               longjmp(colljmp, 1);
+       }
 }
 
 /*
 }
 
 /*
- * On interrupt, go here to save the partial
- * message on ~/dead.letter.
- * Then restore signals and execute the normal
- * signal routine.  We only come here if signals
- * were previously set anyway.
+ * On interrupt, come here to save the partial message in ~/dead.letter.
+ * Then jump out of the collection loop.
  */
  */
-collrub(s)
+/*ARGSUSED*/
+void
+collint(s)
+       int s;
 {
 {
-       register FILE *dbuf;
-       register int c;
-
-       if (s == SIGINT && hadintr == 0) {
+       /*
+        * the control flow is subtle, because we can be called from ~q.
+        */
+       if (!hadintr) {
+               if (value("ignore") != NOSTR) {
+                       puts("@");
+                       fflush(stdout);
+                       clearerr(stdin);
+                       return;
+               }
                hadintr = 1;
                hadintr = 1;
-               longjmp(coljmp, 1);
+               longjmp(colljmp, 1);
        }
        rewind(collf);
        }
        rewind(collf);
-       if (s == SIGINT && value("nosave") != NOSTR || fsize(collf) == 0)
-               goto done;
-       if ((dbuf = fopen(deadletter, "w")) == NULL)
-               goto done;
-       chmod(deadletter, 0600);
-       while ((c = getc(collf)) != EOF)
-               putc(c, dbuf);
-       fclose(dbuf);
-done:
-       fclose(collf);
-       signal(SIGINT, saveint);
-       signal(SIGHUP, savehup);
-       signal(SIGCONT, savecont);
-       if (rcvmode) {
-               if (s == SIGHUP)
-                       hangup(SIGHUP);
-               else
-                       stop(s);
-       } else
-               exit(1);
+       if (value("nosave") == NOSTR)
+               savedeadletter(collf);
+       longjmp(collabort, 1);
 }
 
 }
 
-/*
- * Acknowledge an interrupt signal from the tty by typing an @
- */
 /*ARGSUSED*/
 /*ARGSUSED*/
-intack(s)
+void
+collhup(s)
+       int s;
 {
 {
+       rewind(collf);
+       savedeadletter(collf);
+       /*
+        * Let's pretend nobody else wants to clean up,
+        * a true statement at this time.
+        */
+       exit(1);
+}
 
 
-       puts("@");
-       fflush(stdout);
-       clearerr(stdin);
+void
+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");
+       (void) umask(c);
+       if (dbuf == NULL)
+               return;
+       while ((c = getc(fp)) != EOF)
+               (void) putc(c, dbuf);
+       Fclose(dbuf);
+       rewind(fp);
 }
 }