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