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

1.8     ! nicm        1: /* $OpenBSD: tty-term.c,v 1.7 2009/08/05 19:05:02 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" },
                     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:
1.6       nicm      199:                                        if (code->type == TTYCODE_STRING)
                    200:                                                xfree(code->value.string);
1.5       nicm      201:                                        code->value.string = xstrdup(val);
                    202:                                        code->type = ent->type;
                    203:                                        break;
                    204:                                case TTYCODE_NUMBER:
                    205:                                        n = strtonum(val, 0, INT_MAX, &errstr);
                    206:                                        if (errstr != NULL)
                    207:                                                break;
                    208:                                        code->value.number = n;
                    209:                                        code->type = ent->type;
                    210:                                        break;
                    211:                                case TTYCODE_FLAG:
                    212:                                        code->value.flag = 1;
                    213:                                        code->type = ent->type;
                    214:                                        break;
                    215:                                }
                    216:                        }
                    217:
                    218:                        if (val != NULL)
                    219:                                xfree(val);
1.1       nicm      220:                }
                    221:        }
                    222:
1.5       nicm      223:        xfree(s);
1.1       nicm      224: }
                    225:
                    226: struct tty_term *
1.5       nicm      227: tty_term_find(char *name, int fd, const char *overrides, char **cause)
1.1       nicm      228: {
                    229:        struct tty_term                 *term;
                    230:        struct tty_term_code_entry      *ent;
                    231:        struct tty_code                 *code;
                    232:        u_int                            i;
                    233:        int                              n, error;
                    234:        char                            *s;
                    235:
                    236:        SLIST_FOREACH(term, &tty_terms, entry) {
                    237:                if (strcmp(term->name, name) == 0) {
                    238:                        term->references++;
                    239:                        return (term);
                    240:                }
                    241:        }
                    242:
                    243:        log_debug("new term: %s", name);
                    244:        term = xmalloc(sizeof *term);
                    245:        term->name = xstrdup(name);
                    246:        term->references = 1;
                    247:        term->flags = 0;
1.7       nicm      248:        memset(&term->codes, 0, sizeof term->codes);
1.1       nicm      249:        SLIST_INSERT_HEAD(&tty_terms, term, entry);
                    250:
1.3       nicm      251:        /* Set up curses terminal. */
1.1       nicm      252:        if (setupterm(name, fd, &error) != OK) {
                    253:                switch (error) {
1.4       matthieu  254:                case 1:
                    255:                        xasprintf(cause, "can't use hardcopy terminal: %s", name);
                    256:                        break;
1.1       nicm      257:                case 0:
1.4       matthieu  258:                        xasprintf(cause, "missing or unsuitable terminal: %s", name);
1.1       nicm      259:                        break;
1.4       matthieu  260:                case -1:
1.1       nicm      261:                        xasprintf(cause, "can't find terminfo database");
                    262:                        break;
                    263:                default:
                    264:                        xasprintf(cause, "unknown error");
                    265:                        break;
                    266:                }
                    267:                goto error;
                    268:        }
                    269:
                    270:        /* Fill in codes. */
                    271:        for (i = 0; i < NTTYCODE; i++) {
                    272:                ent = &tty_term_codes[i];
                    273:
                    274:                code = &term->codes[ent->code];
                    275:                code->type = TTYCODE_NONE;
                    276:                switch (ent->type) {
                    277:                case TTYCODE_NONE:
                    278:                        break;
                    279:                case TTYCODE_STRING:
                    280:                        s = tigetstr((char *) ent->name);
                    281:                        if (s == NULL || s == (char *) -1)
                    282:                                break;
                    283:                        code->type = TTYCODE_STRING;
                    284:                        code->value.string = tty_term_strip(s);
                    285:                        break;
                    286:                case TTYCODE_NUMBER:
                    287:                        n = tigetnum((char *) ent->name);
                    288:                        if (n == -1 || n == -2)
                    289:                                break;
                    290:                        code->type = TTYCODE_NUMBER;
                    291:                        code->value.number = n;
                    292:                        break;
                    293:                case TTYCODE_FLAG:
                    294:                        n = tigetflag((char *) ent->name);
                    295:                        if (n == -1)
                    296:                                break;
                    297:                        code->type = TTYCODE_FLAG;
                    298:                        code->value.number = n;
                    299:                        break;
                    300:                }
                    301:        }
