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