xref: /netbsd-src/external/bsd/tmux/dist/tty-term.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /* $Id: tty-term.c,v 1.2 2011/03/12 03:02:59 christos 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 #ifdef HAVE_BROKEN_CURSES_H
22 #include <ncurses.h>
23 #else
24 #include <curses.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 = SLIST_HEAD_INITIALIZER(tty_terms);
37 
38 struct tty_term_code_entry tty_term_codes[NTTYCODE] = {
39 	{ TTYC_ACSC, TTYCODE_STRING, "acsc" },
40 	{ TTYC_AX, TTYCODE_FLAG, "AX" },
41 	{ TTYC_BEL, TTYCODE_STRING, "bel" },
42 	{ TTYC_BLINK, TTYCODE_STRING, "blink" },
43 	{ TTYC_BOLD, TTYCODE_STRING, "bold" },
44 	{ TTYC_CIVIS, TTYCODE_STRING, "civis" },
45 	{ TTYC_CLEAR, TTYCODE_STRING, "clear" },
46 	{ TTYC_CNORM, TTYCODE_STRING, "cnorm" },
47 	{ TTYC_COLORS, TTYCODE_NUMBER, "colors" },
48 	{ TTYC_CSR, TTYCODE_STRING, "csr" },
49 	{ TTYC_CUB, TTYCODE_STRING, "cub" },
50 	{ TTYC_CUB1, TTYCODE_STRING, "cub1" },
51 	{ TTYC_CUD, TTYCODE_STRING, "cud" },
52 	{ TTYC_CUD1, TTYCODE_STRING, "cud1" },
53 	{ TTYC_CUF, TTYCODE_STRING, "cuf" },
54 	{ TTYC_CUF1, TTYCODE_STRING, "cuf1" },
55 	{ TTYC_CUP, TTYCODE_STRING, "cup" },
56 	{ TTYC_CUU, TTYCODE_STRING, "cuu" },
57 	{ TTYC_CUU1, TTYCODE_STRING, "cuu1" },
58 	{ TTYC_DCH, TTYCODE_STRING, "dch" },
59 	{ TTYC_DCH1, TTYCODE_STRING, "dch1" },
60 	{ TTYC_DIM, TTYCODE_STRING, "dim" },
61 	{ TTYC_DL, TTYCODE_STRING, "dl" },
62 	{ TTYC_DL1, TTYCODE_STRING, "dl1" },
63 	{ TTYC_EL, TTYCODE_STRING, "el" },
64 	{ TTYC_EL1, TTYCODE_STRING, "el1" },
65 	{ TTYC_ENACS, TTYCODE_STRING, "enacs" },
66 	{ TTYC_HOME, TTYCODE_STRING, "home" },
67 	{ TTYC_HPA, TTYCODE_STRING, "hpa" },
68 	{ TTYC_ICH, TTYCODE_STRING, "ich" },
69 	{ TTYC_ICH1, TTYCODE_STRING, "ich1" },
70 	{ TTYC_IL, TTYCODE_STRING, "il" },
71 	{ TTYC_IL1, TTYCODE_STRING, "il1" },
72 	{ TTYC_INVIS, TTYCODE_STRING, "invis" },
73 	{ TTYC_IS1, TTYCODE_STRING, "is1" },
74 	{ TTYC_IS2, TTYCODE_STRING, "is2" },
75 	{ TTYC_IS3, TTYCODE_STRING, "is3" },
76 	{ TTYC_KCBT, TTYCODE_STRING, "kcbt" },
77 	{ TTYC_KCUB1, TTYCODE_STRING, "kcub1" },
78 	{ TTYC_KCUD1, TTYCODE_STRING, "kcud1" },
79 	{ TTYC_KCUF1, TTYCODE_STRING, "kcuf1" },
80 	{ TTYC_KCUU1, TTYCODE_STRING, "kcuu1" },
81 	{ TTYC_KDC2, TTYCODE_STRING, "kDC" },
82 	{ TTYC_KDC3, TTYCODE_STRING, "kDC3" },
83 	{ TTYC_KDC4, TTYCODE_STRING, "kDC4" },
84 	{ TTYC_KDC5, TTYCODE_STRING, "kDC5" },
85 	{ TTYC_KDC6, TTYCODE_STRING, "kDC6" },
86 	{ TTYC_KDC7, TTYCODE_STRING, "kDC7" },
87 	{ TTYC_KDCH1, TTYCODE_STRING, "kdch1" },
88 	{ TTYC_KDN2, TTYCODE_STRING, "kDN" },
89 	{ TTYC_KDN3, TTYCODE_STRING, "kDN3" },
90 	{ TTYC_KDN4, TTYCODE_STRING, "kDN4" },
91 	{ TTYC_KDN5, TTYCODE_STRING, "kDN5" },
92 	{ TTYC_KDN6, TTYCODE_STRING, "kDN6" },
93 	{ TTYC_KDN7, TTYCODE_STRING, "kDN7" },
94 	{ TTYC_KEND, TTYCODE_STRING, "kend" },
95 	{ TTYC_KEND2, TTYCODE_STRING, "kEND" },
96 	{ TTYC_KEND3, TTYCODE_STRING, "kEND3" },
97 	{ TTYC_KEND4, TTYCODE_STRING, "kEND4" },
98 	{ TTYC_KEND5, TTYCODE_STRING, "kEND5" },
99 	{ TTYC_KEND6, TTYCODE_STRING, "kEND6" },
100 	{ TTYC_KEND7, TTYCODE_STRING, "kEND7" },
101 	{ TTYC_KF1, TTYCODE_STRING, "kf1" },
102 	{ TTYC_KF10, TTYCODE_STRING, "kf10" },
103 	{ TTYC_KF11, TTYCODE_STRING, "kf11" },
104 	{ TTYC_KF12, TTYCODE_STRING, "kf12" },
105 	{ TTYC_KF13, TTYCODE_STRING, "kf13" },
106 	{ TTYC_KF14, TTYCODE_STRING, "kf14" },
107 	{ TTYC_KF15, TTYCODE_STRING, "kf15" },
108 	{ TTYC_KF16, TTYCODE_STRING, "kf16" },
109 	{ TTYC_KF17, TTYCODE_STRING, "kf17" },
110 	{ TTYC_KF18, TTYCODE_STRING, "kf18" },
111 	{ TTYC_KF19, TTYCODE_STRING, "kf19" },
112 	{ TTYC_KF2, TTYCODE_STRING, "kf2" },
113 	{ TTYC_KF20, TTYCODE_STRING, "kf20" },
114 	{ TTYC_KF3, TTYCODE_STRING, "kf3" },
115 	{ TTYC_KF4, TTYCODE_STRING, "kf4" },
116 	{ TTYC_KF5, TTYCODE_STRING, "kf5" },
117 	{ TTYC_KF6, TTYCODE_STRING, "kf6" },
118 	{ TTYC_KF7, TTYCODE_STRING, "kf7" },
119 	{ TTYC_KF8, TTYCODE_STRING, "kf8" },
120 	{ TTYC_KF9, TTYCODE_STRING, "kf9" },
121 	{ TTYC_KHOM2, TTYCODE_STRING, "kHOM" },
122 	{ TTYC_KHOM3, TTYCODE_STRING, "kHOM3" },
123 	{ TTYC_KHOM4, TTYCODE_STRING, "kHOM4" },
124 	{ TTYC_KHOM5, TTYCODE_STRING, "kHOM5" },
125 	{ TTYC_KHOM6, TTYCODE_STRING, "kHOM6" },
126 	{ TTYC_KHOM7, TTYCODE_STRING, "kHOM7" },
127 	{ TTYC_KHOME, TTYCODE_STRING, "khome" },
128 	{ TTYC_KIC2, TTYCODE_STRING, "kIC" },
129 	{ TTYC_KIC3, TTYCODE_STRING, "kIC3" },
130 	{ TTYC_KIC4, TTYCODE_STRING, "kIC4" },
131 	{ TTYC_KIC5, TTYCODE_STRING, "kIC5" },
132 	{ TTYC_KIC6, TTYCODE_STRING, "kIC6" },
133 	{ TTYC_KIC7, TTYCODE_STRING, "kIC7" },
134 	{ TTYC_KICH1, TTYCODE_STRING, "kich1" },
135 	{ TTYC_KLFT2, TTYCODE_STRING, "kLFT" },
136 	{ TTYC_KLFT3, TTYCODE_STRING, "kLFT3" },
137 	{ TTYC_KLFT4, TTYCODE_STRING, "kLFT4" },
138 	{ TTYC_KLFT5, TTYCODE_STRING, "kLFT5" },
139 	{ TTYC_KLFT6, TTYCODE_STRING, "kLFT6" },
140 	{ TTYC_KLFT7, TTYCODE_STRING, "kLFT7" },
141 	{ TTYC_KMOUS, TTYCODE_STRING, "kmous" },
142 	{ TTYC_KNP, TTYCODE_STRING, "knp" },
143 	{ TTYC_KNXT2, TTYCODE_STRING, "kNXT" },
144 	{ TTYC_KNXT3, TTYCODE_STRING, "kNXT3" },
145 	{ TTYC_KNXT4, TTYCODE_STRING, "kNXT4" },
146 	{ TTYC_KNXT5, TTYCODE_STRING, "kNXT5" },
147 	{ TTYC_KNXT6, TTYCODE_STRING, "kNXT6" },
148 	{ TTYC_KNXT7, TTYCODE_STRING, "kNXT7" },
149 	{ TTYC_KPP, TTYCODE_STRING, "kpp" },
150 	{ TTYC_KPRV2, TTYCODE_STRING, "kPRV" },
151 	{ TTYC_KPRV3, TTYCODE_STRING, "kPRV3" },
152 	{ TTYC_KPRV4, TTYCODE_STRING, "kPRV4" },
153 	{ TTYC_KPRV5, TTYCODE_STRING, "kPRV5" },
154 	{ TTYC_KPRV6, TTYCODE_STRING, "kPRV6" },
155 	{ TTYC_KPRV7, TTYCODE_STRING, "kPRV7" },
156 	{ TTYC_KRIT2, TTYCODE_STRING, "kRIT" },
157 	{ TTYC_KRIT3, TTYCODE_STRING, "kRIT3" },
158 	{ TTYC_KRIT4, TTYCODE_STRING, "kRIT4" },
159 	{ TTYC_KRIT5, TTYCODE_STRING, "kRIT5" },
160 	{ TTYC_KRIT6, TTYCODE_STRING, "kRIT6" },
161 	{ TTYC_KRIT7, TTYCODE_STRING, "kRIT7" },
162 	{ TTYC_KUP2, TTYCODE_STRING, "kUP" },
163 	{ TTYC_KUP3, TTYCODE_STRING, "kUP3" },
164 	{ TTYC_KUP4, TTYCODE_STRING, "kUP4" },
165 	{ TTYC_KUP5, TTYCODE_STRING, "kUP5" },
166 	{ TTYC_KUP6, TTYCODE_STRING, "kUP6" },
167 	{ TTYC_KUP7, TTYCODE_STRING, "kUP7" },
168 	{ TTYC_OP, TTYCODE_STRING, "op" },
169 	{ TTYC_REV, TTYCODE_STRING, "rev" },
170 	{ TTYC_RI, TTYCODE_STRING, "ri" },
171 	{ TTYC_RMACS, TTYCODE_STRING, "rmacs" },
172 	{ TTYC_RMCUP, TTYCODE_STRING, "rmcup" },
173 	{ TTYC_RMIR, TTYCODE_STRING, "rmir" },
174 	{ TTYC_RMKX, TTYCODE_STRING, "rmkx" },
175 	{ TTYC_SETAB, TTYCODE_STRING, "setab" },
176 	{ TTYC_SETAF, TTYCODE_STRING, "setaf" },
177 	{ TTYC_SGR0, TTYCODE_STRING, "sgr0" },
178 	{ TTYC_SMACS, TTYCODE_STRING, "smacs" },
179 	{ TTYC_SMCUP, TTYCODE_STRING, "smcup" },
180 	{ TTYC_SMIR, TTYCODE_STRING, "smir" },
181 	{ TTYC_SMKX, TTYCODE_STRING, "smkx" },
182 	{ TTYC_SMSO, TTYCODE_STRING, "smso" },
183 	{ TTYC_SMUL, TTYCODE_STRING, "smul" },
184 	{ TTYC_VPA, TTYCODE_STRING, "vpa" },
185 	{ TTYC_XENL, TTYCODE_FLAG, "xenl" },
186 };
187 
188 char *
189 tty_term_strip(const char *s)
190 {
191 	const char     *ptr;
192 	static char	buf[BUFSIZ];
193 	size_t		len;
194 
195 	/* Ignore strings with no padding. */
196 	if (strchr(s, '$') == NULL)
197 		return (xstrdup(s));
198 
199 	len = 0;
200 	for (ptr = s; *ptr != '\0'; ptr++) {
201 		if (*ptr == '$' && *(ptr + 1) == '<') {
202 			while (*ptr != '\0' && *ptr != '>')
203 				ptr++;
204 			if (*ptr == '>')
205 				ptr++;
206 		}
207 
208 		buf[len++] = *ptr;
209 		if (len == (sizeof buf) - 1)
210 			break;
211 	}
212 	buf[len] = '\0';
213 
214 	return (xstrdup(buf));
215 }
216 
217 void
218 tty_term_override(struct tty_term *term, const char *overrides)
219 {
220 	struct tty_term_code_entry	*ent;
221 	struct tty_code			*code;
222 	char				*termnext, *termstr, *entnext, *entstr;
223 	char				*s, *ptr, *val;
224 	const char			*errstr;
225 	u_int				 i;
226 	int				 n, removeflag;
227 
228 	s = xstrdup(overrides);
229 
230 	termnext = s;
231 	while ((termstr = strsep(&termnext, ",")) != NULL) {
232 		entnext = termstr;
233 
234 		entstr = strsep(&entnext, ":");
235 		if (entstr == NULL || entnext == NULL)
236 			continue;
237 		if (fnmatch(entstr, term->name, 0) != 0)
238 			continue;
239 		while ((entstr = strsep(&entnext, ":")) != NULL) {
240 			if (*entstr == '\0')
241 				continue;
242 
243 			val = NULL;
244 			removeflag = 0;
245 			if ((ptr = strchr(entstr, '=')) != NULL) {
246 				*ptr++ = '\0';
247 				val = xstrdup(ptr);
248 				if (strunvis(val, ptr) == -1) {
249 					xfree(val);
250 					val = xstrdup(ptr);
251 				}
252 			} else if (entstr[strlen(entstr) - 1] == '@') {
253 				entstr[strlen(entstr) - 1] = '\0';
254 				removeflag = 1;
255 			} else
256 				continue;
257 
258 			for (i = 0; i < NTTYCODE; i++) {
259 				ent = &tty_term_codes[i];
260 				if (strcmp(entstr, ent->name) != 0)
261 					continue;
262 				code = &term->codes[ent->code];
263 
264 				if (removeflag) {
265 					code->type = TTYCODE_NONE;
266 					continue;
267 				}
268 				switch (ent->type) {
269 				case TTYCODE_NONE:
270 					break;
271 				case TTYCODE_STRING:
272 					if (code->type == TTYCODE_STRING)
273 						xfree(code->value.string);
274 					code->value.string = xstrdup(val);
275 					code->type = ent->type;
276 					break;
277 				case TTYCODE_NUMBER:
278 					n = strtonum(val, 0, INT_MAX, &errstr);
279 					if (errstr != NULL)
280 						break;
281 					code->value.number = n;
282 					code->type = ent->type;
283 					break;
284 				case TTYCODE_FLAG:
285 					code->value.flag = 1;
286 					code->type = ent->type;
287 					break;
288 				}
289 			}
290 
291 			if (val != NULL)
292 				xfree(val);
293 		}
294 	}
295 
296 	xfree(s);
297 }
298 
299 struct tty_term *
300 tty_term_find(char *name, int fd, const char *overrides, char **cause)
301 {
302 	struct tty_term			*term;
303 	struct tty_term_code_entry	*ent;
304 	struct tty_code			*code;
305 	u_int				 i;
306 	int		 		 n, error;
307 	char				*s;
308 	const char                      *acs;
309 
310 	SLIST_FOREACH(term, &tty_terms, entry) {
311 		if (strcmp(term->name, name) == 0) {
312 			term->references++;
313 			return (term);
314 		}
315 	}
316 
317 	log_debug("new term: %s", name);
318 	term = xmalloc(sizeof *term);
319 	term->name = xstrdup(name);
320 	term->references = 1;
321 	term->flags = 0;
322 	memset(term->codes, 0, sizeof term->codes);
323 	SLIST_INSERT_HEAD(&tty_terms, term, entry);
324 
325 	/* Set up curses terminal. */
326 	if (setupterm(name, fd, &error) != OK) {
327 		switch (error) {
328 		case 1:
329 			xasprintf(
330 			    cause, "can't use hardcopy terminal: %s", name);
331 			break;
332 		case 0:
333 			xasprintf(
334 			    cause, "missing or unsuitable terminal: %s", name);
335 			break;
336 		case -1:
337 			xasprintf(cause, "can't find terminfo database");
338 			break;
339 		default:
340 			xasprintf(cause, "unknown error");
341 			break;
342 		}
343 		goto error;
344 	}
345 
346 	/* Fill in codes. */
347 	for (i = 0; i < NTTYCODE; i++) {
348 		ent = &tty_term_codes[i];
349 
350 		code = &term->codes[ent->code];
351 		code->type = TTYCODE_NONE;
352 		switch (ent->type) {
353 		case TTYCODE_NONE:
354 			break;
355 		case TTYCODE_STRING:
356 			s = tigetstr(ent->name);
357 			if (s == NULL || s == (char *) -1)
358 				break;
359 			code->type = TTYCODE_STRING;
360 			code->value.string = tty_term_strip(s);
361 			break;
362 		case TTYCODE_NUMBER:
363 			n = tigetnum(ent->name);
364 			if (n == -1 || n == -2)
365 				break;
366 			code->type = TTYCODE_NUMBER;
367 			code->value.number = n;
368 			break;
369 		case TTYCODE_FLAG:
370 			n = tigetflag(ent->name);
371 			if (n == -1)
372 				break;
373 			code->type = TTYCODE_FLAG;
374 			code->value.number = n;
375 			break;
376 		}
377 	}
378 	tty_term_override(term, overrides);
379 
380 	/* Delete curses data. */
381 #if !defined(__FreeBSD_version) || __FreeBSD_version >= 700000
382 	del_curterm(cur_term);
383 #endif
384 
385 	/* These are always required. */
386 	if (!tty_term_has(term, TTYC_CLEAR)) {
387 		xasprintf(cause, "terminal does not support clear");
388 		goto error;
389 	}
390 	if (!tty_term_has(term, TTYC_CUP)) {
391 		xasprintf(cause, "terminal does not support cup");
392 		goto error;
393 	}
394 
395 	/* These can be emulated so one of the two is required. */
396 	if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
397 		xasprintf(cause, "terminal does not support cud1 or cud");
398 		goto error;
399 	}
400 
401 	/* Figure out if we have 256 or 88 colours. */
402 	if (tty_term_number(term, TTYC_COLORS) == 256)
403 		term->flags |= TERM_256COLOURS;
404 	if (tty_term_number(term, TTYC_COLORS) == 88)
405 		term->flags |= TERM_88COLOURS;
406 
407 	/*
408 	 * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
409 	 * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
410 	 *
411 	 * This is irritating, most notably because it is impossible to write
412 	 * to the very bottom-right of the screen without scrolling.
413 	 *
414 	 * Flag the terminal here and apply some workarounds in other places to
415 	 * do the best possible.
416 	 */
417 	if (!tty_term_flag(term, TTYC_XENL))
418 		term->flags |= TERM_EARLYWRAP;
419 
420 	/* Generate ACS table. If none is present, use nearest ASCII. */
421 	memset(term->acs, 0, sizeof term->acs);
422 	if (tty_term_has(term, TTYC_ACSC))
423 		acs = tty_term_string(term, TTYC_ACSC);
424 	else
425 		acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~.";
426 	for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2)
427 		term->acs[(u_char) acs[0]][0] = acs[1];
428 
429 	return (term);
430 
431 error:
432 	tty_term_free(term);
433 	return (NULL);
434 }
435 
436 void
437 tty_term_free(struct tty_term *term)
438 {
439 	u_int	i;
440 
441 	if (--term->references != 0)
442 		return;
443 
444 	SLIST_REMOVE(&tty_terms, term, tty_term, entry);
445 
446 	for (i = 0; i < NTTYCODE; i++) {
447 		if (term->codes[i].type == TTYCODE_STRING)
448 			xfree(term->codes[i].value.string);
449 	}
450 	xfree(term->name);
451 	xfree(term);
452 }
453 
454 int
455 tty_term_has(struct tty_term *term, enum tty_code_code code)
456 {
457 	return (term->codes[code].type != TTYCODE_NONE);
458 }
459 
460 const char *
461 tty_term_string(struct tty_term *term, enum tty_code_code code)
462 {
463 	if (!tty_term_has(term, code))
464 		return ("");
465 	if (term->codes[code].type != TTYCODE_STRING)
466 		log_fatalx("not a string: %d", code);
467 	return (term->codes[code].value.string);
468 }
469 
470 /* No vtparm. Fucking curses. */
471 const char *
472 tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
473 {
474 	return (tparm(tty_term_string(term, code), a, 0, 0, 0, 0, 0, 0, 0, 0));
475 }
476 
477 const char *
478 tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
479 {
480 	return (tparm(tty_term_string(term, code), a, b, 0, 0, 0, 0, 0, 0, 0));
481 }
482 
483 int
484 tty_term_number(struct tty_term *term, enum tty_code_code code)
485 {
486 	if (!tty_term_has(term, code))
487 		return (0);
488 	if (term->codes[code].type != TTYCODE_NUMBER)
489 		log_fatalx("not a number: %d", code);
490 	return (term->codes[code].value.number);
491 }
492 
493 int
494 tty_term_flag(struct tty_term *term, enum tty_code_code code)
495 {
496 	if (!tty_term_has(term, code))
497 		return (0);
498 	if (term->codes[code].type != TTYCODE_FLAG)
499 		log_fatalx("not a flag: %d", code);
500 	return (term->codes[code].value.flag);
501 }
502