Home | History | Annotate | Line # | Download | only in mail
tty.c revision 1.22
      1 /*	$NetBSD: tty.c,v 1.22 2006/09/26 23:33:23 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.22 2006/09/26 23:33:23 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 #ifdef USE_READLINE
     50 #include "complete.h"
     51 #endif
     52 
     53 static	cc_t	c_erase;		/* Current erase char */
     54 static	cc_t	c_kill;			/* Current kill char */
     55 static	jmp_buf	rewrite;		/* Place to go when continued */
     56 static	jmp_buf	intjmp;			/* Place to go when interrupted */
     57 #ifndef TIOCSTI
     58 static	int	ttyset;			/* We must now do erase/kill */
     59 #endif
     60 
     61 /*
     62  * Read all relevant header fields.
     63  */
     64 
     65 int
     66 grabh(struct header *hp, int gflags)
     67 {
     68 	struct termios ttybuf;
     69 	sig_t saveint;
     70 	sig_t savetstp;
     71 	sig_t savettou;
     72 	sig_t savettin;
     73 	int errs;
     74 #ifndef TIOCSTI
     75 	sig_t savequit;
     76 #else
     77 # ifdef TIOCEXT
     78 	int extproc, flag;
     79 # endif /* TIOCEXT */
     80 #endif /* TIOCSTI */
     81 
     82 #ifdef __GNUC__
     83 	/* Avoid longjmp clobbering */
     84 # if defined(TIOCSTI) && defined(TIOCEXT)
     85 	(void)&extproc;
     86 # endif
     87 	(void)&saveint;
     88 	saveint = 0;	/* XXX gcc */
     89 #endif /* __GNUC__ */
     90 
     91 	savetstp = signal(SIGTSTP, SIG_DFL);
     92 	savettou = signal(SIGTTOU, SIG_DFL);
     93 	savettin = signal(SIGTTIN, SIG_DFL);
     94 	errs = 0;
     95 #ifndef TIOCSTI
     96 	ttyset = 0;
     97 #endif
     98 	if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
     99 		warn("tcgetattr");
    100 		return(-1);
    101 	}
    102 	c_erase = ttybuf.c_cc[VERASE];
    103 	c_kill = ttybuf.c_cc[VKILL];
    104 #ifndef TIOCSTI
    105 	ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
    106 	ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
    107 	if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
    108 		(void)signal(SIGINT, SIG_DFL);
    109 	if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
    110 		(void)signal(SIGQUIT, SIG_DFL);
    111 #else
    112 # ifdef		TIOCEXT
    113 	extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
    114 	if (extproc) {
    115 		flag = 0;
    116 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
    117 			warn("TIOCEXT: off");
    118 	}
    119 # endif	/* TIOCEXT */
    120 	if (setjmp(intjmp)) {
    121 #ifdef USE_READLINE
    122 		(void)printf("\n(continue)\n");
    123 #endif /* USE_READLINE */
    124 		goto out;
    125 	}
    126 	saveint = signal(SIGINT, ttyint);
    127 #endif
    128 	if (gflags & GTO) {
    129 #ifndef TIOCSTI
    130 		if (!ttyset && hp->h_to != NULL)
    131 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    132 #endif
    133 		hp->h_to =
    134 			extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
    135 	}
    136 	if (gflags & GSUBJECT) {
    137 #ifndef TIOCSTI
    138 		if (!ttyset && hp->h_subject != NULL)
    139 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    140 #endif
    141 		hp->h_subject = readtty("Subject: ", hp->h_subject);
    142 	}
    143 	if (gflags & GCC) {
    144 #ifndef TIOCSTI
    145 		if (!ttyset && hp->h_cc != NULL)
    146 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    147 #endif
    148 		hp->h_cc =
    149 			extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
    150 	}
    151 	if (gflags & GBCC) {
    152 #ifndef TIOCSTI
    153 		if (!ttyset && hp->h_bcc != NULL)
    154 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    155 #endif
    156 		hp->h_bcc =
    157 			extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
    158 	}
    159 	if (gflags & GSMOPTS) {
    160 		char *smopts;
    161 		char *argv[MAXARGC];
    162 		int argc, i;
    163 		struct name *np, *t;
    164 
    165 #ifndef TIOCSTI
    166 		if (!ttyset && hp->h_smopts != NULL)
    167 			ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    168 #endif
    169 		smopts = readtty("Smopts: ", detract(hp->h_smopts, GSMOPTS));
    170 
    171 		/* Parse smopts with getrawlist() rather than expand()
    172 		 * to get a shell-like expansion.
    173 		 */
    174 		hp->h_smopts = NULL;
    175 		if (smopts) {
    176 			np = NULL;
    177 			argc = getrawlist(smopts, argv, sizeof(argv)/sizeof(*argv));
    178 			for (i = 0 ; i < argc ; i++) {
    179 				t = nalloc(argv[i], GSMOPTS);
    180 				if (hp->h_smopts == NULL)
    181 			hp->h_smopts = t;
    182 				else
    183 			np->n_flink = t;
    184 				t->n_blink = np;
    185 				np = t;
    186 			}
    187 		}
    188 	}
    189 out:
    190 	(void)signal(SIGTSTP, savetstp);
    191 	(void)signal(SIGTTOU, savettou);
    192 	(void)signal(SIGTTIN, savettin);
    193 #ifndef TIOCSTI
    194 	ttybuf.c_cc[VERASE] = c_erase;
    195 	ttybuf.c_cc[VKILL] = c_kill;
    196 	if (ttyset)
    197 		tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
    198 	(void)signal(SIGQUIT, savequit);
    199 #else
    200 # ifdef		TIOCEXT
    201 	if (extproc) {
    202 		flag = 1;
    203 		if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
    204 			warn("TIOCEXT: on");
    205 	}
    206 # endif	/* TIOCEXT */
    207 #endif
    208 	(void)signal(SIGINT, saveint);
    209 	return(errs);
    210 }
    211 
    212 /*
    213  * Read up a header from standard input.
    214  * The source string has the preliminary contents to
    215  * be read.
    216  *
    217  */
    218 
    219 #ifdef USE_READLINE
    220 char *
    221 readtty(const char pr[], char src[])
    222 {
    223   return savestr(rl_getline(pr, src));
    224 }
    225 #else
    226 char *
    227 readtty(const char pr[], char src[])
    228 {
    229 	char ch, canonb[BUFSIZ];
    230 	int c;
    231 	char *cp, *cp2;
    232 	static char empty[] = "";
    233 #if __GNUC__
    234 	/* Avoid longjmp clobbering */
    235 	(void)&c;
    236 	(void)&cp2;
    237 #endif
    238 
    239 	(void)fputs(pr, stdout);
    240 	(void)fflush(stdout);
    241 	if (src != NULL && strlen(src) > BUFSIZ - 2) {
    242 		(void)printf("too long to edit\n");
    243 		return(src);
    244 	}
    245 #ifndef TIOCSTI
    246 	if (src != NULL)
    247 		cp = copy(src, canonb);
    248 	else
    249 		cp = copy("", canonb);
    250 	(void)fputs(canonb, stdout);
    251 	(void)fflush(stdout);
    252 #else
    253 	cp = src == NULL ? empty : src;
    254 	while ((c = *cp++) != '\0') {
    255 		if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
    256 		    (c_kill != _POSIX_VDISABLE && c == c_kill)) {
    257 			ch = '\\';
    258 			(void)ioctl(0, TIOCSTI, &ch);
    259 		}
    260 		ch = c;
    261 		(void)ioctl(0, TIOCSTI, &ch);
    262 	}
    263 	cp = canonb;
    264 	*cp = 0;
    265 #endif
    266 	cp2 = cp;
    267 	while (cp2 < canonb + BUFSIZ)
    268 		*cp2++ = 0;
    269 	cp2 = cp;
    270 	if (setjmp(rewrite))
    271 		goto redo;
    272 	(void)signal(SIGTSTP, ttystop);
    273 	(void)signal(SIGTTOU, ttystop);
    274 	(void)signal(SIGTTIN, ttystop);
    275 	clearerr(stdin);
    276 	while (cp2 < canonb + BUFSIZ) {
    277 		c = getc(stdin);
    278 		if (c == EOF || c == '\n')
    279 			break;
    280 		*cp2++ = c;
    281 	}
    282 	*cp2 = 0;
    283 	(void)signal(SIGTSTP, SIG_DFL);
    284 	(void)signal(SIGTTOU, SIG_DFL);
    285 	(void)signal(SIGTTIN, SIG_DFL);
    286 	if (c == EOF && ferror(stdin)) {
    287 redo:
    288 		cp = strlen(canonb) > 0 ? canonb : NULL;
    289 		clearerr(stdin);
    290 		return(readtty(pr, cp));
    291 	}
    292 #ifndef TIOCSTI
    293 	if (cp == NULL || *cp == '\0')
    294 		return(src);
    295 	cp2 = cp;
    296 	if (!ttyset)
    297 		return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
    298 	while (*cp != '\0') {
    299 		c = *cp++;
    300 		if (c_erase != _POSIX_VDISABLE && c == c_erase) {
    301 			if (cp2 == canonb)
    302 				continue;
    303 			if (cp2[-1] == '\\') {
    304 				cp2[-1] = c;
    305 				continue;
    306 			}
    307 			cp2--;
    308 			continue;
    309 		}
    310 		if (c_kill != _POSIX_VDISABLE && c == c_kill) {
    311 			if (cp2 == canonb)
    312 				continue;
    313 			if (cp2[-1] == '\\') {
    314 				cp2[-1] = c;
    315 				continue;
    316 			}
    317 			cp2 = canonb;
    318 			continue;
    319 		}
    320 		*cp2++ = c;
    321 	}
    322 	*cp2 = '\0';
    323 #endif
    324 	if (equal("", canonb))
    325 		return(NULL);
    326 	return(savestr(canonb));
    327 }
    328 #endif
    329 
    330 /*
    331  * Receipt continuation.
    332  */
    333 void
    334 ttystop(int s)
    335 {
    336 	sig_t old_action = signal(s, SIG_DFL);
    337 	sigset_t nset;
    338 
    339 	(void)sigemptyset(&nset);
    340 	(void)sigaddset(&nset, s);
    341 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
    342 	(void)kill(0, s);
    343 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
    344 	(void)signal(s, old_action);
    345 	longjmp(rewrite, 1);
    346 }
    347 
    348 /*ARGSUSED*/
    349 void
    350 ttyint(int s)
    351 {
    352 	longjmp(intjmp, 1);
    353 }
    354