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

1.17    ! nicm        1: /* $OpenBSD: tty-term.c,v 1.16 2009/12/03 22:50:10 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] = {
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" },
                     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" },
1.13      nicm       78:        { TTYC_KDC2, TTYCODE_STRING, "kDC" },
                     79:        { TTYC_KDC3, TTYCODE_STRING, "kDC3" },
                     80:        { TTYC_KDC4, TTYCODE_STRING, "kDC4" },
                     81:        { TTYC_KDC5, TTYCODE_STRING, "kDC5" },
                     82:        { TTYC_KDC6, TTYCODE_STRING, "kDC6" },
                     83:        { TTYC_KDC7, TTYCODE_STRING, "kDC7" },
1.1       nicm       84:        { TTYC_KDCH1, TTYCODE_STRING, "kdch1" },
1.13      nicm       85:        { TTYC_KDN2, TTYCODE_STRING, "kDN" },
                     86:        { TTYC_KDN3, TTYCODE_STRING, "kDN3" },
                     87:        { TTYC_KDN4, TTYCODE_STRING, "kDN4" },
                     88:        { TTYC_KDN5, TTYCODE_STRING, "kDN5" },
                     89:        { TTYC_KDN6, TTYCODE_STRING, "kDN6" },
                     90:        { TTYC_KDN7, TTYCODE_STRING, "kDN7" },
1.1       nicm       91:        { TTYC_KEND, TTYCODE_STRING, "kend" },
1.13      nicm       92:        { TTYC_KEND2, TTYCODE_STRING, "kEND" },
                     93:        { TTYC_KEND3, TTYCODE_STRING, "kEND3" },
                     94:        { TTYC_KEND4, TTYCODE_STRING, "kEND4" },
                     95:        { TTYC_KEND5, TTYCODE_STRING, "kEND5" },
                     96:        { TTYC_KEND6, TTYCODE_STRING, "kEND6" },
                     97:        { TTYC_KEND7, TTYCODE_STRING, "kEND7" },
1.1       nicm       98:        { TTYC_KF1, TTYCODE_STRING, "kf1" },
                     99:        { TTYC_KF10, TTYCODE_STRING, "kf10" },
                    100:        { TTYC_KF11, TTYCODE_STRING, "kf11" },
                    101:        { TTYC_KF12, TTYCODE_STRING, "kf12" },
                    102:        { TTYC_KF13, TTYCODE_STRING, "kf13" },
                    103:        { TTYC_KF14, TTYCODE_STRING, "kf14" },
                    104:        { TTYC_KF15, TTYCODE_STRING, "kf15" },
                    105:        { TTYC_KF16, TTYCODE_STRING, "kf16" },
                    106:        { TTYC_KF17, TTYCODE_STRING, "kf17" },
                    107:        { TTYC_KF18, TTYCODE_STRING, "kf18" },
                    108:        { TTYC_KF19, TTYCODE_STRING, "kf19" },
1.10      nicm      109:        { TTYC_KF2, TTYCODE_STRING, "kf2" },
1.1       nicm      110:        { TTYC_KF20, TTYCODE_STRING, "kf20" },
                    111:        { TTYC_KF3, TTYCODE_STRING, "kf3" },
                    112:        { TTYC_KF4, TTYCODE_STRING, "kf4" },
                    113:        { TTYC_KF5, TTYCODE_STRING, "kf5" },
                    114:        { TTYC_KF6, TTYCODE_STRING, "kf6" },
                    115:        { TTYC_KF7, TTYCODE_STRING, "kf7" },
                    116:        { TTYC_KF8, TTYCODE_STRING, "kf8" },
                    117:        { TTYC_KF9, TTYCODE_STRING, "kf9" },
1.13      nicm      118:        { TTYC_KHOM2, TTYCODE_STRING, "kHOM" },
                    119:        { TTYC_KHOM3, TTYCODE_STRING, "kHOM3" },
                    120:        { TTYC_KHOM4, TTYCODE_STRING, "kHOM4" },
                    121:        { TTYC_KHOM5, TTYCODE_STRING, "kHOM5" },
                    122:        { TTYC_KHOM6, TTYCODE_STRING, "kHOM6" },
                    123:        { TTYC_KHOM7, TTYCODE_STRING, "kHOM7" },
1.1       nicm      124:        { TTYC_KHOME, TTYCODE_STRING, "khome" },
1.13      nicm      125:        { TTYC_KIC2, TTYCODE_STRING, "kIC" },
                    126:        { TTYC_KIC3, TTYCODE_STRING, "kIC3" },
                    127:        { TTYC_KIC4, TTYCODE_STRING, "kIC4" },
                    128:        { TTYC_KIC5, TTYCODE_STRING, "kIC5" },
                    129:        { TTYC_KIC6, TTYCODE_STRING, "kIC6" },
                    130:        { TTYC_KIC7, TTYCODE_STRING, "kIC7" },
1.1       nicm      131:        { TTYC_KICH1, TTYCODE_STRING, "kich1" },
1.13      nicm      132:        { TTYC_KLFT2, TTYCODE_STRING, "kLFT" },
                    133:        { TTYC_KLFT3, TTYCODE_STRING, "kLFT3" },
                    134:        { TTYC_KLFT4, TTYCODE_STRING, "kLFT4" },
                    135:        { TTYC_KLFT5, TTYCODE_STRING, "kLFT5" },
                    136:        { TTYC_KLFT6, TTYCODE_STRING, "kLFT6" },
                    137:        { TTYC_KLFT7, TTYCODE_STRING, "kLFT7" },
1.1       nicm      138:        { TTYC_KMOUS, TTYCODE_STRING, "kmous" },
                    139:        { TTYC_KNP, TTYCODE_STRING, "knp" },
1.13      nicm      140:        { TTYC_KNXT2, TTYCODE_STRING, "kNXT" },
                    141:        { TTYC_KNXT3, TTYCODE_STRING, "kNXT3" },
                    142:        { TTYC_KNXT4, TTYCODE_STRING, "kNXT4" },
                    143:        { TTYC_KNXT5, TTYCODE_STRING, "kNXT5" },
                    144:        { TTYC_KNXT6, TTYCODE_STRING, "kNXT6" },
                    145:        { TTYC_KNXT7, TTYCODE_STRING, "kNXT7" },
1.1       nicm      146:        { TTYC_KPP, TTYCODE_STRING, "kpp" },
1.13      nicm      147:        { TTYC_KPRV2, TTYCODE_STRING, "kPRV" },
                    148:        { TTYC_KPRV3, TTYCODE_STRING, "kPRV3" },
                    149:        { TTYC_KPRV4, TTYCODE_STRING, "kPRV4" },
                    150:        { TTYC_KPRV5, TTYCODE_STRING, "kPRV5" },
                    151:        { TTYC_KPRV6, TTYCODE_STRING, "kPRV6" },
                    152:        { TTYC_KPRV7, TTYCODE_STRING, "kPRV7" },
                    153:        { TTYC_KRIT2, TTYCODE_STRING, "kRIT" },
                    154:        { TTYC_KRIT3, TTYCODE_STRING, "kRIT3" },
                    155:        { TTYC_KRIT4, TTYCODE_STRING, "kRIT4" },
                    156:        { TTYC_KRIT5, TTYCODE_STRING, "kRIT5" },
                    157:        { TTYC_KRIT6, TTYCODE_STRING, "kRIT6" },
                    158:        { TTYC_KRIT7, TTYCODE_STRING, "kRIT7" },
                    159:        { TTYC_KUP2, TTYCODE_STRING, "kUP" },
                    160:        { TTYC_KUP3, TTYCODE_STRING, "kUP3" },
                    161:        { TTYC_KUP4, TTYCODE_STRING, "kUP4" },
                    162:        { TTYC_KUP5, TTYCODE_STRING, "kUP5" },
                    163:        { TTYC_KUP6, TTYCODE_STRING, "kUP6" },
                    164:        { TTYC_KUP7, TTYCODE_STRING, "kUP7" },
1.1       nicm      165:        { TTYC_OP, TTYCODE_STRING, "op" },
                    166:        { TTYC_REV, TTYCODE_STRING, "rev" },
                    167:        { TTYC_RI, TTYCODE_STRING, "ri" },
                    168:        { TTYC_RMACS, TTYCODE_STRING, "rmacs" },
                    169:        { TTYC_RMCUP, TTYCODE_STRING, "rmcup" },
                    170:        { TTYC_RMIR, TTYCODE_STRING, "rmir" },
                    171:        { TTYC_RMKX, TTYCODE_STRING, "rmkx" },
                    172:        { TTYC_SETAB, TTYCODE_STRING, "setab" },
                    173:        { TTYC_SETAF, TTYCODE_STRING, "setaf" },
                    174:        { TTYC_SGR0, TTYCODE_STRING, "sgr0" },
                    175:        { TTYC_SMACS, TTYCODE_STRING, "smacs" },
                    176:        { TTYC_SMCUP, TTYCODE_STRING, "smcup" },
                    177:        { TTYC_SMIR, TTYCODE_STRING, "smir" },
                    178:        { TTYC_SMKX, TTYCODE_STRING, "smkx" },
                    179:        { TTYC_SMSO, TTYCODE_STRING, "smso" },
                    180:        { TTYC_SMUL, TTYCODE_STRING, "smul" },
1.10      nicm      181:        { TTYC_VPA, TTYCODE_STRING, "vpa" },
1.1       nicm      182:        { TTYC_XENL, TTYCODE_FLAG, "xenl" },
                    183: };
                    184:
                    185: char *
                    186: tty_term_strip(const char *s)
                    187: {
                    188:        const char     *ptr;
                    189:        static char     buf[BUFSIZ];
                    190:        size_t          len;
                    191:
                    192:        /* Ignore strings with no padding. */
                    193:        if (strchr(s, '$') == NULL)
                    194:                return (xstrdup(s));
                    195:
                    196:        len = 0;
                    197:        for (ptr = s; *ptr != '\0'; ptr++) {
                    198:                if (*ptr == '$' && *(ptr + 1) == '<') {
                    199:                        while (*ptr != '\0' && *ptr != '>')
                    200:                                ptr++;
                    201:                        if (*ptr == '>')
                    202:                                ptr++;
                    203:                }
                    204:
                    205:                buf[len++] = *ptr;
                    206:                if (len == (sizeof buf) - 1)
                    207:                        break;
                    208:        }
                    209:        buf[len] = '\0';
                    210:
                    211:        return (xstrdup(buf));
                    212: }
                    213:
                    214: void
