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