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