xref: /original-bsd/usr.bin/tset/set.c (revision f737e041)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)set.c	8.2 (Berkeley) 02/28/94";
10 #endif /* not lint */
11 
12 #include <termios.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include "extern.h"
16 
17 #define	CHK(val, dft)	(val <= 0 ? dft : val)
18 
19 int	set_tabs __P((void));
20 
21 /*
22  * Reset the terminal mode bits to a sensible state.  Very useful after
23  * a child program dies in raw mode.
24  */
25 void
26 reset_mode()
27 {
28 	tcgetattr(STDERR_FILENO, &mode);
29 
30 #if defined(VDISCARD) && defined(CDISCARD)
31 	mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
32 #endif
33 	mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
34 	mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
35 #if defined(VFLUSH) && defined(CFLUSH)
36 	mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
37 #endif
38 	mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
39 	mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
40 #if defined(VLNEXT) && defined(CLNEXT)
41 	mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
42 #endif
43 	mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
44 #if defined(VREPRINT) && defined(CRPRNT)
45 	mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
46 #endif
47 	mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
48 	mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
49 	mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
50 #if defined(VWERASE) && defined(CWERASE)
51 	mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
52 #endif
53 
54 	mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
55 #ifdef IUCLC
56 			  | IUCLC
57 #endif
58 #ifdef IXANY
59 			  | IXANY
60 #endif
61 			  | IXOFF);
62 
63 	mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
64 #ifdef IMAXBEL
65 			 | IMAXBEL
66 #endif
67 			 );
68 
69 	mode.c_oflag &= ~(0
70 #ifdef OLCUC
71 			  | OLCUC
72 #endif
73 #ifdef OCRNL
74 			  | OCRNL
75 #endif
76 #ifdef ONOCR
77 			  | ONOCR
78 #endif
79 #ifdef ONLRET
80 			  | ONLRET
81 #endif
82 #ifdef OFILL
83 			  | OFILL
84 #endif
85 #ifdef OFDEL
86 			  | OFDEL
87 #endif
88 #ifdef NLDLY
89 			  | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
90 #endif
91 			  );
92 
93 	mode.c_oflag |= (OPOST
94 #ifdef ONLCR
95 			 | ONLCR
96 #endif
97 			 );
98 
99 	mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
100 	mode.c_cflag |= (CS8 | CREAD);
101 	mode.c_lflag &= ~(ECHONL | NOFLSH | TOSTOP
102 #ifdef ECHOPTR
103 			  | ECHOPRT
104 #endif
105 #ifdef XCASE
106 			  | XCASE
107 #endif
108 			  );
109 
110 	mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
111 #ifdef ECHOCTL
112 			 | ECHOCTL
113 #endif
114 #ifdef ECHOKE
115 			 | ECHOKE
116 #endif
117  			 );
118 
119 	tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
120 }
121 
122 /*
123  * Determine the erase, interrupt, and kill characters from the termcap
124  * entry and command line and update their values in 'mode'.
125  */
126 void
127 set_control_chars()
128 {
129 	char *bp, *p, bs_char, buf[1024];
130 
131 	bp = buf;
132 	p = tgetstr("kb", &bp);
133 	if (p == NULL || p[1] != '\0')
134 		p = tgetstr("bc", &bp);
135 	if (p != NULL && p[1] == '\0')
136 		bs_char = p[0];
137 	else if (tgetflag("bs"))
138 		bs_char = CTRL('h');
139 	else
140 		bs_char = 0;
141 
142 	if (erasechar == 0 && !tgetflag("os") && mode.c_cc[VERASE] != CERASE) {
143 		if (tgetflag("bs") || bs_char != 0)
144 			erasechar = -1;
145 	}
146 	if (erasechar < 0)
147 		erasechar = (bs_char != 0) ? bs_char : CTRL('h');
148 
149 	if (mode.c_cc[VERASE] == 0 || erasechar != 0)
150 		mode.c_cc[VERASE] = erasechar ? erasechar : CERASE;
151 
152 	if (mode.c_cc[VINTR] == 0 || intrchar != 0)
153 		 mode.c_cc[VINTR] = intrchar ? intrchar : CINTR;
154 
155 	if (mode.c_cc[VKILL] == 0 || killchar != 0)
156 		mode.c_cc[VKILL] = killchar ? killchar : CKILL;
157 }
158 
159 /*
160  * Set up various conversions in 'mode', including parity, tabs, returns,
161  * echo, and case, according to the termcap entry.  If the program we're
162  * running was named with a leading upper-case character, map external
163  * uppercase to internal lowercase.
164  */
165 void
166 set_conversions(usingupper)
167 	int usingupper;
168 {
169 	if (tgetflag("UC") || usingupper) {
170 #ifdef IUCLC
171 		mode.c_iflag |= IUCLC;
172 		mode.c_oflag |= OLCUC;
173 #endif
174 	} else if (tgetflag("LC")) {
175 #ifdef IUCLC
176 		mode.c_iflag &= ~IUCLC;
177 		mode.c_oflag &= ~OLCUC;
178 #endif
179 	}
180 	mode.c_iflag &= ~(PARMRK | INPCK);
181 	mode.c_lflag |= ICANON;
182 	if (tgetflag("EP")) {
183 		mode.c_cflag |= PARENB;
184 		mode.c_cflag &= ~PARODD;
185 	}
186 	if (tgetflag("OP")) {
187 		mode.c_cflag |= PARENB;
188 		mode.c_cflag |= PARODD;
189 	}
190 
191 #ifdef ONLCR
192 	mode.c_oflag |= ONLCR;
193 #endif
194 	mode.c_iflag |= ICRNL;
195 	mode.c_lflag |= ECHO;
196 	mode.c_oflag |= OXTABS;
197 	if (tgetflag("NL")) {			/* Newline, not linefeed. */
198 #ifdef ONLCR
199 		mode.c_oflag &= ~ONLCR;
200 #endif
201 		mode.c_iflag &= ~ICRNL;
202 	}
203 	if (tgetflag("HD"))			/* Half duplex. */
204 		mode.c_lflag &= ~ECHO;
205 	if (tgetflag("pt"))			/* Print tabs. */
206 		mode.c_oflag &= ~OXTABS;
207 	mode.c_lflag |= (ECHOE | ECHOK);
208 }
209 
210 /* Output startup string. */
211 void
212 set_init()
213 {
214 	char *bp, buf[1024];
215 	int settle;
216 
217 	bp = buf;
218 	if (tgetstr("pc", &bp) != 0)		/* Get/set pad character. */
219 		PC = buf[0];
220 
221 #ifdef TAB3
222 	if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
223 		oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
224 		tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode);
225 	}
226 #endif
227 	settle = set_tabs();
228 
229 	if (isreset) {
230 		bp = buf;
231 		if (tgetstr("rs", &bp) != 0 || tgetstr("is", &bp) != 0) {
232 			tputs(buf, 0, outc);
233 			settle = 1;
234 		}
235 		bp = buf;
236 		if (tgetstr("rf", &bp) != 0 || tgetstr("if", &bp) != 0) {
237 			cat(buf);
238 			settle = 1;
239 		}
240 	}
241 
242 	if (settle) {
243 		(void)putc('\r', stderr);
244 		(void)fflush(stderr);
245 		(void)sleep(1);			/* Settle the terminal. */
246 	}
247 }
248 
249 /*
250  * Set the hardware tabs on the terminal, using the ct (clear all tabs),
251  * st (set one tab) and ch (horizontal cursor addressing) capabilities.
252  * This is done before if and is, so they can patch in case we blow this.
253  * Return nonzero if we set any tab stops, zero if not.
254  */
255 int
256 set_tabs()
257 {
258 	int c;
259 	char *capsp, *clear_tabs;
260 	char *set_column, *set_pos, *set_tab, *tg_out;
261 	char caps[1024];
262 
263 	capsp = caps;
264 	set_tab = tgetstr("st", &capsp);
265 
266 	if (set_tab && (clear_tabs = tgetstr("ct", &capsp))) {
267 		(void)putc('\r', stderr);	/* Force to left margin. */
268 		tputs(clear_tabs, 0, outc);
269 	}
270 
271 	set_column = tgetstr("ch", &capsp);
272 	set_pos = set_column ? NULL : tgetstr("cm", &capsp);
273 
274 	if (set_tab) {
275 		for (c = 8; c < columns; c += 8) {
276 			/*
277 			 * Get to the right column.  "OOPS" is returned by
278 			 * tgoto() if it can't do the job.  (*snarl*)
279 			 */
280 			tg_out = "OOPS";
281 			if (set_column)
282 				tg_out = tgoto(set_column, 0, c);
283 			if (*tg_out == 'O' && set_pos)
284 				tg_out = tgoto(set_pos, c, lines - 1);
285 			if (*tg_out != 'O')
286 				tputs(tg_out, 1, outc);
287 			else
288 				(void)fprintf(stderr, "%s", "        ");
289 			/* Set the tab. */
290 			tputs(set_tab, 0, outc);
291 		}
292 		putc('\r', stderr);
293 		return (1);
294 	}
295 	return (0);
296 }
297