1 /* $NetBSD: setterm.c,v 1.47 2010/02/11 11:45:47 roy Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)setterm.c 8.8 (Berkeley) 10/25/94";
37 __RCSID("$NetBSD: setterm.c,v 1.47 2010/02/11 11:45:47 roy Exp $");
41 #include <sys/ioctl.h> /* TIOCGWINSZ on old systems. */
49 #include "curses_private.h"
51 static int does_esc_m(const char *cap
);
52 static int does_ctrl_o(const char *cap
);
54 attr_t __mask_op
, __mask_me
, __mask_ue
, __mask_se
;
59 return _cursesi_setterm(type
, _cursesi_screen
);
63 _cursesi_setterm(char *type
, SCREEN
*screen
)
72 (void)ti_setupterm(&screen
->term
, type
, fileno(screen
->outfd
), &r
);
73 if (screen
->term
== NULL
) {
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
)
82 __CTRACE(__CTRACE_INIT
, "setterm: tty = %s\n", type
);
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
;
95 screen
->LINES
= t_lines(screen
->term
);
96 screen
->COLS
= t_columns(screen
->term
);
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);
110 * Want cols > 4, otherwise things will fail.
112 if (screen
->COLS
<= 4)
115 LINES
= screen
->LINES
;
119 __CTRACE(__CTRACE_INIT
, "setterm: LINES = %d, COLS = %d\n",
124 * set the pad char, only take the first char of the pc capability
125 * as this is all we can use.
127 screen
->padchar
= t_pad_char(screen
->term
) ?
128 t_pad_char(screen
->term
)[0] : 0;
130 /* Get full name of terminal */
132 strcpy(screen
->ttytype
, "dumb");
135 if (screen
->term
->desc
== NULL
)
136 screen
->ttytype
[0] = '\0';
138 strlcpy(screen
->ttytype
, screen
->term
->desc
,
139 sizeof(screen
->ttytype
));
141 /* If no scrolling commands, no quick change. */
143 (t_change_scroll_region(screen
->term
) == NULL
||
144 t_cursor_home(screen
->term
) == NULL
||
145 (t_parm_index(screen
->term
) == NULL
&&
146 t_scroll_forward(screen
->term
) == NULL
) ||
147 (t_parm_rindex(screen
->term
) == NULL
&&
148 t_scroll_reverse(screen
->term
) == NULL
)) &&
149 ((t_parm_insert_line(screen
->term
) == NULL
&&
150 t_insert_line(screen
->term
) == NULL
) ||
151 (t_parm_delete_line(screen
->term
) == NULL
&&
152 t_delete_line(screen
->term
) == NULL
));
155 * Precalculate conflict info for color/attribute end commands.
158 screen
->mask_op
= __ATTRIBUTES
& ~__COLOR
;
160 screen
->mask_op
= WA_ATTRIBUTES
& ~__COLOR
;
161 #endif /* HAVE_WCHAR */
162 if (t_orig_pair(screen
->term
) != NULL
) {
163 if (does_esc_m(t_orig_pair(screen
->term
)))
165 ~(__STANDOUT
| __UNDERSCORE
| __TERMATTR
);
167 if (t_exit_standout_mode(screen
->term
) != NULL
&&
168 !strcmp(t_orig_pair(screen
->term
),
169 t_exit_standout_mode(screen
->term
)))
170 screen
->mask_op
&= ~__STANDOUT
;
171 if (t_exit_underline_mode(screen
->term
) != NULL
&&
172 !strcmp(t_orig_pair(screen
->term
),
173 t_exit_underline_mode(screen
->term
)))
174 screen
->mask_op
&= ~__UNDERSCORE
;
175 if (t_exit_attribute_mode(screen
->term
) != NULL
&&
176 !strcmp(t_orig_pair(screen
->term
),
177 t_exit_attribute_mode(screen
->term
)))
178 screen
->mask_op
&= ~__TERMATTR
;
182 * Assume that "me" turns off all attributes apart from ACS.
183 * It might turn off ACS, so check for that.
185 if (t_exit_attribute_mode(screen
->term
) != NULL
&&
186 does_ctrl_o(t_exit_attribute_mode(screen
->term
)))
189 screen
->mask_me
= __ALTCHARSET
;
191 /* Check what turning off the attributes also turns off */
193 screen
->mask_ue
= __ATTRIBUTES
& ~__UNDERSCORE
;
195 screen
->mask_ue
= WA_ATTRIBUTES
& ~__UNDERSCORE
;
196 #endif /* HAVE_WCHAR */
197 if (t_exit_underline_mode(screen
->term
) != NULL
) {
198 if (does_esc_m(t_exit_underline_mode(screen
->term
)))
200 ~(__STANDOUT
| __TERMATTR
| __COLOR
);
202 if (t_exit_standout_mode(screen
->term
) != NULL
&&
203 !strcmp(t_exit_underline_mode(screen
->term
),
204 t_exit_standout_mode(screen
->term
)))
205 screen
->mask_ue
&= ~__STANDOUT
;
206 if (t_exit_attribute_mode(screen
->term
) != NULL
&&
207 !strcmp(t_exit_underline_mode(screen
->term
),
208 t_exit_attribute_mode(screen
->term
)))
209 screen
->mask_ue
&= ~__TERMATTR
;
210 if (t_orig_pair(screen
->term
) != NULL
&&
211 !strcmp(t_exit_underline_mode(screen
->term
),
212 t_orig_pair(screen
->term
)))
213 screen
->mask_ue
&= ~__COLOR
;
217 screen
->mask_se
= __ATTRIBUTES
& ~__STANDOUT
;
219 screen
->mask_se
= WA_ATTRIBUTES
& ~__STANDOUT
;
220 #endif /* HAVE_WCHAR */
221 if (t_exit_standout_mode(screen
->term
) != NULL
) {
222 if (does_esc_m(t_exit_standout_mode(screen
->term
)))
224 ~(__UNDERSCORE
| __TERMATTR
| __COLOR
);
226 if (t_exit_underline_mode(screen
->term
) != NULL
&&
227 !strcmp(t_exit_standout_mode(screen
->term
),
228 t_exit_underline_mode(screen
->term
)))
229 screen
->mask_se
&= ~__UNDERSCORE
;
230 if (t_exit_attribute_mode(screen
->term
) != NULL
&&
231 !strcmp(t_exit_standout_mode(screen
->term
),
232 t_exit_attribute_mode(screen
->term
)))
233 screen
->mask_se
&= ~__TERMATTR
;
234 if (t_orig_pair(screen
->term
) != NULL
&&
235 !strcmp(t_exit_standout_mode(screen
->term
),
236 t_orig_pair(screen
->term
)))
237 screen
->mask_se
&= ~__COLOR
;
241 return (unknown
? ERR
: OK
);
245 * _cursesi_resetterm --
246 * Copy the terminal instance data for the given screen to the global
250 _cursesi_resetterm(SCREEN
*screen
)
253 LINES
= screen
->LINES
;
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
;
263 set_curterm(screen
->term
);
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.
274 does_esc_m(const char *cap
)
284 __CTRACE(__CTRACE_INIT
, "does_esc_m: Checking %s\n", cap
);
286 /* Is it just "\E[m" or "\E[0m"? */
287 if (!strcmp(cap
, "\x1b[m") || !strcmp(cap
, "\x1b[0m"))
290 /* Does it contain a "\E[...m" sequence? */
295 while (*capptr
!= 0) {
297 /* Start of sequence */
299 if (!strncmp(capptr
, "\x1b[", 2)) {
309 /* Looking for '0' */
313 else if (*capptr
> '0' && *capptr
<= '9')
315 else if (*capptr
!= ';')
318 /* Some other number */
322 else if (!(*capptr
>= '0' && *capptr
<= '9'))
329 else if (!((*capptr
>= '0' && *capptr
<= '9') ||
343 * A hack for vt100/xterm-like terminals where the "me" capability also
344 * unsets acs (i.e. it contains the character '\017').
347 does_ctrl_o(const char *cap
)
349 const char *capptr
= cap
;
352 __CTRACE(__CTRACE_INIT
, "does_ctrl_o: Looping on %s\n", capptr
);
354 while (*capptr
!= 0) {
355 if (*capptr
== '\x0f')