Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / mail / tty.c
index 572b8d1..c7236be 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[] = "@(#)tty.c      5.7 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -7,9 +26,6 @@
  */
 
 #include "rcv.h"
  */
 
 #include "rcv.h"
-#include <sgtty.h>
-
-static char *SccsId = "@(#)tty.c       1.4 %G%";
 
 static int     c_erase;                /* Current erase char */
 static int     c_kill;                 /* Current kill char */
 
 static int     c_erase;                /* Current erase char */
 static int     c_kill;                 /* Current kill char */
@@ -27,15 +43,13 @@ grabh(hp, gflags)
        struct header *hp;
 {
        struct sgttyb ttybuf;
        struct header *hp;
 {
        struct sgttyb ttybuf;
-       int ttycont(), signull();
 #ifndef TIOCSTI
 #ifndef TIOCSTI
-       int (*savesigs[2])();
+       int (*saveint)(), (*savequit)();
 #endif
        int (*savecont)();
 #endif
        int (*savecont)();
-       register int s;
        int errs;
 
        int errs;
 
-       savecont = sigset(SIGCONT, signull);
+       savecont = signal(SIGCONT, SIG_DFL);
        errs = 0;
 #ifndef TIOCSTI
        ttyset = 0;
        errs = 0;
 #ifndef TIOCSTI
        ttyset = 0;
@@ -49,18 +63,18 @@ grabh(hp, gflags)
 #ifndef TIOCSTI
        ttybuf.sg_erase = 0;
        ttybuf.sg_kill = 0;
 #ifndef TIOCSTI
        ttybuf.sg_erase = 0;
        ttybuf.sg_kill = 0;
-       for (s = SIGINT; s <= SIGQUIT; s++)
-               if ((savesigs[s-SIGINT] = sigset(s, SIG_IGN)) == SIG_DFL)
-                       sigset(s, SIG_DFL);
+       if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
+               signal(SIGINT, SIG_DFL);
+       if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
+               signal(SIGQUIT, SIG_DFL);
 #endif
        if (gflags & GTO) {
 #ifndef TIOCSTI
 #endif
        if (gflags & GTO) {
 #ifndef TIOCSTI
-               if (!ttyset && hp->h_to != NOSTR)
+               if (!ttyset && hp->h_to != NIL)
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
-               hp->h_to = readtty("To: ", hp->h_to);
-               if (hp->h_to != NOSTR)
-                       hp->h_seq++;
+               hp->h_to =
+                       extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
        }
        if (gflags & GSUBJECT) {
 #ifndef TIOCSTI
        }
        if (gflags & GSUBJECT) {
 #ifndef TIOCSTI
@@ -68,35 +82,31 @@ grabh(hp, gflags)
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
                hp->h_subject = readtty("Subject: ", hp->h_subject);
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
                hp->h_subject = readtty("Subject: ", hp->h_subject);
-               if (hp->h_subject != NOSTR)
-                       hp->h_seq++;
        }
        if (gflags & GCC) {
 #ifndef TIOCSTI
        }
        if (gflags & GCC) {
 #ifndef TIOCSTI
-               if (!ttyset && hp->h_cc != NOSTR)
+               if (!ttyset && hp->h_cc != NIL)
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
-               hp->h_cc = readtty("Cc: ", hp->h_cc);
-               if (hp->h_cc != NOSTR)
-                       hp->h_seq++;
+               hp->h_cc =
+                       extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
        }
        if (gflags & GBCC) {
 #ifndef TIOCSTI
        }
        if (gflags & GBCC) {
 #ifndef TIOCSTI
-               if (!ttyset && hp->h_bcc != NOSTR)
+               if (!ttyset && hp->h_bcc != NIL)
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
                        ttyset++, stty(fileno(stdin), &ttybuf);
 #endif
-               hp->h_bcc = readtty("Bcc: ", hp->h_bcc);
-               if (hp->h_bcc != NOSTR)
-                       hp->h_seq++;
+               hp->h_bcc =
+                       extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
        }
        }
-       sigset(SIGCONT, savecont);
+       signal(SIGCONT, savecont);
 #ifndef TIOCSTI
        ttybuf.sg_erase = c_erase;
        ttybuf.sg_kill = c_kill;
        if (ttyset)
                stty(fileno(stdin), &ttybuf);
 #ifndef TIOCSTI
        ttybuf.sg_erase = c_erase;
        ttybuf.sg_kill = c_kill;
        if (ttyset)
                stty(fileno(stdin), &ttybuf);
-       for (s = SIGINT; s <= SIGQUIT; s++)
-               sigset(s, savesigs[s-SIGINT]);
+       signal(SIGINT, saveint);
+       signal(SIGQUIT, savequit);
 #endif
        return(errs);
 }
 #endif
        return(errs);
 }
