[BACK]Return to tty.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/tty.c, Revision 1.9

1.9     ! millert     1: /*     $OpenBSD: tty.c,v 1.8 1997/07/30 06:32:41 millert Exp $ */
1.3       millert     2: /*     $NetBSD: tty.c,v 1.7 1997/07/09 05:25:46 mikel Exp $    */
1.2       deraadt     3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
1.2       deraadt    38: #if 0
1.3       millert    39: static char sccsid[] = "@(#)tty.c      8.2 (Berkeley) 4/20/95";
1.2       deraadt    40: #else
1.9     ! millert    41: static char rcsid[] = "$OpenBSD: tty.c,v 1.8 1997/07/30 06:32:41 millert Exp $";
1.2       deraadt    42: #endif
1.1       deraadt    43: #endif /* not lint */
                     44:
                     45: /*
                     46:  * Mail -- a mail program
                     47:  *
                     48:  * Generally useful tty stuff.
                     49:  */
                     50:
                     51: #include "rcv.h"
                     52: #include "extern.h"
1.2       deraadt    53: #include <sys/ioctl.h>
1.1       deraadt    54:
1.5       millert    55: static cc_t            c_erase;        /* Current erase char */
                     56: static cc_t            c_kill;         /* Current kill char */
                     57: static sigjmp_buf      rewrite;        /* Place to go when continued */
                     58: static sigjmp_buf      intjmp;         /* Place to go when interrupted */
1.1       deraadt    59: #ifndef TIOCSTI
1.5       millert    60: static int             ttyset;         /* We must now do erase/kill */
1.1       deraadt    61: #endif
                     62:
                     63: /*
                     64:  * Read all relevant header fields.
                     65:  */
                     66:
                     67: int
                     68: grabh(hp, gflags)
                     69:        struct header *hp;
                     70:        int gflags;
                     71: {
                     72:        struct termios ttybuf;
                     73:        sig_t saveint;
                     74: #ifndef TIOCSTI
                     75:        sig_t savequit;
                     76: #endif
                     77:        sig_t savetstp;
                     78:        sig_t savettou;
                     79:        sig_t savettin;
1.7       millert    80:        int errs = 0;
1.2       deraadt    81: #ifdef __GNUC__
1.5       millert    82:        /* Avoid siglongjmp clobbering */
1.4       millert    83:        (void)&saveint;
1.7       millert    84:        (void)&errs;
1.2       deraadt    85: #endif
1.1       deraadt    86:
                     87:        savetstp = signal(SIGTSTP, SIG_DFL);
                     88:        savettou = signal(SIGTTOU, SIG_DFL);
                     89:        savettin = signal(SIGTTIN, SIG_DFL);
                     90:        errs = 0;
                     91: #ifndef TIOCSTI
                     92:        ttyset = 0;
                     93: #endif
                     94:        if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
1.3       millert    95:                warn("tcgetattr");
1.1       deraadt    96:                return(-1);
                     97:        }
                     98:        c_erase = ttybuf.c_cc[VERASE];
                     99:        c_kill = ttybuf.c_cc[VKILL];
                    100: #ifndef TIOCSTI
                    101:        ttybuf.c_cc[VERASE] = 0;
                    102:        ttybuf.c_cc[VKILL] = 0;
                    103:        if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
1.8       millert   104:                (void)signal(SIGINT, SIG_DFL);
1.1       deraadt   105:        if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
1.8       millert   106:                (void)signal(SIGQUIT, SIG_DFL);
1.1       deraadt   107: #else
1.7       millert   108:        if (sigsetjmp(intjmp, 1)) {
                    109:                errs = SIGINT;
1.1       deraadt   110:                goto out;
1.7       millert   111:        }
1.1       deraadt   112:        saveint = signal(SIGINT, ttyint);
                    113: #endif
                    114:        if (gflags & GTO) {
                    115: #ifndef TIOCSTI
                    116:                if (!ttyset && hp->h_to != NIL)
                    117:                        ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
                    118: #endif
                    119:                hp->h_to =
                    120:                        extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
                    121:        }
                    122:        if (gflags & GSUBJECT) {
                    123: #ifndef TIOCSTI
1.5       millert   124:                if (!ttyset && hp->h_subject != NULL)
1.1       deraadt   125:                        ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
                    126: #endif
                    127:                hp->h_subject = readtty("Subject: ", hp->h_subject);
                    128:        }
                    129:        if (gflags & GCC) {
                    130: #ifndef TIOCSTI
                    131:                if (!ttyset && hp->h_cc != NIL)
                    132:                        ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
                    133: #endif
                    134:                hp->h_cc =
                    135:                        extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
                    136:        }
                    137:        if (gflags & GBCC) {
                    138: #ifndef TIOCSTI
                    139:                if (!ttyset && hp->h_bcc != NIL)
                    140:                        ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
                    141: #endif
                    142:                hp->h_bcc =
                    143:                        extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
                    144:        }
                    145: out:
1.8       millert   146:        (void)signal(SIGTSTP, savetstp);
                    147:        (void)signal(SIGTTOU, savettou);
                    148:        (void)signal(SIGTTIN, savettin);
1.1       deraadt   149: #ifndef TIOCSTI
                    150:        ttybuf.c_cc[VERASE] = c_erase;
                    151:        ttybuf.c_cc[VKILL] = c_kill;
                    152:        if (ttyset)
                    153:                tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1.8       millert   154:        (void)signal(SIGQUIT, savequit);