1.5       nicm      302:        tty_term_override(term, overrides);
1.1       nicm      303:
1.3       nicm      304:        /* Delete curses data. */
1.1       nicm      305:        del_curterm(cur_term);
                    306:
                    307:        /* These are always required. */
                    308:        if (!tty_term_has(term, TTYC_CLEAR)) {
                    309:                xasprintf(cause, "terminal does not support clear");
                    310:                goto error;
                    311:        }
                    312:        if (!tty_term_has(term, TTYC_RI)) {
                    313:                xasprintf(cause, "terminal does not support ri");
                    314:                goto error;
                    315:        }
                    316:        if (!tty_term_has(term, TTYC_CUP)) {
                    317:                xasprintf(cause, "terminal does not support cup");
                    318:                goto error;
                    319:        }
                    320:
                    321:        /* These can be emulated so one of the two is required. */
                    322:        if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
                    323:                xasprintf(cause, "terminal does not support cud1 or cud");
                    324:                goto error;
                    325:        }
                    326:        if (!tty_term_has(term, TTYC_IL1) && !tty_term_has(term, TTYC_IL)) {
                    327:                xasprintf(cause, "terminal does not support il1 or il");
                    328:                goto error;
                    329:        }
                    330:        if (!tty_term_has(term, TTYC_DL1) && !tty_term_has(term, TTYC_DL)) {
                    331:                xasprintf(cause, "terminal does not support dl1 or dl");
                    332:                goto error;
                    333:        }
                    334:        if (!tty_term_has(term, TTYC_ICH1) &&
                    335:            !tty_term_has(term, TTYC_ICH) && (!tty_term_has(term, TTYC_SMIR) ||
                    336:            !tty_term_has(term, TTYC_RMIR))) {
                    337:                xasprintf(cause,
                    338:                    "terminal does not support ich1 or ich or smir and rmir");
                    339:                goto error;
                    340:        }
                    341:
                    342:        /*
                    343:         * Figure out if terminal support default colours. AX is a screen
                    344:         * extension which indicates this. Also check if op (orig_pair) uses
                    345:         * the default colours - if it does, this is a good indication the
                    346:         * terminal supports them.
                    347:         */
                    348:        if (tty_term_flag(term, TTYC_AX))
                    349:                term->flags |= TERM_HASDEFAULTS;
                    350:        if (strcmp(tty_term_string(term, TTYC_OP), "\033[39;49m") == 0)
                    351:                term->flags |= TERM_HASDEFAULTS;
                    352:
                    353:        /*
                    354:         * Try to figure out if we have 256 or 88 colours. The standard xterm
                    355:         * definitions are broken (well, or the way they are parsed is: in any
                    356:         * case they end up returning 8). So also do a hack.
                    357:         */
                    358:        if (tty_term_number(term, TTYC_COLORS) == 256)
                    359:                term->flags |= TERM_256COLOURS;
                    360:        if (tty_term_number(term, TTYC_COLORS) == 88)
                    361:                term->flags |= TERM_88COLOURS;
                    362:
                    363:        /*
                    364:         * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
                    365:         * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
                    366:         *
                    367:         * This is irritating, most notably because it is impossible to write
                    368:         * to the very bottom-right of the screen without scrolling.
                    369:         *
                    370:         * Flag the terminal here and apply some workarounds in other places to
                    371:         * do the best possible.
                    372:         */
                    373:        if (!tty_term_flag(term, TTYC_XENL))
                    374:                term->flags |= TERM_EARLYWRAP;
                    375:
                    376:        return (term);
                    377:
                    378: error:
                    379:        tty_term_free(term);
                    380:        return (NULL);
                    381: }
                    382:
                    383: void
                    384: tty_term_free(struct tty_term *term)
                    385: {
                    386:        u_int   i;
                    387:
                    388:        if (--term->references != 0)
                    389:                return;
                    390:
                    391:        SLIST_REMOVE(&tty_terms, term, tty_term, entry);
                    392:
                    393:        for (i = 0; i < NTTYCODE; i++) {
                    394:                if (term->codes[i].type == TTYCODE_STRING)
                    395:                        xfree(term->codes[i].value.string);
                    396:        }
                    397:        xfree(term->name);
                    398:        xfree(term);
                    399: }
                    400:
                    401: int
                    402: tty_term_has(struct tty_term *term, enum tty_code_code code)
                    403: {
                    404:        return (term->codes[code].type != TTYCODE_NONE);
                    405: }
                    406:
                    407: const char *
                    408: tty_term_string(struct tty_term *term, enum tty_code_code code)
                    409: {
                    410:        if (!tty_term_has(term, code))
                    411:                return ("");
                    412:        if (term->codes[code].type != TTYCODE_STRING)
                    413:                log_fatalx("not a string: %d", code);
                    414:        return (term->codes[code].value.string);
                    415: }
                    416:
1.3       nicm      417: /* No vtparm. Fucking curses. */
1.1       nicm      418: const char *
                    419: tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
                    420: {
                    421:        return (tparm((char *) tty_term_string(term, code), a));
                    422: }
                    423:
                    424: const char *
                    425: tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
                    426: {
                    427:        return (tparm((char *) tty_term_string(term, code), a, b));
                    428: }
                    429:
                    430: int
                    431: tty_term_number(struct tty_term *term, enum tty_code_code code)
                    432: {
                    433:        if (!tty_term_has(term, code))
                    434:                return (0);
                    435:        if (term->codes[code].type != TTYCODE_NUMBER)
                    436:                log_fatalx("not a number: %d", code);
                    437:        return (term->codes[code].value.number);
                    438: }
                    439:
                    440: int
                    441: tty_term_flag(struct tty_term *term, enum tty_code_code code)
                    442: {
                    443:        if (!tty_term_has(term, code))
                    444:                return (0);
                    445:        if (term->codes[code].type != TTYCODE_FLAG)
                    446:                log_fatalx("not a flag: %d", code);
                    447:        return (term->codes[code].value.flag);
                    448: }