xref: /openbsd-src/usr.bin/tmux/tty-term.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /* $OpenBSD: tty-term.c,v 1.59 2018/05/07 13:39:09 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 void	 tty_term_override(struct tty_term *, const char *);
31 static char	*tty_term_strip(const char *);
32 
33 struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms);
34 
35 enum tty_code_type {
36 	TTYCODE_NONE = 0,
37 	TTYCODE_STRING,
38 	TTYCODE_NUMBER,
39 	TTYCODE_FLAG,
40 };
41 
42 struct tty_code {
43 	enum tty_code_type	type;
44 	union {
45 		char	       *string;
46 		int		number;
47 		int		flag;
48 	} value;
49 };
50 
51 struct tty_term_code_entry {
52 	enum tty_code_type	type;
53 	const char	       *name;
54 };
55 
56 static const struct tty_term_code_entry tty_term_codes[] = {
57 	[TTYC_ACSC] = { TTYCODE_STRING, "acsc" },
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_CNORM] = { TTYCODE_STRING, "cnorm" },
66 	[TTYC_COLORS] = { TTYCODE_NUMBER, "colors" },
67 	[TTYC_CR] = { TTYCODE_STRING, "Cr" },
68 	[TTYC_CSR] = { TTYCODE_STRING, "csr" },
69 	[TTYC_CS] = { TTYCODE_STRING, "Cs" },
70 	[TTYC_CUB1] = { TTYCODE_STRING, "cub1" },
71 	[TTYC_CUB] = { TTYCODE_STRING, "cub" },
72 	[TTYC_CUD1] = { TTYCODE_STRING, "cud1" },
73 	[TTYC_CUD] = { TTYCODE_STRING, "cud" },
74 	[TTYC_CUF1] = { TTYCODE_STRING, "cuf1" },
75 	[TTYC_CUF] = { TTYCODE_STRING, "cuf" },
76 	[TTYC_CUP] = { TTYCODE_STRING, "cup" },
77 	[TTYC_CUU1] = { TTYCODE_STRING, "cuu1" },
78 	[TTYC_CUU] = { TTYCODE_STRING, "cuu" },
79 	[TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" },
80 	[TTYC_DCH1] = { TTYCODE_STRING, "dch1" },
81 	[TTYC_DCH] = { TTYCODE_STRING, "dch" },
82 	[TTYC_DIM] = { TTYCODE_STRING, "dim" },
83 	[TTYC_DL1] = { TTYCODE_STRING, "dl1" },
84 	[TTYC_DL] = { TTYCODE_STRING, "dl" },
85 	[TTYC_E3] = { TTYCODE_STRING, "E3" },
86 	[TTYC_ECH] = { TTYCODE_STRING, "ech" },
87 	[TTYC_ED] = { TTYCODE_STRING, "ed" },
88 	[TTYC_EL1] = { TTYCODE_STRING, "el1" },
89 	[TTYC_EL] = { TTYCODE_STRING, "el" },
90 	[TTYC_ENACS] = { TTYCODE_STRING, "enacs" },
91 	[TTYC_FSL] = { TTYCODE_STRING, "fsl" },
92 	[TTYC_HOME] = { TTYCODE_STRING, "home" },
93 	[TTYC_HPA] = { TTYCODE_STRING, "hpa" },
94 	[TTYC_ICH1] = { TTYCODE_STRING, "ich1" },
95 	[TTYC_ICH] = { TTYCODE_STRING, "ich" },
96 	[TTYC_IL1] = { TTYCODE_STRING, "il1" },
97 	[TTYC_IL] = { TTYCODE_STRING, "il" },
98 	[TTYC_INDN] = { TTYCODE_STRING, "indn" },
99 	[TTYC_INVIS] = { TTYCODE_STRING, "invis" },
100 	[TTYC_KCBT] = { TTYCODE_STRING, "kcbt" },
101 	[TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" },
102 	[TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" },
103 	[TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" },
104 	[TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" },
105 	[TTYC_KDC2] = { TTYCODE_STRING, "kDC" },
106 	[TTYC_KDC3] = { TTYCODE_STRING, "kDC3" },
107 	[TTYC_KDC4] = { TTYCODE_STRING, "kDC4" },
108 	[TTYC_KDC5] = { TTYCODE_STRING, "kDC5" },
109 	[TTYC_KDC6] = { TTYCODE_STRING, "kDC6" },
110 	[TTYC_KDC7] = { TTYCODE_STRING, "kDC7" },
111 	[TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" },
112 	[TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, /* not kDN2 */
113 	[TTYC_KDN3] = { TTYCODE_STRING, "kDN3" },
114 	[TTYC_KDN4] = { TTYCODE_STRING, "kDN4" },
115 	[TTYC_KDN5] = { TTYCODE_STRING, "kDN5" },
116 	[TTYC_KDN6] = { TTYCODE_STRING, "kDN6" },
117 	[TTYC_KDN7] = { TTYCODE_STRING, "kDN7" },
118 	[TTYC_KEND2] = { TTYCODE_STRING, "kEND" },
119 	[TTYC_KEND3] = { TTYCODE_STRING, "kEND3" },
120 	[TTYC_KEND4] = { TTYCODE_STRING, "kEND4" },
121 	[TTYC_KEND5] = { TTYCODE_STRING, "kEND5" },
122 	[TTYC_KEND6] = { TTYCODE_STRING, "kEND6" },
123 	[TTYC_KEND7] = { TTYCODE_STRING, "kEND7" },
124 	[TTYC_KEND] = { TTYCODE_STRING, "kend" },
125 	[TTYC_KF10] = { TTYCODE_STRING, "kf10" },
126 	[TTYC_KF11] = { TTYCODE_STRING, "kf11" },
127 	[TTYC_KF12] = { TTYCODE_STRING, "kf12" },
128 	[TTYC_KF13] = { TTYCODE_STRING, "kf13" },
129 	[TTYC_KF14] = { TTYCODE_STRING, "kf14" },
130 	[TTYC_KF15] = { TTYCODE_STRING, "kf15" },
131 	[TTYC_KF16] = { TTYCODE_STRING, "kf16" },
132 	[TTYC_KF17] = { TTYCODE_STRING, "kf17" },
133 	[TTYC_KF18] = { TTYCODE_STRING, "kf18" },
134 	[TTYC_KF19] = { TTYCODE_STRING, "kf19" },
135 	[TTYC_KF1] = { TTYCODE_STRING, "kf1" },
136 	[TTYC_KF20] = { TTYCODE_STRING, "kf20" },
137 	[TTYC_KF21] = { TTYCODE_STRING, "kf21" },
138 	[TTYC_KF22] = { TTYCODE_STRING, "kf22" },
139 	[TTYC_KF23] = { TTYCODE_STRING, "kf23" },
140 	[TTYC_KF24] = { TTYCODE_STRING, "kf24" },
141 	[TTYC_KF25] = { TTYCODE_STRING, "kf25" },
142 	[TTYC_KF26] = { TTYCODE_STRING, "kf26" },
143 	[TTYC_KF27] = { TTYCODE_STRING, "kf27" },
144 	[TTYC_KF28] = { TTYCODE_STRING, "kf28" },
145 	[TTYC_KF29] = { TTYCODE_STRING, "kf29" },
146 	[TTYC_KF2] = { TTYCODE_STRING, "kf2" },
147 	[TTYC_KF30] = { TTYCODE_STRING, "kf30" },
148 	[TTYC_KF31] = { TTYCODE_STRING, "kf31" },
149 	[TTYC_KF32] = { TTYCODE_STRING, "kf32" },
150 	[TTYC_KF33] = { TTYCODE_STRING, "kf33" },
151 	[TTYC_KF34] = { TTYCODE_STRING, "kf34" },
152 	[TTYC_KF35] = { TTYCODE_STRING, "kf35" },
153 	[TTYC_KF36] = { TTYCODE_STRING, "kf36" },
154 	[TTYC_KF37] = { TTYCODE_STRING, "kf37" },
155 	[TTYC_KF38] = { TTYCODE_STRING, "kf38" },
156 	[TTYC_KF39] = { TTYCODE_STRING, "kf39" },
157 	[TTYC_KF3] = { TTYCODE_STRING, "kf3" },
158 	[TTYC_KF40] = { TTYCODE_STRING, "kf40" },
159 	[TTYC_KF41] = { TTYCODE_STRING, "kf41" },
160 	[TTYC_KF42] = { TTYCODE_STRING, "kf42" },
161 	[TTYC_KF43] = { TTYCODE_STRING, "kf43" },
162 	[TTYC_KF44] = { TTYCODE_STRING, "kf44" },
163 	[TTYC_KF45] = { TTYCODE_STRING, "kf45" },
164 	[TTYC_KF46] = { TTYCODE_STRING, "kf46" },
165 	[TTYC_KF47] = { TTYCODE_STRING, "kf47" },
166 	[TTYC_KF48] = { TTYCODE_STRING, "kf48" },
167 	[TTYC_KF49] = { TTYCODE_STRING, "kf49" },
168 	[TTYC_KF4] = { TTYCODE_STRING, "kf4" },
169 	[TTYC_KF50] = { TTYCODE_STRING, "kf50" },
170 	[TTYC_KF51] = { TTYCODE_STRING, "kf51" },
171 	[TTYC_KF52] = { TTYCODE_STRING, "kf52" },
172 	[TTYC_KF53] = { TTYCODE_STRING, "kf53" },
173 	[TTYC_KF54] = { TTYCODE_STRING, "kf54" },
174 	[TTYC_KF55] = { TTYCODE_STRING, "kf55" },
175 	[TTYC_KF56] = { TTYCODE_STRING, "kf56" },
176 	[TTYC_KF57] = { TTYCODE_STRING, "kf57" },
177 	[TTYC_KF58] = { TTYCODE_STRING, "kf58" },
178 	[TTYC_KF59] = { TTYCODE_STRING, "kf59" },
179 	[TTYC_KF5] = { TTYCODE_STRING, "kf5" },
180 	[TTYC_KF60] = { TTYCODE_STRING, "kf60" },
181 	[TTYC_KF61] = { TTYCODE_STRING, "kf61" },
182 	[TTYC_KF62] = { TTYCODE_STRING, "kf62" },
183 	[TTYC_KF63] = { TTYCODE_STRING, "kf63" },
184 	[TTYC_KF6] = { TTYCODE_STRING, "kf6" },
185 	[TTYC_KF7] = { TTYCODE_STRING, "kf7" },
186 	[TTYC_KF8] = { TTYCODE_STRING, "kf8" },
187 	[TTYC_KF9] = { TTYCODE_STRING, "kf9" },
188 	[TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" },
189 	[TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" },
190 	[TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" },
191 	[TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" },
192 	[TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" },
193 	[TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" },
194 	[TTYC_KHOME] = { TTYCODE_STRING, "khome" },
195 	[TTYC_KIC2] = { TTYCODE_STRING, "kIC" },
196 	[TTYC_KIC3] = { TTYCODE_STRING, "kIC3" },
197 	[TTYC_KIC4] = { TTYCODE_STRING, "kIC4" },
198 	[TTYC_KIC5] = { TTYCODE_STRING, "kIC5" },
199 	[TTYC_KIC6] = { TTYCODE_STRING, "kIC6" },
200 	[TTYC_KIC7] = { TTYCODE_STRING, "kIC7" },
201 	[TTYC_KICH1] = { TTYCODE_STRING, "kich1" },
202 	[TTYC_KIND] = { TTYCODE_STRING, "kind" },
203 	[TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" },
204 	[TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" },
205 	[TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" },
206 	[TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" },
207 	[TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" },
208 	[TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" },
209 	[TTYC_KMOUS] = { TTYCODE_STRING, "kmous" },
210 	[TTYC_KNP] = { TTYCODE_STRING, "knp" },
211 	[TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" },
212 	[TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" },
213 	[TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" },
214 	[TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" },
215 	[TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" },
216 	[TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" },
217 	[TTYC_KPP] = { TTYCODE_STRING, "kpp" },
218 	[TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" },
219 	[TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" },
220 	[TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" },
221 	[TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" },
222 	[TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" },
223 	[TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" },
224 	[TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" },
225 	[TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" },
226 	[TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" },
227 	[TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" },
228 	[TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" },
229 	[TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" },
230 	[TTYC_KRI] = { TTYCODE_STRING, "kri" },
231 	[TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, /* not kUP2 */
232 	[TTYC_KUP3] = { TTYCODE_STRING, "kUP3" },
233 	[TTYC_KUP4] = { TTYCODE_STRING, "kUP4" },
234 	[TTYC_KUP5] = { TTYCODE_STRING, "kUP5" },
235 	[TTYC_KUP6] = { TTYCODE_STRING, "kUP6" },
236 	[TTYC_KUP7] = { TTYCODE_STRING, "kUP7" },
237 	[TTYC_MS] = { TTYCODE_STRING, "Ms" },
238 	[TTYC_OP] = { TTYCODE_STRING, "op" },
239 	[TTYC_REV] = { TTYCODE_STRING, "rev" },
240 	[TTYC_RGB] = { TTYCODE_FLAG, "RGB" },
241 	[TTYC_RI] = { TTYCODE_STRING, "ri" },
242 	[TTYC_RMACS] = { TTYCODE_STRING, "rmacs" },
243 	[TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" },
244 	[TTYC_RMKX] = { TTYCODE_STRING, "rmkx" },
245 	[TTYC_SETAB] = { TTYCODE_STRING, "setab" },
246 	[TTYC_SETAF] = { TTYCODE_STRING, "setaf" },
247 	[TTYC_SETRGBB] = { TTYCODE_STRING, "setrgbb" },
248 	[TTYC_SETRGBF] = { TTYCODE_STRING, "setrgbf" },
249 	[TTYC_SE] = { TTYCODE_STRING, "Se" },
250 	[TTYC_SGR0] = { TTYCODE_STRING, "sgr0" },
251 	[TTYC_SITM] = { TTYCODE_STRING, "sitm" },
252 	[TTYC_SMACS] = { TTYCODE_STRING, "smacs" },
253 	[TTYC_SMCUP] = { TTYCODE_STRING, "smcup" },
254 	[TTYC_SMKX] = { TTYCODE_STRING, "smkx" },
255 	[TTYC_SMSO] = { TTYCODE_STRING, "smso" },
256 	[TTYC_SMUL] = { TTYCODE_STRING, "smul" },
257 	[TTYC_SMXX] =  { TTYCODE_STRING, "smxx" },
258 	[TTYC_SS] = { TTYCODE_STRING, "Ss" },
259 	[TTYC_TC] = { TTYCODE_FLAG, "Tc" },
260 	[TTYC_TSL] = { TTYCODE_STRING, "tsl" },
261 	[TTYC_U8] = { TTYCODE_NUMBER, "U8" },
262 	[TTYC_VPA] = { TTYCODE_STRING, "vpa" },
263 	[TTYC_XENL] = { TTYCODE_FLAG, "xenl" },
264 	[TTYC_XT] = { TTYCODE_FLAG, "XT" },
265 };
266 
267 u_int
268 tty_term_ncodes(void)
269 {
270 	return (nitems(tty_term_codes));
271 }
272 
273 static char *
274 tty_term_strip(const char *s)
275 {
276 	const char     *ptr;
277 	static char	buf[BUFSIZ];
278 	size_t		len;
279 
280 	/* Ignore strings with no padding. */
281 	if (strchr(s, '$') == NULL)
282 		return (xstrdup(s));
283 
284 	len = 0;
285 	for (ptr = s; *ptr != '\0'; ptr++) {
286 		if (*ptr == '$' && *(ptr + 1) == '<') {
287 			while (*ptr != '\0' && *ptr != '>')
288 				ptr++;
289 			if (*ptr == '>')
290 				ptr++;
291 		}
292 
293 		buf[len++] = *ptr;
294 		if (len == (sizeof buf) - 1)
295 			break;
296 	}
297 	buf[len] = '\0';
298 
299 	return (xstrdup(buf));
300 }
301 
302 static void
303 tty_term_override(struct tty_term *term, const char *override)
304 {
305 	const struct tty_term_code_entry	*ent;
306 	struct tty_code				*code;
307 	char					*next, *s, *copy, *cp, *value;
308 	const char				*errstr;
309 	u_int					 i;
310 	int					 n, remove;
311 
312 	copy = next = xstrdup(override);
313 
314 	s = strsep(&next, ":");
315 	if (s == NULL || next == NULL || fnmatch(s, term->name, 0) != 0) {
316 		free(copy);
317 		return;
318 	}
319 
320 	while ((s = strsep(&next, ":")) != NULL) {
321 		if (*s == '\0')
322 			continue;
323 		value = NULL;
324 
325 		remove = 0;
326 		if ((cp = strchr(s, '=')) != NULL) {
327 			*cp++ = '\0';
328 			value = xstrdup(cp);
329 			if (strunvis(value, cp) == -1) {
330 				free(value);
331 				value = xstrdup(cp);
332 			}
333 		} else if (s[strlen(s) - 1] == '@') {
334 			s[strlen(s) - 1] = '\0';
335 			remove = 1;
336 		} else
337 			value = xstrdup("");
338 
339 		if (remove)
340 			log_debug("%s override: %s@", term->name, s);
341 		else
342 			log_debug("%s override: %s=%s", term->name, s, value);
343 
344 		for (i = 0; i < tty_term_ncodes(); i++) {
345 			ent = &tty_term_codes[i];
346 			if (strcmp(s, ent->name) != 0)
347 				continue;
348 			code = &term->codes[i];
349 
350 			if (remove) {
351 				code->type = TTYCODE_NONE;
352 				continue;
353 			}
354 			switch (ent->type) {
355 			case TTYCODE_NONE:
356 				break;
357 			case TTYCODE_STRING:
358 				if (code->type == TTYCODE_STRING)
359 					free(code->value.string);
360 				code->value.string = xstrdup(value);
361 				code->type = ent->type;
362 				break;
363 			case TTYCODE_NUMBER:
364 				n = strtonum(value, 0, INT_MAX, &errstr);
365 				if (errstr != NULL)
366 					break;
367 				code->value.number = n;
368 				code->type = ent->type;
369 				break;
370 			case TTYCODE_FLAG:
371 				code->value.flag = 1;
372 				code->type = ent->type;
373 				break;
374 			}
375 		}
376 
377 		free(value);
378 	}
379 	free(s);
380 }
381 
382 struct tty_term *
383 tty_term_find(char *name, int fd, char **cause)
384 {
385 	struct tty_term				*term;
386 	const struct tty_term_code_entry	*ent;
387 	struct tty_code				*code;
388 	struct options_entry			*o;
389 	u_int					 size, i;
390 	int		 			 n, error;
391 	const char				*s, *acs;
392 
393 	LIST_FOREACH(term, &tty_terms, entry) {
394 		if (strcmp(term->name, name) == 0) {
395 			term->references++;
396 			return (term);
397 		}
398 	}
399 	log_debug("new term: %s", name);
400 
401 	term = xmalloc(sizeof *term);
402 	term->name = xstrdup(name);
403 	term->references = 1;
404 	term->flags = 0;
405 	term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes);
406 	LIST_INSERT_HEAD(&tty_terms, term, entry);
407 
408 	/* Set up curses terminal. */
409 	if (setupterm(name, fd, &error) != OK) {
410 		switch (error) {
411 		case 1:
412 			xasprintf(cause, "can't use hardcopy terminal: %s",
413 			    name);
414 			break;
415 		case 0:
416 			xasprintf(cause, "missing or unsuitable terminal: %s",
417 			    name);
418 			break;
419 		case -1:
420 			xasprintf(cause, "can't find terminfo database");
421 			break;
422 		default:
423 			xasprintf(cause, "unknown error");
424 			break;
425 		}
426 		goto error;
427 	}
428 
429 	/* Fill in codes. */
430 	for (i = 0; i < tty_term_ncodes(); i++) {
431 		ent = &tty_term_codes[i];
432 
433 		code = &term->codes[i];
434 		code->type = TTYCODE_NONE;
435 		switch (ent->type) {
436 		case TTYCODE_NONE:
437 			break;
438 		case TTYCODE_STRING:
439 			s = tigetstr((char *) ent->name);
440 			if (s == NULL || s == (char *) -1)
441 				break;
442 			code->type = TTYCODE_STRING;
443 			code->value.string = tty_term_strip(s);
444 			break;
445 		case TTYCODE_NUMBER:
446 			n = tigetnum((char *) ent->name);
447 			if (n == -1 || n == -2)
448 				break;
449 			code->type = TTYCODE_NUMBER;
450 			code->value.number = n;
451 			break;
452 		case TTYCODE_FLAG:
453 			n = tigetflag((char *) ent->name);
454 			if (n == -1)
455 				break;
456 			code->type = TTYCODE_FLAG;
457 			code->value.flag = n;
458 			break;
459 		}
460 	}
461 
462 	/* Apply terminal overrides. */
463 	o = options_get_only(global_options, "terminal-overrides");
464 	if (options_array_size(o, &size) != -1) {
465 		for (i = 0; i < size; i++) {
466 			s = options_array_get(o, i);
467 			if (s != NULL)
468 				tty_term_override(term, s);
469 		}
470 	}
471 
472 	/* Delete curses data. */
473 	del_curterm(cur_term);
474 
475 	/* These are always required. */
476 	if (!tty_term_has(term, TTYC_CLEAR)) {
477 		xasprintf(cause, "terminal does not support clear");
478 		goto error;
479 	}
480 	if (!tty_term_has(term, TTYC_CUP)) {
481 		xasprintf(cause, "terminal does not support cup");
482 		goto error;
483 	}
484 
485 	/* These can be emulated so one of the two is required. */
486 	if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
487 		xasprintf(cause, "terminal does not support cud1 or cud");
488 		goto error;
489 	}
490 
491 	/* Figure out if we have 256 colours (or more). */
492 	if (tty_term_number(term, TTYC_COLORS) >= 256 ||
493 	    tty_term_has(term, TTYC_RGB))
494 		term->flags |= TERM_256COLOURS;
495 
496 	/*
497 	 * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
498 	 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
499 	 *
500 	 * This is irritating, most notably because it is impossible to write
501 	 * to the very bottom-right of the screen without scrolling.
502 	 *
503 	 * Flag the terminal here and apply some workarounds in other places to
504 	 * do the best possible.
505 	 */
506 	if (!tty_term_flag(term, TTYC_XENL))
507 		term->flags |= TERM_EARLYWRAP;
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];
517 
518 	/* On terminals with xterm titles (XT), fill in tsl and fsl. */
519 	if (tty_term_flag(term, TTYC_XT) &&
520 	    !tty_term_has(term, TTYC_TSL) &&
521 	    !tty_term_has(term, TTYC_FSL)) {
522 		code = &term->codes[TTYC_TSL];
523 		code->value.string = xstrdup("\033]0;");
524 		code->type = TTYCODE_STRING;
525 		code = &term->codes[TTYC_FSL];
526 		code->value.string = xstrdup("\007");
527 		code->type = TTYCODE_STRING;
528 	}
529 
530 	/*
531 	 * On terminals with RGB colour (Tc or RGB), fill in setrgbf and
532 	 * setrgbb if they are missing.
533 	 */
534 	if ((tty_term_flag(term, TTYC_TC) || tty_term_flag(term, TTYC_RGB)) &&
535 	    !tty_term_has(term, TTYC_SETRGBF) &&
536 	    !tty_term_has(term, TTYC_SETRGBB)) {
537 		code = &term->codes[TTYC_SETRGBF];
538 		code->value.string = xstrdup("\033[38;2;%p1%d;%p2%d;%p3%dm");
539 		code->type = TTYCODE_STRING;
540 		code = &term->codes[TTYC_SETRGBB];
541 		code->value.string = xstrdup("\033[48;2;%p1%d;%p2%d;%p3%dm");
542 		code->type = TTYCODE_STRING;
543 	}
544 
545 	/* Log it. */
546 	for (i = 0; i < tty_term_ncodes(); i++)
547 		log_debug("%s%s", name, tty_term_describe(term, i));
548 
549 	return (term);
550 
551 error:
552 	tty_term_free(term);
553 	return (NULL);
554 }
555 
556 void
557 tty_term_free(struct tty_term *term)
558 {
559 	u_int	i;
560 
561 	if (--term->references != 0)
562 		return;
563 
564 	LIST_REMOVE(term, entry);
565 
566 	for (i = 0; i < tty_term_ncodes(); i++) {
567 		if (term->codes[i].type == TTYCODE_STRING)
568 			free(term->codes[i].value.string);
569 	}
570 	free(term->codes);
571 
572 	free(term->name);
573 	free(term);
574 }
575 
576 int
577 tty_term_has(struct tty_term *term, enum tty_code_code code)
578 {
579 	return (term->codes[code].type != TTYCODE_NONE);
580 }
581 
582 const char *
583 tty_term_string(struct tty_term *term, enum tty_code_code code)
584 {
585 	if (!tty_term_has(term, code))
586 		return ("");
587 	if (term->codes[code].type != TTYCODE_STRING)
588 		fatalx("not a string: %d", code);
589 	return (term->codes[code].value.string);
590 }
591 
592 const char *
593 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
594 {
595 	return (tparm((char *) tty_term_string(term, code), a));
596 }
597 
598 const char *
599 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
600 {
601 	return (tparm((char *) tty_term_string(term, code), a, b));
602 }
603 
604 const char *
605 tty_term_string3(struct tty_term *term, enum tty_code_code code, int a, int b, int c)
606 {
607 	return (tparm((char *) tty_term_string(term, code), a, b, c));
608 }
609 
610 const char *
611 tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a)
612 {
613 	return (tparm((char *) tty_term_string(term, code), a));
614 }
615 
616 const char *
617 tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a,
618     const void *b)
619 {
620 	return (tparm((char *) tty_term_string(term, code), a, b));
621 }
622 
623 int
624 tty_term_number(struct tty_term *term, enum tty_code_code code)
625 {
626 	if (!tty_term_has(term, code))
627 		return (0);
628 	if (term->codes[code].type != TTYCODE_NUMBER)
629 		fatalx("not a number: %d", code);
630 	return (term->codes[code].value.number);
631 }
632 
633 int
634 tty_term_flag(struct tty_term *term, enum tty_code_code code)
635 {
636 	if (!tty_term_has(term, code))
637 		return (0);
638 	if (term->codes[code].type != TTYCODE_FLAG)
639 		fatalx("not a flag: %d", code);
640 	return (term->codes[code].value.flag);
641 }
642 
643 const char *
644 tty_term_describe(struct tty_term *term, enum tty_code_code code)
645 {
646 	static char	 s[256];
647 	char		 out[128];
648 
649 	switch (term->codes[code].type) {
650 	case TTYCODE_NONE:
651 		xsnprintf(s, sizeof s, "%4u: %s: [missing]",
652 		    code, tty_term_codes[code].name);
653 		break;
654 	case TTYCODE_STRING:
655 		strnvis(out, term->codes[code].value.string, sizeof out,
656 		    VIS_OCTAL|VIS_TAB|VIS_NL);
657 		xsnprintf(s, sizeof s, "%4u: %s: (string) %s",
658 		    code, tty_term_codes[code].name,
659 		    out);
660 		break;
661 	case TTYCODE_NUMBER:
662 		xsnprintf(s, sizeof s, "%4u: %s: (number) %d",
663 		    code, tty_term_codes[code].name,
664 		    term->codes[code].value.number);
665 		break;
666 	case TTYCODE_FLAG:
667 		xsnprintf(s, sizeof s, "%4u: %s: (flag) %s",
668 		    code, tty_term_codes[code].name,
669 		    term->codes[code].value.flag ? "true" : "false");
670 		break;
671 	}
672 	return (s);
673 }
674