1.1       deraadt   155: #endif
1.8       millert   156:        (void)signal(SIGINT, saveint);
1.1       deraadt   157:        return(errs);
                    158: }
                    159:
                    160: /*
                    161:  * Read up a header from standard input.
                    162:  * The source string has the preliminary contents to
                    163:  * be read.
                    164:  *
                    165:  */
                    166:
                    167: char *
                    168: readtty(pr, src)
                    169:        char pr[], src[];
                    170: {
                    171:        char ch, canonb[BUFSIZ];
                    172:        int c;
1.2       deraadt   173:        char *cp, *cp2;
                    174: #if __GNUC__
1.5       millert   175:        /* Avoid siglongjmp clobbering */
1.4       millert   176:        (void)&c;
                    177:        (void)&cp2;
1.2       deraadt   178: #endif
1.1       deraadt   179:
                    180:        fputs(pr, stdout);
                    181:        fflush(stdout);
1.5       millert   182:        if (src != NULL && strlen(src) > BUFSIZ - 2) {
1.3       millert   183:                puts("too long to edit");
1.1       deraadt   184:                return(src);
                    185:        }
                    186: #ifndef TIOCSTI
1.5       millert   187:        if (src != NULL)
1.1       deraadt   188:                cp = copy(src, canonb);
                    189:        else
                    190:                cp = copy("", canonb);
                    191:        fputs(canonb, stdout);
                    192:        fflush(stdout);
                    193: #else
1.5       millert   194:        cp = src == NULL ? "" : src;
1.2       deraadt   195:        while ((c = *cp++) != '\0') {
1.1       deraadt   196:                if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
                    197:                    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
                    198:                        ch = '\\';
                    199:                        ioctl(0, TIOCSTI, &ch);
                    200:                }
                    201:                ch = c;
                    202:                ioctl(0, TIOCSTI, &ch);
                    203:        }
                    204:        cp = canonb;
                    205:        *cp = 0;
                    206: #endif
                    207:        cp2 = cp;
                    208:        while (cp2 < canonb + BUFSIZ)
                    209:                *cp2++ = 0;
                    210:        cp2 = cp;
1.5       millert   211:        if (sigsetjmp(rewrite, 1))
1.1       deraadt   212:                goto redo;
1.8       millert   213:        (void)signal(SIGTSTP, ttystop);
                    214:        (void)signal(SIGTTOU, ttystop);
                    215:        (void)signal(SIGTTIN, ttystop);
1.1       deraadt   216:        clearerr(stdin);
                    217:        while (cp2 < canonb + BUFSIZ) {
                    218:                c = getc(stdin);
                    219:                if (c == EOF || c == '\n')
                    220:                        break;
                    221:                *cp2++ = c;
                    222:        }
                    223:        *cp2 = 0;
1.8       millert   224:        (void)signal(SIGTSTP, SIG_DFL);
                    225:        (void)signal(SIGTTOU, SIG_DFL);
                    226:        (void)signal(SIGTTIN, SIG_DFL);
1.1       deraadt   227:        if (c == EOF && ferror(stdin)) {
                    228: redo:
1.5       millert   229:                cp = strlen(canonb) > 0 ? canonb : NULL;
1.1       deraadt   230:                clearerr(stdin);
                    231:                return(readtty(pr, cp));
                    232:        }
                    233: #ifndef TIOCSTI
1.5       millert   234:        if (cp == NULL || *cp == '\0')
1.1       deraadt   235:                return(src);
                    236:        cp2 = cp;
                    237:        if (!ttyset)
1.5       millert   238:                return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
1.1       deraadt   239:        while (*cp != '\0') {
                    240:                c = *cp++;
                    241:                if (c_erase != _POSIX_VDISABLE && c == c_erase) {
                    242:                        if (cp2 == canonb)
                    243:                                continue;
                    244:                        if (cp2[-1] == '\\') {
                    245:                                cp2[-1] = c;
                    246:                                continue;
                    247:                        }
                    248:                        cp2--;
                    249:                        continue;
                    250:                }
                    251:                if (c_kill != _POSIX_VDISABLE && c == c_kill) {
                    252:                        if (cp2 == canonb)
                    253:                                continue;
                    254:                        if (cp2[-1] == '\\') {
                    255:                                cp2[-1] = c;
                    256:                                continue;
                    257:                        }
                    258:                        cp2 = canonb;
                    259:                        continue;
                    260:                }
                    261:                *cp2++ = c;
                    262:        }
                    263:        *cp2 = '\0';
                    264: #endif
                    265:        if (equal("", canonb))
1.5       millert   266:                return(NULL);
1.1       deraadt   267:        return(savestr(canonb));
                    268: }
                    269:
                    270: /*
                    271:  * Receipt continuation.
                    272:  */
                    273: void
                    274: ttystop(s)
                    275:        int s;
                    276: {
                    277:        sig_t old_action = signal(s, SIG_DFL);
1.2       deraadt   278:        sigset_t nset;
1.1       deraadt   279:
1.8       millert   280:        (void)sigemptyset(&nset);
                    281:        (void)sigaddset(&nset, s);
                    282:        (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
                    283:        (void)kill(0, s);
                    284:        (void)sigprocmask(SIG_BLOCK, &nset, NULL);
                    285:        (void)signal(s, old_action);
1.5       millert   286:        siglongjmp(rewrite, 1);
1.1       deraadt   287: }
                    288:
                    289: /*ARGSUSED*/
                    290: void
                    291: ttyint(s)
                    292:        int s;
                    293: {
1.5       millert   294:        siglongjmp(intjmp, 1);
1.1       deraadt   295: }