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