xref: /openbsd-src/usr.bin/tmux/tty-term.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: tty-term.c,v 1.10 2009/10/12 13:01:18 nicm Exp $ */
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 #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 void	 tty_term_override(struct tty_term *, const char *);
31 char	*tty_term_strip(const char *);
32 
33 struct tty_terms tty_terms = SLIST_HEAD_INITIALIZER(tty_terms);
34 
35 struct tty_term_code_entry tty_term_codes[NTTYCODE] = {
36 	{ TTYC_AX, TTYCODE_FLAG, "AX" },
37 	{ TTYC_ACSC, TTYCODE_STRING, "acsc" },
38 	{ TTYC_BEL, TTYCODE_STRING, "bel" },
39 	{ TTYC_BLINK, TTYCODE_STRING, "blink" },
40 	{ TTYC_BOLD, TTYCODE_STRING, "bold" },
41 	{ TTYC_CIVIS, TTYCODE_STRING, "civis" },
42 	{ TTYC_CLEAR, TTYCODE_STRING, "clear" },
43 	{ TTYC_CNORM, TTYCODE_STRING, "cnorm" },
44 	{ TTYC_COLORS, TTYCODE_NUMBER, "colors" },
45 	{ TTYC_CSR, TTYCODE_STRING, "csr" },
46 	{ TTYC_CUB, TTYCODE_STRING, "cub" },
47 	{ TTYC_CUB1, TTYCODE_STRING, "cub1" },
48 	{ TTYC_CUD, TTYCODE_STRING, "cud" },
49 	{ TTYC_CUD1, TTYCODE_STRING, "cud1" },
50 	{ TTYC_CUF, TTYCODE_STRING, "cuf" },
51 	{ TTYC_CUF1, TTYCODE_STRING, "cuf1" },
52 	{ TTYC_CUP, TTYCODE_STRING, "cup" },
53 	{ TTYC_CUU, TTYCODE_STRING, "cuu" },
54 	{ TTYC_CUU1, TTYCODE_STRING, "cuu1" },
55 	{ TTYC_DCH, TTYCODE_STRING, "dch" },
56 	{ TTYC_DCH1, TTYCODE_STRING, "dch1" },
57 	{ TTYC_DIM, TTYCODE_STRING, "dim" },
58 	{ TTYC_DL, TTYCODE_STRING, "dl" },
59 	{ TTYC_DL1, TTYCODE_STRING, "dl1" },
60 	{ TTYC_EL, TTYCODE_STRING, "el" },
61 	{ TTYC_EL1, TTYCODE_STRING, "el1" },
62 	{ TTYC_ENACS, TTYCODE_STRING, "enacs" },
63 	{ TTYC_HOME, TTYCODE_STRING, "home" },
64 	{ TTYC_HPA, TTYCODE_STRING, "hpa" },
65 	{ TTYC_ICH, TTYCODE_STRING, "ich" },
66 	{ TTYC_ICH1, TTYCODE_STRING, "ich1" },
67 	{ TTYC_IL, TTYCODE_STRING, "il" },
68 	{ TTYC_IL1, TTYCODE_STRING, "il1" },
69 	{ TTYC_INVIS, TTYCODE_STRING, "invis" },
70 	{ TTYC_IS1, TTYCODE_STRING, "is1" },
71 	{ TTYC_IS2, TTYCODE_STRING, "is2" },
72 	{ TTYC_IS3, TTYCODE_STRING, "is3" },
73 	{ TTYC_KCBT, TTYCODE_STRING, "kcbt" },
74 	{ TTYC_KCUB1, TTYCODE_STRING, "kcub1" },
75 	{ TTYC_KCUD1, TTYCODE_STRING, "kcud1" },
76 	{ TTYC_KCUF1, TTYCODE_STRING, "kcuf1" },
77 	{ TTYC_KCUU1, TTYCODE_STRING, "kcuu1" },
78 	{ TTYC_KDCH1, TTYCODE_STRING, "kdch1" },
79 	{ TTYC_KEND, TTYCODE_STRING, "kend" },
80 	{ TTYC_KF1, TTYCODE_STRING, "kf1" },
81 	{ TTYC_KF10, TTYCODE_STRING, "kf10" },
82 	{ TTYC_KF11, TTYCODE_STRING, "kf11" },
83 	{ TTYC_KF12, TTYCODE_STRING, "kf12" },
84 	{ TTYC_KF13, TTYCODE_STRING, "kf13" },
85 	{ TTYC_KF14, TTYCODE_STRING, "kf14" },
86 	{ TTYC_KF15, TTYCODE_STRING, "kf15" },
87 	{ TTYC_KF16, TTYCODE_STRING, "kf16" },
88 	{ TTYC_KF17, TTYCODE_STRING, "kf17" },
89 	{ TTYC_KF18, TTYCODE_STRING, "kf18" },
90 	{ TTYC_KF19, TTYCODE_STRING, "kf19" },
91 	{ TTYC_KF2, TTYCODE_STRING, "kf2" },
92 	{ TTYC_KF20, TTYCODE_STRING, "kf20" },
93 	{ TTYC_KF3, TTYCODE_STRING, "kf3" },
94 	{ TTYC_KF4, TTYCODE_STRING, "kf4" },
95 	{ TTYC_KF5, TTYCODE_STRING, "kf5" },
96 	{ TTYC_KF6, TTYCODE_STRING, "kf6" },
97 	{ TTYC_KF7, TTYCODE_STRING, "kf7" },
98 	{ TTYC_KF8, TTYCODE_STRING, "kf8" },
99 	{ TTYC_KF9, TTYCODE_STRING, "kf9" },
100 	{ TTYC_KHOME, TTYCODE_STRING, "khome" },
101 	{ TTYC_KICH1, TTYCODE_STRING, "kich1" },
102 	{ TTYC_KMOUS, TTYCODE_STRING, "kmous" },
103 	{ TTYC_KNP, TTYCODE_STRING, "knp" },
104 	{ TTYC_KPP, TTYCODE_STRING, "kpp" },
105 	{ TTYC_OP, TTYCODE_STRING, "op" },
106 	{ TTYC_REV, TTYCODE_STRING, "rev" },
107 	{ TTYC_RI, TTYCODE_STRING, "ri" },
108 	{ TTYC_RMACS, TTYCODE_STRING, "rmacs" },
109 	{ TTYC_RMCUP, TTYCODE_STRING, "rmcup" },
110 	{ TTYC_RMIR, TTYCODE_STRING, "rmir" },
111 	{ TTYC_RMKX, TTYCODE_STRING, "rmkx" },
112 	{ TTYC_SETAB, TTYCODE_STRING, "setab" },
113 	{ TTYC_SETAF, TTYCODE_STRING, "setaf" },
114 	{ TTYC_SGR0, TTYCODE_STRING, "sgr0" },
115 	{ TTYC_SMACS, TTYCODE_STRING, "smacs" },
116 	{ TTYC_SMCUP, TTYCODE_STRING, "smcup" },
117 	{ TTYC_SMIR, TTYCODE_STRING, "smir" },
118 	{ TTYC_SMKX, TTYCODE_STRING, "smkx" },
119 	{ TTYC_SMSO, TTYCODE_STRING, "smso" },
120 	{ TTYC_SMUL, TTYCODE_STRING, "smul" },
121 	{ TTYC_VPA, TTYCODE_STRING, "vpa" },
122 	{ TTYC_XENL, TTYCODE_FLAG, "xenl" },
123 };
124 
125 char *
126 tty_term_strip(const char *s)
127 {
128 	const char     *ptr;
129 	static char	buf[BUFSIZ];
130 	size_t		len;
131 
132 	/* Ignore strings with no padding. */
133 	if (strchr(s, '$') == NULL)
134 		return (xstrdup(s));
135 
136 	len = 0;
137 	for (ptr = s; *ptr != '\0'; ptr++) {
138 		if (*ptr == '$' && *(ptr + 1) == '<') {
139 			while (*ptr != '\0' && *ptr != '>')
140 				ptr++;
141 			if (*ptr == '>')
142 				ptr++;
143 		}
144 
145 		buf[len++] = *ptr;
146 		if (len == (sizeof buf) - 1)
147 			break;
148 	}
149 	buf[len] = '\0';
150 
151 	return (xstrdup(buf));
152 }
153 
154 void
155 tty_term_override(struct tty_term *term, const char *overrides)
156 {
157 	struct tty_term_code_entry	*ent;
158 	struct tty_code			*code;
159 	char				*termnext, *termstr, *entnext, *entstr;
160 	char				*s, *ptr, *val;
161 	const char			*errstr;
162 	u_int				 i;
163 	int				 n, removeflag;
164 
165 	s = xstrdup(overrides);
166 
167 	termnext = s;
168 	while ((termstr = strsep(&termnext, ",")) != NULL) {
169 		entnext = termstr;
170 
171 		entstr = strsep(&entnext, ":");
172 		if (entstr == NULL || entnext == NULL)
173 			continue;
174 		if (fnmatch(entstr, term->name, 0) != 0)
175 			continue;
176 		while ((entstr = strsep(&entnext, ":")) != NULL) {
177 			if (*entstr == '\0')
178 				continue;
179 
180 			val = NULL;
181 			removeflag = 0;
182  			if ((ptr = strchr(entstr, '=')) != NULL) {
183 				*ptr++ = '\0';
184 				val = xstrdup(ptr);
185 				if (strunvis(val, ptr) == -1) {
186 					xfree(val);
187 					val = xstrdup(ptr);
188 				}
189 			} else if (entstr[strlen(entstr) - 1] == '@') {
190 				entstr[strlen(entstr) - 1] = '\0';
191 				removeflag = 1;
192 			}
193 
194 			for (i = 0; i < NTTYCODE; i++) {
195 				ent = &tty_term_codes[i];
196 				if (strcmp(entstr, ent->name) != 0)
197 					continue;
198 				code = &term->codes[ent->code];
199 
200 				if (removeflag) {
201 					code->type = TTYCODE_NONE;
202 					continue;
203 				}
204 				switch (ent->type) {
205 				case TTYCODE_NONE:
206 					break;
207 				case TTYCODE_STRING:
208 					if (code->type == TTYCODE_STRING)
209 						xfree(code->value.string);
210 					code->value.string = xstrdup(val);
211 					code->type = ent->type;
212 					break;
213 				case TTYCODE_NUMBER:
214 					n = strtonum(val, 0, INT_MAX, &errstr);
215 					if (errstr != NULL)
216 						break;
217 					code->value.number = n;
218 					code->type = ent->type;
219 					break;
220 				case TTYCODE_FLAG:
221 					code->value.flag = 1;
222 					code->type = ent->type;
223 					break;
224 				}
225 			}
226 
227 			if (val != NULL)
228 				xfree(val);
229 		}
230 	}
231 
232 	xfree(s);
233 }
234 
235 struct tty_term *
236 tty_term_find(char *name, int fd, const char *overrides, char **cause)
237 {
238 	struct tty_term			*term;
239 	struct tty_term_code_entry	*ent;
240 	struct tty_code			*code;
241 	u_int				 i;
242 	int		 		 n, error;
243 	char				*s;
244 
245 	SLIST_FOREACH(term, &tty_terms, entry) {
246 		if (strcmp(term->name, name) == 0) {
247 			term->references++;
248 			return (term);
249 		}
250 	}
251 
252 	log_debug("new term: %s", name);
253 	term = xmalloc(sizeof *term);
254 	term->name = xstrdup(name);
255 	term->references = 1;
256 	term->flags = 0;
257 	memset(&term->codes, 0, sizeof term->codes);
258 	SLIST_INSERT_HEAD(&tty_terms, term, entry);
259 
260 	/* Set up curses terminal. */
261 	if (setupterm(name, fd, &error) != OK) {
262 		switch (error) {
263 		case 1:
264 			xasprintf(cause, "can't use hardcopy terminal: %s", name);
265 			break;
266 		case 0:
267 			xasprintf(cause, "missing or unsuitable terminal: %s", name);
268 			break;
269 		case -1:
270 			xasprintf(cause, "can't find terminfo database");
271 			break;
272 		default:
273 			xasprintf(cause, "unknown error");
274 			break;
275 		}
276 		goto error;
277 	}
278 
279 	/* Fill in codes. */
280 	for (i = 0; i < NTTYCODE; i++) {
281 		ent = &tty_term_codes[i];
282 
283 		code = &term->codes[ent->code];
284 		code->type = TTYCODE_NONE;
285 		switch (ent->type) {
286 		case TTYCODE_NONE:
287 			break;
288 		case TTYCODE_STRING:
289 			s = tigetstr((char *) ent->name);
290 			if (s == NULL || s == (char *) -1)
291 				break;
292 			code->type = TTYCODE_STRING;
293 			code->value.string = tty_term_strip(s);
294 			break;
295 		case TTYCODE_NUMBER:
296 			n = tigetnum((char *) ent->name);
297 			if (n == -1 || n == -2)
298 				break;
299 			code->type = TTYCODE_NUMBER;
300 			code->value.number = n;
301 			break;
302 		case TTYCODE_FLAG:
303 			n = tigetflag((char *) ent->name);
304 			if (n == -1)
305 				break;
306 			code->type = TTYCODE_FLAG;
307 			code->value.number = n;
308 			break;
309 		}
310 	}
311 	tty_term_override(term, overrides);
312 
313 	/* Delete curses data. */
314 	del_curterm(cur_term);
315 
316 	/* These are always required. */
317 	if (!tty_term_has(term, TTYC_CLEAR)) {
318 		xasprintf(cause, "terminal does not support clear");
319 		goto error;
320 	}
321 	if (!tty_term_has(term, TTYC_RI)) {
322 		xasprintf(cause, "terminal does not support ri");
323 		goto error;
324 	}
325 	if (!tty_term_has(term, TTYC_CUP)) {
326 		xasprintf(cause, "terminal does not support cup");
327 		goto error;
328 	}
329 
330 	/* These can be emulated so one of the two is required. */
331 	if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
332 		xasprintf(cause, "terminal does not support cud1 or cud");
333 		goto error;
334 	}
335 	if (!tty_term_has(term, TTYC_IL1) && !tty_term_has(term, TTYC_IL)) {
336 		xasprintf(cause, "terminal does not support il1 or il");
337 		goto error;
338 	}
339 	if (!tty_term_has(term, TTYC_DL1) && !tty_term_has(term, TTYC_DL)) {
340 		xasprintf(cause, "terminal does not support dl1 or dl");
341 		goto error;
342 	}
343 	if (!tty_term_has(term, TTYC_ICH1) &&
344 	    !tty_term_has(term, TTYC_ICH) && (!tty_term_has(term, TTYC_SMIR) ||
345 	    !tty_term_has(term, TTYC_RMIR))) {
346 		xasprintf(cause,
347 		    "terminal does not support ich1 or ich or smir and rmir");
348 		goto error;
349 	}
350 
351 	/*
352 	 * Figure out if terminal support default colours. AX is a screen
353 	 * extension which indicates this. Also check if op (orig_pair) uses
354 	 * the default colours - if it does, this is a good indication the
355 	 * terminal supports them.
356 	 */
357 	if (tty_term_flag(term, TTYC_AX))
358 		term->flags |= TERM_HASDEFAULTS;
359 	if (strcmp(tty_term_string(term, TTYC_OP), "\033[39;49m") == 0)
360 		term->flags |= TERM_HASDEFAULTS;
361 
362 	/*
363 	 * Try to figure out if we have 256 or 88 colours. The standard xterm
364 	 * definitions are broken (well, or the way they are parsed is: in any
365 	 * case they end up returning 8). So also do a hack.
366 	 */
367 	if (tty_term_number(term, TTYC_COLORS) == 256)
368 		term->flags |= TERM_256COLOURS;
369 	if (tty_term_number(term, TTYC_COLORS) == 88)
370 		term->flags |= TERM_88COLOURS;
371 
372 	/*
373 	 * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
374 	 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
375 	 *
376 	 * This is irritating, most notably because it is impossible to write
377 	 * to the very bottom-right of the screen without scrolling.
378 	 *
379 	 * Flag the terminal here and apply some workarounds in other places to
380 	 * do the best possible.
381 	 */
382 	if (!tty_term_flag(term, TTYC_XENL))
383 		term->flags |= TERM_EARLYWRAP;
384 
385 	return (term);
386 
387 error:
388 	tty_term_free(term);
389 	return (NULL);
390 }
391 
392 void
393 tty_term_free(struct tty_term *term)
394 {
395 	u_int	i;
396 
397 	if (--term->references != 0)
398 		return;
399 
400 	SLIST_REMOVE(&tty_terms, term, tty_term, entry);
401 
402 	for (i = 0; i < NTTYCODE; i++) {
403 		if (term->codes[i].type == TTYCODE_STRING)
404 			xfree(term->codes[i].value.string);
405 	}
406 	xfree(term->name);
407 	xfree(term);
408 }
409 
410 int
411 tty_term_has(struct tty_term *term, enum tty_code_code code)
412 {
413 	return (term->codes[code].type != TTYCODE_NONE);
414 }
415 
416 const char *
417 tty_term_string(struct tty_term *term, enum tty_code_code code)
418 {
419 	if (!tty_term_has(term, code))
420 		return ("");
421 	if (term->codes[code].type != TTYCODE_STRING)
422 		log_fatalx("not a string: %d", code);
423 	return (term->codes[code].value.string);
424 }
425 
426 /* No vtparm. Fucking curses. */
427 const char *
428 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
429 {
430 	return (tparm((char *) tty_term_string(term, code), a));
431 }
432 
433 const char *
434 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
435 {
436 	return (tparm((char *) tty_term_string(term, code), a, b));
437 }
438 
439 int
440 tty_term_number(struct tty_term *term, enum tty_code_code code)
441 {
442 	if (!tty_term_has(term, code))
443 		return (0);
444 	if (term->codes[code].type != TTYCODE_NUMBER)
445 		log_fatalx("not a number: %d", code);
446 	return (term->codes[code].value.number);
447 }
448 
449 int
450 tty_term_flag(struct tty_term *term, enum tty_code_code code)
451 {
452 	if (!tty_term_has(term, code))
453 		return (0);
454 	if (term->codes[code].type != TTYCODE_FLAG)
455 		log_fatalx("not a flag: %d", code);
456 	return (term->codes[code].value.flag);
457 }
458