xref: /original-bsd/usr.bin/tset/set.c (revision cc6acb7c)
124f68c83Sbostic /*-
2c0ac44d0Sbostic  * Copyright (c) 1991, 1993
3c0ac44d0Sbostic  *	The Regents of the University of California.  All rights reserved.
424f68c83Sbostic  *
524f68c83Sbostic  * %sccs.include.redist.c%
624f68c83Sbostic  */
724f68c83Sbostic 
824f68c83Sbostic #ifndef lint
9*cc6acb7cSbostic static char sccsid[] = "@(#)set.c	8.2 (Berkeley) 02/28/94";
1024f68c83Sbostic #endif /* not lint */
1124f68c83Sbostic 
1224f68c83Sbostic #include <termios.h>
1324f68c83Sbostic #include <unistd.h>
1424f68c83Sbostic #include <stdio.h>
1524f68c83Sbostic #include "extern.h"
1624f68c83Sbostic 
1724f68c83Sbostic #define	CHK(val, dft)	(val <= 0 ? dft : val)
1824f68c83Sbostic 
1924f68c83Sbostic int	set_tabs __P((void));
2024f68c83Sbostic 
2124f68c83Sbostic /*
2224f68c83Sbostic  * Reset the terminal mode bits to a sensible state.  Very useful after
2324f68c83Sbostic  * a child program dies in raw mode.
2424f68c83Sbostic  */
2524f68c83Sbostic void
reset_mode()2624f68c83Sbostic reset_mode()
2724f68c83Sbostic {
2824f68c83Sbostic 	tcgetattr(STDERR_FILENO, &mode);
2924f68c83Sbostic 
3024f68c83Sbostic #if defined(VDISCARD) && defined(CDISCARD)
3124f68c83Sbostic 	mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
3224f68c83Sbostic #endif
3324f68c83Sbostic 	mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
3424f68c83Sbostic 	mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
3524f68c83Sbostic #if defined(VFLUSH) && defined(CFLUSH)
3624f68c83Sbostic 	mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
3724f68c83Sbostic #endif
3824f68c83Sbostic 	mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
3924f68c83Sbostic 	mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
4024f68c83Sbostic #if defined(VLNEXT) && defined(CLNEXT)
4124f68c83Sbostic 	mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
4224f68c83Sbostic #endif
4324f68c83Sbostic 	mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
4424f68c83Sbostic #if defined(VREPRINT) && defined(CRPRNT)
4524f68c83Sbostic 	mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
4624f68c83Sbostic #endif
4724f68c83Sbostic 	mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
4824f68c83Sbostic 	mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
4924f68c83Sbostic 	mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
5024f68c83Sbostic #if defined(VWERASE) && defined(CWERASE)
5124f68c83Sbostic 	mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
5224f68c83Sbostic #endif
5324f68c83Sbostic 
5424f68c83Sbostic 	mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
5524f68c83Sbostic #ifdef IUCLC
5624f68c83Sbostic 			  | IUCLC
5724f68c83Sbostic #endif
5824f68c83Sbostic #ifdef IXANY
5924f68c83Sbostic 			  | IXANY
6024f68c83Sbostic #endif
6124f68c83Sbostic 			  | IXOFF);
6224f68c83Sbostic 
6324f68c83Sbostic 	mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
6424f68c83Sbostic #ifdef IMAXBEL
6524f68c83Sbostic 			 | IMAXBEL
6624f68c83Sbostic #endif
6724f68c83Sbostic 			 );
6824f68c83Sbostic 
6924f68c83Sbostic 	mode.c_oflag &= ~(0
7024f68c83Sbostic #ifdef OLCUC
7124f68c83Sbostic 			  | OLCUC
7224f68c83Sbostic #endif
7324f68c83Sbostic #ifdef OCRNL
7424f68c83Sbostic 			  | OCRNL
7524f68c83Sbostic #endif
7624f68c83Sbostic #ifdef ONOCR
7724f68c83Sbostic 			  | ONOCR
7824f68c83Sbostic #endif
7924f68c83Sbostic #ifdef ONLRET
8024f68c83Sbostic 			  | ONLRET
8124f68c83Sbostic #endif
8224f68c83Sbostic #ifdef OFILL
8324f68c83Sbostic 			  | OFILL
8424f68c83Sbostic #endif
8524f68c83Sbostic #ifdef OFDEL
8624f68c83Sbostic 			  | OFDEL
8724f68c83Sbostic #endif
8824f68c83Sbostic #ifdef NLDLY
8924f68c83Sbostic 			  | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
9024f68c83Sbostic #endif
9124f68c83Sbostic 			  );
9224f68c83Sbostic 
9324f68c83Sbostic 	mode.c_oflag |= (OPOST
9424f68c83Sbostic #ifdef ONLCR
9524f68c83Sbostic 			 | ONLCR
9624f68c83Sbostic #endif
9724f68c83Sbostic 			 );
9824f68c83Sbostic 
9924f68c83Sbostic 	mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
10024f68c83Sbostic 	mode.c_cflag |= (CS8 | CREAD);
10124f68c83Sbostic 	mode.c_lflag &= ~(ECHONL | NOFLSH | TOSTOP
10224f68c83Sbostic #ifdef ECHOPTR
10324f68c83Sbostic 			  | ECHOPRT
10424f68c83Sbostic #endif
10524f68c83Sbostic #ifdef XCASE
10624f68c83Sbostic 			  | XCASE
10724f68c83Sbostic #endif
10824f68c83Sbostic 			  );
10924f68c83Sbostic 
11024f68c83Sbostic 	mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
11124f68c83Sbostic #ifdef ECHOCTL
11224f68c83Sbostic 			 | ECHOCTL
11324f68c83Sbostic #endif
11424f68c83Sbostic #ifdef ECHOKE
11524f68c83Sbostic 			 | ECHOKE
11624f68c83Sbostic #endif
11724f68c83Sbostic  			 );
11824f68c83Sbostic 
11924f68c83Sbostic 	tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
12024f68c83Sbostic }
12124f68c83Sbostic 
12224f68c83Sbostic /*
12324f68c83Sbostic  * Determine the erase, interrupt, and kill characters from the termcap
12424f68c83Sbostic  * entry and command line and update their values in 'mode'.
12524f68c83Sbostic  */
12624f68c83Sbostic void
set_control_chars()12724f68c83Sbostic set_control_chars()
12824f68c83Sbostic {
12924f68c83Sbostic 	char *bp, *p, bs_char, buf[1024];
13024f68c83Sbostic 
13124f68c83Sbostic 	bp = buf;
13224f68c83Sbostic 	p = tgetstr("kb", &bp);
13324f68c83Sbostic 	if (p == NULL || p[1] != '\0')
13424f68c83Sbostic 		p = tgetstr("bc", &bp);
13524f68c83Sbostic 	if (p != NULL && p[1] == '\0')
13624f68c83Sbostic 		bs_char = p[0];
13724f68c83Sbostic 	else if (tgetflag("bs"))
13824f68c83Sbostic 		bs_char = CTRL('h');
13924f68c83Sbostic 	else
14024f68c83Sbostic 		bs_char = 0;
14124f68c83Sbostic 
142257f08a2Sbostic 	if (erasechar == 0 && !tgetflag("os") && mode.c_cc[VERASE] != CERASE) {
14324f68c83Sbostic 		if (tgetflag("bs") || bs_char != 0)
14424f68c83Sbostic 			erasechar = -1;
14524f68c83Sbostic 	}
14624f68c83Sbostic 	if (erasechar < 0)
14724f68c83Sbostic 		erasechar = (bs_char != 0) ? bs_char : CTRL('h');
14824f68c83Sbostic 
14924f68c83Sbostic 	if (mode.c_cc[VERASE] == 0 || erasechar != 0)
15024f68c83Sbostic 		mode.c_cc[VERASE] = erasechar ? erasechar : CERASE;
15124f68c83Sbostic 
15224f68c83Sbostic 	if (mode.c_cc[VINTR] == 0 || intrchar != 0)
15324f68c83Sbostic 		 mode.c_cc[VINTR] = intrchar ? intrchar : CINTR;
15424f68c83Sbostic 
15524f68c83Sbostic 	if (mode.c_cc[VKILL] == 0 || killchar != 0)
15624f68c83Sbostic 		mode.c_cc[VKILL] = killchar ? killchar : CKILL;
15724f68c83Sbostic }
15824f68c83Sbostic 
15924f68c83Sbostic /*
16024f68c83Sbostic  * Set up various conversions in 'mode', including parity, tabs, returns,
16124f68c83Sbostic  * echo, and case, according to the termcap entry.  If the program we're
16224f68c83Sbostic  * running was named with a leading upper-case character, map external
16324f68c83Sbostic  * uppercase to internal lowercase.
16424f68c83Sbostic  */
16524f68c83Sbostic void
set_conversions(usingupper)16624f68c83Sbostic set_conversions(usingupper)
16724f68c83Sbostic 	int usingupper;
16824f68c83Sbostic {
16924f68c83Sbostic 	if (tgetflag("UC") || usingupper) {
17024f68c83Sbostic #ifdef IUCLC
17124f68c83Sbostic 		mode.c_iflag |= IUCLC;
17224f68c83Sbostic 		mode.c_oflag |= OLCUC;
17324f68c83Sbostic #endif
17424f68c83Sbostic 	} else if (tgetflag("LC")) {
17524f68c83Sbostic #ifdef IUCLC
17624f68c83Sbostic 		mode.c_iflag &= ~IUCLC;
17724f68c83Sbostic 		mode.c_oflag &= ~OLCUC;
17824f68c83Sbostic #endif
17924f68c83Sbostic 	}
18024f68c83Sbostic 	mode.c_iflag &= ~(PARMRK | INPCK);
18124f68c83Sbostic 	mode.c_lflag |= ICANON;
18224f68c83Sbostic 	if (tgetflag("EP")) {
18324f68c83Sbostic 		mode.c_cflag |= PARENB;
18424f68c83Sbostic 		mode.c_cflag &= ~PARODD;
18524f68c83Sbostic 	}
18624f68c83Sbostic 	if (tgetflag("OP")) {
18724f68c83Sbostic 		mode.c_cflag |= PARENB;
18824f68c83Sbostic 		mode.c_cflag |= PARODD;
18924f68c83Sbostic 	}
19024f68c83Sbostic 
19124f68c83Sbostic #ifdef ONLCR
19224f68c83Sbostic 	mode.c_oflag |= ONLCR;
19324f68c83Sbostic #endif
19424f68c83Sbostic 	mode.c_iflag |= ICRNL;
19524f68c83Sbostic 	mode.c_lflag |= ECHO;
19624f68c83Sbostic 	mode.c_oflag |= OXTABS;
19724f68c83Sbostic 	if (tgetflag("NL")) {			/* Newline, not linefeed. */
19824f68c83Sbostic #ifdef ONLCR
19924f68c83Sbostic 		mode.c_oflag &= ~ONLCR;
20024f68c83Sbostic #endif
20124f68c83Sbostic 		mode.c_iflag &= ~ICRNL;
20224f68c83Sbostic 	}
20324f68c83Sbostic 	if (tgetflag("HD"))			/* Half duplex. */
20424f68c83Sbostic 		mode.c_lflag &= ~ECHO;
20524f68c83Sbostic 	if (tgetflag("pt"))			/* Print tabs. */
20624f68c83Sbostic 		mode.c_oflag &= ~OXTABS;
20724f68c83Sbostic 	mode.c_lflag |= (ECHOE | ECHOK);
20824f68c83Sbostic }
20924f68c83Sbostic 
21024f68c83Sbostic /* Output startup string. */
21124f68c83Sbostic void
set_init()21224f68c83Sbostic set_init()
21324f68c83Sbostic {
21424f68c83Sbostic 	char *bp, buf[1024];
21524f68c83Sbostic 	int settle;
21624f68c83Sbostic 
21724f68c83Sbostic 	bp = buf;
21824f68c83Sbostic 	if (tgetstr("pc", &bp) != 0)		/* Get/set pad character. */
21924f68c83Sbostic 		PC = buf[0];
22024f68c83Sbostic 
22124f68c83Sbostic #ifdef TAB3
22224f68c83Sbostic 	if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
22324f68c83Sbostic 		oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
22424f68c83Sbostic 		tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode);
22524f68c83Sbostic 	}
22624f68c83Sbostic #endif
22724f68c83Sbostic 	settle = set_tabs();
22824f68c83Sbostic 
22924f68c83Sbostic 	if (isreset) {
230*cc6acb7cSbostic 		bp = buf;
23124f68c83Sbostic 		if (tgetstr("rs", &bp) != 0 || tgetstr("is", &bp) != 0) {
23224f68c83Sbostic 			tputs(buf, 0, outc);
23324f68c83Sbostic 			settle = 1;
23424f68c83Sbostic 		}
235*cc6acb7cSbostic 		bp = buf;
23624f68c83Sbostic 		if (tgetstr("rf", &bp) != 0 || tgetstr("if", &bp) != 0) {
23724f68c83Sbostic 			cat(buf);
23824f68c83Sbostic 			settle = 1;
23924f68c83Sbostic 		}
24024f68c83Sbostic 	}
24124f68c83Sbostic 
24224f68c83Sbostic 	if (settle) {
24324f68c83Sbostic 		(void)putc('\r', stderr);
24424f68c83Sbostic 		(void)fflush(stderr);
24524f68c83Sbostic 		(void)sleep(1);			/* Settle the terminal. */
24624f68c83Sbostic 	}
24724f68c83Sbostic }
24824f68c83Sbostic 
24924f68c83Sbostic /*
25024f68c83Sbostic  * Set the hardware tabs on the terminal, using the ct (clear all tabs),
25124f68c83Sbostic  * st (set one tab) and ch (horizontal cursor addressing) capabilities.
25224f68c83Sbostic  * This is done before if and is, so they can patch in case we blow this.
25324f68c83Sbostic  * Return nonzero if we set any tab stops, zero if not.
25424f68c83Sbostic  */
25524f68c83Sbostic int
set_tabs()25624f68c83Sbostic set_tabs()
25724f68c83Sbostic {
25824f68c83Sbostic 	int c;
25924f68c83Sbostic 	char *capsp, *clear_tabs;
26024f68c83Sbostic 	char *set_column, *set_pos, *set_tab, *tg_out;
26124f68c83Sbostic 	char caps[1024];
26224f68c83Sbostic 
26324f68c83Sbostic 	capsp = caps;
26424f68c83Sbostic 	set_tab = tgetstr("st", &capsp);
26524f68c83Sbostic 
26624f68c83Sbostic 	if (set_tab && (clear_tabs = tgetstr("ct", &capsp))) {
26724f68c83Sbostic 		(void)putc('\r', stderr);	/* Force to left margin. */
26824f68c83Sbostic 		tputs(clear_tabs, 0, outc);
26924f68c83Sbostic 	}
27024f68c83Sbostic 
27124f68c83Sbostic 	set_column = tgetstr("ch", &capsp);
27224f68c83Sbostic 	set_pos = set_column ? NULL : tgetstr("cm", &capsp);
27324f68c83Sbostic 
27424f68c83Sbostic 	if (set_tab) {
27524f68c83Sbostic 		for (c = 8; c < columns; c += 8) {
27624f68c83Sbostic 			/*
27724f68c83Sbostic 			 * Get to the right column.  "OOPS" is returned by
27824f68c83Sbostic 			 * tgoto() if it can't do the job.  (*snarl*)
27924f68c83Sbostic 			 */
28024f68c83Sbostic 			tg_out = "OOPS";
28124f68c83Sbostic 			if (set_column)
28224f68c83Sbostic 				tg_out = tgoto(set_column, 0, c);
28324f68c83Sbostic 			if (*tg_out == 'O' && set_pos)
28424f68c83Sbostic 				tg_out = tgoto(set_pos, c, lines - 1);
28524f68c83Sbostic 			if (*tg_out != 'O')
28624f68c83Sbostic 				tputs(tg_out, 1, outc);
28724f68c83Sbostic 			else
28824f68c83Sbostic 				(void)fprintf(stderr, "%s", "        ");
28924f68c83Sbostic 			/* Set the tab. */
29024f68c83Sbostic 			tputs(set_tab, 0, outc);
29124f68c83Sbostic 		}
29224f68c83Sbostic 		putc('\r', stderr);
29324f68c83Sbostic 		return (1);
29424f68c83Sbostic 	}
29524f68c83Sbostic 	return (0);
29624f68c83Sbostic }
297