Annotation of embedaddon/tmux/tty-term.c, revision 1.1
1.1 ! misho 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
! 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: #if defined(HAVE_CURSES_H)
! 22: #include <curses.h>
! 23: #elif defined(HAVE_NCURSES_H)
! 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: static void tty_term_override(struct tty_term *, const char *);
! 34: static char *tty_term_strip(const char *);
! 35:
! 36: struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms);
! 37:
! 38: enum tty_code_type {
! 39: TTYCODE_NONE = 0,
! 40: TTYCODE_STRING,
! 41: TTYCODE_NUMBER,
! 42: TTYCODE_FLAG,
! 43: };
! 44:
! 45: struct tty_code {
! 46: enum tty_code_type type;
! 47: union {
! 48: char *string;
! 49: int number;
! 50: int flag;
! 51: } value;
! 52: };
! 53:
! 54: struct tty_term_code_entry {
! 55: enum tty_code_type type;
! 56: const char *name;
! 57: };
! 58:
! 59: static const struct tty_term_code_entry tty_term_codes[] = {
! 60: [TTYC_ACSC] = { TTYCODE_STRING, "acsc" },
! 61: [TTYC_AX] = { TTYCODE_FLAG, "AX" },
! 62: [TTYC_BCE] = { TTYCODE_FLAG, "bce" },
! 63: [TTYC_BEL] = { TTYCODE_STRING, "bel" },
! 64: [TTYC_BLINK] = { TTYCODE_STRING, "blink" },
! 65: [TTYC_BOLD] = { TTYCODE_STRING, "bold" },
! 66: [TTYC_CIVIS] = { TTYCODE_STRING, "civis" },
! 67: [TTYC_CLEAR] = { TTYCODE_STRING, "clear" },
! 68: [TTYC_CNORM] = { TTYCODE_STRING, "cnorm" },
! 69: [TTYC_COLORS] = { TTYCODE_NUMBER, "colors" },
! 70: [TTYC_CR] = { TTYCODE_STRING, "Cr" },
! 71: [TTYC_CS] = { TTYCODE_STRING, "Cs" },
! 72: [TTYC_CSR] = { TTYCODE_STRING, "csr" },
! 73: [TTYC_CUB] = { TTYCODE_STRING, "cub" },
! 74: [TTYC_CUB1] = { TTYCODE_STRING, "cub1" },
! 75: [TTYC_CUD] = { TTYCODE_STRING, "cud" },
! 76: [TTYC_CUD1] = { TTYCODE_STRING, "cud1" },
! 77: [TTYC_CUF] = { TTYCODE_STRING, "cuf" },
! 78: [TTYC_CUF1] = { TTYCODE_STRING, "cuf1" },
! 79: [TTYC_CUP] = { TTYCODE_STRING, "cup" },
! 80: [TTYC_CUU] = { TTYCODE_STRING, "cuu" },
! 81: [TTYC_CUU1] = { TTYCODE_STRING, "cuu1" },
! 82: [TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" },
! 83: [TTYC_DCH] = { TTYCODE_STRING, "dch" },
! 84: [TTYC_DCH1] = { TTYCODE_STRING, "dch1" },
! 85: [TTYC_DIM] = { TTYCODE_STRING, "dim" },
! 86: [TTYC_DL] = { TTYCODE_STRING, "dl" },
! 87: [TTYC_DL1] = { TTYCODE_STRING, "dl1" },
! 88: [TTYC_E3] = { TTYCODE_STRING, "E3" },
! 89: [TTYC_ECH] = { TTYCODE_STRING, "ech" },
! 90: [TTYC_ED] = { TTYCODE_STRING, "ed" },
! 91: [TTYC_EL] = { TTYCODE_STRING, "el" },
! 92: [TTYC_EL1] = { TTYCODE_STRING, "el1" },
! 93: [TTYC_ENACS] = { TTYCODE_STRING, "enacs" },
! 94: [TTYC_FSL] = { TTYCODE_STRING, "fsl" },
! 95: [TTYC_HOME] = { TTYCODE_STRING, "home" },
! 96: [TTYC_HPA] = { TTYCODE_STRING, "hpa" },
! 97: [TTYC_ICH] = { TTYCODE_STRING, "ich" },
! 98: [TTYC_ICH1] = { TTYCODE_STRING, "ich1" },
! 99: [TTYC_IL] = { TTYCODE_STRING, "il" },
! 100: [TTYC_IL1] = { TTYCODE_STRING, "il1" },
! 101: [TTYC_INDN] = { TTYCODE_STRING, "indn" },
! 102: [TTYC_INVIS] = { TTYCODE_STRING, "invis" },
! 103: [TTYC_KCBT] = { TTYCODE_STRING, "kcbt" },
! 104: [TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" },
! 105: [TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" },
! 106: [TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" },
! 107: [TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" },
! 108: [TTYC_KDC2] = { TTYCODE_STRING, "kDC" },
! 109: [TTYC_KDC3] = { TTYCODE_STRING, "kDC3" },
! 110: [TTYC_KDC4] = { TTYCODE_STRING, "kDC4" },
! 111: [TTYC_KDC5] = { TTYCODE_STRING, "kDC5" },
! 112: [TTYC_KDC6] = { TTYCODE_STRING, "kDC6" },
! 113: [TTYC_KDC7] = { TTYCODE_STRING, "kDC7" },
! 114: [TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" },
! 115: [TTYC_KDN2] = { TTYCODE_STRING, "kDN" },
! 116: [TTYC_KDN3] = { TTYCODE_STRING, "kDN3" },
! 117: [TTYC_KDN4] = { TTYCODE_STRING, "kDN4" },
! 118: [TTYC_KDN5] = { TTYCODE_STRING, "kDN5" },
! 119: [TTYC_KDN6] = { TTYCODE_STRING, "kDN6" },
! 120: [TTYC_KDN7] = { TTYCODE_STRING, "kDN7" },
! 121: [TTYC_KEND] = { TTYCODE_STRING, "kend" },
! 122: [TTYC_KEND2] = { TTYCODE_STRING, "kEND" },
! 123: [TTYC_KEND3] = { TTYCODE_STRING, "kEND3" },
! 124: [TTYC_KEND4] = { TTYCODE_STRING, "kEND4" },
! 125: [TTYC_KEND5] = { TTYCODE_STRING, "kEND5" },
! 126: [TTYC_KEND6] = { TTYCODE_STRING, "kEND6" },
! 127: [TTYC_KEND7] = { TTYCODE_STRING, "kEND7" },
! 128: [TTYC_KF1] = { TTYCODE_STRING, "kf1" },
! 129: [TTYC_KF10] = { TTYCODE_STRING, "kf10" },
! 130: [TTYC_KF11] = { TTYCODE_STRING, "kf11" },
! 131: [TTYC_KF12] = { TTYCODE_STRING, "kf12" },
! 132: [TTYC_KF13] = { TTYCODE_STRING, "kf13" },
! 133: [TTYC_KF14] = { TTYCODE_STRING, "kf14" },
! 134: [TTYC_KF15] = { TTYCODE_STRING, "kf15" },
! 135: [TTYC_KF16] = { TTYCODE_STRING, "kf16" },
! 136: [TTYC_KF17] = { TTYCODE_STRING, "kf17" },
! 137: [TTYC_KF18] = { TTYCODE_STRING, "kf18" },
! 138: [TTYC_KF19] = { TTYCODE_STRING, "kf19" },
! 139: [TTYC_KF2] = { TTYCODE_STRING, "kf2" },
! 140: [TTYC_KF20] = { TTYCODE_STRING, "kf20" },
! 141: [TTYC_KF21] = { TTYCODE_STRING, "kf21" },
! 142: [TTYC_KF22] = { TTYCODE_STRING, "kf22" },
! 143: [TTYC_KF23] = { TTYCODE_STRING, "kf23" },
! 144: [TTYC_KF24] = { TTYCODE_STRING, "kf24" },
! 145: [TTYC_KF25] = { TTYCODE_STRING, "kf25" },
! 146: [TTYC_KF26] = { TTYCODE_STRING, "kf26" },
! 147: [TTYC_KF27] = { TTYCODE_STRING, "kf27" },
! 148: [TTYC_KF28] = { TTYCODE_STRING, "kf28" },
! 149: [TTYC_KF29] = { TTYCODE_STRING, "kf29" },
! 150: [TTYC_KF3] = { TTYCODE_STRING, "kf3" },
! 151: [TTYC_KF30] = { TTYCODE_STRING, "kf30" },
! 152: [TTYC_KF31] = { TTYCODE_STRING, "kf31" },
! 153: [TTYC_KF32] = { TTYCODE_STRING, "kf32" },
! 154: [TTYC_KF33] = { TTYCODE_STRING, "kf33" },
! 155: [TTYC_KF34] = { TTYCODE_STRING, "kf34" },
! 156: [TTYC_KF35] = { TTYCODE_STRING, "kf35" },
! 157: [TTYC_KF36] = { TTYCODE_STRING, "kf36" },
! 158: [TTYC_KF37] = { TTYCODE_STRING, "kf37" },
! 159: [TTYC_KF38] = { TTYCODE_STRING, "kf38" },
! 160: [TTYC_KF39] = { TTYCODE_STRING, "kf39" },
! 161: [TTYC_KF4] = { TTYCODE_STRING, "kf4" },
! 162: [TTYC_KF40] = { TTYCODE_STRING, "kf40" },
! 163: [TTYC_KF41] = { TTYCODE_STRING, "kf41" },
! 164: [TTYC_KF42] = { TTYCODE_STRING, "kf42" },
! 165: [TTYC_KF43] = { TTYCODE_STRING, "kf43" },
! 166: [TTYC_KF44] = { TTYCODE_STRING, "kf44" },
! 167: [TTYC_KF45] = { TTYCODE_STRING, "kf45" },
! 168: [TTYC_KF46] = { TTYCODE_STRING, "kf46" },
! 169: [TTYC_KF47] = { TTYCODE_STRING, "kf47" },
! 170: [TTYC_KF48] = { TTYCODE_STRING, "kf48" },
! 171: [TTYC_KF49] = { TTYCODE_STRING, "kf49" },
! 172: [TTYC_KF5] = { TTYCODE_STRING, "kf5" },
! 173: [TTYC_KF50] = { TTYCODE_STRING, "kf50" },
! 174: [TTYC_KF51] = { TTYCODE_STRING, "kf51" },
! 175: [TTYC_KF52] = { TTYCODE_STRING, "kf52" },
! 176: [TTYC_KF53] = { TTYCODE_STRING, "kf53" },
! 177: [TTYC_KF54] = { TTYCODE_STRING, "kf54" },
! 178: [TTYC_KF55] = { TTYCODE_STRING, "kf55" },
! 179: [TTYC_KF56] = { TTYCODE_STRING, "kf56" },
! 180: [TTYC_KF57] = { TTYCODE_STRING, "kf57" },
! 181: [TTYC_KF58] = { TTYCODE_STRING, "kf58" },
! 182: [TTYC_KF59] = { TTYCODE_STRING, "kf59" },
! 183: [TTYC_KF6] = { TTYCODE_STRING, "kf6" },
! 184: [TTYC_KF60] = { TTYCODE_STRING, "kf60" },
! 185: [TTYC_KF61] = { TTYCODE_STRING, "kf61" },
! 186: [TTYC_KF62] = { TTYCODE_STRING, "kf62" },
! 187: [TTYC_KF63] = { TTYCODE_STRING, "kf63" },
! 188: [TTYC_KF7] = { TTYCODE_STRING, "kf7" },
! 189: [TTYC_KF8] = { TTYCODE_STRING, "kf8" },
! 190: [TTYC_KF9] = { TTYCODE_STRING, "kf9" },
! 191: [TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" },
! 192: [TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" },
! 193: [TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" },
! 194: [TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" },
! 195: [TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" },
! 196: [TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" },
! 197: [TTYC_KHOME] = { TTYCODE_STRING, "khome" },
! 198: [TTYC_KIC2] = { TTYCODE_STRING, "kIC" },
! 199: [TTYC_KIC3] = { TTYCODE_STRING, "kIC3" },
! 200: [TTYC_KIC4] = { TTYCODE_STRING, "kIC4" },
! 201: [TTYC_KIC5] = { TTYCODE_STRING, "kIC5" },
! 202: [TTYC_KIC6] = { TTYCODE_STRING, "kIC6" },
! 203: [TTYC_KIC7] = { TTYCODE_STRING, "kIC7" },
! 204: [TTYC_KICH1] = { TTYCODE_STRING, "kich1" },
! 205: [TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" },
! 206: [TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" },
! 207: [TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" },
! 208: [TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" },
! 209: [TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" },
! 210: [TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" },
! 211: [TTYC_KMOUS] = { TTYCODE_STRING, "kmous" },
! 212: [TTYC_KNP] = { TTYCODE_STRING, "knp" },
! 213: [TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" },
! 214: [TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" },
! 215: [TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" },
! 216: [TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" },
! 217: [TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" },
! 218: [TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" },
! 219: [TTYC_KPP] = { TTYCODE_STRING, "kpp" },
! 220: [TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" },
! 221: [TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" },
! 222: [TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" },
! 223: [TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" },
! 224: [TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" },
! 225: [TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" },
! 226: [TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" },
! 227: [TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" },
! 228: [TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" },
! 229: [TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" },
! 230: [TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" },
! 231: [TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" },
! 232: [TTYC_KUP2] = { TTYCODE_STRING, "kUP" },
! 233: [TTYC_KUP3] = { TTYCODE_STRING, "kUP3" },
! 234: [TTYC_KUP4] = { TTYCODE_STRING, "kUP4" },
! 235: [TTYC_KUP5] = { TTYCODE_STRING, "kUP5" },
! 236: [TTYC_KUP6] = { TTYCODE_STRING, "kUP6" },
! 237: [TTYC_KUP7] = { TTYCODE_STRING, "kUP7" },
! 238: [TTYC_MS] = { TTYCODE_STRING, "Ms" },
! 239: [TTYC_OP] = { TTYCODE_STRING, "op" },
! 240: [TTYC_REV] = { TTYCODE_STRING, "rev" },
! 241: [TTYC_RI] = { TTYCODE_STRING, "ri" },
! 242: [TTYC_RMACS] = { TTYCODE_STRING, "rmacs" },
! 243: [TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" },
! 244: [TTYC_RMKX] = { TTYCODE_STRING, "rmkx" },
! 245: [TTYC_SE] = { TTYCODE_STRING, "Se" },
! 246: [TTYC_SETAB] = { TTYCODE_STRING, "setab" },
! 247: [TTYC_SETAF] = { TTYCODE_STRING, "setaf" },
! 248: [TTYC_SGR0] = { TTYCODE_STRING, "sgr0" },
! 249: [TTYC_SITM] = { TTYCODE_STRING, "sitm" },
! 250: [TTYC_SMACS] = { TTYCODE_STRING, "smacs" },
! 251: [TTYC_SMCUP] = { TTYCODE_STRING, "smcup" },
! 252: [TTYC_SMKX] = { TTYCODE_STRING, "smkx" },
! 253: [TTYC_SMSO] = { TTYCODE_STRING, "smso" },
! 254: [TTYC_SMUL] = { TTYCODE_STRING, "smul" },
! 255: [TTYC_SMXX] = { TTYCODE_STRING, "smxx" },
! 256: [TTYC_SS] = { TTYCODE_STRING, "Ss" },
! 257: [TTYC_TC] = { TTYCODE_FLAG, "Tc" },
! 258: [TTYC_TSL] = { TTYCODE_STRING, "tsl" },
! 259: [TTYC_VPA] = { TTYCODE_STRING, "vpa" },
! 260: [TTYC_XENL] = { TTYCODE_FLAG, "xenl" },
! 261: [TTYC_XT] = { TTYCODE_FLAG, "XT" },
! 262: };
! 263:
! 264: u_int
! 265: tty_term_ncodes(void)
! 266: {
! 267: return (nitems(tty_term_codes));
! 268: }
! 269:
! 270: static char *
! 271: tty_term_strip(const char *s)
! 272: {
! 273: const char *ptr;
! 274: static char buf[BUFSIZ];
! 275: size_t len;
! 276:
! 277: /* Ignore strings with no padding. */
! 278: if (strchr(s, '$') == NULL)
! 279: return (xstrdup(s));
! 280:
! 281: len = 0;
! 282: for (ptr = s; *ptr != '\0'; ptr++) {
! 283: if (*ptr == '$' && *(ptr + 1) == '<') {
! 284: while (*ptr != '\0' && *ptr != '>')
! 285: ptr++;
! 286: if (*ptr == '>')
! 287: ptr++;
! 288: }
! 289:
! 290: buf[len++] = *ptr;
! 291: if (len == (sizeof buf) - 1)
! 292: break;
! 293: }
! 294: buf[len] = '\0';
! 295:
! 296: return (xstrdup(buf));
! 297: }
! 298:
! 299: static void
! 300: tty_term_override(struct tty_term *term, const char *override)
! 301: {
! 302: const struct tty_term_code_entry *ent;
! 303: struct tty_code *code;
! 304: char *next, *s, *copy, *cp, *value;
! 305: const char *errstr;
! 306: u_int i;
! 307: int n, remove;
! 308:
! 309: copy = next = xstrdup(override);
! 310:
! 311: s = strsep(&next, ":");
! 312: if (s == NULL || next == NULL || fnmatch(s, term->name, 0) != 0) {
! 313: free(copy);
! 314: return;
! 315: }
! 316:
! 317: while ((s = strsep(&next, ":")) != NULL) {
! 318: if (*s == '\0')
! 319: continue;
! 320: value = NULL;
! 321:
! 322: remove = 0;
! 323: if ((cp = strchr(s, '=')) != NULL) {
! 324: *cp++ = '\0';
! 325: value = xstrdup(cp);
! 326: if (strunvis(value, cp) == -1) {
! 327: free(value);
! 328: value = xstrdup(cp);
! 329: }
! 330: } else if (s[strlen(s) - 1] == '@') {
! 331: s[strlen(s) - 1] = '\0';
! 332: remove = 1;
! 333: } else
! 334: value = xstrdup("");
! 335:
! 336: if (remove)
! 337: log_debug("%s override: %s@", term->name, s);
! 338: else
! 339: log_debug("%s override: %s=%s", term->name, s, value);
! 340:
! 341: for (i = 0; i < tty_term_ncodes(); i++) {
! 342: ent = &tty_term_codes[i];
! 343: if (strcmp(s, ent->name) != 0)
! 344: continue;
! 345: code = &term->codes[i];
! 346:
! 347: if (remove) {
! 348: code->type = TTYCODE_NONE;
! 349: continue;
! 350: }
! 351: switch (ent->type) {
! 352: case TTYCODE_NONE:
! 353: break;
! 354: case TTYCODE_STRING:
! 355: if (code->type == TTYCODE_STRING)
! 356: free(code->value.string);
! 357: code->value.string = xstrdup(value);
! 358: code->type = ent->type;
! 359: break;
! 360: case TTYCODE_NUMBER:
! 361: n = strtonum(value, 0, INT_MAX, &errstr);
! 362: if (errstr != NULL)
! 363: break;
! 364: code->value.number = n;
! 365: code->type = ent->type;
! 366: break;
! 367: case TTYCODE_FLAG:
! 368: code->value.flag = 1;
! 369: code->type = ent->type;
! 370: break;
! 371: }
! 372: }
! 373:
! 374: free(value);
! 375: }
! 376: free(s);
! 377: }
! 378:
! 379: struct tty_term *
! 380: tty_term_find(char *name, int fd, char **cause)
! 381: {
! 382: struct tty_term *term;
! 383: const struct tty_term_code_entry *ent;
! 384: struct tty_code *code;
! 385: struct options_entry *o;
! 386: u_int size, i;
! 387: int n, error;
! 388: const char *s, *acs;
! 389:
! 390: LIST_FOREACH(term, &tty_terms, entry) {
! 391: if (strcmp(term->name, name) == 0) {
! 392: term->references++;
! 393: return (term);
! 394: }
! 395: }
! 396:
! 397: log_debug("new term: %s", name);
! 398: term = xmalloc(sizeof *term);
! 399: term->name = xstrdup(name);
! 400: term->references = 1;
! 401: term->flags = 0;
! 402: term->codes = xcalloc(tty_term_ncodes(), sizeof *term->codes);
! 403: LIST_INSERT_HEAD(&tty_terms, term, entry);
! 404:
! 405: /* Set up curses terminal. */
! 406: if (setupterm(name, fd, &error) != OK) {
! 407: switch (error) {
! 408: case 1:
! 409: xasprintf(cause, "can't use hardcopy terminal: %s",
! 410: name);
! 411: break;
! 412: case 0:
! 413: xasprintf(cause, "missing or unsuitable terminal: %s",
! 414: name);
! 415: break;
! 416: case -1:
! 417: xasprintf(cause, "can't find terminfo database");
! 418: break;
! 419: default:
! 420: xasprintf(cause, "unknown error");
! 421: break;
! 422: }
! 423: goto error;
! 424: }
! 425:
! 426: /* Fill in codes. */
! 427: for (i = 0; i < tty_term_ncodes(); i++) {
! 428: ent = &tty_term_codes[i];
! 429:
! 430: code = &term->codes[i];
! 431: code->type = TTYCODE_NONE;
! 432: switch (ent->type) {
! 433: case TTYCODE_NONE:
! 434: break;
! 435: case TTYCODE_STRING:
! 436: s = tigetstr((char *) ent->name);
! 437: if (s == NULL || s == (char *) -1)
! 438: break;
! 439: code->type = TTYCODE_STRING;
! 440: code->value.string = tty_term_strip(s);
! 441: break;
! 442: case TTYCODE_NUMBER:
! 443: n = tigetnum((char *) ent->name);
! 444: if (n == -1 || n == -2)
! 445: break;
! 446: code->type = TTYCODE_NUMBER;
! 447: code->value.number = n;
! 448: break;
! 449: case TTYCODE_FLAG:
! 450: n = tigetflag((char *) ent->name);
! 451: if (n == -1)
! 452: break;
! 453: code->type = TTYCODE_FLAG;
! 454: code->value.flag = n;
! 455: break;
! 456: }
! 457: }
! 458:
! 459: /* Apply terminal overrides. */
! 460: o = options_get_only(global_options, "terminal-overrides");
! 461: if (options_array_size(o, &size) != -1) {
! 462: for (i = 0; i < size; i++) {
! 463: s = options_array_get(o, i);
! 464: if (s != NULL)
! 465: tty_term_override(term, s);
! 466: }
! 467: }
! 468:
! 469: /* Delete curses data. */
! 470: #if !defined(NCURSES_VERSION_MAJOR) || NCURSES_VERSION_MAJOR > 5 || \
! 471: (NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR > 6)
! 472: del_curterm(cur_term);
! 473: #endif
! 474:
! 475: /* These are always required. */
! 476: if (!tty_term_has(term, TTYC_CLEAR)) {
! 477: xasprintf(cause, "terminal does not support clear");
! 478: goto error;
! 479: }
! 480: if (!tty_term_has(term, TTYC_CUP)) {
! 481: xasprintf(cause, "terminal does not support cup");
! 482: goto error;
! 483: }
! 484:
! 485: /* These can be emulated so one of the two is required. */
! 486: if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
! 487: xasprintf(cause, "terminal does not support cud1 or cud");
! 488: goto error;
! 489: }
! 490:
! 491: /* Figure out if we have 256. */
! 492: if (tty_term_number(term, TTYC_COLORS) == 256)
! 493: term->flags |= TERM_256COLOURS;
! 494:
! 495: /*
! 496: * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
! 497: * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
! 498: *
! 499: * This is irritating, most notably because it is impossible to write
! 500: * to the very bottom-right of the screen without scrolling.
! 501: *
! 502: * Flag the terminal here and apply some workarounds in other places to
! 503: * do the best possible.
! 504: */
! 505: if (!tty_term_flag(term, TTYC_XENL))
! 506: term->flags |= TERM_EARLYWRAP;
! 507:
! 508: /* Generate ACS table. If none is present, use nearest ASCII. */
! 509: memset(term->acs, 0, sizeof term->acs);
! 510: if (tty_term_has(term, TTYC_ACSC))
! 511: acs = tty_term_string(term, TTYC_ACSC);
! 512: else
! 513: acs = "a#j+k+l+m+n+o-p-q-r-s-t+u+v+w+x|y<z>~.";
! 514: for (; acs[0] != '\0' && acs[1] != '\0'; acs += 2)
! 515: term->acs[(u_char) acs[0]][0] = acs[1];
! 516:
! 517: /* On terminals with xterm titles (XT), fill in tsl and fsl. */
! 518: if (tty_term_flag(term, TTYC_XT) &&
! 519: !tty_term_has(term, TTYC_TSL) &&
! 520: !tty_term_has(term, TTYC_FSL)) {
! 521: code = &term->codes[TTYC_TSL];
! 522: code->value.string = xstrdup("\033]0;");
! 523: code->type = TTYCODE_STRING;
! 524: code = &term->codes[TTYC_FSL];
! 525: code->value.string = xstrdup("\007");
! 526: code->type = TTYCODE_STRING;
! 527: }
! 528:
! 529: return (term);
! 530:
! 531: error:
! 532: tty_term_free(term);
! 533: return (NULL);
! 534: }
! 535:
! 536: void
! 537: tty_term_free(struct tty_term *term)
! 538: {
! 539: u_int i;
! 540:
! 541: if (--term->references != 0)
! 542: return;
! 543:
! 544: LIST_REMOVE(term, entry);
! 545:
! 546: for (i = 0; i < tty_term_ncodes(); i++) {
! 547: if (term->codes[i].type == TTYCODE_STRING)
! 548: free(term->codes[i].value.string);
! 549: }
! 550: free(term->codes);
! 551:
! 552: free(term->name);
! 553: free(term);
! 554: }
! 555:
! 556: int
! 557: tty_term_has(struct tty_term *term, enum tty_code_code code)
! 558: {
! 559: return (term->codes[code].type != TTYCODE_NONE);
! 560: }
! 561:
! 562: const char *
! 563: tty_term_string(struct tty_term *term, enum tty_code_code code)
! 564: {
! 565: if (!tty_term_has(term, code))
! 566: return ("");
! 567: if (term->codes[code].type != TTYCODE_STRING)
! 568: fatalx("not a string: %d", code);
! 569: return (term->codes[code].value.string);
! 570: }
! 571:
! 572: const char *
! 573: tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
! 574: {
! 575: return (tparm((char *) tty_term_string(term, code), a, 0, 0, 0, 0, 0, 0, 0, 0));
! 576: }
! 577:
! 578: const char *
! 579: tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
! 580: {
! 581: return (tparm((char *) tty_term_string(term, code), a, b, 0, 0, 0, 0, 0, 0, 0));
! 582: }
! 583:
! 584: const char *
! 585: tty_term_ptr1(struct tty_term *term, enum tty_code_code code, const void *a)
! 586: {
! 587: return (tparm((char *) tty_term_string(term, code), a, 0, 0, 0, 0, 0, 0, 0, 0));
! 588: }
! 589:
! 590: const char *
! 591: tty_term_ptr2(struct tty_term *term, enum tty_code_code code, const void *a,
! 592: const void *b)
! 593: {
! 594: return (tparm((char *) tty_term_string(term, code), a, b, 0, 0, 0, 0, 0, 0, 0));
! 595: }
! 596:
! 597: int
! 598: tty_term_number(struct tty_term *term, enum tty_code_code code)
! 599: {
! 600: if (!tty_term_has(term, code))
! 601: return (0);
! 602: if (term->codes[code].type != TTYCODE_NUMBER)
! 603: fatalx("not a number: %d", code);
! 604: return (term->codes[code].value.number);
! 605: }
! 606:
! 607: int
! 608: tty_term_flag(struct tty_term *term, enum tty_code_code code)
! 609: {
! 610: if (!tty_term_has(term, code))
! 611: return (0);
! 612: if (term->codes[code].type != TTYCODE_FLAG)
! 613: fatalx("not a flag: %d", code);
! 614: return (term->codes[code].value.flag);
! 615: }
! 616:
! 617: const char *
! 618: tty_term_describe(struct tty_term *term, enum tty_code_code code)
! 619: {
! 620: static char s[256];
! 621: char out[128];
! 622:
! 623: switch (term->codes[code].type) {
! 624: case TTYCODE_NONE:
! 625: xsnprintf(s, sizeof s, "%4u: %s: [missing]",
! 626: code, tty_term_codes[code].name);
! 627: break;
! 628: case TTYCODE_STRING:
! 629: strnvis(out, term->codes[code].value.string, sizeof out,
! 630: VIS_OCTAL|VIS_TAB|VIS_NL);
! 631: xsnprintf(s, sizeof s, "%4u: %s: (string) %s",
! 632: code, tty_term_codes[code].name,
! 633: out);
! 634: break;
! 635: case TTYCODE_NUMBER:
! 636: xsnprintf(s, sizeof s, "%4u: %s: (number) %d",
! 637: code, tty_term_codes[code].name,
! 638: term->codes[code].value.number);
! 639: break;
! 640: case TTYCODE_FLAG:
! 641: xsnprintf(s, sizeof s, "%4u: %s: (flag) %s",
! 642: code, tty_term_codes[code].name,
! 643: term->codes[code].value.flag ? "true" : "false");
! 644: break;
! 645: }
! 646: return (s);
! 647: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>