1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #if defined(HAVE_CURSES_H) 22 #include <curses.h> 23 #elif defined(HAVE_NCURSES_H) 24 #include <ncurses.h> 25 #endif 26 #include <fnmatch.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <term.h> 30 31 #include "tmux.h" 32 33 static void tty_term_override(struct tty_term *, const char *); 34 static char *tty_term_strip(const char *); 35 36 struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms); 37 38 enum tty_code_type { 39 TTYCODE_NONE = 0, 40 TTYCODE_STRING, 41 TTYCODE_NUMBER, 42 TTYCODE_FLAG, 43 }; 44 45 struct tty_code { 46 enum tty_code_type type; 47 union { 48 char *string; 49 int number; 50 int flag; 51 } value; 52 }; 53 54 struct tty_term_code_entry { 55 enum tty_code_type type; 56 const char *name; 57 }; 58 59 static const struct tty_term_code_entry tty_term_codes[] = { 60 [TTYC_ACSC] = { TTYCODE_STRING, "acsc" }, 61 [TTYC_AX] = { TTYCODE_FLAG, "AX" }, 62 [TTYC_BCE] = { TTYCODE_FLAG, "bce" }, 63 [TTYC_BEL] = { TTYCODE_STRING, "bel" }, 64 [TTYC_BLINK] = { TTYCODE_STRING, "blink" }, 65 [TTYC_BOLD] = { TTYCODE_STRING, "bold" }, 66 [TTYC_CIVIS] = { TTYCODE_STRING, "civis" }, 67 [TTYC_CLEAR] = { TTYCODE_STRING, "clear" }, 68 [TTYC_CNORM] = { TTYCODE_STRING, "cnorm" }, 69 [TTYC_COLORS] = { TTYCODE_NUMBER, "colors" }, 70 [TTYC_CR] = { TTYCODE_STRING, "Cr" }, 71 [TTYC_CSR] = { TTYCODE_STRING, "csr" }, 72 [TTYC_CS] = { TTYCODE_STRING, "Cs" }, 73 [TTYC_CUB1] = { TTYCODE_STRING, "cub1" }, 74 [TTYC_CUB] = { TTYCODE_STRING, "cub" }, 75 [TTYC_CUD1] = { TTYCODE_STRING, "cud1" }, 76 [TTYC_CUD] = { TTYCODE_STRING, "cud" }, 77 [TTYC_CUF1] = { TTYCODE_STRING, "cuf1" }, 78 [TTYC_CUF] = { TTYCODE_STRING, "cuf" }, 79 [TTYC_CUP] = { TTYCODE_STRING, "cup" }, 80 [TTYC_CUU1] = { TTYCODE_STRING, "cuu1" }, 81 [TTYC_CUU] = { TTYCODE_STRING, "cuu" }, 82 [TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" }, 83 [TTYC_DCH1] = { TTYCODE_STRING, "dch1" }, 84 [TTYC_DCH] = { TTYCODE_STRING, "dch" }, 85 [TTYC_DIM] = { TTYCODE_STRING, "dim" }, 86 [TTYC_DL1] = { TTYCODE_STRING, "dl1" }, 87 [TTYC_DL] = { TTYCODE_STRING, "dl" }, 88 [TTYC_E3] = { TTYCODE_STRING, "E3" }, 89 [TTYC_ECH] = { TTYCODE_STRING, "ech" }, 90 [TTYC_ED] = { TTYCODE_STRING, "ed" }, 91 [TTYC_EL1] = { TTYCODE_STRING, "el1" }, 92 [TTYC_EL] = { TTYCODE_STRING, "el" }, 93 [TTYC_ENACS] = { TTYCODE_STRING, "enacs" }, 94 [TTYC_FSL] = { TTYCODE_STRING, "fsl" }, 95 [TTYC_HOME] = { TTYCODE_STRING, "home" }, 96 [TTYC_HPA] = { TTYCODE_STRING, "hpa" }, 97 [TTYC_ICH1] = { TTYCODE_STRING, "ich1" }, 98 [TTYC_ICH] = { TTYCODE_STRING, "ich" }, 99 [TTYC_IL1] = { TTYCODE_STRING, "il1" }, 100 [TTYC_IL] = { TTYCODE_STRING, "il" }, 101 [TTYC_INDN] = { TTYCODE_STRING, "indn" }, 102 [TTYC_INVIS] = { TTYCODE_STRING, "invis" }, 103 [TTYC_KCBT] = { TTYCODE_STRING, "kcbt" }, 104 [TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" }, 105 [TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" }, 106 [TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" }, 107 [TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" }, 108 [TTYC_KDC2] = { TTYCODE_STRING, "kDC" }, 109 [TTYC_KDC3] = { TTYCODE_STRING, "kDC3" }, 110 [TTYC_KDC4] = { TTYCODE_STRING, "kDC4" }, 111 [TTYC_KDC5] = { TTYCODE_STRING, "kDC5" }, 112 [TTYC_KDC6] = { TTYCODE_STRING, "kDC6" }, 113 [TTYC_KDC7] = { TTYCODE_STRING, "kDC7" }, 114 [TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" }, 115 [TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, /* not kDN2 */ 116 [TTYC_KDN3] = { TTYCODE_STRING, "kDN3" }, 117 [TTYC_KDN4] = { TTYCODE_STRING, "kDN4" }, 118 [TTYC_KDN5] = { TTYCODE_STRING, "kDN5" }, 119 [TTYC_KDN6] = { TTYCODE_STRING, "kDN6" }, 120 [TTYC_KDN7] = { TTYCODE_STRING, "kDN7" }, 121 [TTYC_KEND2] = { TTYCODE_STRING, "kEND" }, 122 [TTYC_KEND3] = { TTYCODE_STRING, "kEND3" }, 123 [TTYC_KEND4] = { TTYCODE_STRING, "kEND4" }, 124 [TTYC_KEND5] = { TTYCODE_STRING, "kEND5" }, 125 [TTYC_KEND6] = { TTYCODE_STRING, "kEND6" }, 126 [TTYC_KEND7] = { TTYCODE_STRING, "kEND7" }, 127 [TTYC_KEND] = { TTYCODE_STRING, "kend" }, 128 [TTYC_KF10] = { TTYCODE_STRING, "kf10" }, 129 [TTYC_KF11] = { TTYCODE_STRING, "kf11" }, 130 [TTYC_KF12] = { TTYCODE_STRING, "kf12" }, 131 [TTYC_KF13] = { TTYCODE_STRING, "kf13" }, 132 [TTYC_KF14] = { TTYCODE_STRING, "kf14" }, 133 [TTYC_KF15] = { TTYCODE_STRING, "kf15" }, 134 [TTYC_KF16] = { TTYCODE_STRING, "kf16" }, 135 [TTYC_KF17] = { TTYCODE_STRING, "kf17" }, 136 [TTYC_KF18] = { TTYCODE_STRING, "kf18" }, 137 [TTYC_KF19] = { TTYCODE_STRING, "kf19" }, 138 [TTYC_KF1] = { TTYCODE_STRING, "kf1" }, 139 [TTYC_KF20] = { TTYCODE_STRING, "kf20" }, 140 [TTYC_KF21] = { TTYCODE_STRING, "kf21" }, 141 [TTYC_KF22] = { TTYCODE_STRING, "kf22" }, 142 [TTYC_KF23] = { TTYCODE_STRING, "kf23" }, 143 [TTYC_KF24] = { TTYCODE_STRING, "kf24" }, 144 [TTYC_KF25] = { TTYCODE_STRING, "kf25" }, 145 [TTYC_KF26] = { TTYCODE_STRING, "kf26" }, 146 [TTYC_KF27] = { TTYCODE_STRING, "kf27" }, 147 [TTYC_KF28] = { TTYCODE_STRING, "kf28" }, 148 [TTYC_KF29] = { TTYCODE_STRING, "kf29" }, 149 [TTYC_KF2] = { TTYCODE_STRING, "kf2" }, 150 [TTYC_KF30] = { TTYCODE_STRING, "kf30" }, 151 [TTYC_KF31] = { TTYCODE_STRING, "kf31" }, 152 [TTYC_KF32] = { TTYCODE_STRING, "kf32" }, 153 [TTYC_KF33] = { TTYCODE_STRING, "kf33" }, 154 [TTYC_KF34] = { TTYCODE_STRING, "kf34" }, 155 [TTYC_KF35] = { TTYCODE_STRING, "kf35" }, 156 [TTYC_KF36] = { TTYCODE_STRING, "kf36" }, 157 [TTYC_KF37] = { TTYCODE_STRING, "kf37" }, 158 [TTYC_KF38] = { TTYCODE_STRING, "kf38" }, 159 [TTYC_KF39] = { TTYCODE_STRING, "kf39" }, 160 [TTYC_KF3] = { TTYCODE_STRING, "kf3" }, 161 [TTYC_KF40] = { TTYCODE_STRING, "kf40" }, 162 [TTYC_KF41] = { TTYCODE_STRING, "kf41" }, 163 [TTYC_KF42] = { TTYCODE_STRING, "kf42" }, 164 [TTYC_KF43] = { TTYCODE_STRING, "kf43" }, 165 [TTYC_KF44] = { TTYCODE_STRING, "kf44" }, 166 [TTYC_KF45] = { TTYCODE_STRING, "kf45" }, 167 [TTYC_KF46] = { TTYCODE_STRING, "kf46" }, 168 [TTYC_KF47] = { TTYCODE_STRING, "kf47" }, 169 [TTYC_KF48] = { TTYCODE_STRING, "kf48" }, 170 [TTYC_KF49] = { TTYCODE_STRING, "kf49" }, 171 [TTYC_KF4] = { TTYCODE_STRING, "kf4" }, 172 [TTYC_KF50] = { TTYCODE_STRING, "kf50" }, 173 [TTYC_KF51] = { TTYCODE_STRING, "kf51" }, 174 [TTYC_KF52] = { TTYCODE_STRING, "kf52" }, 175 [TTYC_KF53] = { TTYCODE_STRING, "kf53" }, 176 [TTYC_KF54] = { TTYCODE_STRING, "kf54" }, 177 [TTYC_KF55] = { TTYCODE_STRING, "kf55" }, 178 [TTYC_KF56] = { TTYCODE_STRING, "kf56" }, 179 [TTYC_KF57] = { TTYCODE_STRING, "kf57" }, 180 [TTYC_KF58] = { TTYCODE_STRING, "kf58" }, 181 [TTYC_KF59] = { TTYCODE_STRING, "kf59" }, 182 [TTYC_KF5] = { TTYCODE_STRING, "kf5" }, 183 [TTYC_KF60] = { TTYCODE_STRING, "kf60" }, 184 [TTYC_KF61] = { TTYCODE_STRING, "kf61" }, 185 [TTYC_KF62] = { TTYCODE_STRING, "kf62" }, 186 [TTYC_KF63] = { TTYCODE_STRING, "kf63" }, 187 [TTYC_KF6] = { TTYCODE_STRING, "kf6" }, 188 [TTYC_KF7] = { TTYCODE_STRING, "kf7" }, 189 [TTYC_KF8] = { TTYCODE_STRING, "kf8" }, 190 [TTYC_KF9] = { TTYCODE_STRING, "kf9" }, 191 [TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" }, 192 [TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" }, 193 [TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" }, 194 [TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" }, 195 [TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" }, 196 [TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" }, 197 [TTYC_KHOME] = { TTYCODE_STRING, "khome" }, 198 [TTYC_KIC2] = { TTYCODE_STRING, "kIC" }, 199 [TTYC_KIC3] = { TTYCODE_STRING, "kIC3" }, 200 [TTYC_KIC4] = { TTYCODE_STRING, "kIC4" }, 201 [TTYC_KIC5] = { TTYCODE_STRING, "kIC5" }, 202 [TTYC_KIC6] = { TTYCODE_STRING, "kIC6" }, 203 [TTYC_KIC7] = { TTYCODE_STRING, "kIC7" }, 204 [TTYC_KICH1] = { TTYCODE_STRING, "kich1" }, 205 [TTYC_KIND] = { TTYCODE_STRING, "kind" }, 206 [TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" }, 207 [TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" }, 208 [TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" }, 209 [TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" }, 210 [TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" }, 211 [TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" }, 212 [TTYC_KMOUS] = { TTYCODE_STRING, "kmous" }, 213 [TTYC_KNP] = { TTYCODE_STRING, "knp" }, 214 [TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" }, 215 [TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" }, 216 [TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" }, 217 [TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" }, 218 [TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" }, 219 [TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" }, 220 [TTYC_KPP] = { TTYCODE_STRING, "kpp" }, 221 [TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" }, 222 [TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" }, 223 [TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" }, 224 [TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" }, 225 [TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" }, 226 [TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" }, 227 [TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" }, 228 [TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" }, 229 [TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" }, 230 [TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" }, 231 [TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" }, 232 [TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" }, 233 [TTYC_KRI] = { TTYCODE_STRING, "kri" }, 234 [TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, /* not kUP2 */ 235 [TTYC_KUP3] = { TTYCODE_STRING, "kUP3" }, 236 [TTYC_KUP4] = { TTYCODE_STRING, "kUP4" }, 237 [TTYC_KUP5] = { TTYCODE_STRING, "kUP5" }, 238 [TTYC_KUP6] = { TTYCODE_STRING, "kUP6" }, 239 [TTYC_KUP7] = { TTYCODE_STRING, "kUP7" }, 240 [TTYC_MS] = { TTYCODE_STRING, "Ms" }, 241 [TTYC_OP] = { TTYCODE_STRING, "op" }, 242 [TTYC_REV] = { TTYCODE_STRING, "rev" }, 243 [TTYC_RI] = { TTYCODE_STRING, "ri" }, 244 [TTYC_RMACS] = { TTYCODE_STRING, "rmacs" }, 245 [TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" }, 246 [TTYC_RMKX] = { TTYCODE_STRING, "rmkx" }, 247 [TTYC_SETAB] = { TTYCODE_STRING, "setab" }, 248 [TTYC_SETAF] = { TTYCODE_STRING, "setaf" }, 249 [TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" }, 250 [TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" }, 251 [TTYC_SE] = { TTYCODE_STRING, "Se" }, 252 [TTYC_SGR0] = { TTYCODE_STRING, "sgr0" }, 253 [TTYC_SITM] = { TTYCODE_STRING, "sitm" }, 254 [TTYC_SMACS] = { TTYCODE_STRING, "smacs" }, 255 [TTYC_SMCUP] = { TTYCODE_STRING, "smcup" }, 256 [TTYC_SMKX] = { TTYCODE_STRING, "smkx" }, 257 [TTYC_SMSO] = { TTYCODE_STRING, "smso" }, 258 [TTYC_SMUL] = { TTYCODE_STRING, "smul" }, 259 [TTYC_SMXX] = { TTYCODE_STRING, "smxx" }, 260 [TTYC_SS] = { TTYCODE_STRING, "Ss" }, 261 [TTYC_TC] = { TTYCODE_FLAG, "Tc" }, 262 [TTYC_TSL] = { TTYCODE_STRING, "tsl" }, 263 [TTYC_U8] = { TTYCODE_NUMBER, "U8" }, 264 [TTYC_VPA] = { TTYCODE_STRING, "vpa" }, 265 [TTYC_XENL] = { TTYCODE_FLAG, "xenl" }, 266 [TTYC_XT] = { TTYCODE_FLAG, "XT" }, 267 }; 268 269 u_int 270 tty_term_ncodes(void) 271 { 272 return (nitems(tty_term_codes)); 273 } 274 275 static char * 276 tty_term_strip(const char *s) 277 { 278 const char *ptr; 279 static char buf[BUFSIZ]; 280 size_t len; 281 282 /* Ignore strings with no padding. */ 283 if (strchr(s, '$') == NULL) 284 return (xstrdup(s)); 285 286 len = 0; 287 for (ptr = s; *ptr != '\0'; ptr++) { 288 if (*ptr == '$' && *(ptr + 1) == '<') { 289 while (*ptr != '\0' && *ptr != '>') 290 ptr++; 291 if (*ptr == '>') 292 ptr++; 293 } 294 295 buf[len++] = *ptr; 296 if (len == (sizeof buf) - 1) 297 break; 298 } 299 buf[len] = '\0'; 300 301 return (xstrdup(buf)); 302 } 303 304 static void 305 tty_term_override(struct tty_term *term, const char *override) 306 { 307 const struct tty_term_code_entry *ent; 308 struct tty_code *code; 309 char *next, *s, *copy, *cp, *value; 310 const char *errstr; 311 u_int i; 312 int n, remove; 313 314 copy = next = xstrdup(override); 315 316 s = strsep(&next, ":"); 317 if (s == NULL || next == NULL || fnmatch(s, term->name, 0) != 0) { 318 free(copy); 319 return; 320 } 321 322 while ((s = strsep(&next, ":")) != NULL) { 323 if (*s == '\0') 324 continue; 325 value = NULL; 326 327 remove = 0; 328 if ((cp = strchr(s, '=')) != NULL) { 329 *cp++ = '\0'; 330 value = xstrdup(cp); 331 if (strunvis(value, cp) == -1) { 332 free(value); 333 value = xstrdup(cp); 334 } 335 } else if (s[strlen(s) - 1] == '@') { 336 s[strlen(s) - 1] = '\0'; 337 remove = 1; 338 } else 339 value = xstrdup(""); 340 341 if (remove) 342 log_debug("%s override: %s@", term->name, s); 343 else 344 log_debug("%s override: %s=%s", term->name, s, value); 345 346 for (i = 0; i < tty_term_ncodes(); i++) { 347 ent = &tty_term_codes[i]; 348 if (strcmp(s, ent->name) != 0) 349 continue; 350 code = &term->codes[i]; 351 352 if (remove) { 353 code->type = TTYCODE_NONE; 354 continue; 355 } 356 switch (ent->type) { 357 case TTYCODE_NONE: 358 break; 359 case TTYCODE_STRING: 360 if (code->type == TTYCODE_STRING) 361 free(code->value.string); 362 code->value.string = xstrdup(value); 363 code->type = ent->type; 364 break; 365 case TTYCODE_NUMBER: 366 n = strtonum(value, 0, INT_MAX, &errstr); 367 if (errstr != NULL) 368 break; 369 code->value.number = n; 370 code->type = ent->type; 371 break; 372 case TTYCODE_FLAG: 373 code->value.flag = 1; 374 code->type = ent->type; 375 break; 376 } 377 } 378 379 free(value); 380 } 381 free(s); 382 } 383 384 struct tty_term * 385 tty_term_find(char *name, int fd, char **cause) 386 { 387 struct tty_term *term; 388 const struct tty_term_code_entry *ent; 389 struct tty_code *code; 390 struct options_entry *o; 391 u_int size, i; 392 int n, error; 393 const char *s, *acs; 394 395 LIST_FOREACH(term, &tty_terms, entry) { 396 if (strcmp(term->name, name) == 0) { 397 term->references++; 398 return (term); 399 } 400 } 401 log_debug("new term: %s", name); 402 403 term = xmalloc(sizeof *term); 404 term->name = xstrdup(name); 405 term->references = 1; 406 term->flags = 0; 407 term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes); 408 LIST_INSERT_HEAD(&tty_terms, term, entry); 409 410 /* Set up curses terminal. */ 411 if (setupterm(name, fd, &error) != OK) { 412 switch (error) { 413 case 1: 414 xasprintf(cause, "can't use hardcopy terminal: %s", 415 name); 416 break; 417 case 0: 418 xasprintf(cause, "missing or unsuitable terminal: %s", 419 name); 420 break; 421 case -1: 422 xasprintf(cause, "can't find terminfo database"); 423 break; 424 default: 425 xasprintf(cause, "unknown error"); 426 break; 427 } 428 goto error; 429 } 430 431 /* Fill in codes. */ 432 for (i = 0; i < tty_term_ncodes(); i++) { 433 ent = &tty_term_codes[i]; 434 435 code = &term->codes[i]; 436 code->type = TTYCODE_NONE; 437 switch (ent->type) { 438 case TTYCODE_NONE: 439 break; 440 case TTYCODE_STRING: 441 s = tigetstr(ent->name); 442 if (s == NULL || s == (char *) -1) 443 break; 444 code->type = TTYCODE_STRING; 445 code->value.string = tty_term_strip(s); 446 break; 447 case TTYCODE_NUMBER: 448 n = tigetnum(ent->name); 449 if (n == -1 || n == -2) 450 break; 451 code->type = TTYCODE_NUMBER; 452 code->value.number = n; 453 break; 454 case TTYCODE_FLAG: 455 n = tigetflag(ent->name); 456 if (n == -1) 457 break; 458 code->type = TTYCODE_FLAG; 459 code->value.flag = n; 460 break; 461 } 462 } 463 464 /* Apply terminal overrides. */ 465 o = options_get_only(global_options, "terminal-overrides"); 466 if (options_array_size(o, &size) != -1) { 467 for (i = 0; i < size; i++) { 468 s = options_array_get(o, i); 469 if (s != NULL) 470 tty_term_override(term, s); 471 } 472 } 473 474 /* Delete curses data. */ 475 #if !defined(NCURSES_VERSION_MAJOR) || NCURSES_VERSION_MAJOR > 5 || \ 476 (NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR > 6) 477 del_curterm(cur_term); 478 #endif 479 480 /* These are always required. */ 481 if (!tty_term_has(term, TTYC_CLEAR)) { 482 xasprintf(cause, "terminal does not support clear"); 483 goto error; 484 } 485 if (!tty_term_has(term, TTYC_CUP)) { 486 xasprintf(cause, "terminal does not support cup"); 487 goto error; 488 } 489 490 /* These can be emulated so one of the two is required. */ 491 if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) { 492 xasprintf(cause, "terminal does not support cud1 or cud"); 493 goto error; 494 } 495 496 /* Figure out if we have 256. */ 497 if (tty_term_number(term, TTYC_COLORS) == 256) 498 term->flags |= TERM_256COLOURS; 499 500 /* 501 * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1 502 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1). 503 * 504 * This is irritating, most notably because it is impossible to write 505 * to the very bottom-right of the screen without scrolling. 506 * 507 * Flag the terminal here and apply some workarounds in other places to 508 * do the best possible. 509 */ 510 if (!tty_term_flag(term, TTYC_XENL)) 511 term->flags |= TERM_EARLYWRAP; 512 513 /* Generate ACS table. If none is present, use nearest ASCII. */ 514 memset(term->acs, 0, sizeof term->acs); 515 if (tty_term_has(term, TTYC_ACSC)) 516 acs = tty_term_string(term, TTYC_ACSC); 517 else 518 acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~."; 519 for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2) 520 term->acs[(u_char) acs[0]][0] = acs[1]; 521 522 /* On terminals with xterm titles (XT), fill in tsl and fsl. */ 523 if (tty_term_flag(term, TTYC_XT) && 524 !tty_term_has(term, TTYC_TSL) && 525 !tty_term_has(term, TTYC_FSL)) { 526 code = &term->codes[TTYC_TSL]; 527 code->value.string = xstrdup("\033]0;"); 528 code->type = TTYCODE_STRING; 529 code = &term->codes[TTYC_FSL]; 530 code->value.string = xstrdup("\007"); 531 code->type = TTYCODE_STRING; 532 } 533 534 /* On terminals with RGB colour (TC), fill in setrgbf and setrgbb. */ 535 if (tty_term_flag(term, TTYC_TC) && 536 !tty_term_has(term, TTYC_SETRGBF) && 537 !tty_term_has(term, TTYC_SETRGBB)) { 538 code = &term->codes[TTYC_SETRGBF]; 539 code->value.string = xstrdup("\033[38;2;%p1%d;%p2%d;%p3%dm"); 540 code->type = TTYCODE_STRING; 541 code = &term->codes[TTYC_SETRGBB]; 542 code->value.string = xstrdup("\033[48;2;%p1%d;%p2%d;%p3%dm"); 543 code->type = TTYCODE_STRING; 544 } 545 546 /* Log it. */ 547 for (i = 0; i < tty_term_ncodes(); i++) 548 log_debug("%s%s", name, tty_term_describe(term, i)); 549 550 return (term); 551 552 error: 553 tty_term_free(term); 554 return (NULL); 555 } 556 557 void 558 tty_term_free(struct tty_term *term) 559 { 560 u_int i; 561 562 if (--term->references != 0) 563 return; 564 565 LIST_REMOVE(term, entry); 566 567 for (i = 0; i < tty_term_ncodes(); i++) { 568 if (term->codes[i].type == TTYCODE_STRING) 569 free(term->codes[i].value.string); 570 } 571 free(term->codes); 572 573 free(term->name); 574 free(term); 575 } 576 577 int 578 tty_term_has(struct tty_term *term, enum tty_code_code code) 579 { 580 return (term->codes[code].type != TTYCODE_NONE); 581 } 582 583 const char * 584 tty_term_string(struct tty_term *term, enum tty_code_code code) 585 { 586 if (!tty_term_has(term, code)) 587 return (""); 588 if (term->codes[code].type != TTYCODE_STRING) 589 fatalx("not a string: %d", code); 590 return (term->codes[code].value.string); 591 } 592 593 const char * 594 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a) 595 { 596 return (tparm(tty_term_string(term, code), a, 0, 0, 0, 0, 0, 0, 0, 0)); 597 } 598 599 const char * 600 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b) 601 { 602 return (tparm(tty_term_string(term, code), a, b, 0, 0, 0, 0, 0, 0, 0)); 603 } 604 605 const char * 606 tty_term_string3(struct tty_term *term, enum tty_code_code code, int a, int b, int c) 607 { 608 return (tparm(tty_term_string(term, code), a, b, c, 0, 0, 0, 0, 0, 0)); 609 } 610 611 const char * 612 tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a) 613 { 614 return (tparm(tty_term_string(term, code), (intptr_t)a, 0, 0, 0, 0, 0, 0, 0, 0)); 615 } 616 617 const char * 618 tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a, 619 const void *b) 620 { 621 return (tparm(tty_term_string(term, code), (intptr_t)a, (intptr_t)b, 0, 0, 0, 0, 0, 0, 0)); 622 } 623 624 int 625 tty_term_number(struct tty_term *term, enum tty_code_code code) 626 { 627 if (!tty_term_has(term, code)) 628 return (0); 629 if (term->codes[code].type != TTYCODE_NUMBER) 630 fatalx("not a number: %d", code); 631 return (term->codes[code].value.number); 632 } 633 634 int 635 tty_term_flag(struct tty_term *term, enum tty_code_code code) 636 { 637 if (!tty_term_has(term, code)) 638 return (0); 639 if (term->codes[code].type != TTYCODE_FLAG) 640 fatalx("not a flag: %d", code); 641 return (term->codes[code].value.flag); 642 } 643 644 const char * 645 tty_term_describe(struct tty_term *term, enum tty_code_code code) 646 { 647 static char s[256]; 648 char out[128]; 649 650 switch (term->codes[code].type) { 651 case TTYCODE_NONE: 652 xsnprintf(s, sizeof s, "%4u: %s: [missing]", 653 code, tty_term_codes[code].name); 654 break; 655 case TTYCODE_STRING: 656 strnvis(out, sizeof out, term->codes[code].value.string, 657 VIS_OCTAL|VIS_TAB|VIS_NL); 658 xsnprintf(s, sizeof s, "%4u: %s: (string) %s", 659 code, tty_term_codes[code].name, 660 out); 661 break; 662 case TTYCODE_NUMBER: 663 xsnprintf(s, sizeof s, "%4u: %s: (number) %d", 664 code, tty_term_codes[code].name, 665 term->codes[code].value.number); 666 break; 667 case TTYCODE_FLAG: 668 xsnprintf(s, sizeof s, "%4u: %s: (flag) %s", 669 code, tty_term_codes[code].name, 670 term->codes[code].value.flag ? "true" : "false"); 671 break; 672 } 673 return (s); 674 } 675