xref: /openbsd/usr.bin/tmux/tty-term.c (revision 527d0d53)
1 /* $OpenBSD: tty-term.c,v 1.102 2024/08/26 13:02:15 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_HLS] = { TTYCODE_STRING, "Hls" },
104 	[TTYC_HOME] = { TTYCODE_STRING, "home" },
105 	[TTYC_HPA] = { TTYCODE_STRING, "hpa" },
106 	[TTYC_ICH1] = { TTYCODE_STRING, "ich1" },
107 	[TTYC_ICH] = { TTYCODE_STRING, "ich" },
108 	[TTYC_IL1] = { TTYCODE_STRING, "il1" },
109 	[TTYC_IL] = { TTYCODE_STRING, "il" },
110 	[TTYC_INDN] = { TTYCODE_STRING, "indn" },
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" },
124 	[TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, /* not kDN2 */
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" },
136 	[TTYC_KEND] = { TTYCODE_STRING, "kend" },
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" },
147 	[TTYC_KF1] = { TTYCODE_STRING, "kf1" },
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" },
158 	[TTYC_KF2] = { TTYCODE_STRING, "kf2" },
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" },
169 	[TTYC_KF3] = { TTYCODE_STRING, "kf3" },
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" },
180 	[TTYC_KF4] = { TTYCODE_STRING, "kf4" },
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" },
191 	[TTYC_KF5] = { TTYCODE_STRING, "kf5" },
192 	[TTYC_KF60] = { TTYCODE_STRING, "kf60" },
193 	[TTYC_KF61] = { TTYCODE_STRING, "kf61" },
194 	[TTYC_KF62] = { TTYCODE_STRING, "kf62" },
195 	[TTYC_KF63] = { TTYCODE_STRING, "kf63" },
196 	[TTYC_KF6] = { TTYCODE_STRING, "kf6" },
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" },
214 	[TTYC_KIND] = { TTYCODE_STRING, "kind" },
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" },
242 	[TTYC_KRI] = { TTYCODE_STRING, "kri" },
243 	[TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, /* not kUP2 */
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" },
250 	[TTYC_NOBR] = { TTYCODE_STRING, "Nobr" },
251 	[TTYC_OL] = { TTYCODE_STRING, "ol" },
252 	[TTYC_OP] = { TTYCODE_STRING, "op" },
253 	[TTYC_RECT] = { TTYCODE_STRING, "Rect" },
254 	[TTYC_REV] = { TTYCODE_STRING, "rev" },
255 	[TTYC_RGB] = { TTYCODE_FLAG, "RGB" },
256 	[TTYC_RIN] = { TTYCODE_STRING, "rin" },
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" },
263 	[TTYC_SETAL] = { TTYCODE_STRING, "setal" },
264 	[TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" },
265 	[TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" },
266 	[TTYC_SETULC] = { TTYCODE_STRING, "Setulc" },
267 	[TTYC_SETULC1] = { TTYCODE_STRING, "Setulc1" },
268 	[TTYC_SE] = { TTYCODE_STRING, "Se" },
269 	[TTYC_SXL] =  { TTYCODE_FLAG, "Sxl" },
270 	[TTYC_SGR0] = { TTYCODE_STRING, "sgr0" },
271 	[TTYC_SITM] = { TTYCODE_STRING, "sitm" },
272 	[TTYC_SMACS] = { TTYCODE_STRING, "smacs" },
273 	[TTYC_SMCUP] = { TTYCODE_STRING, "smcup" },
274 	[TTYC_SMKX] = { TTYCODE_STRING, "smkx" },
275 	[TTYC_SMOL] = { TTYCODE_STRING, "Smol" },
276 	[TTYC_SMSO] = { TTYCODE_STRING, "smso" },
277 	[TTYC_SMULX] = { TTYCODE_STRING, "Smulx" },
278 	[TTYC_SMUL] = { TTYCODE_STRING, "smul" },
279 	[TTYC_SMXX] =  { TTYCODE_STRING, "smxx" },
280 	[TTYC_SS] = { TTYCODE_STRING, "Ss" },
281 	[TTYC_SWD] = { TTYCODE_STRING, "Swd" },
282 	[TTYC_SYNC] = { TTYCODE_STRING, "Sync" },
283 	[TTYC_TC] = { TTYCODE_FLAG, "Tc" },
284 	[TTYC_TSL] = { TTYCODE_STRING, "tsl" },
285 	[TTYC_U8] = { TTYCODE_NUMBER, "U8" },
286 	[TTYC_VPA] = { TTYCODE_STRING, "vpa" },
287 	[TTYC_XT] = { TTYCODE_FLAG, "XT" }
288 };
289 
290 u_int
tty_term_ncodes(void)291 tty_term_ncodes(void)
292 {
293 	return (nitems(tty_term_codes));
294 }
295 
296 static char *
tty_term_strip(const char * s)297 tty_term_strip(const char *s)
298 {
299 	const char     *ptr;
300 	static char	buf[8192];
301 	size_t		len;
302 
303 	/* Ignore strings with no padding. */
304 	if (strchr(s, '$') == NULL)
305 		return (xstrdup(s));
306 
307 	len = 0;
308 	for (ptr = s; *ptr != '\0'; ptr++) {
309 		if (*ptr == '$' && *(ptr + 1) == '<') {
310 			while (*ptr != '\0' && *ptr != '>')
311 				ptr++;
312 			if (*ptr == '>')
313 				ptr++;
314 			if (*ptr == '\0')
315 				break;
316 		}
317 
318 		buf[len++] = *ptr;
319 		if (len == (sizeof buf) - 1)
320 			break;
321 	}
322 	buf[len] = '\0';
323 
324 	return (xstrdup(buf));
325 }
326 
327 static char *
tty_term_override_next(const char * s,size_t * offset)328 tty_term_override_next(const char *s, size_t *offset)
329 {
330 	static char	value[8192];
331 	size_t		n = 0, at = *offset;
332 
333 	if (s[at] == '\0')
334 		return (NULL);
335 
336 	while (s[at] != '\0') {
337 		if (s[at] == ':') {
338 			if (s[at + 1] == ':') {
339 				value[n++] = ':';
340 				at += 2;
341 			} else
342 				break;
343 		} else {
344 			value[n++] = s[at];
345 			at++;
346 		}
347 		if (n == (sizeof value) - 1)
348 			return (NULL);
349 	}
350 	if (s[at] != '\0')
351 		*offset = at + 1;
352 	else
353 		*offset = at;
354 	value[n] = '\0';
355 	return (value);
356 }
357 
358 void
tty_term_apply(struct tty_term * term,const char * capabilities,int quiet)359 tty_term_apply(struct tty_term *term, const char *capabilities, int quiet)
360 {
361 	const struct tty_term_code_entry	*ent;
362 	struct tty_code				*code;
363 	size_t                                   offset = 0;
364 	char					*cp, *value, *s;
365 	const char				*errstr, *name = term->name;
366 	u_int					 i;
367 	int					 n, remove;
368 
369 	while ((s = tty_term_override_next(capabilities, &offset)) != NULL) {
370 		if (*s == '\0')
371 			continue;
372 		value = NULL;
373 
374 		remove = 0;
375 		if ((cp = strchr(s, '=')) != NULL) {
376 			*cp++ = '\0';
377 			value = xstrdup(cp);
378 			if (strunvis(value, cp) == -1) {
379 				free(value);
380 				value = xstrdup(cp);
381 			}
382 		} else if (s[strlen(s) - 1] == '@') {
383 			s[strlen(s) - 1] = '\0';
384 			remove = 1;
385 		} else
386 			value = xstrdup("");
387 
388 		if (!quiet) {
389 			if (remove)
390 				log_debug("%s override: %s@", name, s);
391 			else if (*value == '\0')
392 				log_debug("%s override: %s", name, s);
393 			else
394 				log_debug("%s override: %s=%s", name, s, value);
395 		}
396 
397 		for (i = 0; i < tty_term_ncodes(); i++) {
398 			ent = &tty_term_codes[i];
399 			if (strcmp(s, ent->name) != 0)
400 				continue;
401 			code = &term->codes[i];
402 
403 			if (remove) {
404 				code->type = TTYCODE_NONE;
405 				continue;
406 			}
407 			switch (ent->type) {
408 			case TTYCODE_NONE:
409 				break;
410 			case TTYCODE_STRING:
411 				if (code->type == TTYCODE_STRING)
412 					free(code->value.string);
413 				code->value.string = xstrdup(value);
414 				code->type = ent->type;
415 				break;
416 			case TTYCODE_NUMBER:
417 				n = strtonum(value, 0, INT_MAX, &errstr);
418 				if (errstr != NULL)
419 					break;
420 				code->value.number = n;
421 				code->type = ent->type;
422 				break;
423 			case TTYCODE_FLAG:
424 				code->value.flag = 1;
425 				code->type = ent->type;
426 				break;
427 			}
428 		}
429 
430 		free(value);
431 	}
432 }
433 
434 void
tty_term_apply_overrides(struct tty_term * term)435 tty_term_apply_overrides(struct tty_term *term)
436 {
437 	struct options_entry		*o;
438 	struct options_array_item	*a;
439 	union options_value		*ov;
440 	const char			*s, *acs;
441 	size_t				 offset;
442 	char				*first;
443 
444 	/* Update capabilities from the option. */
445 	o = options_get_only(global_options, "terminal-overrides");
446 	a = options_array_first(o);
447 	while (a != NULL) {
448 		ov = options_array_item_value(a);
449 		s = ov->string;
450 
451 		offset = 0;
452 		first = tty_term_override_next(s, &offset);
453 		if (first != NULL && fnmatch(first, term->name, 0) == 0)
454 			tty_term_apply(term, s + offset, 0);
455 		a = options_array_next(a);
456 	}
457 
458 	/* Log the SIXEL flag. */
459 	log_debug("SIXEL flag is %d", !!(term->flags & TERM_SIXEL));
460 
461 	/* Update the RGB flag if the terminal has RGB colours. */
462 	if (tty_term_has(term, TTYC_SETRGBF) &&
463 	    tty_term_has(term, TTYC_SETRGBB))
464 		term->flags |= TERM_RGBCOLOURS;
465 	else
466 		term->flags &= ~TERM_RGBCOLOURS;
467 	log_debug("RGBCOLOURS flag is %d", !!(term->flags & TERM_RGBCOLOURS));
468 
469 	/*
470 	 * Set or clear the DECSLRM flag if the terminal has the margin
471 	 * capabilities.
472 	 */
473 	if (tty_term_has(term, TTYC_CMG) && tty_term_has(term, TTYC_CLMG))
474 		term->flags |= TERM_DECSLRM;
475 	else
476 		term->flags &= ~TERM_DECSLRM;
477 	log_debug("DECSLRM flag is %d", !!(term->flags & TERM_DECSLRM));
478 
479 	/*
480 	 * Set or clear the DECFRA flag if the terminal has the rectangle
481 	 * capability.
482 	 */
483 	if (tty_term_has(term, TTYC_RECT))
484 		term->flags |= TERM_DECFRA;
485 	else
486 		term->flags &= ~TERM_DECFRA;
487 	log_debug("DECFRA flag is %d", !!(term->flags & TERM_DECFRA));
488 
489 	/*
490 	 * Terminals without am (auto right margin) wrap at at $COLUMNS - 1
491 	 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
492 	 *
493 	 * Terminals without xenl (eat newline glitch) ignore a newline beyond
494 	 * the right edge of the terminal, but tmux doesn't care about this -
495 	 * it always uses absolute only moves the cursor with a newline when
496 	 * also sending a linefeed.
497 	 *
498 	 * This is irritating, most notably because it is painful to write to
499 	 * the very bottom-right of the screen without scrolling.
500 	 *
501 	 * Flag the terminal here and apply some workarounds in other places to
502 	 * do the best possible.
503 	 */
504 	if (!tty_term_flag(term, TTYC_AM))
505 		term->flags |= TERM_NOAM;
506 	else
507 		term->flags &= ~TERM_NOAM;
508 	log_debug("NOAM flag is %d", !!(term->flags & TERM_NOAM));
509 
510 	/* Generate ACS table. If none is present, use nearest ASCII. */
511 	memset(term->acs, 0, sizeof term->acs);
512 	if (tty_term_has(term, TTYC_ACSC))
513 		acs = tty_term_string(term, TTYC_ACSC);
514 	else
515 		acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~.";
516 	for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2)
517 		term->acs[(u_char) acs[0]][0] = acs[1];
518 }
519 
520 struct tty_term *
tty_term_create(struct tty * tty,char * name,char ** caps,u_int ncaps,int * feat,char ** cause)521 tty_term_create(struct tty *tty, char *name, char **caps, u_int ncaps,
522     int *feat, char **cause)
523 {
524 	struct tty_term				*term;
525 	const struct tty_term_code_entry	*ent;
526 	struct tty_code				*code;
527 	struct options_entry			*o;
528 	struct options_array_item		*a;
529 	union options_value			*ov;
530 	u_int					 i, j;
531 	const char				*s, *value, *errstr;
532 	size_t					 offset, namelen;
533 	char					*first;
534 	int					 n;
535 
536 	log_debug("adding term %s", name);
537 
538 	term = xcalloc(1, sizeof *term);
539 	term->tty = tty;
540 	term->name = xstrdup(name);
541 	term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes);
542 	LIST_INSERT_HEAD(&tty_terms, term, entry);
543 
544 	/* Fill in codes. */
545 	for (i = 0; i < ncaps; i++) {
546 		namelen = strcspn(caps[i], "=");
547 		if (namelen == 0)
548 			continue;
549 		value = caps[i] + namelen + 1;
550 
551 		for (j = 0; j < tty_term_ncodes(); j++) {
552 			ent = &tty_term_codes[j];
553 			if (strncmp(ent->name, caps[i], namelen) != 0)
554 				continue;
555 			if (ent->name[namelen] != '\0')
556 				continue;
557 
558 			code = &term->codes[j];
559 			code->type = TTYCODE_NONE;
560 			switch (ent->type) {
561 			case TTYCODE_NONE:
562 				break;
563 			case TTYCODE_STRING:
564 				code->type = TTYCODE_STRING;
565 				code->value.string = tty_term_strip(value);
566 				break;
567 			case TTYCODE_NUMBER:
568 				n = strtonum(value, 0, INT_MAX, &errstr);
569 				if (errstr != NULL)
570 					log_debug("%s: %s", ent->name, errstr);
571 				else {
572 					code->type = TTYCODE_NUMBER;
573 					code->value.number = n;
574 				}
575 				break;
576 			case TTYCODE_FLAG:
577 				code->type = TTYCODE_FLAG;
578 				code->value.flag = (*value == '1');
579 				break;
580 			}
581 		}
582 	}
583 
584 	/* Apply terminal features. */
585 	o = options_get_only(global_options, "terminal-features");
586 	a = options_array_first(o);
587 	while (a != NULL) {
588 		ov = options_array_item_value(a);
589 		s = ov->string;
590 
591 		offset = 0;
592 		first = tty_term_override_next(s, &offset);
593 		if (first != NULL && fnmatch(first, term->name, 0) == 0)
594 			tty_add_features(feat, s + offset, ":");
595 		a = options_array_next(a);
596 	}
597 
598 	/* Apply overrides so any capabilities used for features are changed. */
599 	tty_term_apply_overrides(term);
600 
601 	/* These are always required. */
602 	if (!tty_term_has(term, TTYC_CLEAR)) {
603 		xasprintf(cause, "terminal does not support clear");
604 		goto error;
605 	}
606 	if (!tty_term_has(term, TTYC_CUP)) {
607 		xasprintf(cause, "terminal does not support cup");
608 		goto error;
609 	}
610 
611 	/*
612 	 * If TERM has XT or clear starts with CSI then it is safe to assume
613 	 * the terminal is derived from the VT100. This controls whether device
614 	 * attributes requests are sent to get more information.
615 	 *
616 	 * This is a bit of a hack but there aren't that many alternatives.
617 	 * Worst case tmux will just fall back to using whatever terminfo(5)
618 	 * says without trying to correct anything that is missing.
619 	 *
620 	 * Also add few features that VT100-like terminals should either
621 	 * support or safely ignore.
622 	 */
623 	s = tty_term_string(term, TTYC_CLEAR);
624 	if (tty_term_flag(term, TTYC_XT) || strncmp(s, "\033[", 2) == 0) {
625 		term->flags |= TERM_VT100LIKE;
626 		tty_add_features(feat, "bpaste,focus,title", ",");
627 	}
628 
629 	/* Add RGB feature if terminal has RGB colours. */
630 	if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) &&
631 	    (!tty_term_has(term, TTYC_SETRGBF) ||
632 	    !tty_term_has(term, TTYC_SETRGBB)))
633 		tty_add_features(feat, "RGB", ",");
634 
635 	/* Apply the features and overrides again. */
636 	if (tty_apply_features(term, *feat))
637 		tty_term_apply_overrides(term);
638 
639 	/* Log the capabilities. */
640 	for (i = 0; i < tty_term_ncodes(); i++)
641 		log_debug("%s%s", name, tty_term_describe(term, i));
642 
643 	return (term);
644 
645 error:
646 	tty_term_free(term);
647 	return (NULL);
648 }
649 
650 void
tty_term_free(struct tty_term * term)651 tty_term_free(struct tty_term *term)
652 {
653 	u_int	i;
654 
655 	log_debug("removing term %s", term->name);
656 
657 	for (i = 0; i < tty_term_ncodes(); i++) {
658 		if (term->codes[i].type == TTYCODE_STRING)
659 			free(term->codes[i].value.string);
660 	}
661 	free(term->codes);
662 
663 	LIST_REMOVE(term, entry);
664 	free(term->name);
665 	free(term);
666 }
667 
668 int
tty_term_read_list(const char * name,int fd,char *** caps,u_int * ncaps,char ** cause)669 tty_term_read_list(const char *name, int fd, char ***caps, u_int *ncaps,
670     char **cause)
671 {
672 	const struct tty_term_code_entry	*ent;
673 	int					 error, n;
674 	u_int					 i;
675 	const char				*s;
676 	char					 tmp[11];
677 
678 	if (setupterm((char *)name, fd, &error) != OK) {
679 		switch (error) {
680 		case 1:
681 			xasprintf(cause, "can't use hardcopy terminal: %s",
682 			    name);
683 			break;
684 		case 0:
685 			xasprintf(cause, "missing or unsuitable terminal: %s",
686 			    name);
687 			break;
688 		case -1:
689 			xasprintf(cause, "can't find terminfo database");
690 			break;
691 		default:
692 			xasprintf(cause, "unknown error");
693 			break;
694 		}
695 		return (-1);
696 	}
697 
698 	*ncaps = 0;
699 	*caps = NULL;
700 
701 	for (i = 0; i < tty_term_ncodes(); i++) {
702 		ent = &tty_term_codes[i];
703 		switch (ent->type) {
704 		case TTYCODE_NONE:
705 			continue;
706 		case TTYCODE_STRING:
707 			s = tigetstr((char *)ent->name);
708 			if (s == NULL || s == (char *)-1)
709 				continue;
710 			break;
711 		case TTYCODE_NUMBER:
712 			n = tigetnum((char *)ent->name);
713 			if (n == -1 || n == -2)
714 				continue;
715 			xsnprintf(tmp, sizeof tmp, "%d", n);
716 			s = tmp;
717 			break;
718 		case TTYCODE_FLAG:
719 			n = tigetflag((char *)ent->name);
720 			if (n == -1)
721 				continue;
722 			if (n)
723 				s = "1";
724 			else
725 				s = "0";
726 			break;
727 		default:
728 			fatalx("unknown capability type");
729 		}
730 		*caps = xreallocarray(*caps, (*ncaps) + 1, sizeof **caps);
731 		xasprintf(&(*caps)[*ncaps], "%s=%s", ent->name, s);
732 		(*ncaps)++;
733 	}
734 
735 	del_curterm(cur_term);
736 	return (0);
737 }
738 
739 void
tty_term_free_list(char ** caps,u_int ncaps)740 tty_term_free_list(char **caps, u_int ncaps)
741 {
742 	u_int	i;
743 
744 	for (i = 0; i < ncaps; i++)
745 		free(caps[i]);
746 	free(caps);
747 }
748 
749 int
tty_term_has(struct tty_term * term,enum tty_code_code code)750 tty_term_has(struct tty_term *term, enum tty_code_code code)
751 {
752 	return (term->codes[code].type != TTYCODE_NONE);
753 }
754 
755 const char *
tty_term_string(struct tty_term * term,enum tty_code_code code)756 tty_term_string(struct tty_term *term, enum tty_code_code code)
757 {
758 	if (!tty_term_has(term, code))
759 		return ("");
760 	if (term->codes[code].type != TTYCODE_STRING)
761 		fatalx("not a string: %d", code);
762 	return (term->codes[code].value.string);
763 }
764 
765 const char *
tty_term_string_i(struct tty_term * term,enum tty_code_code code,int a)766 tty_term_string_i(struct tty_term *term, enum tty_code_code code, int a)
767 {
768 	const char	*x = tty_term_string(term, code), *s;
769 
770 	s = tiparm_s(1, 0, x, a);
771 	if (s == NULL) {
772 		log_debug("could not expand %s", tty_term_codes[code].name);
773 		return ("");
774 	}
775 	return (s);
776 }
777 
778 const char *
tty_term_string_ii(struct tty_term * term,enum tty_code_code code,int a,int b)779 tty_term_string_ii(struct tty_term *term, enum tty_code_code code, int a, int b)
780 {
781 	const char	*x = tty_term_string(term, code), *s;
782 
783 	s = tiparm_s(2, 0, x, a, b);
784 	if (s == NULL) {
785 		log_debug("could not expand %s", tty_term_codes[code].name);
786 		return ("");
787 	}
788 	return (s);
789 }
790 
791 const char *
tty_term_string_iii(struct tty_term * term,enum tty_code_code code,int a,int b,int c)792 tty_term_string_iii(struct tty_term *term, enum tty_code_code code, int a,
793     int b, int c)
794 {
795 	const char	*x = tty_term_string(term, code), *s;
796 
797 	s = tiparm_s(3, 0, x, a, b, c);
798 	if (s == NULL) {
799 		log_debug("could not expand %s", tty_term_codes[code].name);
800 		return ("");
801 	}
802 	return (s);
803 }
804 
805 const char *
tty_term_string_s(struct tty_term * term,enum tty_code_code code,const char * a)806 tty_term_string_s(struct tty_term *term, enum tty_code_code code, const char *a)
807 {
808 	const char	*x = tty_term_string(term, code), *s;
809 
810 	s = tiparm_s(1, 1, x, a);
811 	if (s == NULL) {
812 		log_debug("could not expand %s", tty_term_codes[code].name);
813 		return ("");
814 	}
815 	return (s);
816 }
817 
818 const char *
tty_term_string_ss(struct tty_term * term,enum tty_code_code code,const char * a,const char * b)819 tty_term_string_ss(struct tty_term *term, enum tty_code_code code,
820     const char *a, const char *b)
821 {
822 	const char	*x = tty_term_string(term, code), *s;
823 
824 	s = tiparm_s(2, 3, x, a, b);
825 	if (s == NULL) {
826 		log_debug("could not expand %s", tty_term_codes[code].name);
827 		return ("");
828 	}
829 	return (s);
830 }
831 
832 int
tty_term_number(struct tty_term * term,enum tty_code_code code)833 tty_term_number(struct tty_term *term, enum tty_code_code code)
834 {
835 	if (!tty_term_has(term, code))
836 		return (0);
837 	if (term->codes[code].type != TTYCODE_NUMBER)
838 		fatalx("not a number: %d", code);
839 	return (term->codes[code].value.number);
840 }
841 
842 int
tty_term_flag(struct tty_term * term,enum tty_code_code code)843 tty_term_flag(struct tty_term *term, enum tty_code_code code)
844 {
845 	if (!tty_term_has(term, code))
846 		return (0);
847 	if (term->codes[code].type != TTYCODE_FLAG)
848 		fatalx("not a flag: %d", code);
849 	return (term->codes[code].value.flag);
850 }
851 
852 const char *
tty_term_describe(struct tty_term * term,enum tty_code_code code)853 tty_term_describe(struct tty_term *term, enum tty_code_code code)
854 {
855 	static char	 s[256];
856 	char		 out[128];
857 
858 	switch (term->codes[code].type) {
859 	case TTYCODE_NONE:
860 		xsnprintf(s, sizeof s, "%4u: %s: [missing]",
861 		    code, tty_term_codes[code].name);
862 		break;
863 	case TTYCODE_STRING:
864 		strnvis(out, term->codes[code].value.string, sizeof out,
865 		    VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
866 		xsnprintf(s, sizeof s, "%4u: %s: (string) %s",
867 		    code, tty_term_codes[code].name,
868 		    out);
869 		break;
870 	case TTYCODE_NUMBER:
871 		xsnprintf(s, sizeof s, "%4u: %s: (number) %d",
872 		    code, tty_term_codes[code].name,
873 		    term->codes[code].value.number);
874 		break;
875 	case TTYCODE_FLAG:
876 		xsnprintf(s, sizeof s, "%4u: %s: (flag) %s",
877 		    code, tty_term_codes[code].name,
878 		    term->codes[code].value.flag ? "true" : "false");
879 		break;
880 	}
881 	return (s);
882 }
883