1 /* $NetBSD: setterm.c,v 1.34 2002/12/05 17:04:25 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.34 2002/12/05 17:04:25 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 struct tinfo *_cursesi_genbuf; 58 59 static char *sflags[] = { 60 /* am bs cc da eo */ 61 &__tc_am, &__tc_bs, &__tc_cc, &__tc_da, &__tc_eo, 62 /* hc hl in mi ms */ 63 &__tc_hc, &__tc_hl, &__tc_in, &__tc_mi, &__tc_ms, 64 /* nc ns os ul ut */ 65 &__tc_nc, &__tc_ns, &__tc_os, &__tc_ul, &__tc_ut, 66 /* xb xn xt xs xx */ 67 &__tc_xb, &__tc_xn, &__tc_xt, &__tc_xs, &__tc_xx 68 }; 69 70 static int *svals[] = { 71 /* pa Co NC */ 72 &__tc_pa, &__tc_Co, &__tc_NC 73 }; 74 75 static char *_PC, 76 **sstrs[] = { 77 /* AB ac ae AF AL */ 78 &__tc_AB, &__tc_ac, &__tc_ae, &__tc_AF, &__tc_AL, 79 /* al as bc bl bt */ 80 &__tc_al, &__tc_as, &__tc_bc, &__tc_bl, &__tc_bt, 81 /* cd ce cl cm cr */ 82 &__tc_cd, &__tc_ce, &__tc_cl, &__tc_cm, &__tc_cr, 83 /* cs dc DL dl dm */ 84 &__tc_cs, &__tc_dc, &__tc_DL, &__tc_dl, &__tc_dm, 85 /* DO do eA ed ei */ 86 &__tc_DO, &__tc_do, &__tc_eA, &__tc_ed, &__tc_ei, 87 /* ho Ic ic im Ip */ 88 &__tc_ho, &__tc_Ic, &__tc_ic, &__tc_im, &__tc_Ip, 89 /* ip k0 k1 k2 k3 */ 90 &__tc_ip, &__tc_k0, &__tc_k1, &__tc_k2, &__tc_k3, 91 /* k4 k5 k6 k7 k8 */ 92 &__tc_k4, &__tc_k5, &__tc_k6, &__tc_k7, &__tc_k8, 93 /* k9 kd ke kh kl */ 94 &__tc_k9, &__tc_kd, &__tc_ke, &__tc_kh, &__tc_kl, 95 /* kr ks ku LE ll */ 96 &__tc_kr, &__tc_ks, &__tc_ku, &__tc_LE, &__tc_ll, 97 /* ma mb md me mh */ 98 &__tc_ma, &__tc_mb, &__tc_md, &__tc_me, &__tc_mh, 99 /* mk mm mo mp mr */ 100 &__tc_mk, &__tc_mm, &__tc_mo, &__tc_mp, &__tc_mr, 101 /* nd nl oc op pc */ 102 &__tc_nd, &__tc_nl, &__tc_oc, &__tc_op, &_PC, 103 /* rc RI sc Sb se */ 104 &__tc_rc, &__tc_RI, &__tc_Sb, &__tc_sc, &__tc_se, 105 /* SF Sf sf so sp */ 106 &__tc_SF, &__tc_Sf, &__tc_sf, &__tc_so, &__tc_sp, 107 /* SR sr ta te ti */ 108 &__tc_SR, &__tc_sr, &__tc_ta, &__tc_te, &__tc_ti, 109 /* uc ue UP up us */ 110 &__tc_uc, &__tc_ue, &__tc_UP, &__tc_up, &__tc_us, 111 /* vb ve vi vs */ 112 &__tc_vb, &__tc_ve, &__tc_vi, &__tc_vs 113 }; 114 115 attr_t __mask_op, __mask_me, __mask_ue, __mask_se; 116 117 const char *__attroff = "\x1b[m"; 118 119 int 120 setterm(char *type) 121 { 122 return _cursesi_setterm(type, _cursesi_screen); 123 } 124 125 int 126 _cursesi_setterm(char *type, SCREEN *screen) 127 { 128 int unknown; 129 struct winsize win; 130 char *p; 131 char cm_buff[64]; 132 133 if (type[0] == '\0') 134 type = "xx"; 135 unknown = 0; 136 if (t_getent(&screen->cursesi_genbuf, type) != 1) { 137 unknown++; 138 } 139 #ifdef DEBUG 140 __CTRACE("setterm: tty = %s\n", type); 141 #endif 142 143 /* Try TIOCGWINSZ, and, if it fails, the termcap entry. */ 144 if (ioctl(fileno(screen->outfd), TIOCGWINSZ, &win) != -1 && 145 win.ws_row != 0 && win.ws_col != 0) { 146 screen->LINES = win.ws_row; 147 screen->COLS = win.ws_col; 148 } else { 149 if (unknown) { 150 screen->LINES = -1; 151 screen->COLS = -1; 152 } else { 153 screen->LINES = t_getnum(screen->cursesi_genbuf, "li"); 154 screen->COLS = t_getnum(screen->cursesi_genbuf, "co"); 155 } 156 157 } 158 159 /* POSIX 1003.2 requires that the environment override. */ 160 if ((p = getenv("LINES")) != NULL) 161 screen->LINES = (int) strtol(p, NULL, 10); 162 if ((p = getenv("COLUMNS")) != NULL) 163 screen->COLS = (int) strtol(p, NULL, 10); 164 165 /* 166 * Want cols > 4, otherwise things will fail. 167 */ 168 if (screen->COLS <= 4) 169 return (ERR); 170 171 LINES = screen->LINES; 172 COLS = screen->COLS; 173 174 #ifdef DEBUG 175 __CTRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS); 176 #endif 177 if (!unknown) { 178 if (zap(screen) == ERR) /* Get terminal description.*/ 179 return ERR; 180 } 181 182 /* If we can't tab, we can't backtab, either. */ 183 if (!screen->GT) 184 screen->tc_bt = NULL; 185 186 /* 187 * Test for cursor motion capability. 188 * 189 */ 190 if (t_goto(NULL, screen->tc_cm, 0, 0, cm_buff, sizeof(cm_buff) - 1) < 0) { 191 screen->CA = 0; 192 screen->tc_cm = 0; 193 } else 194 screen->CA = 1; 195 196 /* 197 * set the pad char, only take the first char of the pc capability 198 * as this is all we can use. 199 */ 200 screen->pad_char = screen->tc_pc ? screen->tc_pc[0] : 0; 201 202 /* Get full name of terminal */ 203 if (unknown) { 204 strcpy(screen->ttytype, "dumb"); 205 } else { 206 char *tcptr; 207 size_t limit = 0; 208 if (t_getterm(screen->cursesi_genbuf, 0, &limit)) 209 return ERR; 210 if ((tcptr = malloc(limit + 1)) == NULL) 211 return ERR; 212 if (t_getterm(screen->cursesi_genbuf, &tcptr, 0) < 0) 213 return ERR; 214 __longname(tcptr, screen->ttytype); 215 free(tcptr); 216 } 217 218 /* If no scrolling commands, no quick change. */ 219 screen->noqch = 220 (screen->tc_cs == NULL || screen->tc_ho == NULL || 221 (screen->tc_SF == NULL && screen->tc_sf == NULL) || 222 (screen->tc_SR == NULL && screen->tc_sr == NULL)) && 223 ((screen->tc_AL == NULL && screen->tc_al == NULL) || 224 (screen->tc_DL == NULL && screen->tc_dl == NULL)); 225 226 /* 227 * Precalculate conflict info for color/attribute end commands. 228 * Add a hack for xterm-like terminals where "\E[m" will turn off 229 * other attributes. 230 */ 231 screen->mask_op = __ATTRIBUTES & ~__COLOR; 232 if (screen->tc_op != NULL) { 233 if (screen->tc_se != NULL && 234 (!strcmp(screen->tc_op, screen->tc_se) || 235 !strcmp(screen->tc_op, __attroff))) 236 screen->mask_op &= ~__STANDOUT; 237 if (screen->tc_ue != NULL && 238 (!strcmp(screen->tc_op, screen->tc_ue) || 239 !strcmp(screen->tc_op, __attroff))) 240 screen->mask_op &= ~__UNDERSCORE; 241 if (screen->tc_me != NULL && 242 (!strcmp(screen->tc_op, screen->tc_me) || 243 !strcmp(screen->tc_op, __attroff))) 244 screen->mask_op &= ~__TERMATTR; 245 } 246 screen->mask_me = __ATTRIBUTES & ~__TERMATTR; 247 if (screen->tc_me != NULL) { 248 if (screen->tc_se != NULL && 249 (!strcmp(screen->tc_me, screen->tc_se) || 250 !strcmp(screen->tc_me, __attroff))) 251 screen->mask_me &= ~__STANDOUT; 252 if (screen->tc_ue != NULL && 253 (!strcmp(screen->tc_me, screen->tc_ue) || 254 !strcmp(screen->tc_me, __attroff))) 255 screen->mask_me &= ~__UNDERSCORE; 256 if (screen->tc_op != NULL && 257 (!strcmp(screen->tc_me, screen->tc_op) || 258 !strcmp(screen->tc_me, __attroff))) 259 screen->mask_me &= ~__COLOR; 260 } 261 screen->mask_ue = __ATTRIBUTES & ~__UNDERSCORE; 262 if (screen->tc_ue != NULL) { 263 if (screen->tc_se != NULL && 264 (!strcmp(screen->tc_ue, screen->tc_se) || 265 !strcmp(screen->tc_ue, __attroff))) 266 screen->mask_ue &= ~__STANDOUT; 267 if (screen->tc_me != NULL && 268 (!strcmp(screen->tc_ue, screen->tc_me) || 269 !strcmp(screen->tc_ue, __attroff))) 270 screen->mask_ue &= ~__TERMATTR; 271 if (screen->tc_op != NULL && 272 (!strcmp(screen->tc_ue, screen->tc_op) || 273 !strcmp(screen->tc_ue, __attroff))) 274 screen->mask_ue &= ~__COLOR; 275 } 276 screen->mask_se = __ATTRIBUTES & ~__STANDOUT; 277 if (screen->tc_se != NULL) { 278 if (screen->tc_ue != NULL && 279 (!strcmp(screen->tc_se, screen->tc_ue) || 280 !strcmp(screen->tc_se, __attroff))) 281 screen->mask_se &= ~__UNDERSCORE; 282 if (screen->tc_me != NULL && 283 (!strcmp(screen->tc_se, screen->tc_me) || 284 !strcmp(screen->tc_se, __attroff))) 285 screen->mask_se &= ~__TERMATTR; 286 if (screen->tc_op != NULL && 287 (!strcmp(screen->tc_se, screen->tc_op) || 288 !strcmp(screen->tc_se, __attroff))) 289 screen->mask_se &= ~__COLOR; 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