xref: /openbsd/usr.bin/tmux/tty-term.c (revision 4cfece93)
1 /* $OpenBSD: tty-term.c,v 1.82 2020/06/05 09:32: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_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_OP] = { TTYCODE_STRING, "op" },
249 	[TTYC_REV] = { TTYCODE_STRING, "rev" },
250 	[TTYC_RGB] = { TTYCODE_FLAG, "RGB" },
251 	[TTYC_RIN] = { TTYCODE_STRING, "rin" },
252 	[TTYC_RI] = { TTYCODE_STRING, "ri" },
253 	[TTYC_RMACS] = { TTYCODE_STRING, "rmacs" },
254 	[TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" },
255 	[TTYC_RMKX] = { TTYCODE_STRING, "rmkx" },
256 	[TTYC_SETAB] = { TTYCODE_STRING, "setab" },
257 	[TTYC_SETAF] = { TTYCODE_STRING, "setaf" },
258 	[TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" },
259 	[TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" },
260 	[TTYC_SETULC] = { TTYCODE_STRING, "Setulc" },
261 	[TTYC_SE] = { TTYCODE_STRING, "Se" },
262 	[TTYC_SGR0] = { TTYCODE_STRING, "sgr0" },
263 	[TTYC_SITM] = { TTYCODE_STRING, "sitm" },
264 	[TTYC_SMACS] = { TTYCODE_STRING, "smacs" },
265 	[TTYC_SMCUP] = { TTYCODE_STRING, "smcup" },
266 	[TTYC_SMKX] = { TTYCODE_STRING, "smkx" },
267 	[TTYC_SMOL] = { TTYCODE_STRING, "Smol" },
268 	[TTYC_SMSO] = { TTYCODE_STRING, "smso" },
269 	[TTYC_SMULX] = { TTYCODE_STRING, "Smulx" },
270 	[TTYC_SMUL] = { TTYCODE_STRING, "smul" },
271 	[TTYC_SMXX] =  { TTYCODE_STRING, "smxx" },
272 	[TTYC_SS] = { TTYCODE_STRING, "Ss" },
273 	[TTYC_SYNC] = { TTYCODE_STRING, "Sync" },
274 	[TTYC_TC] = { TTYCODE_FLAG, "Tc" },
275 	[TTYC_TSL] = { TTYCODE_STRING, "tsl" },
276 	[TTYC_U8] = { TTYCODE_NUMBER, "U8" },
277 	[TTYC_VPA] = { TTYCODE_STRING, "vpa" },
278 	[TTYC_XT] = { TTYCODE_FLAG, "XT" }
279 };
280 
281 u_int
282 tty_term_ncodes(void)
283 {
284 	return (nitems(tty_term_codes));
285 }
286 
287 static char *
288 tty_term_strip(const char *s)
289 {
290 	const char     *ptr;
291 	static char	buf[8192];
292 	size_t		len;
293 
294 	/* Ignore strings with no padding. */
295 	if (strchr(s, '$') == NULL)
296 		return (xstrdup(s));
297 
298 	len = 0;
299 	for (ptr = s; *ptr != '\0'; ptr++) {
300 		if (*ptr == '$' && *(ptr + 1) == '<') {
301 			while (*ptr != '\0' && *ptr != '>')
302 				ptr++;
303 			if (*ptr == '>')
304 				ptr++;
305 		}
306 
307 		buf[len++] = *ptr;
308 		if (len == (sizeof buf) - 1)
309 			break;
310 	}
311 	buf[len] = '\0';
312 
313 	return (xstrdup(buf));
314 }
315 
316 static char *
317 tty_term_override_next(const char *s, size_t *offset)
318 {
319 	static char	value[8192];
320 	size_t		n = 0, at = *offset;
321 
322 	if (s[at] == '\0')
323 		return (NULL);
324 
325 	while (s[at] != '\0') {
326 		if (s[at] == ':') {
327 			if (s[at + 1] == ':') {
328 				value[n++] = ':';
329 				at += 2;
330 			} else
331 				break;
332 		} else {
333 			value[n++] = s[at];
334 			at++;
335 		}
336 		if (n == (sizeof value) - 1)
337 			return (NULL);
338 	}
339 	if (s[at] != '\0')
340 		*offset = at + 1;
341 	else
342 		*offset = at;
343 	value[n] = '\0';
344 	return (value);
345 }
346 
347 void
348 tty_term_apply(struct tty_term *term, const char *capabilities, int quiet)
349 {
350 	const struct tty_term_code_entry	*ent;
351 	struct tty_code				*code;
352 	size_t                                   offset = 0;
353 	char					*cp, *value, *s;
354 	const char				*errstr, *name = term->name;
355 	u_int					 i;
356 	int					 n, remove;
357 
358 	while ((s = tty_term_override_next(capabilities, &offset)) != NULL) {
359 		if (*s == '\0')
360 			continue;
361 		value = NULL;
362 
363 		remove = 0;
364 		if ((cp = strchr(s, '=')) != NULL) {
365 			*cp++ = '\0';
366 			value = xstrdup(cp);
367 			if (strunvis(value, cp) == -1) {
368 				free(value);
369 				value = xstrdup(cp);
370 			}
371 		} else if (s[strlen(s) - 1] == '@') {
372 			s[strlen(s) - 1] = '\0';
373 			remove = 1;
374 		} else
375 			value = xstrdup("");
376 
377 		if (!quiet) {
378 			if (remove)
379 				log_debug("%s override: %s@", name, s);
380 			else if (*value == '\0')
381 				log_debug("%s override: %s", name, s);
382 			else
383 				log_debug("%s override: %s=%s", name, s, value);
384 		}
385 
386 		for (i = 0; i < tty_term_ncodes(); i++) {
387 			ent = &tty_term_codes[i];
388 			if (strcmp(s, ent->name) != 0)
389 				continue;
390 			code = &term->codes[i];
391 
392 			if (remove) {
393 				code->type = TTYCODE_NONE;
394 				continue;
395 			}
396 			switch (ent->type) {
397 			case TTYCODE_NONE:
398 				break;
399 			case TTYCODE_STRING:
400 				if (code->type == TTYCODE_STRING)
401 					free(code->value.string);
402 				code->value.string = xstrdup(value);
403 				code->type = ent->type;
404 				break;
405 			case TTYCODE_NUMBER:
406 				n = strtonum(value, 0, INT_MAX, &errstr);
407 				if (errstr != NULL)
408 					break;
409 				code->value.number = n;
410 				code->type = ent->type;
411 				break;
412 			case TTYCODE_FLAG:
413 				code->value.flag = 1;
414 				code->type = ent->type;
415 				break;
416 			}
417 		}
418 
419 		free(value);
420 	}
421 }
422 
423 void
424 tty_term_apply_overrides(struct tty_term *term)
425 {
426 	struct options_entry		*o;
427 	struct options_array_item	*a;
428 	union options_value		*ov;
429 	const char			*s;
430 	size_t				 offset;
431 	char				*first;
432 
433 	o = options_get_only(global_options, "terminal-overrides");
434 	a = options_array_first(o);
435 	while (a != NULL) {
436 		ov = options_array_item_value(a);
437 		s = ov->string;
438 
439 		offset = 0;
440 		first = tty_term_override_next(s, &offset);
441 		if (first != NULL && fnmatch(first, term->name, 0) == 0)
442 			tty_term_apply(term, s + offset, 0);
443 		a = options_array_next(a);
444 	}
445 }
446 
447 struct tty_term *
448 tty_term_create(struct tty *tty, char *name, int *feat, int fd, char **cause)
449 {
450 	struct tty_term				*term;
451 	const struct tty_term_code_entry	*ent;
452 	struct tty_code				*code;
453 	struct options_entry			*o;
454 	struct options_array_item		*a;
455 	union options_value			*ov;
456 	u_int					 i;
457 	int		 			 n, error;
458 	const char				*s, *acs;
459 	size_t					 offset;
460 	char					*first;
461 
462 	log_debug("adding term %s", name);
463 
464 	term = xcalloc(1, sizeof *term);
465 	term->tty = tty;
466 	term->name = xstrdup(name);
467 	term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes);
468 	LIST_INSERT_HEAD(&tty_terms, term, entry);
469 
470 	/* Set up curses terminal. */
471 	if (setupterm(name, fd, &error) != OK) {
472 		switch (error) {
473 		case 1:
474 			xasprintf(cause, "can't use hardcopy terminal: %s",
475 			    name);
476 			break;
477 		case 0:
478 			xasprintf(cause, "missing or unsuitable terminal: %s",
479 			    name);
480 			break;
481 		case -1:
482 			xasprintf(cause, "can't find terminfo database");
483 			break;
484 		default:
485 			xasprintf(cause, "unknown error");
486 			break;
487 		}
488 		goto error;
489 	}
490 
491 	/* Fill in codes. */
492 	for (i = 0; i < tty_term_ncodes(); i++) {
493 		ent = &tty_term_codes[i];
494 
495 		code = &term->codes[i];
496 		code->type = TTYCODE_NONE;
497 		switch (ent->type) {
498 		case TTYCODE_NONE:
499 			break;
500 		case TTYCODE_STRING:
501 			s = tigetstr((char *) ent->name);
502 			if (s == NULL || s == (char *) -1)
503 				break;
504 			code->type = TTYCODE_STRING;
505 			code->value.string = tty_term_strip(s);
506 			break;
507 		case TTYCODE_NUMBER:
508 			n = tigetnum((char *) ent->name);
509 			if (n == -1 || n == -2)
510 				break;
511 			code->type = TTYCODE_NUMBER;
512 			code->value.number = n;
513 			break;
514 		case TTYCODE_FLAG:
515 			n = tigetflag((char *) ent->name);
516 			if (n == -1)
517 				break;
518 			code->type = TTYCODE_FLAG;
519 			code->value.flag = n;
520 			break;
521 		}
522 	}
523 
524 	/* Apply terminal features. */
525 	o = options_get_only(global_options, "terminal-features");
526 	a = options_array_first(o);
527 	while (a != NULL) {
528 		ov = options_array_item_value(a);
529 		s = ov->string;
530 
531 		offset = 0;
532 		first = tty_term_override_next(s, &offset);
533 		if (first != NULL && fnmatch(first, term->name, 0) == 0)
534 			tty_add_features(feat, s + offset, ":");
535 		a = options_array_next(a);
536 	}
537 
538 	/* Delete curses data. */
539 	del_curterm(cur_term);
540 
541 	/* Apply overrides so any capabilities used for features are changed. */
542 	tty_term_apply_overrides(term);
543 
544 	/* These are always required. */
545 	if (!tty_term_has(term, TTYC_CLEAR)) {
546 		xasprintf(cause, "terminal does not support clear");
547 		goto error;
548 	}
549 	if (!tty_term_has(term, TTYC_CUP)) {
550 		xasprintf(cause, "terminal does not support cup");
551 		goto error;
552 	}
553 
554 	/*
555 	 * If TERM has XT or clear starts with CSI then it is safe to assume
556 	 * the terminal is derived from the VT100. This controls whether device
557 	 * attributes requests are sent to get more information.
558 	 *
559 	 * This is a bit of a hack but there aren't that many alternatives.
560 	 * Worst case tmux will just fall back to using whatever terminfo(5)
561 	 * says without trying to correct anything that is missing.
562 	 *
563 	 * Also add few features that VT100-like terminals should either
564 	 * support or safely ignore.
565 	 */
566 	s = tty_term_string(term, TTYC_CLEAR);
567 	if (tty_term_flag(term, TTYC_XT) || strncmp(s, "\033[", 2) == 0) {
568 		term->flags |= TERM_VT100LIKE;
569 		tty_add_features(feat, "bpaste,focus,title", ",");
570 	}
571 
572 	/* Add RGB feature if terminal has RGB colours. */
573 	if ((tty_term_flag(term, TTYC_TC) || tty_term_has(term, TTYC_RGB)) &&
574 	    (!tty_term_has(term, TTYC_SETRGBF) ||
575 	    !tty_term_has(term, TTYC_SETRGBB)))
576 		tty_add_features(feat, "RGB", ",");
577 
578 	/* Apply the features and overrides again. */
579 	tty_apply_features(term, *feat);
580 	tty_term_apply_overrides(term);
581 
582 	/*
583 	 * Terminals without am (auto right margin) wrap at at $COLUMNS - 1
584 	 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
585 	 *
586 	 * Terminals without xenl (eat newline glitch) ignore a newline beyond
587 	 * the right edge of the terminal, but tmux doesn't care about this -
588 	 * it always uses absolute only moves the cursor with a newline when
589 	 * also sending a linefeed.
590 	 *
591 	 * This is irritating, most notably because it is painful to write to
592 	 * the very bottom-right of the screen without scrolling.
593 	 *
594 	 * Flag the terminal here and apply some workarounds in other places to
595 	 * do the best possible.
596 	 */
597 	if (!tty_term_flag(term, TTYC_AM))
598 		term->flags |= TERM_NOAM;
599 
600 	/* Generate ACS table. If none is present, use nearest ASCII. */
601 	memset(term->acs, 0, sizeof term->acs);
602 	if (tty_term_has(term, TTYC_ACSC))
603 		acs = tty_term_string(term, TTYC_ACSC);
604 	else
605 		acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~.";
606 	for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2)
607 		term->acs[(u_char) acs[0]][0] = acs[1];
608 
609 	/* Log the capabilities. */
610 	for (i = 0; i < tty_term_ncodes(); i++)
611 		log_debug("%s%s", name, tty_term_describe(term, i));
612 
613 	return (term);
614 
615 error:
616 	tty_term_free(term);
617 	return (NULL);
618 }
619 
620 void
621 tty_term_free(struct tty_term *term)
622 {
623 	u_int	i;
624 
625 	log_debug("removing term %s", term->name);
626 
627 	for (i = 0; i < tty_term_ncodes(); i++) {
628 		if (term->codes[i].type == TTYCODE_STRING)
629 			free(term->codes[i].value.string);
630 	}
631 	free(term->codes);
632 
633 	LIST_REMOVE(term, entry);
634 	free(term->name);
635 	free(term);
636 }
637 
638 int
639 tty_term_has(struct tty_term *term, enum tty_code_code code)
640 {
641 	return (term->codes[code].type != TTYCODE_NONE);
642 }
643 
644 const char *
645 tty_term_string(struct tty_term *term, enum tty_code_code code)
646 {
647 	if (!tty_term_has(term, code))
648 		return ("");
649 	if (term->codes[code].type != TTYCODE_STRING)
650 		fatalx("not a string: %d", code);
651 	return (term->codes[code].value.string);
652 }
653 
654 const char *
655 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
656 {
657 	return (tparm((char *) tty_term_string(term, code), a));
658 }
659 
660 const char *
661 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
662 {
663 	return (tparm((char *) tty_term_string(term, code), a, b));
664 }
665 
666 const char *
667 tty_term_string3(struct tty_term *term, enum tty_code_code code, int a, int b,
668     int c)
669 {
670 	return (tparm((char *) tty_term_string(term, code), a, b, c));
671 }
672 
673 const char *
674 tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a)
675 {
676 	return (tparm((char *) tty_term_string(term, code), a));
677 }
678 
679 const char *
680 tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a,
681     const void *b)
682 {
683 	return (tparm((char *) tty_term_string(term, code), a, b));
684 }
685 
686 int
687 tty_term_number(struct tty_term *term, enum tty_code_code code)
688 {
689 	if (!tty_term_has(term, code))
690 		return (0);
691 	if (term->codes[code].type != TTYCODE_NUMBER)
692 		fatalx("not a number: %d", code);
693 	return (term->codes[code].value.number);
694 }
695 
696 int
697 tty_term_flag(struct tty_term *term, enum tty_code_code code)
698 {
699 	if (!tty_term_has(term, code))
700 		return (0);
701 	if (term->codes[code].type != TTYCODE_FLAG)
702 		fatalx("not a flag: %d", code);
703 	return (term->codes[code].value.flag);
704 }
705 
706 const char *
707 tty_term_describe(struct tty_term *term, enum tty_code_code code)
708 {
709 	static char	 s[256];
710 	char		 out[128];
711 
712 	switch (term->codes[code].type) {
713 	case TTYCODE_NONE:
714 		xsnprintf(s, sizeof s, "%4u: %s: [missing]",
715 		    code, tty_term_codes[code].name);
716 		break;
717 	case TTYCODE_STRING:
718 		strnvis(out, term->codes[code].value.string, sizeof out,
719 		    VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
720 		xsnprintf(s, sizeof s, "%4u: %s: (string) %s",
721 		    code, tty_term_codes[code].name,
722 		    out);
723 		break;
724 	case TTYCODE_NUMBER:
725 		xsnprintf(s, sizeof s, "%4u: %s: (number) %d",
726 		    code, tty_term_codes[code].name,
727 		    term->codes[code].value.number);
728 		break;
729 	case TTYCODE_FLAG:
730 		xsnprintf(s, sizeof s, "%4u: %s: (flag) %s",
731 		    code, tty_term_codes[code].name,
732 		    term->codes[code].value.flag ? "true" : "false");
733 		break;
734 	}
735 	return (s);
736 }
737