[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.11

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