[BACK]Return to tty-term.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/tty-term.c, Revision 1.26

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