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