1 /* $OpenBSD: tty-term.c,v 1.85 2020/10/13 07:29:24 nicm Exp $ */ 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 #include <curses.h> 22 #include <fnmatch.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <term.h> 26 #include <vis.h> 27 28 #include "tmux.h" 29 30 static char *tty_term_strip(const char *); 31 32 struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms); 33 34 enum tty_code_type { 35 TTYCODE_NONE = 0, 36 TTYCODE_STRING, 37 TTYCODE_NUMBER, 38 TTYCODE_FLAG, 39 }; 40 41 struct tty_code { 42 enum tty_code_type type; 43 union { 44 char *string; 45 int number; 46 int flag; 47 } value; 48 }; 49 50 struct tty_term_code_entry { 51 enum tty_code_type type; 52 const char *name; 53 }; 54 55 static const struct tty_term_code_entry tty_term_codes[] = { 56 [TTYC_ACSC] = { TTYCODE_STRING, "acsc" }, 57 [TTYC_AM] = { TTYCODE_FLAG, "am" }, 58 [TTYC_AX] = { TTYCODE_FLAG, "AX" }, 59 [TTYC_BCE] = { TTYCODE_FLAG, "bce" }, 60 [TTYC_BEL] = { TTYCODE_STRING, "bel" }, 61 [TTYC_BLINK] = { TTYCODE_STRING, "blink" }, 62 [TTYC_BOLD] = { TTYCODE_STRING, "bold" }, 63 [TTYC_CIVIS] = { TTYCODE_STRING, "civis" }, 64 [TTYC_CLEAR] = { TTYCODE_STRING, "clear" }, 65 [TTYC_CLMG] = { TTYCODE_STRING, "Clmg" }, 66 [TTYC_CMG] = { TTYCODE_STRING, "Cmg" }, 67 [TTYC_CNORM] = { TTYCODE_STRING, "cnorm" }, 68 [TTYC_COLORS] = { TTYCODE_NUMBER, "colors" }, 69 [TTYC_CR] = { TTYCODE_STRING, "Cr" }, 70 [TTYC_CSR] = { TTYCODE_STRING, "csr" }, 71 [TTYC_CS] = { TTYCODE_STRING, "Cs" }, 72 [TTYC_CUB1] = { TTYCODE_STRING, "cub1" }, 73 [TTYC_CUB] = { TTYCODE_STRING, "cub" }, 74 [TTYC_CUD1] = { TTYCODE_STRING, "cud1" }, 75 [TTYC_CUD] = { TTYCODE_STRING, "cud" }, 76 [TTYC_CUF1] = { TTYCODE_STRING, "cuf1" }, 77 [TTYC_CUF] = { TTYCODE_STRING, "cuf" }, 78 [TTYC_CUP] = { TTYCODE_STRING, "cup" }, 79 [TTYC_CUU1] = { TTYCODE_STRING, "cuu1" }, 80 [TTYC_CUU] = { TTYCODE_STRING, "cuu" }, 81 [TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" }, 82 [TTYC_DCH1] = { TTYCODE_STRING, "dch1" }, 83 [TTYC_DCH] = { TTYCODE_STRING, "dch" }, 84 [TTYC_DIM] = { TTYCODE_STRING, "dim" }, 85 [TTYC_DL1] = { TTYCODE_STRING, "dl1" }, 86 [TTYC_DL] = { TTYCODE_STRING, "dl" }, 87 [TTYC_DSEKS] = { TTYCODE_STRING, "Dseks" }, 88 [TTYC_DSFCS] = { TTYCODE_STRING, "Dsfcs" }, 89 [TTYC_DSBP] = { TTYCODE_STRING, "Dsbp" }, 90 [TTYC_DSMG] = { TTYCODE_STRING, "Dsmg" }, 91 [TTYC_E3] = { TTYCODE_STRING, "E3" }, 92 [TTYC_ECH] = { TTYCODE_STRING, "ech" }, 93 [TTYC_ED] = { TTYCODE_STRING, "ed" }, 94 [TTYC_EL1] = { TTYCODE_STRING, "el1" }, 95 [TTYC_EL] = { TTYCODE_STRING, "el" }, 96 [TTYC_ENACS] = { TTYCODE_STRING, "enacs" }, 97 [TTYC_ENBP] = { TTYCODE_STRING, "Enbp" }, 98 [TTYC_ENEKS] = { TTYCODE_STRING, "Eneks" }, 99 [TTYC_ENFCS] = { TTYCODE_STRING, "Enfcs" }, 100 [TTYC_ENMG] = { TTYCODE_STRING, "Enmg" }, 101 [TTYC_FSL] = { TTYCODE_STRING, "fsl" }, 102 [TTYC_HOME] = { TTYCODE_STRING, "home" }, 103 [TTYC_HPA] = { TTYCODE_STRING, "hpa" }, 104 [TTYC_ICH1] = { TTYCODE_STRING, "ich1" }, 105 [TTYC_ICH] = { TTYCODE_STRING, "ich" }, 106 [TTYC_IL1] = { TTYCODE_STRING, "il1" }, 107 [TTYC_IL] = { TTYCODE_STRING, "il" }, 108 [TTYC_INDN] = { TTYCODE_STRING, "indn" }, 109 [TTYC_INVIS] = { TTYCODE_STRING, "invis" }, 110 [TTYC_KCBT] = { TTYCODE_STRING, "kcbt" }, 111 [TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" }, 112 [TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" }, 113 [TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" }, 114 [TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" }, 115 [TTYC_KDC2] = { TTYCODE_STRING, "kDC" }, 116 [TTYC_KDC3] = { TTYCODE_STRING, "kDC3" }, 117 [TTYC_KDC4] = { TTYCODE_STRING, "kDC4" }, 118 [TTYC_KDC5] = { TTYCODE_STRING, "kDC5" }, 119 [TTYC_KDC6] = { TTYCODE_STRING, "kDC6" }, 120 [TTYC_KDC7] = { TTYCODE_STRING, "kDC7" }, 121 [TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" }, 122 [TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, /* not kDN2 */ 123 [TTYC_KDN3] = { TTYCODE_STRING, "kDN3" }, 124 [TTYC_KDN4] = { TTYCODE_STRING, "kDN4" }, 125 [TTYC_KDN5] = { TTYCODE_STRING, "kDN5" }, 126 [TTYC_KDN6] = { TTYCODE_STRING, "kDN6" }, 127 [TTYC_KDN7] = { TTYCODE_STRING, "kDN7" }, 128 [TTYC_KEND2] = { TTYCODE_STRING, "kEND" }, 129 [TTYC_KEND3] = { TTYCODE_STRING, "kEND3" }, 130 [TTYC_KEND4] = { TTYCODE_STRING, "kEND4" }, 131 [TTYC_KEND5] = { TTYCODE_STRING, "kEND5" }, 132 [TTYC_KEND6] = { TTYCODE_STRING, "kEND6" }, 133 [TTYC_KEND7] = { TTYCODE_STRING, "kEND7" }, 134 [TTYC_KEND] = { TTYCODE_STRING, "kend" }, 135 [TTYC_KF10] = { TTYCODE_STRING, "kf10" }, 136 [TTYC_KF11] = { TTYCODE_STRING, "kf11" }, 137 [TTYC_KF12] = { TTYCODE_STRING, "kf12" }, 138 [TTYC_KF13] = { TTYCODE_STRING, "kf13" }, 139 [TTYC_KF14] = { TTYCODE_STRING, "kf14" }, 140 [TTYC_KF15] = { TTYCODE_STRING, "kf15" }, 141 [TTYC_KF16] = { TTYCODE_STRING, "kf16" }, 142 [TTYC_KF17] = { TTYCODE_STRING, "kf17" }, 143 [TTYC_KF18] = { TTYCODE_STRING, "kf18" }, 144 [TTYC_KF19] = { TTYCODE_STRING, "kf19" }, 145 [TTYC_KF1] = { TTYCODE_STRING, "kf1" }, 146 [TTYC_KF20] = { TTYCODE_STRING, "kf20" }, 147 [TTYC_KF21] = { TTYCODE_STRING, "kf21" }, 148 [TTYC_KF22] = { TTYCODE_STRING, "kf22" }, 149 [TTYC_KF23] = { TTYCODE_STRING, "kf23" }, 150 [TTYC_KF24] = { TTYCODE_STRING, "kf24" }, 151 [TTYC_KF25] = { TTYCODE_STRING, "kf25" }, 152 [TTYC_KF26] = { TTYCODE_STRING, "kf26" }, 153 [TTYC_KF27] = { TTYCODE_STRING, "kf27" }, 154 [TTYC_KF28] = { TTYCODE_STRING, "kf28" }, 155 [TTYC_KF29] = { TTYCODE_STRING, "kf29" }, 156 [TTYC_KF2] = { TTYCODE_STRING, "kf2" }, 157 [TTYC_KF30] = { TTYCODE_STRING, "kf30" }, 158 [TTYC_KF31] = { TTYCODE_STRING, "kf31" }, 159 [TTYC_KF32] = { TTYCODE_STRING, "kf32" }, 160 [TTYC_KF33] = { TTYCODE_STRING, "kf33" }, 161 [TTYC_KF34] = { TTYCODE_STRING, "kf34" }, 162 [TTYC_KF35] = { TTYCODE_STRING, "kf35" }, 163 [TTYC_KF36] = { TTYCODE_STRING, "kf36" }, 164 [TTYC_KF37] = { TTYCODE_STRING, "kf37" }, 165 [TTYC_KF38] = { TTYCODE_STRING, "kf38" }, 166 [TTYC_KF39] = { TTYCODE_STRING, "kf39" }, 167 [TTYC_KF3] = { TTYCODE_STRING, "kf3" }, 168 [TTYC_KF40] = { TTYCODE_STRING, "kf40" }, 169 [TTYC_KF41] = { TTYCODE_STRING, "kf41" }, 170 [TTYC_KF42] = { TTYCODE_STRING, "kf42" }, 171 [TTYC_KF43] = { TTYCODE_STRING, "kf43" }, 172 [TTYC_KF44] = { TTYCODE_STRING, "kf44" }, 173 [TTYC_KF45] = { TTYCODE_STRING, "kf45" }, 174 [TTYC_KF46] = { TTYCODE_STRING, "kf46" }, 175 [TTYC_KF47] = { TTYCODE_STRING, "kf47" }, 176 [TTYC_KF48] = { TTYCODE_STRING, "kf48" }, 177 [TTYC_KF49] = { TTYCODE_STRING, "kf49" }, 178 [TTYC_KF4] = { TTYCODE_STRING, "kf4" }, 179 [TTYC_KF50] = { TTYCODE_STRING, "kf50" }, 180 [TTYC_KF51] = { TTYCODE_STRING, "kf51" }, 181 [TTYC_KF52] = { TTYCODE_STRING, "kf52" }, 182 [TTYC_KF53] = { TTYCODE_STRING, "kf53" }, 183 [TTYC_KF54] = { TTYCODE_STRING, "kf54" }, 184 [TTYC_KF55] = { TTYCODE_STRING, "kf55" }, 185 [TTYC_KF56] = { TTYCODE_STRING, "kf56" }, 186 [TTYC_KF57] = { TTYCODE_STRING, "kf57" }, 187 [TTYC_KF58] = { TTYCODE_STRING, "kf58" }, 188 [TTYC_KF59] = { TTYCODE_STRING, "kf59" }, 189 [TTYC_KF5] = { TTYCODE_STRING, "kf5" }, 190 [TTYC_KF60] = { TTYCODE_STRING, "kf60" }, 191 [TTYC_KF61] = { TTYCODE_STRING, "kf61" }, 192 [TTYC_KF62] = { TTYCODE_STRING, "kf62" }, 193 [TTYC_KF63] = { TTYCODE_STRING, "kf63" }, 194 [TTYC_KF6] = { TTYCODE_STRING, "kf6" }, 195 [TTYC_KF7] = { TTYCODE_STRING, "kf7" }, 196 [TTYC_KF8] = { TTYCODE_STRING, "kf8" }, 197 [TTYC_KF9] = { TTYCODE_STRING, "kf9" }, 198 [TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" }, 199 [TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" }, 200 [TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" }, 201 [TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" }, 202 [TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" }, 203 [TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" }, 204 [TTYC_KHOME] = { TTYCODE_STRING, "khome" }, 205 [TTYC_KIC2] = { TTYCODE_STRING, "kIC" }, 206 [TTYC_KIC3] = { TTYCODE_STRING, "kIC3" }, 207 [TTYC_KIC4] = { TTYCODE_STRING, "kIC4" }, 208 [TTYC_KIC5] = { TTYCODE_STRING, "kIC5" }, 209 [TTYC_KIC6] = { TTYCODE_STRING, "kIC6" }, 210 [TTYC_KIC7] = { TTYCODE_STRING, "kIC7" }, 211 [TTYC_KICH1] = { TTYCODE_STRING, "kich1" }, 212 [TTYC_KIND] = { TTYCODE_STRING, "kind" }, 213 [TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" }, 214 [TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" }, 215 [TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" }, 216 [TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" }, 217 [TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" }, 218 [TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" }, 219 [TTYC_KMOUS] = { TTYCODE_STRING, "kmous" }, 220 [TTYC_KNP] = { TTYCODE_STRING, "knp" }, 221 [TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" }, 222 [TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" }, 223 [TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" }, 224 [TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" }, 225 [TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" }, 226 [TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" }, 227 [TTYC_KPP] = { TTYCODE_STRING, "kpp" }, 228 [TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" }, 229 [TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" }, 230 [TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" }, 231 [TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" }, 232 [TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" }, 233 [TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" }, 234 [TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" }, 235 [TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" }, 236 [TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" }, 237 [TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" }, 238 [TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" }, 239 [TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" }, 240 [TTYC_KRI] = { TTYCODE_STRING, "kri" }, 241 [TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, /* not kUP2 */ 242 [TTYC_KUP3] = { TTYCODE_STRING, "kUP3" }, 243 [TTYC_KUP4] = { TTYCODE_STRING, "kUP4" }, 244 [TTYC_KUP5] = { TTYCODE_STRING, "kUP5" }, 245 [TTYC_KUP6] = { TTYCODE_STRING, "kUP6" }, 246 [TTYC_KUP7] = { TTYCODE_STRING, "kUP7" }, 247 [TTYC_MS] = { TTYCODE_STRING, "Ms" }, 248 [TTYC_OL] = { TTYCODE_STRING, "ol" }, 249 [TTYC_OP] = { TTYCODE_STRING, "op" }, 250 [TTYC_REV] = { TTYCODE_STRING, "rev" }, 251 [TTYC_RGB] = { TTYCODE_FLAG, "RGB" }, 252 [TTYC_RIN] = { TTYCODE_STRING, "rin" }, 253 [TTYC_RI] = { TTYCODE_STRING, "ri" }, 254 [TTYC_RMACS] = { TTYCODE_STRING, "rmacs" }, 255 [TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" }, 256 [TTYC_RMKX] = { TTYCODE_STRING, "rmkx" }, 257 [TTYC_SETAB] = { TTYCODE_STRING, "setab" }, 258 [TTYC_SETAF] = { TTYCODE_STRING, "setaf" }, 259 [TTYC_SETAL] = { TTYCODE_STRING, "setal" }, 260 [TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" }, 261 [TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" }, 262 [TTYC_SETULC] = { TTYCODE_STRING, "Setulc" }, 263 [TTYC_SE] = { TTYCODE_STRING, "Se" }, 264 [TTYC_SGR0] = { TTYCODE_STRING, "sgr0" }, 265 [TTYC_SITM] = { TTYCODE_STRING, "sitm" }, 266 [TTYC_SMACS] = { TTYCODE_STRING, "smacs" }, 267 [TTYC_SMCUP] = { TTYCODE_STRING, "smcup" }, 268 [TTYC_SMKX] = { TTYCODE_STRING, "smkx" }, 269 [TTYC_SMOL] = { TTYCODE_STRING, "Smol" }, 270 [TTYC_SMSO] = { TTYCODE_STRING, "smso" }, 271 [TTYC_SMULX] = { TTYCODE_STRING, "Smulx" }, 272 [TTYC_SMUL] = { TTYCODE_STRING, "smul" }, 273 [TTYC_SMXX] = { TTYCODE_STRING, "smxx" }, 274 [TTYC_SS] = { TTYCODE_STRING, "Ss" }, 275 [TTYC_SYNC] = { TTYCODE_STRING, "Sync" }, 276 [TTYC_TC] = { TTYCODE_FLAG, "Tc" }, 277 [TTYC_TSL] = { TTYCODE_STRING, "tsl" }, 278 [TTYC_U8] = { TTYCODE_NUMBER, "U8" }, 279 [TTYC_VPA] = { TTYCODE_STRING, "vpa" }, 280 [TTYC_XT] = { TTYCODE_FLAG, "XT" } 281 }; 282 283 u_int 284 tty_term_ncodes(void) 285 { 286 return (nitems(tty_term_codes)); 287 } 288 289 static char * 290 tty_term_strip(const char *s) 291 { 292 const char *ptr; 293 static char buf[8192]; 294 size_t len; 295 296 /* Ignore strings with no padding. */ 297 if (strchr(s, '$') == NULL) 298 return (xstrdup(s)); 299 300 len = 0; 301 for (ptr = s; *ptr != '\0'; ptr++) { 302 if (*ptr == '$' && *(ptr + 1) == '<') { 303 while (*ptr != '\0' && *ptr != '>') 304 ptr++; 305 if (*ptr == '>') 306 ptr++; 307 if (*ptr == '\0') 308 break; 309 } 310 311 buf[len++] = *ptr; 312 if (len == (sizeof buf) - 1) 313 break; 314 } 315 buf[len] = '\0'; 316 317 return (xstrdup(buf)); 318 } 319 320 static char * 321 tty_term_override_next(const char *s, size_t *offset) 322 { 323 static char value[8192]; 324 size_t n = 0, at = *offset; 325 326 if (s[at] == '\0') 327 return (NULL); 328 329 while (s[at] != '\0') { 330 if (s[at] == ':') { 331 if (s[at + 1] == ':') { 332 value[n++] = ':'; 333 at += 2; 334 } else 335 break; 336 } else { 337 value[n++] = s[at]; 338 at++; 339 } 340 if (n == (sizeof value) - 1) 341 return (NULL); 342 } 343 if (s[at] != '\0') 344 *offset = at + 1; 345 else 346 *offset = at; 347 value[n] = '\0'; 348 return (value); 349 } 350 351 void 352 tty_term_apply(struct tty_term *term, const char *capabilities, int quiet) 353 { 354 const struct tty_term_code_entry *ent; 355 struct tty_code *code; 356 size_t offset = 0; 357 char *cp, *value, *s; 358 const char *errstr, *name = term->name; 359 u_int i; 360 int n, remove; 361 362 while ((s = tty_term_override_next(capabilities, &offset)) != NULL) { 363 if (*s == '\0') 364 continue; 365 value = NULL; 366 367 remove = 0; 368 if ((cp = strchr(s, '=')) != NULL) { 369 *cp++ = '\0'; 370 value = xstrdup(cp); 371 if (strunvis(value, cp) == -1) { 372 free(value); 373 value = xstrdup(cp); 374 } 375 } else if (s[strlen(s) - 1] == '@') { 376 s[strlen(s) - 1] = '\0'; 377 remove = 1; 378 } else 379 value = xstrdup(""); 380 381 if (!quiet) { 382 if (remove) 383 log_debug("%s override: %s@", name, s); 384 else if (*value == '\0') 385 log_debug("%s override: %s", name, s); 386 else 387 log_debug("%s override: %s=%s", name, s, value); 388 } 389 390 for (i = 0; i < tty_term_ncodes(); i++) { 391 ent = &tty_term_codes[i]; 392 if (strcmp(s, ent->name) != 0) 393 continue; 394 code = &term->codes[i]; 395 396 if (remove) { 397 code->type = TTYCODE_NONE; 398 continue; 399 } 400 switch (ent->type) { 401 case TTYCODE_NONE: 402 break; 403 case TTYCODE_STRING: 404 if (code->type == TTYCODE_STRING) 405 free(code->value.string); 406 code->value.string = xstrdup(value); 407 code->type = ent->type; 408 break; 409 case TTYCODE_NUMBER: 410 n = strtonum(value, 0, INT_MAX, &errstr); 411 if (errstr != NULL) 412 break; 413 code->value.number = n; 414 code->type = ent->type; 415 break; 416 case TTYCODE_FLAG: 417 code->value.flag = 1; 418 code->type = ent->type; 419 break; 420 } 421 } 422 423 free(value); 424 } 425 } 426 427 void 428 tty_term_apply_overrides(struct tty_term *term) 429 { 430 struct options_entry *o; 431 struct options_array_item *a; 432 union options_value *ov; 433 const char *s; 434 size_t offset; 435 char *first; 436 437 o = options_get_only(global_options, "terminal-overrides"); 438 a = options_array_first(o); 439 while (a != NULL) { 440 ov = options_array_item_value(a); 441 s = ov->string; 442 443 offset = 0; 444 first = tty_term_override_next(s, &offset); 445 if (first != NULL && fnmatch(first, term->name, 0) == 0) 446 tty_term_apply(term, s + offset, 0); 447 a = options_array_next(a); 448 } 449 } 450 451 struct tty_term * 452 tty_term_create(struct tty *tty, char *name, int *feat, int fd, char **cause) 453 { 454 struct tty_term *term; 455 const struct tty_term_code_entry *ent; 456 struct tty_code *code; 457 struct options_entry *o; 458 struct options_array_item *a; 459 union options_value *ov; 460 u_int i; 461 int n, error; 462 const char *s, *acs; 463 size_t offset; 464 char *first; 465 466 log_debug("adding term %s", name); 467 468 term = xcalloc(1, sizeof *term); 469 term->tty = tty; 470 term->name = xstrdup(name); 471 term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes); 472 LIST_INSERT_HEAD(&tty_terms, term, entry); 473 474 /* Set up curses terminal. */ 475 if (setupterm(name, fd, &error) != OK) { 476 switch (error) { 477 case 1: 478 xasprintf(cause, "can't use hardcopy terminal: %s", 479 name); 480 break; 481 case 0: 482 xasprintf(cause, "missing or unsuitable terminal: %s", 483 name); 484 break; 485 case -1: 486 xasprintf(cause, "can't find terminfo database"); 487 break; 488 default: 489 xasprintf(cause, "unknown error"); 490 break; 491 } 492 goto error; 493 } 494 495 /* Fill in codes. */ 496 for (i = 0; i < tty_term_ncodes(); i++) { 497 ent = &tty_term_codes[i]; 498 499 code = &term->codes[i]; 500 code->type = TTYCODE_NONE; 501 switch (ent->type) { 502 case TTYCODE_NONE: 503 break; 504 case TTYCODE_STRING: 505 s = tigetstr((char *) ent->name); 506 if (s == NULL || s == (char *) -1) 507 break; 508 code->type = TTYCODE_STRING; 509 code->value.string = tty_term_strip(s); 510 break; 511 case TTYCODE_NUMBER: 512 n = tigetnum((char *) ent->name); 513 if (n == -1 || n == -2) 514 break; 515 code->type = TTYCODE_NUMBER; 516 code->value.number = n; 517 break; 518 case TTYCODE_FLAG: 519 n = tigetflag((char *) ent->name); 520 if (n == -1) 521 break; 522 code->type = TTYCODE_FLAG; 523 code->value.flag = n; 524 break; 525 } 526 } 527 528 /* Apply terminal features. */ 529 o = options_get_only(global_options, "terminal-features"); 530 a = options_array_first(o); 531 while (a != NULL) { 532 ov = options_array_item_value(a); 533 s = ov->string; 534 535 offset = 0; 536 first = tty_term_override_next(s, &offset); 537 if (first != NULL && fnmatch(first, term->name, 0) == 0) 538 tty_add_features(feat, s + offset, ":"); 539 a = options_array_next(a); 540 } 541 542 /* Delete curses data. */ 543 del_curterm(cur_term); 544 545 /* Apply overrides so any capabilities used for features are changed. */ 546 tty_term_apply_overrides(term); 547 548 /* These are always required. */ 549 if (!tty_term_has(term, TTYC_CLEAR)) { 550 xasprintf(cause, "terminal does not support clear"); 551 goto error; 552 } 553 if (!tty_term_has(term, TTYC_CUP)) { 554 xasprintf(cause, "terminal does not support cup"); 555 goto error; 556 } 557 558 /* 559 * If TERM has XT or clear starts with CSI then it is safe to assume 560 * the terminal is derived from the VT100. This controls whether device 561 * attributes requests are sent to get more information. 562 * 563 * This is a bit of a hack but there aren't that many alternatives. 564 * Worst case tmux will just fall back to using whatever terminfo(5) 565 * says without trying to correct anything that is missing. 566 * 567 * Also add few features that VT100-like terminals should either 568 * support or safely ignore. 569 */ 570 s = tty_term_string(term, TTYC_CLEAR); 571 if (tty_term_flag(term, TTYC_XT) || strncmp(s, "\033[", 2) == 0) { 572 term->flags |= TERM_VT100LIKE; 573 tty_add_features(feat, "bpaste,focus,title", ","); 574 } 575 576 /* Add RGB feature if terminal has RGB colours. */ 577 if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) && 578 (!tty_term_has(term, TTYC_SETRGBF) || 579 !tty_term_has(term, TTYC_SETRGBB))) 580 tty_add_features(feat, "RGB", ","); 581 if (tty_term_has(term, TTYC_SETRGBF) && 582 tty_term_has(term, TTYC_SETRGBB)) 583 term->flags |= TERM_RGBCOLOURS; 584 585 /* Apply the features and overrides again. */ 586 tty_apply_features(term, *feat); 587 tty_term_apply_overrides(term); 588 589 /* 590 * Terminals without am (auto right margin) wrap at at $COLUMNS - 1 591 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1). 592 * 593 * Terminals without xenl (eat newline glitch) ignore a newline beyond 594 * the right edge of the terminal, but tmux doesn't care about this - 595 * it always uses absolute only moves the cursor with a newline when 596 * also sending a linefeed. 597 * 598 * This is irritating, most notably because it is painful to write to 599 * the very bottom-right of the screen without scrolling. 600 * 601 * Flag the terminal here and apply some workarounds in other places to 602 * do the best possible. 603 */ 604 if (!tty_term_flag(term, TTYC_AM)) 605 term->flags |= TERM_NOAM; 606 607 /* Generate ACS table. If none is present, use nearest ASCII. */ 608 memset(term->acs, 0, sizeof term->acs); 609 if (tty_term_has(term, TTYC_ACSC)) 610 acs = tty_term_string(term, TTYC_ACSC); 611 else 612 acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~."; 613 for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2) 614 term->acs[(u_char) acs[0]][0] = acs[1]; 615 616 /* Log the capabilities. */ 617 for (i = 0; i < tty_term_ncodes(); i++) 618 log_debug("%s%s", name, tty_term_describe(term, i)); 619 620 return (term); 621 622 error: 623 tty_term_free(term); 624 return (NULL); 625 } 626 627 void 628 tty_term_free(struct tty_term *term) 629 { 630 u_int i; 631 632 log_debug("removing term %s", term->name); 633 634 for (i = 0; i < tty_term_ncodes(); i++) { 635 if (term->codes[i].type == TTYCODE_STRING) 636 free(term->codes[i].value.string); 637 } 638 free(term->codes); 639 640 LIST_REMOVE(term, entry); 641 free(term->name); 642 free(term); 643 } 644 645 int 646 tty_term_has(struct tty_term *term, enum tty_code_code code) 647 { 648 return (term->codes[code].type != TTYCODE_NONE); 649 } 650 651 const char * 652 tty_term_string(struct tty_term *term, enum tty_code_code code) 653 { 654 if (!tty_term_has(term, code)) 655 return (""); 656 if (term->codes[code].type != TTYCODE_STRING) 657 fatalx("not a string: %d", code); 658 return (term->codes[code].value.string); 659 } 660 661 const char * 662 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a) 663 { 664 return (tparm((char *) tty_term_string(term, code), a)); 665 } 666 667 const char * 668 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b) 669 { 670 return (tparm((char *) tty_term_string(term, code), a, b)); 671 } 672 673 const char * 674 tty_term_string3(struct tty_term *term, enum tty_code_code code, int a, int b, 675 int c) 676 { 677 return (tparm((char *) tty_term_string(term, code), a, b, c)); 678 } 679 680 const char * 681 tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a) 682 { 683 return (tparm((char *) tty_term_string(term, code), a)); 684 } 685 686 const char * 687 tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a, 688 const void *b) 689 { 690 return (tparm((char *) tty_term_string(term, code), a, b)); 691 } 692 693 int 694 tty_term_number(struct tty_term *term, enum tty_code_code code) 695 { 696 if (!tty_term_has(term, code)) 697 return (0); 698 if (term->codes[code].type != TTYCODE_NUMBER) 699 fatalx("not a number: %d", code); 700 return (term->codes[code].value.number); 701 } 702 703 int 704 tty_term_flag(struct tty_term *term, enum tty_code_code code) 705 { 706 if (!tty_term_has(term, code)) 707 return (0); 708 if (term->codes[code].type != TTYCODE_FLAG) 709 fatalx("not a flag: %d", code); 710 return (term->codes[code].value.flag); 711 } 712 713 const char * 714 tty_term_describe(struct tty_term *term, enum tty_code_code code) 715 { 716 static char s[256]; 717 char out[128]; 718 719 switch (term->codes[code].type) { 720 case TTYCODE_NONE: 721 xsnprintf(s, sizeof s, "%4u: %s: [missing]", 722 code, tty_term_codes[code].name); 723 break; 724 case TTYCODE_STRING: 725 strnvis(out, term->codes[code].value.string, sizeof out, 726 VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL); 727 xsnprintf(s, sizeof s, "%4u: %s: (string) %s", 728 code, tty_term_codes[code].name, 729 out); 730 break; 731 case TTYCODE_NUMBER: 732 xsnprintf(s, sizeof s, "%4u: %s: (number) %d", 733 code, tty_term_codes[code].name, 734 term->codes[code].value.number); 735 break; 736 case TTYCODE_FLAG: 737 xsnprintf(s, sizeof s, "%4u: %s: (flag) %s", 738 code, tty_term_codes[code].name, 739 term->codes[code].value.flag ? "true" : "false"); 740 break; 741 } 742 return (s); 743 } 744