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