1.5       nicm      215: tty_term_override(struct tty_term *term, const char *overrides)
1.1       nicm      216: {
1.5       nicm      217:        struct tty_term_code_entry      *ent;
                    218:        struct tty_code                 *code;
                    219:        char                            *termnext, *termstr, *entnext, *entstr;
                    220:        char                            *s, *ptr, *val;
                    221:        const char                      *errstr;
                    222:        u_int                            i;
                    223:        int                              n, removeflag;
                    224:
                    225:        s = xstrdup(overrides);
                    226:
                    227:        termnext = s;
                    228:        while ((termstr = strsep(&termnext, ",")) != NULL) {
                    229:                entnext = termstr;
1.16      nicm      230:
1.5       nicm      231:                entstr = strsep(&entnext, ":");
                    232:                if (entstr == NULL || entnext == NULL)
                    233:                        continue;
                    234:                if (fnmatch(entstr, term->name, 0) != 0)
                    235:                        continue;
                    236:                while ((entstr = strsep(&entnext, ":")) != NULL) {
                    237:                        if (*entstr == '\0')
                    238:                                continue;
                    239:
                    240:                        val = NULL;
                    241:                        removeflag = 0;
1.16      nicm      242:                        if ((ptr = strchr(entstr, '=')) != NULL) {
1.5       nicm      243:                                *ptr++ = '\0';
                    244:                                val = xstrdup(ptr);
1.9       nicm      245:                                if (strunvis(val, ptr) == -1) {
1.5       nicm      246:                                        xfree(val);
                    247:                                        val = xstrdup(ptr);
                    248:                                }
                    249:                        } else if (entstr[strlen(entstr) - 1] == '@') {
                    250:                                entstr[strlen(entstr) - 1] = '\0';
                    251:                                removeflag = 1;
1.17    ! nicm      252:                        } else
        !           253:                                continue;
1.5       nicm      254:
                    255:                        for (i = 0; i < NTTYCODE; i++) {
                    256:                                ent = &tty_term_codes[i];
                    257:                                if (strcmp(entstr, ent->name) != 0)
                    258:                                        continue;
                    259:                                code = &term->codes[ent->code];
                    260:
                    261:                                if (removeflag) {
                    262:                                        code->type = TTYCODE_NONE;
                    263:                                        continue;
                    264:                                }
                    265:                                switch (ent->type) {
                    266:                                case TTYCODE_NONE:
                    267:                                        break;
                    268:                                case TTYCODE_STRING:
1.6       nicm      269:                                        if (code->type == TTYCODE_STRING)
                    270:                                                xfree(code->value.string);
1.5       nicm      271:                                        code->value.string = xstrdup(val);
                    272:                                        code->type = ent->type;
                    273:                                        break;
                    274:                                case TTYCODE_NUMBER:
                    275:                                        n = strtonum(val, 0, INT_MAX, &errstr);
                    276:                                        if (errstr != NULL)
                    277:                                                break;
                    278:                                        code->value.number = n;
                    279:                                        code->type = ent->type;
                    280:                                        break;
                    281:                                case TTYCODE_FLAG:
                    282:                                        code->value.flag = 1;
                    283:                                        code->type = ent->type;
                    284:                                        break;
                    285:                                }
                    286:                        }
                    287:
                    288:                        if (val != NULL)
                    289:                                xfree(val);
1.1       nicm      290:                }
                    291:        }
                    292:
1.5       nicm      293:        xfree(s);
1.1       nicm      294: }
                    295:
                    296: struct tty_term *
