Sync with cat.c from netbsd-8
[minix3.git] / lib / libcurses / setterm.c
blobf6446741696736647fc78a1932c9fadfa6df000a
1 /* $NetBSD: setterm.c,v 1.52 2013/10/16 19:59:29 roy Exp $ */
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.52 2013/10/16 19:59:29 roy Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/ioctl.h> /* TIOCGWINSZ on old systems. */
43 #include <stdlib.h>
44 #include <string.h>
45 #include <termios.h>
46 #include <unistd.h>
48 #include "curses.h"
49 #include "curses_private.h"
51 static int does_esc_m(const char *cap);
52 static int does_ctrl_o(const char *exit_cap, const char *acs_cap);
54 attr_t __mask_op, __mask_me, __mask_ue, __mask_se;
56 int
57 setterm(char *type)
59 return _cursesi_setterm(type, _cursesi_screen);
62 int
63 _cursesi_setterm(char *type, SCREEN *screen)
65 int unknown, r;
66 struct winsize win;
67 char *p;
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;
81 #ifdef DEBUG
82 __CTRACE(__CTRACE_INIT, "setterm: tty = %s\n", type);
83 #endif
85 /* Try TIOCGWINSZ, and, if it fails, the terminfo 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);
100 /* POSIX 1003.2 requires that the environment override. */
101 if ((p = getenv("LINES")) != NULL)
102 screen->LINES = (int) strtol(p, NULL, 0);
103 if ((p = getenv("COLUMNS")) != NULL)
104 screen->COLS = (int) strtol(p, NULL, 0);
105 if ((p = getenv("ESCDELAY")) != NULL)
106 ESCDELAY = (int) strtol(p, NULL, 0);
107 if ((p = getenv("TABSIZE")) != NULL)
108 screen->TABSIZE = (int) strtol(p, NULL, 0);
109 else if (t_init_tabs(screen->term) >= 0)
110 screen->TABSIZE = (int) t_init_tabs(screen->term);
111 else
112 screen->TABSIZE = 8;
114 * Want cols > 4, otherwise things will fail.
116 if (screen->COLS <= 4)
117 return (ERR);
119 LINES = screen->LINES;
120 COLS = screen->COLS;
121 TABSIZE = screen->TABSIZE;
123 #ifdef DEBUG
124 __CTRACE(__CTRACE_INIT,
125 "setterm: LINES = %d, COLS = %d\n, TABSIZE = %d\n",
126 LINES, COLS, TABSIZE);
127 #endif
130 * set the pad char, only take the first char of the pc capability
131 * as this is all we can use.
133 screen->padchar = t_pad_char(screen->term) ?
134 t_pad_char(screen->term)[0] : 0;
136 /* If no scrolling commands, no quick change. */
137 screen->noqch =
138 (t_change_scroll_region(screen->term) == NULL ||
139 t_cursor_home(screen->term) == NULL ||
140 (t_parm_index(screen->term) == NULL &&
141 t_scroll_forward(screen->term) == NULL) ||
142 (t_parm_rindex(screen->term) == NULL &&
143 t_scroll_reverse(screen->term) == NULL)) &&
144 ((t_parm_insert_line(screen->term) == NULL &&
145 t_insert_line(screen->term) == NULL) ||
146 (t_parm_delete_line(screen->term) == NULL &&
147 t_delete_line(screen->term) == NULL));
150 * Precalculate conflict info for color/attribute end commands.
152 #ifndef HAVE_WCHAR
153 screen->mask_op = __ATTRIBUTES & ~__COLOR;
154 #else
155 screen->mask_op = WA_ATTRIBUTES & ~__COLOR;
156 #endif /* HAVE_WCHAR */
157 if (t_orig_pair(screen->term) != NULL) {
158 if (does_esc_m(t_orig_pair(screen->term)))
159 screen->mask_op &=
160 ~(__STANDOUT | __UNDERSCORE | __TERMATTR);
161 else {
162 if (t_exit_standout_mode(screen->term) != NULL &&
163 !strcmp(t_orig_pair(screen->term),
164 t_exit_standout_mode(screen->term)))
165 screen->mask_op &= ~__STANDOUT;
166 if (t_exit_underline_mode(screen->term) != NULL &&
167 !strcmp(t_orig_pair(screen->term),
168 t_exit_underline_mode(screen->term)))
169 screen->mask_op &= ~__UNDERSCORE;
170 if (t_exit_attribute_mode(screen->term) != NULL &&
171 !strcmp(t_orig_pair(screen->term),
172 t_exit_attribute_mode(screen->term)))
173 screen->mask_op &= ~__TERMATTR;
177 * Assume that "me" turns off all attributes apart from ACS.
178 * It might turn off ACS, so check for that.
180 if (t_exit_attribute_mode(screen->term) != NULL &&
181 t_exit_alt_charset_mode(screen->term) != NULL &&
182 does_ctrl_o(t_exit_attribute_mode(screen->term),
183 t_exit_alt_charset_mode(screen->term)))
184 screen->mask_me = 0;
185 else
186 screen->mask_me = __ALTCHARSET;
188 /* Check what turning off the attributes also turns off */
189 #ifndef HAVE_WCHAR
190 screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE;
191 #else
192 screen->mask_ue = WA_ATTRIBUTES & ~__UNDERSCORE;
193 #endif /* HAVE_WCHAR */
194 if (t_exit_underline_mode(screen->term) != NULL) {
195 if (does_esc_m(t_exit_underline_mode(screen->term)))
196 screen->mask_ue &=
197 ~(__STANDOUT | __TERMATTR | __COLOR);
198 else {
199 if (t_exit_standout_mode(screen->term) != NULL &&
200 !strcmp(t_exit_underline_mode(screen->term),
201 t_exit_standout_mode(screen->term)))
202 screen->mask_ue &= ~__STANDOUT;
203 if (t_exit_attribute_mode(screen->term) != NULL &&
204 !strcmp(t_exit_underline_mode(screen->term),
205 t_exit_attribute_mode(screen->term)))
206 screen->mask_ue &= ~__TERMATTR;
207 if (t_orig_pair(screen->term) != NULL &&
208 !strcmp(t_exit_underline_mode(screen->term),
209 t_orig_pair(screen->term)))
210 screen->mask_ue &= ~__COLOR;
213 #ifndef HAVE_WCHAR
214 screen->mask_se = __ATTRIBUTES & ~__STANDOUT;
215 #else
216 screen->mask_se = WA_ATTRIBUTES & ~__STANDOUT;
217 #endif /* HAVE_WCHAR */
218 if (t_exit_standout_mode(screen->term) != NULL) {
219 if (does_esc_m(t_exit_standout_mode(screen->term)))
220 screen->mask_se &=
221 ~(__UNDERSCORE | __TERMATTR | __COLOR);
222 else {
223 if (t_exit_underline_mode(screen->term) != NULL &&
224 !strcmp(t_exit_standout_mode(screen->term),
225 t_exit_underline_mode(screen->term)))
226 screen->mask_se &= ~__UNDERSCORE;
227 if (t_exit_attribute_mode(screen->term) != NULL &&
228 !strcmp(t_exit_standout_mode(screen->term),
229 t_exit_attribute_mode(screen->term)))
230 screen->mask_se &= ~__TERMATTR;
231 if (t_orig_pair(screen->term) != NULL &&
232 !strcmp(t_exit_standout_mode(screen->term),
233 t_orig_pair(screen->term)))
234 screen->mask_se &= ~__COLOR;
238 return (unknown ? ERR : OK);
242 * _cursesi_resetterm --
243 * Copy the terminal instance data for the given screen to the global
244 * variables.
246 void
247 _cursesi_resetterm(SCREEN *screen)
250 LINES = screen->LINES;
251 COLS = screen->COLS;
252 TABSIZE = screen->TABSIZE;
253 __GT = screen->GT;
255 __noqch = screen->noqch;
256 __mask_op = screen->mask_op;
257 __mask_me = screen->mask_me;
258 __mask_ue = screen->mask_ue;
259 __mask_se = screen->mask_se;
261 set_curterm(screen->term);
265 * does_esc_m --
266 * A hack for xterm-like terminals where "\E[m" or "\E[0m" will turn off
267 * other attributes, so we check for this in the capability passed to us.
268 * Note that we can't just do simple string comparison, as the capability
269 * may contain multiple, ';' separated sequence parts.
271 static int
272 does_esc_m(const char *cap)
274 #define WAITING 0
275 #define PARSING 1
276 #define NUMBER 2
277 #define FOUND 4
278 const char *capptr;
279 int seq;
281 #ifdef DEBUG
282 __CTRACE(__CTRACE_INIT, "does_esc_m: Checking %s\n", cap);
283 #endif
284 /* Is it just "\E[m" or "\E[0m"? */
285 if (!strcmp(cap, "\x1b[m") || !strcmp(cap, "\x1b[0m"))
286 return 1;
288 /* Does it contain a "\E[...m" sequence? */
289 if (strlen(cap) < 4)
290 return 0;
291 capptr = cap;
292 seq = WAITING;
293 while (*capptr != 0) {
294 switch (seq) {
295 /* Start of sequence */
296 case WAITING:
297 if (!strncmp(capptr, "\x1b[", 2)) {
298 capptr+=2;
299 if (*capptr == 'm')
300 return 1;
301 else {
302 seq=PARSING;
303 continue;
306 break;
307 /* Looking for '0' */
308 case PARSING:
309 if (*capptr == '0')
310 seq=FOUND;
311 else if (*capptr > '0' && *capptr <= '9')
312 seq=NUMBER;
313 else if (*capptr != ';')
314 seq=WAITING;
315 break;
316 /* Some other number */
317 case NUMBER:
318 if (*capptr == ';')
319 seq=PARSING;
320 else if (!(*capptr >= '0' && *capptr <= '9'))
321 seq=WAITING;
322 break;
323 /* Found a '0' */
324 case FOUND:
325 if (*capptr == 'm')
326 return 1;
327 else if (!((*capptr >= '0' && *capptr <= '9') ||
328 *capptr == ';'))
329 seq=WAITING;
330 break;
331 default:
332 break;
334 capptr++;
336 return 0;
340 * does_ctrl_o --
341 * A hack for vt100/xterm-like terminals where the "me" capability also
342 * unsets acs.
344 static int
345 does_ctrl_o(const char *exit_cap, const char *acs_cap)
347 const char *eptr = exit_cap, *aptr = acs_cap;
348 int l;
350 #ifdef DEBUG
351 __CTRACE(__CTRACE_INIT, "does_ctrl_o: Testing %s for %s\n", eptr, aptr);
352 #endif
353 l = strlen(acs_cap);
354 while (*eptr != 0) {
355 if (!strncmp(eptr, aptr, l))
356 return 1;
357 eptr++;
359 return 0;