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