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