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

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