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

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