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