1 /* $NetBSD: setterm.c,v 1.36 2003/02/02 17:54:38 jdc 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)setterm.c 8.8 (Berkeley) 10/25/94"; 40 #else 41 __RCSID("$NetBSD: setterm.c,v 1.36 2003/02/02 17:54:38 jdc Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/ioctl.h> /* TIOCGWINSZ on old systems. */ 46 47 #include <stdlib.h> 48 #include <string.h> 49 #include <termios.h> 50 #include <unistd.h> 51 52 #include "curses.h" 53 #include "curses_private.h" 54 55 static int zap(SCREEN *screen); 56 57 static int does_esc_m(char *cap); 58 static int does_ctrl_o(char *cap); 59 60 struct tinfo *_cursesi_genbuf; 61 62 static char *sflags[] = { 63 /* am bs cc da eo */ 64 &__tc_am, &__tc_bs, &__tc_cc, &__tc_da, &__tc_eo, 65 /* hc hl in mi ms */ 66 &__tc_hc, &__tc_hl, &__tc_in, &__tc_mi, &__tc_ms, 67 /* nc ns os ul ut */ 68 &__tc_nc, &__tc_ns, &__tc_os, &__tc_ul, &__tc_ut, 69 /* xb xn xt xs xx */ 70 &__tc_xb, &__tc_xn, &__tc_xt, &__tc_xs, &__tc_xx 71 }; 72 73 static int *svals[] = { 74 /* pa Co NC */ 75 &__tc_pa, &__tc_Co, &__tc_NC 76 }; 77 78 static char *_PC, 79 **sstrs[] = { 80 /* AB ac ae AF AL */ 81 &__tc_AB, &__tc_ac, &__tc_ae, &__tc_AF, &__tc_AL, 82 /* al as bc bl bt */ 83 &__tc_al, &__tc_as, &__tc_bc, &__tc_bl, &__tc_bt, 84 /* cd ce cl cm cr */ 85 &__tc_cd, &__tc_ce, &__tc_cl, &__tc_cm, &__tc_cr, 86 /* cs dc DL dl dm */ 87 &__tc_cs, &__tc_dc, &__tc_DL, &__tc_dl, &__tc_dm, 88 /* DO do eA ed ei */ 89 &__tc_DO, &__tc_do, &__tc_eA, &__tc_ed, &__tc_ei, 90 /* ho Ic ic im Ip */ 91 &__tc_ho, &__tc_Ic, &__tc_ic, &__tc_im, &__tc_Ip, 92 /* ip k0 k1 k2 k3 */ 93 &__tc_ip, &__tc_k0, &__tc_k1, &__tc_k2, &__tc_k3, 94 /* k4 k5 k6 k7 k8 */ 95 &__tc_k4, &__tc_k5, &__tc_k6, &__tc_k7, &__tc_k8, 96 /* k9 kd ke kh kl */ 97 &__tc_k9, &__tc_kd, &__tc_ke, &__tc_kh, &__tc_kl, 98 /* kr ks ku LE ll */ 99 &__tc_kr, &__tc_ks, &__tc_ku, &__tc_LE, &__tc_ll, 100 /* ma mb md me mh */ 101 &__tc_ma, &__tc_mb, &__tc_md, &__tc_me, &__tc_mh, 102 /* mk mm mo mp mr */ 103 &__tc_mk, &__tc_mm, &__tc_mo, &__tc_mp, &__tc_mr, 104 /* nd nl oc op pc */ 105 &__tc_nd, &__tc_nl, &__tc_oc, &__tc_op, &_PC, 106 /* rc RI sc Sb se */ 107 &__tc_rc, &__tc_RI, &__tc_Sb, &__tc_sc, &__tc_se, 108 /* SF Sf sf so sp */ 109 &__tc_SF, &__tc_Sf, &__tc_sf, &__tc_so, &__tc_sp, 110 /* SR sr ta te ti */ 111 &__tc_SR, &__tc_sr, &__tc_ta, &__tc_te, &__tc_ti, 112 /* uc ue UP up us */ 113 &__tc_uc, &__tc_ue, &__tc_UP, &__tc_up, &__tc_us, 114 /* vb ve vi vs */ 115 &__tc_vb, &__tc_ve, &__tc_vi, &__tc_vs 116 }; 117 118 attr_t __mask_op, __mask_me, __mask_ue, __mask_se; 119 120 int 121 setterm(char *type) 122 { 123 return _cursesi_setterm(type, _cursesi_screen); 124 } 125 126 int 127 _cursesi_setterm(char *type, SCREEN *screen) 128 { 129 int unknown; 130 struct winsize win; 131 char *p; 132 char cm_buff[64]; 133 134 if (type[0] == '\0') 135 type = "xx"; 136 unknown = 0; 137 if (t_getent(&screen->cursesi_genbuf, type) != 1) { 138 unknown++; 139 } 140 #ifdef DEBUG 141 __CTRACE("setterm: tty = %s\n", type); 142 #endif 143 144 /* Try TIOCGWINSZ, and, if it fails, the termcap entry. */ 145 if (ioctl(fileno(screen->outfd), TIOCGWINSZ, &win) != -1 && 146 win.ws_row != 0 && win.ws_col != 0) { 147 screen->LINES = win.ws_row; 148 screen->COLS = win.ws_col; 149 } else { 150 if (unknown) { 151 screen->LINES = -1; 152 screen->COLS = -1; 153 } else { 154 screen->LINES = t_getnum(screen->cursesi_genbuf, "li"); 155 screen->COLS = t_getnum(screen->cursesi_genbuf, "co"); 156 } 157 158 } 159 160 /* POSIX 1003.2 requires that the environment override. */ 161 if ((p = getenv("LINES")) != NULL) 162 screen->LINES = (int) strtol(p, NULL, 10); 163 if ((p = getenv("COLUMNS")) != NULL) 164 screen->COLS = (int) strtol(p, NULL, 10); 165 166 /* 167 * Want cols > 4, otherwise things will fail. 168 */ 169 if (screen->COLS <= 4) 170 return (ERR); 171 172 LINES = screen->LINES; 173 COLS = screen->COLS; 174 175 #ifdef DEBUG 176 __CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS); 177 #endif 178 if (!unknown) { 179 if (zap(screen) == ERR) /* Get terminal description.*/ 180 return ERR; 181 } 182 183 /* If we can't tab, we can't backtab, either. */ 184 if (!screen->GT) 185 screen->tc_bt = NULL; 186 187 /* 188 * Test for cursor motion capability. 189 * 190 */ 191 if (t_goto(NULL, screen->tc_cm, 0, 0, cm_buff, sizeof(cm_buff) - 1) < 0) { 192 screen->CA = 0; 193 screen->tc_cm = 0; 194 } else 195 screen->CA = 1; 196 197 /* 198 * set the pad char, only take the first char of the pc capability 199 * as this is all we can use. 200 */ 201 screen->pad_char = screen->tc_pc ? screen->tc_pc[0] : 0; 202 203 /* Get full name of terminal */ 204 if (unknown) { 205 strcpy(screen->ttytype, "dumb"); 206 } else { 207 char *tcptr; 208 size_t limit = 0; 209 if (t_getterm(screen->cursesi_genbuf, 0, &limit)) 210 return ERR; 211 if ((tcptr = malloc(limit + 1)) == NULL) 212 return ERR; 213 if (t_getterm(screen->cursesi_genbuf, &tcptr, 0) < 0) 214 return ERR; 215 __longname(tcptr, screen->ttytype); 216 free(tcptr); 217 } 218 219 /* If no scrolling commands, no quick change. */ 220 screen->noqch = 221 (screen->tc_cs == NULL || screen->tc_ho == NULL || 222 (screen->tc_SF == NULL && screen->tc_sf == NULL) || 223 (screen->tc_SR == NULL && screen->tc_sr == NULL)) && 224 ((screen->tc_AL == NULL && screen->tc_al == NULL) || 225 (screen->tc_DL == NULL && screen->tc_dl == NULL)); 226 227 /* 228 * Precalculate conflict info for color/attribute end commands. 229 */ 230 screen->mask_op = __ATTRIBUTES & ~__COLOR; 231 if (screen->tc_op != NULL) { 232 if (does_esc_m(screen->tc_op)) 233 screen->mask_op &= 234 ~(__STANDOUT | __UNDERSCORE | __TERMATTR); 235 else { 236 if (screen->tc_se != NULL && 237 !strcmp(screen->tc_op, screen->tc_se)) 238 screen->mask_op &= ~__STANDOUT; 239 if (screen->tc_ue != NULL && 240 !strcmp(screen->tc_op, screen->tc_ue)) 241 screen->mask_op &= ~__UNDERSCORE; 242 if (screen->tc_me != NULL && 243 !strcmp(screen->tc_op, screen->tc_me)) 244 screen->mask_op &= ~__TERMATTR; 245 } 246 } 247 /* 248 * Assume that "me" turns off all attributes apart from ACS. 249 * It might turn off ACS, so check for that. 250 */ 251 if (screen->tc_me != NULL && does_ctrl_o(screen->tc_me)) 252 screen->mask_me = 0; 253 else 254 screen->mask_me = __ALTCHARSET; 255 256 /* Check what turning off the attributes also turns off */ 257 screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE; 258 if (screen->tc_ue != NULL) { 259 if (does_esc_m(screen->tc_ue)) 260 screen->mask_ue &= 261 ~(__STANDOUT | __TERMATTR | __COLOR); 262 else { 263 if (screen->tc_se != NULL && 264 !strcmp(screen->tc_ue, screen->tc_se)) 265 screen->mask_ue &= ~__STANDOUT; 266 if (screen->tc_me != NULL && 267 !strcmp(screen->tc_ue, screen->tc_me)) 268 screen->mask_ue &= ~__TERMATTR; 269 if (screen->tc_op != NULL && 270 !strcmp(screen->tc_ue, screen->tc_op)) 271 screen->mask_ue &= ~__COLOR; 272 } 273 } 274 screen->mask_se = __ATTRIBUTES & ~__STANDOUT; 275 if (screen->tc_se != NULL) { 276 if (does_esc_m(screen->tc_se)) 277 screen->mask_se &= 278 ~(__UNDERSCORE | __TERMATTR | __COLOR); 279 else { 280 if (screen->tc_ue != NULL && 281 !strcmp(screen->tc_se, screen->tc_ue)) 282 screen->mask_se &= ~__UNDERSCORE; 283 if (screen->tc_me != NULL && 284 !strcmp(screen->tc_se, screen->tc_me)) 285 screen->mask_se &= ~__TERMATTR; 286 if (screen->tc_op != NULL && 287 !strcmp(screen->tc_se, screen->tc_op)) 288 screen->mask_se &= ~__COLOR; 289 } 290 } 291 292 return (unknown ? ERR : OK); 293 } 294 295 /* 296 * _cursesi_resetterm -- 297 * Copy the terminal instance data for the given screen to the global 298 * variables. 299 */ 300 void 301 _cursesi_resetterm(SCREEN *screen) 302 { 303 char ***sp, **scr_sp; 304 int **vp, *scr_vp, i; 305 char **fp, *scr_fp; 306 307 LINES = screen->LINES; 308 COLS = screen->COLS; 309 310 /* reset terminal capabilities */ 311 fp = sflags; 312 scr_fp = &screen->tc_am; 313 for (i = 0; i < screen->flag_count; i++) 314 *(*fp++) = *scr_fp++; 315 316 vp = svals; 317 scr_vp = &screen->tc_pa; 318 for (i = 0; i < screen->int_count; i++) 319 *(*vp++) = *scr_vp++; 320 321 sp = sstrs; 322 scr_sp = &screen->tc_AB; 323 for (i = 0; i < screen->str_count; i++) 324 *(*sp++) = *scr_sp++; 325 326 __GT = screen->GT; 327 __CA = screen->CA; 328 329 PC = screen->pad_char; 330 331 __noqch = screen->noqch; 332 __mask_op = screen->mask_op; 333 __mask_me = screen->mask_me; 334 __mask_ue = screen->mask_ue; 335 __mask_se = screen->mask_se; 336 } 337 338 /* 339 * zap -- 340 * Gets all the terminal flags from the termcap database. 341 */ 342 static int 343 zap(SCREEN *screen) 344 { 345 const char *nampstr, *namp; 346 char **sp; 347 int *vp; 348 char *fp; 349 char tmp[3]; 350 #ifdef DEBUG 351 char *cp; 352 #endif 353 tmp[2] = '\0'; 354 355 namp = "ambsccdaeohchlinmimsncnsosulutxbxnxtxsxx"; 356 fp = &screen->tc_am; 357 screen->flag_count = 0; 358 do { 359 *tmp = *namp; 360 *(tmp + 1) = *(namp + 1); 361 *fp++ = t_getflag(screen->cursesi_genbuf, tmp); 362 #ifdef DEBUG 363 __CTRACE("%2.2s = %s\n", namp, fp[-1] ? "TRUE" : "FALSE"); 364 #endif 365 namp += 2; 366 screen->flag_count++; 367 } while (*namp); 368 namp = "paCoNC"; 369 vp = &screen->tc_pa; 370 screen->int_count = 0; 371 do { 372 *tmp = *namp; 373 *(tmp + 1) = *(namp + 1); 374 *vp++ = t_getnum(screen->cursesi_genbuf, tmp); 375 #ifdef DEBUG 376 __CTRACE("%2.2s = %d\n", namp, vp[-1]); 377 #endif 378 namp += 2; 379 screen->int_count++; 380 } while (*namp); 381 382 nampstr = "ABacaeAFALalasbcblbtcdceclcmcrcsdcDLdldmDOdoeAedeihoIcicimIpipk0k1k2k3k4k5k6k7k8k9kdkekhklkrkskuLEllmambmdmemhmkmmmompmrndnlocoppcrcRISbscseSFSfsfsospSRsrtatetiucueUPupusvbvevivs"; 383 384 namp = nampstr; 385 sp = &screen->tc_AB; 386 screen->str_count = 0; 387 do { 388 *tmp = *namp; 389 *(tmp + 1) = *(namp + 1); 390 *sp++ = t_agetstr(screen->cursesi_genbuf, tmp); 391 #ifdef DEBUG 392 __CTRACE("%2.2s = %s", namp, sp[-1] == NULL ? "NULL\n" : "\""); 393 if (sp[-1] != NULL) { 394 for (cp = sp[-1]; *cp; cp++) 395 __CTRACE("%s", unctrl(*cp)); 396 __CTRACE("\"\n"); 397 } 398 #endif 399 namp += 2; 400 screen->str_count++; 401 } while (*namp); 402 if (screen->tc_xs) 403 screen->tc_so = screen->tc_se = NULL; 404 else { 405 if (t_getnum(screen->cursesi_genbuf, "sg") > 0) 406 screen->tc_so = NULL; 407 if (t_getnum(screen->cursesi_genbuf, "ug") > 0) 408 screen->tc_us = NULL; 409 if (!screen->tc_so && screen->tc_us) { 410 screen->tc_so = screen->tc_us; 411 screen->tc_se = screen->tc_ue; 412 } 413 } 414 415 return OK; 416 } 417 418 /* 419 * getcap -- 420 * Return a capability from termcap. 421 */ 422 char * 423 getcap(char *name) 424 { 425 return (t_agetstr(_cursesi_genbuf, name)); 426 } 427 428 /* 429 * does_esc_m -- 430 * A hack for xterm-like terminals where "\E[m" or "\E[0m" will turn off 431 * other attributes, so we check for this in the capability passed to us. 432 * Note that we can't just do simple string comparison, as the capability 433 * may contain multiple, ';' separated sequence parts. 434 */ 435 static int 436 does_esc_m(char *cap) 437 { 438 #define WAITING 0 439 #define PARSING 1 440 #define NUMBER 2 441 #define FOUND 4 442 char *capptr; 443 int seq; 444 445 #ifdef DEBUG 446 __CTRACE("does_esc_m: Checking %s\n", cap); 447 #endif 448 /* Is it just "\E[m" or "\E[0m"? */ 449 if (!strcmp(cap, "\x1b[m") || !strcmp(cap, "\x1b[0m")) 450 return 1; 451 452 /* Does it contain a "\E[...m" sequence? */ 453 if (strlen(cap) < 4) 454 return 0; 455 capptr = cap; 456 seq = WAITING; 457 while (*capptr != NULL) { 458 switch (seq) { 459 /* Start of sequence */ 460 case WAITING: 461 if (!strncmp(capptr, "\x1b[", 2)) { 462 capptr+=2; 463 if (*capptr == 'm') 464 return 1; 465 else { 466 seq=PARSING; 467 continue; 468 } 469 } 470 break; 471 /* Looking for '0' */ 472 case PARSING: 473 if (*capptr == '0') 474 seq=FOUND; 475 else if (*capptr > '0' && *capptr <= '9') 476 seq=NUMBER; 477 else if (*capptr != ';') 478 seq=WAITING; 479 break; 480 /* Some other number */ 481 case NUMBER: 482 if (*capptr == ';') 483 seq=PARSING; 484 else if (!(*capptr >= '0' && *capptr <= '9')) 485 seq=WAITING; 486 break; 487 /* Found a '0' */ 488 case FOUND: 489 if (*capptr == 'm') 490 return 1; 491 else if (!((*capptr >= '0' && *capptr <= '9') || 492 *capptr == ';')) 493 seq=WAITING; 494 break; 495 default: 496 break; 497 } 498 capptr++; 499 } 500 return 0; 501 } 502 503 /* 504 * does_ctrl_o -- 505 * A hack for vt100/xterm-like terminals where the "me" capability also 506 * unsets acs (i.e. it contains the character '\017'). 507 */ 508 static int 509 does_ctrl_o(char *cap) 510 { 511 char *capptr = cap; 512 513 #ifdef DEBUG 514 __CTRACE("does_ctrl_o: Looping on %s\n", capptr); 515 #endif 516 while (*capptr != NULL) { 517 if (*capptr == '\x0f') 518 return 1; 519 capptr++; 520 } 521 return 0; 522 } 523