1.5       nicm      297: tty_term_find(char *name, int fd, const char *overrides, char **cause)
1.1       nicm      298: {
                    299:        struct tty_term                 *term;
                    300:        struct tty_term_code_entry      *ent;
                    301:        struct tty_code                 *code;
                    302:        u_int                            i;
                    303:        int                              n, error;
                    304:        char                            *s;
                    305:
                    306:        SLIST_FOREACH(term, &tty_terms, entry) {
                    307:                if (strcmp(term->name, name) == 0) {
                    308:                        term->references++;
                    309:                        return (term);
                    310:                }
                    311:        }
                    312:
                    313:        log_debug("new term: %s", name);
                    314:        term = xmalloc(sizeof *term);
                    315:        term->name = xstrdup(name);
                    316:        term->references = 1;
                    317:        term->flags = 0;
1.7       nicm      318:        memset(&term->codes, 0, sizeof term->codes);
1.1       nicm      319:        SLIST_INSERT_HEAD(&tty_terms, term, entry);
                    320:
1.3       nicm      321:        /* Set up curses terminal. */
1.1       nicm      322:        if (setupterm(name, fd, &error) != OK) {
                    323:                switch (error) {
1.4       matthieu  324:                case 1:
1.16      nicm      325:                        xasprintf(
                    326:                            cause, "can't use hardcopy terminal: %s", name);
1.4       matthieu  327:                        break;
1.1       nicm      328:                case 0:
1.16      nicm      329:                        xasprintf(
                    330:                            cause, "missing or unsuitable terminal: %s", name);
1.1       nicm      331:                        break;
1.4       matthieu  332:                case -1:
1.1       nicm      333:                        xasprintf(cause, "can't find terminfo database");
                    334:                        break;
                    335:                default:
                    336:                        xasprintf(cause, "unknown error");
                    337:                        break;
                    338:                }
                    339:                goto error;
                    340:        }
                    341:
                    342:        /* Fill in codes. */
                    343:        for (i = 0; i < NTTYCODE; i++) {
                    344:                ent = &tty_term_codes[i];
                    345:
                    346:                code = &term->codes[ent->code];
                    347:                code->type = TTYCODE_NONE;
                    348:                switch (ent->type) {
                    349:                case TTYCODE_NONE:
                    350:                        break;
                    351:                case TTYCODE_STRING:
                    352:                        s = tigetstr((char *) ent->name);
                    353:                        if (s == NULL || s == (char *) -1)
                    354:                                break;
                    355:                        code->type = TTYCODE_STRING;
                    356:                        code->value.string = tty_term_strip(s);
                    357:                        break;
                    358:                case TTYCODE_NUMBER:
                    359:                        n = tigetnum((char *) ent->name);
                    360:                        if (n == -1 || n == -2)
                    361:                                break;
                    362:                        code->type = TTYCODE_NUMBER;
                    363:                        code->value.number = n;
                    364:                        break;
                    365:                case TTYCODE_FLAG:
                    366:                        n = tigetflag((char *) ent->name);
                    367:                        if (n == -1)
                    368:                                break;
                    369:                        code->type = TTYCODE_FLAG;
                    370:                        code->value.number = n;
                    371:                        break;
                    372:                }
                    373:        }
1.5       nicm      374:        tty_term_override(term, overrides);
1.1       nicm      375:
1.3       nicm      376:        /* Delete curses data. */
1.1       nicm      377:        del_curterm(cur_term);
                    378:
                    379:        /* These are always required. */
                    380:        if (!tty_term_has(term, TTYC_CLEAR)) {
                    381:                xasprintf(cause, "terminal does not support clear");
                    382:                goto error;
                    383:        }
                    384:        if (!tty_term_has(term, TTYC_CUP)) {
                    385:                xasprintf(cause, "terminal does not support cup");
                    386:                goto error;
                    387:        }
                    388:
                    389:        /* These can be emulated so one of the two is required. */
                    390:        if (!tty_term_has(term, TTYC_CUD1) && !tty_term_has(term, TTYC_CUD)) {
                    391:                xasprintf(cause, "terminal does not support cud1 or cud");
                    392:                goto error;
                    393:        }
                    394:
1.11      nicm      395:        /* Figure out if we have 256 or 88 colours. */
1.1       nicm      396:        if (tty_term_number(term, TTYC_COLORS) == 256)
                    397:                term->flags |= TERM_256COLOURS;
                    398:        if (tty_term_number(term, TTYC_COLORS) == 88)
                    399:                term->flags |= TERM_88COLOURS;
                    400:
                    401:        /*
                    402:         * Terminals without xenl (eat newline glitch) wrap at at $COLUMNS - 1
                    403:         * rather than $COLUMNS (the cursor can never be beyond $COLUMNS - 1).
                    404:         *
                    405:         * This is irritating, most notably because it is impossible to write
                    406:         * to the very bottom-right of the screen without scrolling.
                    407:         *
                    408:         * Flag the terminal here and apply some workarounds in other places to
                    409:         * do the best possible.
                    410:         */
                    411:        if (!tty_term_flag(term, TTYC_XENL))
                    412:                term->flags |= TERM_EARLYWRAP;
                    413:
                    414:        return (term);
                    415:
                    416: error:
                    417:        tty_term_free(term);
                    418:        return (NULL);
                    419: }
                    420:
                    421: void
                    422: tty_term_free(struct tty_term *term)
                    423: {
                    424:        u_int   i;
                    425:
                    426:        if (--term->references != 0)
                    427:                return;
                    428:
                    429:        SLIST_REMOVE(&tty_terms, term, tty_term, entry);
                    430:
                    431:        for (i = 0; i < NTTYCODE; i++) {
                    432:                if (term->codes[i].type == TTYCODE_STRING)
                    433:                        xfree(term->codes[i].value.string);
                    434:        }
                    435:        xfree(term->name);
                    436:        xfree(term);
                    437: }
                    438:
                    439: int
                    440: tty_term_has(struct tty_term *term, enum tty_code_code code)
                    441: {
                    442:        return (term->codes[code].type != TTYCODE_NONE);
                    443: }
                    444:
                    445: const char *
                    446: tty_term_string(struct tty_term *term, enum tty_code_code code)
                    447: {
                    448:        if (!tty_term_has(term, code))
                    449:                return ("");
                    450:        if (term->codes[code].type != TTYCODE_STRING)
                    451:                log_fatalx("not a string: %d", code);
                    452:        return (term->codes[code].value.string);
                    453: }
                    454:
