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