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