xref: /netbsd/lib/libcurses/setterm.c (revision a1bd1df7)
1 /*	$NetBSD: setterm.c,v 1.66 2017/03/23 00:55:39 roy Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
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 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)setterm.c	8.8 (Berkeley) 10/25/94";
36 #else
37 __RCSID("$NetBSD: setterm.c,v 1.66 2017/03/23 00:55:39 roy Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <stdlib.h>
42 #include <string.h>
43 #include <termios.h>
44 #include <unistd.h>
45 
46 #include "curses.h"
47 #include "curses_private.h"
48 
49 static int does_esc_m(const char *cap);
50 static int does_ctrl_o(const char *exit_cap, const char *acs_cap);
51 
52 attr_t	 __mask_op, __mask_me, __mask_ue, __mask_se;
53 
54 int
55 setterm(char *type)
56 {
57 
58 	return _cursesi_setterm(type, _cursesi_screen);
59 }
60 
61 int
62 _cursesi_setterm(char *type, SCREEN *screen)
63 {
64 	int unknown, r;
65 	char *p;
66 
67 	if (type[0] == '\0')
68 		type = "xx";
69 	unknown = 0;
70 	if (screen->term)
71 		del_curterm(screen->term);
72 	(void)ti_setupterm(&screen->term, type, fileno(screen->outfd), &r);
73 	if (screen->term == NULL) {
74 		unknown++;
75 		(void)ti_setupterm(&screen->term, "dumb",
76 		    fileno(screen->outfd), &r);
77 		/* No dumb term? We can't continue */
78 		if (screen->term == NULL)
79 			return ERR;
80 	}
81 #ifdef DEBUG
82 	__CTRACE(__CTRACE_INIT, "setterm: tty = %s\n", type);
83 #endif
84 
85 	/* lines and cols will have been setup correctly by ti_setupterm(3). */
86 	screen->LINES = t_lines(screen->term);
87 	screen->COLS = t_columns(screen->term);
88 
89 	if (screen->filtered) {
90 		/* Disable use of clear, cud, cud1, cup, cuu1 and vpa. */
91 		screen->term->strs[TICODE_clear] = NULL;
92 		screen->term->strs[TICODE_cud] = NULL;
93 		screen->term->strs[TICODE_cud1] = NULL;
94 		screen->term->strs[TICODE_cup] = NULL;
95 		screen->term->strs[TICODE_cuu] = NULL;
96 		screen->term->strs[TICODE_cuu1] = NULL;
97 		screen->term->strs[TICODE_vpa] = NULL;
98 		/* Set the value of the home string to the value of
99 		 * the cr string. */
100 		screen->term->strs[TICODE_home] = screen->term->strs[TICODE_cr];
101 		/* Set lines equal to 1. */
102 		screen->LINES = 1;
103 		t_lines(screen->term) = 1;
104 	}
105 #ifdef DEBUG
106 	__CTRACE(__CTRACE_INIT, "setterm: filtered %d", screen->filtered);
107 #endif
108 
109 	if ((p = getenv("ESCDELAY")) != NULL)
110 		screen->ESCDELAY = (int)strtol(p, NULL, 0);
111 	else
112 		screen->ESCDELAY = ESCDELAY_DEFAULT;
113 	if ((p = getenv("TABSIZE")) != NULL)
114 		screen->TABSIZE = (int)strtol(p, NULL, 0);
115 	else if (t_init_tabs(screen->term) >= 0)
116 		screen->TABSIZE = (int)t_init_tabs(screen->term);
117 	else
118 		screen->TABSIZE = TABSIZE_DEFAULT;
119 	/*
120 	 * Want cols > 4, otherwise things will fail.
121 	 */
122 	if (screen->COLS <= 4)
123 		return ERR;
124 
125 	LINES = screen->LINES - __rippedlines(screen);
126 	COLS = screen->COLS;
127 	ESCDELAY = screen->ESCDELAY;
128 	TABSIZE = screen->TABSIZE;
129 
130 #ifdef DEBUG
131 	__CTRACE(__CTRACE_INIT,
132 	    "setterm: LINES = %d, COLS = %d, TABSIZE = %d\n",
133 	    LINES, COLS, TABSIZE);
134 #endif
135 
136 	/*
137 	 * set the pad char, only take the first char of the pc capability
138 	 * as this is all we can use.
139 	 */
140 	screen->padchar = t_pad_char(screen->term) ?
141 	    t_pad_char(screen->term)[0] : 0;
142 
143 	/* If no scrolling commands, no quick change. */
144 	screen->noqch =
145 	    (t_change_scroll_region(screen->term) == NULL ||
146 		t_cursor_home(screen->term) == NULL ||
147 		(t_parm_index(screen->term) == NULL &&
148 		    t_scroll_forward(screen->term) == NULL) ||
149 		(t_parm_rindex(screen->term) == NULL &&
150 		    t_scroll_reverse(screen->term) == NULL)) &&
151 	    ((t_parm_insert_line(screen->term) == NULL &&
152 		t_insert_line(screen->term) == NULL) ||
153 		(t_parm_delete_line(screen->term) == NULL &&
154 		    t_delete_line(screen->term) == NULL));
155 
156 	/*
157 	 * Precalculate conflict info for color/attribute end commands.
158 	 */
159 #ifndef HAVE_WCHAR
160 	screen->mask_op = __ATTRIBUTES & ~__COLOR;
161 #else
162 	screen->mask_op = WA_ATTRIBUTES & ~__COLOR;
163 #endif /* HAVE_WCHAR */
164 
165 	const char *t_op = t_orig_pair(screen->term);
166 	const char *t_esm = t_exit_standout_mode(screen->term);
167 	const char *t_eum = t_exit_underline_mode(screen->term);
168 	const char *t_eam = t_exit_attribute_mode(screen->term);
169 
170 	if (t_op != NULL) {
171 		if (does_esc_m(t_op))
172 			screen->mask_op &=
173 			    ~(__STANDOUT | __UNDERSCORE | __TERMATTR);
174 		else {
175 			if (t_esm != NULL && !strcmp(t_op, t_esm))
176 				screen->mask_op &= ~__STANDOUT;
177 
178 			if (t_eum != NULL && !strcmp(t_op, t_eum))
179 				screen->mask_op &= ~__UNDERSCORE;
180 
181 			if (t_eam != NULL && !strcmp(t_op, t_eam))
182 				screen->mask_op &= ~__TERMATTR;
183 		}
184 	}
185 	/*
186 	 * Assume that "me" turns off all attributes apart from ACS.
187 	 * It might turn off ACS, so check for that.
188 	 */
189 	if (t_exit_attribute_mode(screen->term) != NULL &&
190 	    t_exit_alt_charset_mode(screen->term) != NULL &&
191 	    does_ctrl_o(t_exit_attribute_mode(screen->term),
192 	    t_exit_alt_charset_mode(screen->term)))
193 		screen->mask_me = 0;
194 	else
195 		screen->mask_me = __ALTCHARSET;
196 
197 	/* Check what turning off the attributes also turns off */
198 #ifndef HAVE_WCHAR
199 	screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE;
200 #else
201 	screen->mask_ue = WA_ATTRIBUTES & ~__UNDERSCORE;
202 #endif /* HAVE_WCHAR */
203 	if (t_eum != NULL) {
204 		if (does_esc_m(t_eum))
205 			screen->mask_ue &=
206 			    ~(__STANDOUT | __TERMATTR | __COLOR);
207 		else {
208 			if (t_esm && !strcmp(t_eum, t_esm))
209 				screen->mask_ue &= ~__STANDOUT;
210 
211 			if (t_eam != NULL && !strcmp(t_eum, t_eam))
212 				screen->mask_ue &= ~__TERMATTR;
213 
214 			if (t_op != NULL && !strcmp(t_eum, t_op))
215 				screen->mask_ue &= ~__COLOR;
216 		}
217 	}
218 #ifndef HAVE_WCHAR
219 	screen->mask_se = __ATTRIBUTES & ~__STANDOUT;
220 #else
221 	screen->mask_se = WA_ATTRIBUTES & ~__STANDOUT;
222 #endif /* HAVE_WCHAR */
223 	if (t_esm != NULL) {
224 		if (does_esc_m(t_esm))
225 			screen->mask_se &=
226 			    ~(__UNDERSCORE | __TERMATTR | __COLOR);
227 		else {
228 			if (t_eum != NULL && !strcmp(t_esm, t_eum))
229 				screen->mask_se &= ~__UNDERSCORE;
230 
231 			if (t_eam != NULL && !strcmp(t_esm, t_eam))
232 				screen->mask_se &= ~__TERMATTR;
233 
234 			if (t_op != NULL && !strcmp(t_esm, t_op))
235 				screen->mask_se &= ~__COLOR;
236 		}
237 	}
238 
239 	return unknown ? ERR : OK;
240 }
241 
242 /*
243  * _cursesi_resetterm --
244  *  Copy the terminal instance data for the given screen to the global
245  *  variables.
246  */
247 void
248 _cursesi_resetterm(SCREEN *screen)
249 {
250 
251 	LINES = screen->LINES - __rippedlines(screen);
252 	COLS = screen->COLS;
253 	ESCDELAY = screen->ESCDELAY;
254 	TABSIZE = screen->TABSIZE;
255 	__GT = screen->GT;
256 
257 	__noqch = screen->noqch;
258 	__mask_op = screen->mask_op;
259 	__mask_me = screen->mask_me;
260 	__mask_ue = screen->mask_ue;
261 	__mask_se = screen->mask_se;
262 
263 	set_curterm(screen->term);
264 }
265 
266 /*
267  * does_esc_m --
268  * A hack for xterm-like terminals where "\E[m" or "\E[0m" will turn off
269  * other attributes, so we check for this in the capability passed to us.
270  * Note that we can't just do simple string comparison, as the capability
271  * may contain multiple, ';' separated sequence parts.
272  */
273 static int
274 does_esc_m(const char *cap)
275 {
276 #define WAITING 0
277 #define PARSING 1
278 #define NUMBER 2
279 #define FOUND 4
280 	const char *capptr;
281 	int seq;
282 
283 #ifdef DEBUG
284 	__CTRACE(__CTRACE_INIT, "does_esc_m: Checking %s\n", cap);
285 #endif
286 	/* Is it just "\E[m" or "\E[0m"? */
287 	if (!strcmp(cap, "\x1b[m") || !strcmp(cap, "\x1b[0m"))
288 		return 1;
289 
290 	/* Does it contain a "\E[...m" sequence? */
291 	if (strlen(cap) < 4)
292 		return 0;
293 	capptr = cap;
294 	seq = WAITING;
295 	while (*capptr != 0) {
296 		switch (seq) {
297 		/* Start of sequence */
298 		case WAITING:
299 			if (!strncmp(capptr, "\x1b[", 2)) {
300 				capptr+=2;
301 				if (*capptr == 'm')
302 					return 1;
303 				else {
304 					seq=PARSING;
305 					continue;
306 				}
307 			}
308 			break;
309 		/* Looking for '0' */
310 		case PARSING:
311 			if (*capptr == '0')
312 				seq=FOUND;
313 			else if (*capptr > '0' && *capptr <= '9')
314 				seq=NUMBER;
315 			else if (*capptr != ';')
316 				seq=WAITING;
317 			break;
318 		/* Some other number */
319 		case NUMBER:
320 			if (*capptr == ';')
321 				seq=PARSING;
322 			else if (!(*capptr >= '0' && *capptr <= '9'))
323 				seq=WAITING;
324 			break;
325 		/* Found a '0' */
326 		case FOUND:
327 			if (*capptr == 'm')
328 				return 1;
329 			else if (!((*capptr >= '0' && *capptr <= '9') ||
330 			    *capptr == ';'))
331 				seq=WAITING;
332 			break;
333 		default:
334 			break;
335 		}
336 		capptr++;
337 	}
338 	return 0;
339 }
340 
341 /*
342  * does_ctrl_o --
343  * A hack for vt100/xterm-like terminals where the "me" capability also
344  * unsets acs.
345  */
346 static int
347 does_ctrl_o(const char *exit_cap, const char *acs_cap)
348 {
349 	const char *eptr = exit_cap, *aptr = acs_cap;
350 	int l;
351 
352 #ifdef DEBUG
353 	__CTRACE(__CTRACE_INIT, "does_ctrl_o: Testing %s for %s\n", eptr, aptr);
354 #endif
355 	l = strlen(acs_cap);
356 	while (*eptr != 0) {
357 		if (!strncmp(eptr, aptr, l))
358 			return 1;
359 		eptr++;
360 	}
361 	return 0;
362 }
363 
364 /*
365  * set_tabsize --
366  *   Sets the tabsize for the current screen.
367  */
368 int
369 set_tabsize(int tabsize)
370 {
371 
372 	if (_cursesi_screen == NULL)
373 		return ERR;
374 	_cursesi_screen->TABSIZE = tabsize;
375 	TABSIZE = tabsize;
376 	return OK;
377 }
378