4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / collect.c
index 9afbe13..73a0d98 100644 (file)
@@ -1,22 +1,12 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The 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.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)collect.c  5.15 (Berkeley) %G%";
+static char sccsid[] = "@(#)collect.c  8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -27,7 +17,7 @@ static char sccsid[] = "@(#)collect.c 5.15 (Berkeley) %G%";
  */
 
 #include "rcv.h"
  */
 
 #include "rcv.h"
-#include <sys/stat.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
@@ -40,47 +30,53 @@ static char sccsid[] = "@(#)collect.c       5.15 (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 *fbuf;
 {
        FILE *fbuf;
-       int lc, cc, escape, eof;
-       int collrub(), intack(), collcont();
+       int lc, cc, escape, eofcount;
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
        char getsub;
        int omask;
        register int c, t;
        char linebuf[LINESIZE], *cp;
        extern char tempMail[];
        char getsub;
        int omask;
+       void collint(), collhup(), collstop();
 
 
-       noreset++;
        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 ((collf = fopen(tempMail, "w+")) == NULL) {
+       noreset++;
+       if ((collf = Fopen(tempMail, "w+")) == NULL) {
                perror(tempMail);
                goto err;
        }
                perror(tempMail);
                goto err;
        }
@@ -94,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);
@@ -104,11 +100,10 @@ collect(hp, printheaders)
                escape = *cp;
        else
                escape = ESCAPE;
                escape = *cp;
        else
                escape = ESCAPE;
-       eof = 0;
+       eofcount = 0;
        hadintr = 0;
 
        hadintr = 0;
 
-       if (!setjmp(coljmp)) {
-               signal(SIGCONT, collcont);
+       if (!setjmp(colljmp)) {
                if (getsub)
                        grabh(hp, GSUBJECT);
        } else {
                if (getsub)
                        grabh(hp, GSUBJECT);
        } else {
@@ -128,17 +123,18 @@ 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 &&
                hadintr = 0;
                if (linebuf[0] == '.' && linebuf[1] == '\0' &&
                    value("interactive") != NOSTR &&
@@ -194,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':
                        /*
@@ -252,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;
                        }
@@ -260,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++;
                                if ((t = putline(collf, linebuf)) < 0) {
                                lc++;
                                if ((t = putline(collf, linebuf)) < 0) {
-                                       fclose(fbuf);
+                                       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':
@@ -297,21 +293,17 @@ cont:
                         * standard list processing garbage.
                         * If ~f is given, we don't shift over.
                         */
                         * standard list processing garbage.
                         * If ~f is given, we don't shift over.
                         */
-                       if (!rcvmode) {
-                               printf("No messages to send from!?!\n");
-                               break;
-                       }
                        if (forward(linebuf + 2, collf, c) < 0)
                                goto err;
                        goto cont;
                case '?':
                        if (forward(linebuf + 2, collf, c) < 0)
                                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':
                        /*
                        break;
                case 'p':
                        /*
@@ -322,7 +314,7 @@ cont:
                        printf("-------\nMessage contains:\n");
                        puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
                        while ((t = getc(collf)) != EOF)
                        printf("-------\nMessage contains:\n");
                        puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
                        while ((t = getc(collf)) != EOF)
-                               putchar(t);
+                               (void) putchar(t);
                        goto cont;
                case '|':
                        /*
                        goto cont;
                case '|':
                        /*
@@ -347,27 +339,31 @@ cont:
        goto out;
 err:
        if (collf != NULL) {
        goto out;
 err:
        if (collf != NULL) {
-               fclose(collf);
+               Fclose(collf);
                collf = NULL;
        }
 out:
        if (collf != NULL)
                rewind(collf);
                collf = NULL;
        }
 out:
        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--;
        return collf;
 }
 
 /*
  * Write a file, ex-like if f set.
  */
        return collf;
 }
 
 /*
  * 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;
@@ -385,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);
        }
@@ -395,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);
@@ -412,20 +408,20 @@ exwrite(name, fp, f)
  * Edit the message being collected on fp.
  * On return, make the edit file the new temp file.
  */
  * Edit the message being collected on fp.
  * On return, make the edit file the new temp file.
  */
+void
 mesedit(fp, c)
        FILE *fp;
 mesedit(fp, c)
        FILE *fp;
+       int c;
 {
 {
-       int (*sigint)() = signal(SIGINT, SIG_IGN);
-       int (*sigcont)() = signal(SIGCONT, SIG_DFL);
+       sig_t sigint = signal(SIGINT, SIG_IGN);
        FILE *nf = run_editor(fp, (off_t)-1, c, 0);
 
        if (nf != NULL) {
        FILE *nf = run_editor(fp, (off_t)-1, c, 0);
 
        if (nf != NULL) {
-               fseek(nf, (off_t)0, 2);
+               fseek(nf, 0L, 2);
                collf = nf;
                collf = nf;
-               fclose(fp);
+               Fclose(fp);
        }
        (void) signal(SIGINT, sigint);
        }
        (void) signal(SIGINT, sigint);
-       (void) signal(SIGCONT, sigcont);
 }
 
 /*
 }
 
 /*
@@ -434,16 +430,16 @@ mesedit(fp, c)
  * 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.
  */
+void
 mespipe(fp, cmd)
        FILE *fp;
        char cmd[];
 {
        FILE *nf;
 mespipe(fp, cmd)
        FILE *fp;
        char cmd[];
 {
        FILE *nf;
-       int (*sigint)() = signal(SIGINT, SIG_IGN);
-       int (*sigcont)() = signal(SIGCONT, SIG_DFL);
+       sig_t sigint = signal(SIGINT, SIG_IGN);
        extern char tempEdit[];
 
        extern char tempEdit[];
 
-       if ((nf = fopen(tempEdit, "w+")) == NULL) {
+       if ((nf = Fopen(tempEdit, "w+")) == NULL) {
                perror(tempEdit);
                goto out;
        }
                perror(tempEdit);
                goto out;
        }
@@ -452,13 +448,14 @@ mespipe(fp, cmd)
         * stdin = current message.
         * stdout = new message.
         */
         * stdin = current message.
         * stdout = new message.
         */
-       if (run_command(cmd, 0, fileno(fp), fileno(nf), NOSTR) < 0) {
-               (void) fclose(nf);
+       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);
                goto out;
        }
        if (fsize(nf) == 0) {
                fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
-               (void) fclose(nf);
+               (void) Fclose(nf);
                goto out;
        }
        /*
                goto out;
        }
        /*
@@ -466,10 +463,9 @@ mespipe(fp, cmd)
         */
        (void) fseek(nf, 0L, 2);
        collf = nf;
         */
        (void) fseek(nf, 0L, 2);
        collf = nf;
-       (void) fclose(fp);
+       (void) Fclose(fp);
 out:
        (void) signal(SIGINT, sigint);
 out:
        (void) signal(SIGINT, sigint);
-       (void) signal(SIGCONT, sigcont);
 }
 
 /*
 }
 
 /*
@@ -480,9 +476,11 @@ out:
  * 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;
        extern char tempMail[];
 {
        register int *msgvec;
        extern char tempMail[];
@@ -504,7 +502,7 @@ forward(ms, fp, f)
        }
        if (f == 'f' || f == 'F')
                tabst = NOSTR;
        }
        if (f == 'f' || f == 'F')
                tabst = NOSTR;
-       else if ((tabst = value("tabstr")) == NOSTR)
+       else if ((tabst = value("indentprefix")) == NOSTR)
                tabst = "\t";
        ig = isupper(f) ? NULL : ignore;
        printf("Interpolating:");
                tabst = "\t";
        ig = isupper(f) ? NULL : ignore;
        printf("Interpolating:");
@@ -526,43 +524,66 @@ forward(ms, fp, f)
  * Print (continue) when continued after ^Z.
  */
 /*ARGSUSED*/
  * Print (continue) when continued after ^Z.
  */
 /*ARGSUSED*/
-collcont(s)
+void
+collstop(s)
+       int s;
 {
 {
+       sig_t old_action = signal(s, SIG_DFL);
 
 
-       hadintr = 0;
-       longjmp(coljmp, 1);
+       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;
 {
 {
-
-       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)
+       if (value("nosave") == NOSTR)
                savedeadletter(collf);
                savedeadletter(collf);
-       fclose(collf);
-       signal(SIGINT, saveint);
-       signal(SIGHUP, savehup);
-       signal(SIGCONT, savecont);
-       if (rcvmode) {
-               if (s == SIGHUP)
-                       hangup(SIGHUP);
-               else
-                       stop(s);
-       } else
-               exit(1);
+       longjmp(collabort, 1);
+}
+
+/*ARGSUSED*/
+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);
 }
 
 }
 
+void
 savedeadletter(fp)
        register FILE *fp;
 {
 savedeadletter(fp)
        register FILE *fp;
 {
@@ -574,24 +595,12 @@ savedeadletter(fp)
                return;
        cp = getdeadletter();
        c = umask(077);
                return;
        cp = getdeadletter();
        c = umask(077);
-       dbuf = fopen(cp, "a");
-       umask(c);
+       dbuf = Fopen(cp, "a");
+       (void) umask(c);
        if (dbuf == NULL)
                return;
        while ((c = getc(fp)) != EOF)
        if (dbuf == NULL)
                return;
        while ((c = getc(fp)) != EOF)
-               putc(c, dbuf);
-       fclose(dbuf);
+               (void) putc(c, dbuf);
+       Fclose(dbuf);
        rewind(fp);
 }
        rewind(fp);
 }
-
-/*
- * Acknowledge an interrupt signal from the tty by typing an @
- */
-/*ARGSUSED*/
-intack(s)
-{
-
-       puts("@");
-       fflush(stdout);
-       clearerr(stdin);
-}