@@ -112,9 +122,10 @@ char *
 readtty(pr, src)
        char pr[], src[];
 {
 readtty(pr, src)
        char pr[], src[];
 {
-       char canonb[BUFSIZ];
-       int c, ch, signull();
+       char ch, canonb[BUFSIZ];
+       int c;
        register char *cp, *cp2;
        register char *cp, *cp2;
+       int ttycont();
 
        fputs(pr, stdout);
        fflush(stdout);
 
        fputs(pr, stdout);
        fflush(stdout);
@@ -136,7 +147,8 @@ readtty(pr, src)
                        ch = '\\';
                        ioctl(0, TIOCSTI, &ch);
                }
                        ch = '\\';
                        ioctl(0, TIOCSTI, &ch);
                }
-               ioctl(0, TIOCSTI, &c);
+               ch = c;
+               ioctl(0, TIOCSTI, &ch);
        }
        cp = canonb;
        *cp = 0;
        }
        cp = canonb;
        *cp = 0;
@@ -147,7 +159,8 @@ readtty(pr, src)
        cp2 = cp;
        if (setjmp(rewrite))
                goto redo;
        cp2 = cp;
        if (setjmp(rewrite))
                goto redo;
-       sigset(SIGCONT, ttycont);
+       signal(SIGCONT, ttycont);
+       clearerr(stdin);
        while (cp2 < canonb + BUFSIZ) {
                c = getc(stdin);
                if (c == EOF || c == '\n')
        while (cp2 < canonb + BUFSIZ) {
                c = getc(stdin);
                if (c == EOF || c == '\n')
@@ -155,7 +168,7 @@ readtty(pr, src)
                *cp2++ = c;
        }
        *cp2 = 0;
                *cp2++ = c;
        }
        *cp2 = 0;
-       sigset(SIGCONT, signull);
+       signal(SIGCONT, SIG_DFL);
        if (c == EOF && ferror(stdin) && hadcont) {
 redo:
                hadcont = 0;
        if (c == EOF && ferror(stdin) && hadcont) {
 redo:
                hadcont = 0;
@@ -164,9 +177,9 @@ redo:
                return(readtty(pr, cp));
        }
 #ifndef TIOCSTI
                return(readtty(pr, cp));
        }
 #ifndef TIOCSTI
-       if (cp2 == NOSTR || *cp2 == '\0')
+       if (cp == NOSTR || *cp == '\0')
                return(src);
                return(src);
-       cp = cp2;
+       cp2 = cp;
        if (!ttyset)
                return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
        while (*cp != '\0') {
        if (!ttyset)
                return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
        while (*cp != '\0') {
@@ -203,17 +216,9 @@ redo:
 /*
  * Receipt continuation.
  */
 /*
  * Receipt continuation.
  */
+/*ARGSUSED*/
 ttycont(s)
 {
 ttycont(s)
 {
-
        hadcont++;
        hadcont++;
-       sigrelse(SIGCONT);
        longjmp(rewrite, 1);
 }
        longjmp(rewrite, 1);
 }
-
-/*
- * Null routine to satisfy
- * silly system bug that denies us holding SIGCONT
- */
-signull(s)
-{}