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