xref: /NextBSD/usr.bin/tset/set.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #ifndef lint
35 static const char sccsid[] = "@(#)set.c	8.2 (Berkeley) 2/28/94";
36 #endif
37 
38 #include <stdio.h>
39 #include <termcap.h>
40 #include <termios.h>
41 #include <unistd.h>
42 
43 #include "extern.h"
44 
45 #define	CHK(val, dft)	(val <= 0 ? dft : val)
46 
47 int	set_tabs(void);
48 
49 /*
50  * Reset the terminal mode bits to a sensible state.  Very useful after
51  * a child program dies in raw mode.
52  */
53 void
reset_mode(void)54 reset_mode(void)
55 {
56 	tcgetattr(STDERR_FILENO, &mode);
57 
58 #if defined(VDISCARD) && defined(CDISCARD)
59 	mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
60 #endif
61 	mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
62 	mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
63 #if defined(VFLUSH) && defined(CFLUSH)
64 	mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
65 #endif
66 	mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
67 	mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
68 #if defined(VLNEXT) && defined(CLNEXT)
69 	mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
70 #endif
71 	mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
72 #if defined(VREPRINT) && defined(CRPRNT)
73 	mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
74 #endif
75 	mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
76 	mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
77 	mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
78 #if defined(VWERASE) && defined(CWERASE)
79 	mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
80 #endif
81 
82 	mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
83 #ifdef IUCLC
84 			  | IUCLC
85 #endif
86 #ifdef IXANY
87 			  | IXANY
88 #endif
89 			  | IXOFF);
90 
91 	mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
92 #ifdef IMAXBEL
93 			 | IMAXBEL
94 #endif
95 			 );
96 
97 	mode.c_oflag &= ~(0
98 #ifdef OLCUC
99 			  | OLCUC
100 #endif
101 #ifdef OCRNL
102 			  | OCRNL
103 #endif
104 #ifdef ONOCR
105 			  | ONOCR
106 #endif
107 #ifdef ONLRET
108 			  | ONLRET
109 #endif
110 #ifdef OFILL
111 			  | OFILL
112 #endif
113 #ifdef OFDEL
114 			  | OFDEL
115 #endif
116 #ifdef NLDLY
117 			  | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY
118 #endif
119 			  );
120 
121 	mode.c_oflag |= (OPOST
122 #ifdef ONLCR
123 			 | ONLCR
124 #endif
125 			 );
126 
127 	mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
128 	mode.c_cflag |= (CS8 | CREAD);
129 	mode.c_lflag &= ~(ECHONL | NOFLSH | TOSTOP
130 #ifdef ECHOPTR
131 			  | ECHOPRT
132 #endif
133 #ifdef XCASE
134 			  | XCASE
135 #endif
136 			  );
137 
138 	mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
139 #ifdef ECHOCTL
140 			 | ECHOCTL
141 #endif
142 #ifdef ECHOKE
143 			 | ECHOKE
144 #endif
145  			 );
146 
147 	tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
148 }
149 
150 /*
151  * Determine the erase, interrupt, and kill characters from the termcap
152  * entry and command line and update their values in 'mode'.
153  */
154 void
set_control_chars(void)155 set_control_chars(void)
156 {
157 	char *bp, *p, bs_char, buf[1024];
158 
159 	bp = buf;
160 	p = tgetstr("kb", &bp);
161 	if (p == NULL || p[1] != '\0')
162 		p = tgetstr("bc", &bp);
163 	if (p != NULL && p[1] == '\0')
164 		bs_char = p[0];
165 	else if (tgetflag("bs"))
166 		bs_char = CTRL('h');
167 	else
168 		bs_char = 0;
169 
170 	if (erasech == 0 && bs_char != 0 && !tgetflag("os"))
171 		erasech = -1;
172 	if (erasech < 0)
173 		erasech = (bs_char != 0) ? bs_char : CTRL('h');
174 
175 	if (mode.c_cc[VERASE] == 0 || erasech != 0)
176 		mode.c_cc[VERASE] = erasech ? erasech : CERASE;
177 
178 	if (mode.c_cc[VINTR] == 0 || intrchar != 0)
179 		 mode.c_cc[VINTR] = intrchar ? intrchar : CINTR;
180 
181 	if (mode.c_cc[VKILL] == 0 || killch != 0)
182 		mode.c_cc[VKILL] = killch ? killch : CKILL;
183 }
184 
185 /*
186  * Set up various conversions in 'mode', including parity, tabs, returns,
187  * echo, and case, according to the termcap entry.  If the program we're
188  * running was named with a leading upper-case character, map external
189  * uppercase to internal lowercase.
190  */
191 void
set_conversions(int usingupper)192 set_conversions(int usingupper)
193 {
194 	if (tgetflag("UC") || usingupper) {
195 #ifdef IUCLC
196 		mode.c_iflag |= IUCLC;
197 		mode.c_oflag |= OLCUC;
198 #endif
199 	} else if (tgetflag("LC")) {
200 #ifdef IUCLC
201 		mode.c_iflag &= ~IUCLC;
202 		mode.c_oflag &= ~OLCUC;
203 #endif
204 	}
205 	mode.c_iflag &= ~(PARMRK | INPCK);
206 	mode.c_lflag |= ICANON;
207 	if (tgetflag("EP")) {
208 		mode.c_cflag |= PARENB;
209 		mode.c_cflag &= ~PARODD;
210 	}
211 	if (tgetflag("OP")) {
212 		mode.c_cflag |= PARENB;
213 		mode.c_cflag |= PARODD;
214 	}
215 
216 #ifdef ONLCR
217 	mode.c_oflag |= ONLCR;
218 #endif
219 	mode.c_iflag |= ICRNL;
220 	mode.c_lflag |= ECHO;
221 	mode.c_oflag |= OXTABS;
222 	if (tgetflag("NL")) {			/* Newline, not linefeed. */
223 #ifdef ONLCR
224 		mode.c_oflag &= ~ONLCR;
225 #endif
226 		mode.c_iflag &= ~ICRNL;
227 	}
228 	if (tgetflag("HD"))			/* Half duplex. */
229 		mode.c_lflag &= ~ECHO;
230 	if (tgetflag("pt"))			/* Print tabs. */
231 		mode.c_oflag &= ~OXTABS;
232 	mode.c_lflag |= (ECHOE | ECHOK);
233 }
234 
235 /* Output startup string. */
236 void
set_init(void)237 set_init(void)
238 {
239 	char *bp, buf[1024];
240 	int settle;
241 
242 	bp = buf;
243 	if (tgetstr("pc", &bp) != 0)		/* Get/set pad character. */
244 		PC = buf[0];
245 
246 #ifdef TAB3
247 	if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
248 		oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
249 		tcsetattr(STDERR_FILENO, TCSADRAIN, &oldmode);
250 	}
251 #endif
252 	settle = set_tabs();
253 
254 	if (isreset) {
255 		bp = buf;
256 		if (tgetstr("rs", &bp) != 0 || tgetstr("is", &bp) != 0) {
257 			tputs(buf, 0, outc);
258 			settle = 1;
259 		}
260 		bp = buf;
261 		if (tgetstr("rf", &bp) != 0 || tgetstr("if", &bp) != 0) {
262 			cat(buf);
263 			settle = 1;
264 		}
265 	}
266 
267 	if (settle) {
268 		(void)putc('\r', stderr);
269 		(void)fflush(stderr);
270 		(void)sleep(1);			/* Settle the terminal. */
271 	}
272 }
273 
274 /*
275  * Set the hardware tabs on the terminal, using the ct (clear all tabs),
276  * st (set one tab) and ch (horizontal cursor addressing) capabilities.
277  * This is done before if and is, so they can patch in case we blow this.
278  * Return nonzero if we set any tab stops, zero if not.
279  */
280 int
set_tabs(void)281 set_tabs(void)
282 {
283 	int c;
284 	char *capsp, *clear_tabs;
285 	char *set_column, *set_pos, *Set_tab;
286 	char caps[1024];
287 	const char *tg_out;
288 
289 	capsp = caps;
290 	Set_tab = tgetstr("st", &capsp);
291 
292 	if (Set_tab && (clear_tabs = tgetstr("ct", &capsp))) {
293 		(void)putc('\r', stderr);	/* Force to left margin. */
294 		tputs(clear_tabs, 0, outc);
295 	}
296 
297 	set_column = tgetstr("ch", &capsp);
298 	set_pos = set_column ? NULL : tgetstr("cm", &capsp);
299 
300 	if (Set_tab) {
301 		for (c = 8; c < Columns; c += 8) {
302 			/*
303 			 * Get to the right column.  "OOPS" is returned by
304 			 * tgoto() if it can't do the job.  (*snarl*)
305 			 */
306 			tg_out = "OOPS";
307 			if (set_column)
308 				tg_out = tgoto(set_column, 0, c);
309 			if (*tg_out == 'O' && set_pos)
310 				tg_out = tgoto(set_pos, c, Lines - 1);
311 			if (*tg_out != 'O')
312 				tputs(tg_out, 1, outc);
313 			else
314 				(void)fprintf(stderr, "%s", "        ");
315 			/* Set the tab. */
316 			tputs(Set_tab, 0, outc);
317 		}
318 		putc('\r', stderr);
319 		return (1);
320 	}
321 	return (0);
322 }
323