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