1.3       nicm      455: /* No vtparm. Fucking curses. */
1.1       nicm      456: const char *
                    457: tty_term_string1(struct tty_term *term, enum tty_code_code code, int a)
                    458: {
                    459:        return (tparm((char *) tty_term_string(term, code), a));
                    460: }
                    461:
                    462: const char *
                    463: tty_term_string2(struct tty_term *term, enum tty_code_code code, int a, int b)
                    464: {
                    465:        return (tparm((char *) tty_term_string(term, code), a, b));
                    466: }
                    467:
                    468: int
                    469: tty_term_number(struct tty_term *term, enum tty_code_code code)
                    470: {
                    471:        if (!tty_term_has(term, code))
                    472:                return (0);
                    473:        if (term->codes[code].type != TTYCODE_NUMBER)
                    474:                log_fatalx("not a number: %d", code);
                    475:        return (term->codes[code].value.number);
                    476: }
                    477:
                    478: int
                    479: tty_term_flag(struct tty_term *term, enum tty_code_code code)
                    480: {
                    481:        if (!tty_term_has(term, code))
                    482:                return (0);
                    483:        if (term->codes[code].type != TTYCODE_FLAG)
                    484:                log_fatalx("not a flag: %d", code);
                    485:        return (term->codes[code].value.flag);
                    486: }