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