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