[BACK]Return to input.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/input.c, Revision 1.14

1.14    ! nicm        1: /* $OpenBSD: input.c,v 1.13 2009/08/18 21:41:13 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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:
                     21: #include <stdint.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: #define INPUT_C0CONTROL(ch)    (ch <= 0x1f)
                     28: #define INPUT_INTERMEDIATE(ch) (ch == 0xa0 || (ch >= 0x20 && ch <= 0x2f))
                     29: #define INPUT_PARAMETER(ch)    (ch >= 0x30 && ch <= 0x3f)
                     30: #define INPUT_UPPERCASE(ch)    (ch >= 0x40 && ch <= 0x5f)
                     31: #define INPUT_LOWERCASE(ch)    (ch >= 0x60 && ch <= 0x7e)
                     32: #define INPUT_DELETE(ch)       (ch == 0x7f)
                     33: #define INPUT_C1CONTROL(ch)    (ch >= 0x80 && ch <= 0x9f)
                     34: #define INPUT_G1DISPLAYABLE(ch)        (ch >= 0xa1 && ch <= 0xfe)
                     35: #define INPUT_SPECIAL(ch)      (ch == 0xff)
                     36:
                     37: int     input_get_argument(struct input_ctx *, u_int, uint16_t *, uint16_t);
1.10      nicm       38: void    input_new_argument(struct input_ctx *);
1.1       nicm       39: int     input_add_argument(struct input_ctx *, u_char);
                     40:
                     41: void    input_start_string(struct input_ctx *, int);
                     42: void    input_abort_string(struct input_ctx *);
                     43: int     input_add_string(struct input_ctx *, u_char);
                     44: char   *input_get_string(struct input_ctx *);
                     45:
                     46: void    input_state(struct input_ctx *, void *);
                     47:
                     48: void    input_state_first(u_char, struct input_ctx *);
                     49: void    input_state_escape(u_char, struct input_ctx *);
                     50: void    input_state_intermediate(u_char, struct input_ctx *);
                     51: void    input_state_sequence_first(u_char, struct input_ctx *);
                     52: void    input_state_sequence_next(u_char, struct input_ctx *);
                     53: void    input_state_sequence_intermediate(u_char, struct input_ctx *);
                     54: void    input_state_string_next(u_char, struct input_ctx *);
                     55: void    input_state_string_escape(u_char, struct input_ctx *);
                     56: void    input_state_utf8(u_char, struct input_ctx *);
                     57:
                     58: void    input_handle_character(u_char, struct input_ctx *);
                     59: void    input_handle_c0_control(u_char, struct input_ctx *);
                     60: void    input_handle_c1_control(u_char, struct input_ctx *);
                     61: void    input_handle_private_two(u_char, struct input_ctx *);
                     62: void    input_handle_standard_two(u_char, struct input_ctx *);
                     63: void    input_handle_sequence(u_char, struct input_ctx *);
                     64:
                     65: void    input_handle_sequence_cuu(struct input_ctx *);
                     66: void    input_handle_sequence_cud(struct input_ctx *);
                     67: void    input_handle_sequence_cuf(struct input_ctx *);
                     68: void    input_handle_sequence_cub(struct input_ctx *);
                     69: void    input_handle_sequence_dch(struct input_ctx *);
1.8       nicm       70: void    input_handle_sequence_cbt(struct input_ctx *);
1.14    ! nicm       71: void    input_handle_sequence_da(struct input_ctx *);
1.1       nicm       72: void    input_handle_sequence_dl(struct input_ctx *);
                     73: void    input_handle_sequence_ich(struct input_ctx *);
                     74: void    input_handle_sequence_il(struct input_ctx *);
                     75: void    input_handle_sequence_vpa(struct input_ctx *);
                     76: void    input_handle_sequence_hpa(struct input_ctx *);
                     77: void    input_handle_sequence_cup(struct input_ctx *);
                     78: void    input_handle_sequence_cup(struct input_ctx *);
1.7       nicm       79: void    input_handle_sequence_tbc(struct input_ctx *);
1.1       nicm       80: void    input_handle_sequence_ed(struct input_ctx *);
                     81: void    input_handle_sequence_el(struct input_ctx *);
                     82: void    input_handle_sequence_sm(struct input_ctx *);
                     83: void    input_handle_sequence_rm(struct input_ctx *);
                     84: void    input_handle_sequence_decstbm(struct input_ctx *);
                     85: void    input_handle_sequence_sgr(struct input_ctx *);
                     86: void    input_handle_sequence_dsr(struct input_ctx *);
                     87:
                     88: int     input_sequence_cmp(const void *, const void *);
                     89:
                     90: struct input_sequence_entry {
                     91:        u_char  ch;
                     92:        void    (*fn)(struct input_ctx *);
                     93: };
                     94: const struct input_sequence_entry input_sequence_table[] = {
                     95:        { '@', input_handle_sequence_ich },
                     96:        { 'A', input_handle_sequence_cuu },
                     97:        { 'B', input_handle_sequence_cud },
                     98:        { 'C', input_handle_sequence_cuf },
                     99:        { 'D', input_handle_sequence_cub },
                    100:        { 'G', input_handle_sequence_hpa },
                    101:        { 'H', input_handle_sequence_cup },
                    102:        { 'J', input_handle_sequence_ed },
                    103:        { 'K', input_handle_sequence_el },
                    104:        { 'L', input_handle_sequence_il },
                    105:        { 'M', input_handle_sequence_dl },
                    106:        { 'P', input_handle_sequence_dch },
1.8       nicm      107:        { 'Z', input_handle_sequence_cbt },
1.14    ! nicm      108:        { 'c', input_handle_sequence_da },
1.1       nicm      109:        { 'd', input_handle_sequence_vpa },
                    110:        { 'f', input_handle_sequence_cup },
1.7       nicm      111:        { 'g', input_handle_sequence_tbc },
1.1       nicm      112:        { 'h', input_handle_sequence_sm },
                    113:        { 'l', input_handle_sequence_rm },
                    114:        { 'm', input_handle_sequence_sgr },
                    115:        { 'n', input_handle_sequence_dsr },
                    116:        { 'r', input_handle_sequence_decstbm },
                    117: };
                    118:
                    119: int
                    120: input_sequence_cmp(const void *a, const void *b)
                    121: {
                    122:        int     ai = ((const struct input_sequence_entry *) a)->ch;
                    123:        int     bi = ((const struct input_sequence_entry *) b)->ch;
                    124:
                    125:        return (ai - bi);
                    126: }
                    127:
1.10      nicm      128: void
1.1       nicm      129: input_new_argument(struct input_ctx *ictx)
                    130: {
                    131:        struct input_arg       *arg;
                    132:
                    133:        ARRAY_EXPAND(&ictx->args, 1);
                    134:
                    135:        arg = &ARRAY_LAST(&ictx->args);
                    136:        arg->used = 0;
                    137: }
                    138:
                    139: int
                    140: input_add_argument(struct input_ctx *ictx, u_char ch)
                    141: {
                    142:        struct input_arg       *arg;
                    143:
                    144:        if (ARRAY_LENGTH(&ictx->args) == 0)
                    145:                return (0);
                    146:
                    147:        arg = &ARRAY_LAST(&ictx->args);
                    148:        if (arg->used > (sizeof arg->data) - 1)
                    149:                return (-1);
                    150:        arg->data[arg->used++] = ch;
                    151:
                    152:        return (0);
                    153: }
                    154:
                    155: int
                    156: input_get_argument(struct input_ctx *ictx, u_int i, uint16_t *n, uint16_t d)
                    157: {
                    158:        struct input_arg        *arg;
                    159:        const char              *errstr;
                    160:
                    161:        *n = d;
                    162:        if (i >= ARRAY_LENGTH(&ictx->args))
                    163:                return (0);
                    164:
                    165:        arg = &ARRAY_ITEM(&ictx->args, i);
                    166:        if (*arg->data == '\0')
                    167:                return (0);
                    168:
                    169:        *n = strtonum(arg->data, 0, UINT16_MAX, &errstr);
                    170:        if (errstr != NULL)
                    171:                return (-1);
                    172:        return (0);
                    173: }
                    174:
                    175: void
                    176: input_start_string(struct input_ctx *ictx, int type)
                    177: {
                    178:        ictx->string_type = type;
                    179:        ictx->string_len = 0;
                    180: }
                    181:
                    182: void
                    183: input_abort_string(struct input_ctx *ictx)
                    184: {
                    185:        if (ictx->string_buf != NULL)
                    186:                xfree(ictx->string_buf);
                    187:        ictx->string_buf = NULL;
                    188: }
                    189:
                    190: int
                    191: input_add_string(struct input_ctx *ictx, u_char ch)
                    192: {
                    193:        ictx->string_buf = xrealloc(ictx->string_buf, 1, ictx->string_len + 1);
                    194:        ictx->string_buf[ictx->string_len++] = ch;
                    195:
                    196:        if (ictx->string_len >= MAXSTRINGLEN) {
                    197:                input_abort_string(ictx);
                    198:                return (1);
                    199:        }
                    200:
                    201:        return (0);
                    202: }
                    203:
                    204: char *
                    205: input_get_string(struct input_ctx *ictx)
                    206: {
                    207:        char    *s;
                    208:
                    209:        if (ictx->string_buf == NULL || input_add_string(ictx, '\0') != 0)
                    210:                return (xstrdup(""));
                    211:
                    212:        s = ictx->string_buf;
                    213:        ictx->string_buf = NULL;
                    214:        return (s);
                    215: }
                    216:
                    217: void
                    218: input_state(struct input_ctx *ictx, void *state)
                    219: {
                    220:        ictx->state = state;
                    221: }
                    222:
                    223: void
                    224: input_init(struct window_pane *wp)
                    225: {
                    226:        struct input_ctx        *ictx = &wp->ictx;
                    227:
                    228:        ARRAY_INIT(&ictx->args);
                    229:
                    230:        ictx->string_len = 0;
                    231:        ictx->string_buf = NULL;
                    232:
                    233:        memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell);
                    234:
                    235:        memcpy(&ictx->saved_cell, &grid_default_cell, sizeof ictx->saved_cell);
                    236:        ictx->saved_cx = 0;
                    237:        ictx->saved_cy = 0;
                    238:
                    239:        input_state(ictx, input_state_first);
1.13      nicm      240:
                    241:        ictx->was = 0;
1.1       nicm      242: }
                    243:
                    244: void
                    245: input_free(struct window_pane *wp)
                    246: {
                    247:        if (wp->ictx.string_buf != NULL)
                    248:                xfree(wp->ictx.string_buf);
                    249:
                    250:        ARRAY_FREE(&wp->ictx.args);
                    251: }
                    252:
                    253: void
                    254: input_parse(struct window_pane *wp)
                    255: {
                    256:        struct input_ctx        *ictx = &wp->ictx;
                    257:        u_char                   ch;
                    258:
1.13      nicm      259:        if (BUFFER_USED(wp->in) == ictx->was)
1.1       nicm      260:                return;
1.13      nicm      261:        wp->window->flags |= WINDOW_ACTIVITY;
1.1       nicm      262:
                    263:        ictx->buf = BUFFER_OUT(wp->in);
                    264:        ictx->len = BUFFER_USED(wp->in);
                    265:        ictx->off = 0;
                    266:
                    267:        ictx->wp = wp;
                    268:
                    269:        if (wp->mode == NULL)
                    270:                screen_write_start(&ictx->ctx, wp, &wp->base);
                    271:        else
                    272:                screen_write_start(&ictx->ctx, NULL, &wp->base);
                    273:
                    274:        while (ictx->off < ictx->len) {
                    275:                ch = ictx->buf[ictx->off++];
                    276:                ictx->state(ch, ictx);
                    277:        }
                    278:
                    279:        screen_write_stop(&ictx->ctx);
                    280:
                    281:        buffer_remove(wp->in, ictx->len);
1.13      nicm      282:        ictx->was = BUFFER_USED(wp->in);
1.1       nicm      283: }
                    284:
                    285: void
                    286: input_state_first(u_char ch, struct input_ctx *ictx)
                    287: {
                    288:        ictx->intermediate = '\0';
                    289:
                    290:        if (INPUT_C0CONTROL(ch)) {
                    291:                if (ch == 0x1b)
                    292:                        input_state(ictx, input_state_escape);
                    293:                else
                    294:                        input_handle_c0_control(ch, ictx);
                    295:                return;
                    296:        }
                    297:
                    298: #if 0
                    299:        if (INPUT_C1CONTROL(ch)) {
                    300:                ch -= 0x40;
                    301:                if (ch == '[')
                    302:                        input_state(ictx, input_state_sequence_first);
                    303:                else if (ch == ']') {
                    304:                        input_start_string(ictx, STRING_SYSTEM);
                    305:                        input_state(ictx, input_state_string_next);
                    306:                } else if (ch == '_') {
                    307:                        input_start_string(ictx, STRING_APPLICATION);
                    308:                        input_state(ictx, input_state_string_next);
                    309:                } else
                    310:                        input_handle_c1_control(ch, ictx);
                    311:                return;
                    312:        }
                    313: #endif
                    314:
                    315:        if (INPUT_DELETE(ch))
                    316:                return;
                    317:
                    318:        input_handle_character(ch, ictx);
                    319: }
                    320:
                    321: void
                    322: input_state_escape(u_char ch, struct input_ctx *ictx)
                    323: {
                    324:        /* Treat C1 control and G1 displayable as 7-bit equivalent. */
                    325:        if (INPUT_C1CONTROL(ch) || INPUT_G1DISPLAYABLE(ch))
                    326:                ch &= 0x7f;
                    327:
                    328:        if (INPUT_C0CONTROL(ch)) {
                    329:                input_handle_c0_control(ch, ictx);
                    330:                return;
                    331:        }
                    332:
                    333:        if (INPUT_INTERMEDIATE(ch)) {
                    334:                log_debug2(":: in1 %zu: %hhu (%c)", ictx->off, ch, ch);
                    335:                ictx->intermediate = ch;
                    336:                input_state(ictx, input_state_intermediate);
                    337:                return;
                    338:        }
                    339:
                    340:        if (INPUT_PARAMETER(ch)) {
                    341:                input_state(ictx, input_state_first);
                    342:                input_handle_private_two(ch, ictx);
                    343:                return;
                    344:        }
                    345:
                    346:        if (INPUT_UPPERCASE(ch)) {
                    347:                if (ch == '[')
                    348:                        input_state(ictx, input_state_sequence_first);
                    349:                else if (ch == ']') {
                    350:                        input_start_string(ictx, STRING_SYSTEM);
                    351:                        input_state(ictx, input_state_string_next);
                    352:                } else if (ch == '_') {
                    353:                        input_start_string(ictx, STRING_APPLICATION);
                    354:                        input_state(ictx, input_state_string_next);
                    355:                } else {
                    356:                        input_state(ictx, input_state_first);
                    357:                        input_handle_c1_control(ch, ictx);
                    358:                }
                    359:                return;
                    360:        }
                    361:
                    362:        if (INPUT_LOWERCASE(ch)) {
                    363:                input_state(ictx, input_state_first);
                    364:                input_handle_standard_two(ch, ictx);
                    365:                return;
                    366:        }
                    367:
                    368:        input_state(ictx, input_state_first);
                    369: }
                    370:
                    371: void
                    372: input_state_intermediate(u_char ch, struct input_ctx *ictx)
                    373: {
                    374:        if (INPUT_INTERMEDIATE(ch)) {
                    375:                /* Multiple intermediates currently ignored. */
                    376:                log_debug2(":: in2 %zu: %hhu (%c)", ictx->off, ch, ch);
                    377:                return;
                    378:        }
                    379:
                    380:        if (INPUT_PARAMETER(ch)) {
                    381:                input_state(ictx, input_state_first);
                    382:                input_handle_private_two(ch, ictx);
                    383:                return;
                    384:        }
                    385:
                    386:        if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
                    387:                input_state(ictx, input_state_first);
                    388:                input_handle_standard_two(ch, ictx);
                    389:                return;
                    390:        }
                    391:
                    392:        input_state(ictx, input_state_first);
                    393: }
                    394:
                    395: void
                    396: input_state_sequence_first(u_char ch, struct input_ctx *ictx)
                    397: {
                    398:        ictx->private = '\0';
                    399:        ARRAY_CLEAR(&ictx->args);
                    400:
1.4       nicm      401:        /* Most C0 control are accepted within CSI. */
                    402:        if (INPUT_C0CONTROL(ch)) {
                    403:                if (ch == 0x1b) {                       /* ESC */
                    404:                        /* Abort sequence and begin with new. */
                    405:                        input_state(ictx, input_state_escape);
1.6       nicm      406:                        return;
                    407:                } else if (ch == 0x18 || ch == 0x1a) {  /* CAN and SUB */
1.5       nicm      408:                        /* Abort sequence. */
                    409:                        input_state(ictx, input_state_first);
1.6       nicm      410:                        return;
1.4       nicm      411:                }
1.6       nicm      412:
                    413:                /* Handle C0 immediately. */
                    414:                input_handle_c0_control(ch, ictx);
                    415:
1.4       nicm      416:                /*
                    417:                 * Just come back to this state, in case the next character
                    418:                 * is the start of a private sequence.
                    419:                 */
                    420:                return;
                    421:        }
                    422:
1.1       nicm      423:        input_state(ictx, input_state_sequence_next);
                    424:
1.4       nicm      425:        /* Private sequence: always the first character. */
                    426:        if (ch >= 0x3c && ch <= 0x3f) {
                    427:                ictx->private = ch;
                    428:                return;
1.1       nicm      429:        }
                    430:
                    431:        /* Pass character on directly. */
                    432:        input_state_sequence_next(ch, ictx);
                    433: }
                    434:
                    435: void
                    436: input_state_sequence_next(u_char ch, struct input_ctx *ictx)
                    437: {
                    438:        if (INPUT_INTERMEDIATE(ch)) {
                    439:                if (input_add_argument(ictx, '\0') != 0)
                    440:                        input_state(ictx, input_state_first);
                    441:                else {
                    442:                        log_debug2(":: si1 %zu: %hhu (%c)", ictx->off, ch, ch);
                    443:                        input_state(ictx, input_state_sequence_intermediate);
                    444:                }
                    445:                return;
                    446:        }
                    447:
                    448:        if (INPUT_PARAMETER(ch)) {
1.4       nicm      449:                if (ARRAY_EMPTY(&ictx->args))
                    450:                        input_new_argument(ictx);
                    451:
1.1       nicm      452:                if (ch == ';') {
                    453:                        if (input_add_argument(ictx, '\0') != 0)
                    454:                                input_state(ictx, input_state_first);
                    455:                        else
                    456:                                input_new_argument(ictx);
                    457:                } else if (input_add_argument(ictx, ch) != 0)
                    458:                        input_state(ictx, input_state_first);
                    459:                return;
                    460:        }
                    461:
                    462:        if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
                    463:                if (input_add_argument(ictx, '\0') != 0)
                    464:                        input_state(ictx, input_state_first);
                    465:                else {
                    466:                        input_state(ictx, input_state_first);
                    467:                        input_handle_sequence(ch, ictx);
                    468:                }
                    469:                return;
                    470:        }
                    471:
1.4       nicm      472:        /* Most C0 control are accepted within CSI. */
                    473:        if (INPUT_C0CONTROL(ch)) {
                    474:                if (ch == 0x1b) {                       /* ESC */
                    475:                        /* Abort sequence and begin with new. */
                    476:                        input_state(ictx, input_state_escape);
1.6       nicm      477:                        return;
                    478:                } else if (ch == 0x18 || ch == 0x1a) {  /* CAN and SUB */
1.5       nicm      479:                        /* Abort sequence. */
                    480:                        input_state(ictx, input_state_first);
1.6       nicm      481:                        return;
1.4       nicm      482:                }
1.6       nicm      483:
                    484:                /* Handle C0 immediately. */
                    485:                input_handle_c0_control(ch, ictx);
                    486:
1.4       nicm      487:                return;
                    488:        }
                    489:
1.1       nicm      490:        input_state(ictx, input_state_first);
                    491: }
                    492:
                    493: void
                    494: input_state_sequence_intermediate(u_char ch, struct input_ctx *ictx)
                    495: {
                    496:        if (INPUT_INTERMEDIATE(ch)) {
                    497:                log_debug2(":: si2 %zu: %hhu (%c)", ictx->off, ch, ch);
                    498:                return;
                    499:        }
                    500:
                    501:        if (INPUT_UPPERCASE(ch) || INPUT_LOWERCASE(ch)) {
                    502:                input_state(ictx, input_state_first);
                    503:                input_handle_sequence(ch, ictx);
                    504:                return;
                    505:        }
                    506:
                    507:        input_state(ictx, input_state_first);
                    508: }
                    509:
                    510: void
                    511: input_state_string_next(u_char ch, struct input_ctx *ictx)
                    512: {
                    513:        if (ch == 0x1b) {
                    514:                input_state(ictx, input_state_string_escape);
                    515:                return;
                    516:        }
                    517:        if (ch == 0x07) {
                    518:                input_state_string_escape(ch, ictx);
                    519:                return;
                    520:        }
                    521:
1.2       nicm      522:        if (ch >= 0x20) {
1.1       nicm      523:                if (input_add_string(ictx, ch) != 0)
                    524:                        input_state(ictx, input_state_first);
                    525:                return;
                    526:        }
                    527: }
                    528:
                    529: void
                    530: input_state_string_escape(u_char ch, struct input_ctx *ictx)
                    531: {
                    532:        char    *s;
                    533:
                    534:        if (ch == '\007' || ch == '\\') {
                    535:                input_state(ictx, input_state_first);
                    536:                switch (ictx->string_type) {
                    537:                case STRING_SYSTEM:
                    538:                        if (ch != '\007')
                    539:                                return;
                    540:                        s = input_get_string(ictx);
                    541:                        if ((s[0] != '0' && s[0] != '2') || s[1] != ';') {
                    542:                                xfree(s);
                    543:                                return;
                    544:                        }
                    545:                        screen_set_title(ictx->ctx.s, s + 2);
                    546:                        server_status_window(ictx->wp->window);
                    547:                        xfree(s);
                    548:                        break;
                    549:                case STRING_APPLICATION:
                    550:                        if (ch != '\\')
                    551:                                return;
                    552:                        s = input_get_string(ictx);
                    553:                        screen_set_title(ictx->ctx.s, s);
                    554:                        server_status_window(ictx->wp->window);
                    555:                        xfree(s);
                    556:                        break;
                    557:                case STRING_NAME:
                    558:                        if (ch != '\\')
                    559:                                return;
                    560:                        xfree(ictx->wp->window->name);
                    561:                        ictx->wp->window->name = input_get_string(ictx);
                    562:                        server_status_window(ictx->wp->window);
                    563:                        break;
                    564:                }
                    565:                return;
                    566:        }
                    567:
                    568:        input_state(ictx, input_state_string_next);
                    569:        input_state_string_next(ch, ictx);
                    570: }
                    571:
                    572: void
                    573: input_state_utf8(u_char ch, struct input_ctx *ictx)
                    574: {
                    575:        log_debug2("-- un %zu: %hhu (%c)", ictx->off, ch, ch);
                    576:
                    577:        ictx->utf8_buf[ictx->utf8_off++] = ch;
                    578:        if (--ictx->utf8_len != 0)
                    579:                return;
                    580:        input_state(ictx, input_state_first);
                    581:
                    582:        ictx->cell.flags |= GRID_FLAG_UTF8;
                    583:        screen_write_cell(&ictx->ctx, &ictx->cell, ictx->utf8_buf);
                    584:        ictx->cell.flags &= ~GRID_FLAG_UTF8;
                    585: }
                    586:
                    587: void
                    588: input_handle_character(u_char ch, struct input_ctx *ictx)
                    589: {
                    590:        struct window_pane      *wp = ictx->wp;
                    591:
                    592:        if (ch > 0x7f && options_get_number(&wp->window->options, "utf8")) {
                    593:                /*
                    594:                 * UTF-8 sequence.
                    595:                 *
                    596:                 * 11000010-11011111 C2-DF start of 2-byte sequence
                    597:                 * 11100000-11101111 E0-EF start of 3-byte sequence
                    598:                 * 11110000-11110100 F0-F4 start of 4-byte sequence
                    599:                 */
                    600:                memset(ictx->utf8_buf, 0xff, sizeof ictx->utf8_buf);
                    601:                ictx->utf8_buf[0] = ch;
                    602:                ictx->utf8_off = 1;
                    603:
                    604:                if (ch >= 0xc2 && ch <= 0xdf) {
                    605:                        log_debug2("-- u2 %zu: %hhu (%c)", ictx->off, ch, ch);
                    606:                        input_state(ictx, input_state_utf8);
                    607:                        ictx->utf8_len = 1;
                    608:                        return;
                    609:                }
                    610:                if (ch >= 0xe0 && ch <= 0xef) {
                    611:                        log_debug2("-- u3 %zu: %hhu (%c)", ictx->off, ch, ch);
                    612:                        input_state(ictx, input_state_utf8);
                    613:                        ictx->utf8_len = 2;
                    614:                        return;
                    615:                }
                    616:                if (ch >= 0xf0 && ch <= 0xf4) {
                    617:                        log_debug2("-- u4 %zu: %hhu (%c)", ictx->off, ch, ch);
                    618:                        input_state(ictx, input_state_utf8);
                    619:                        ictx->utf8_len = 3;
                    620:                        return;
                    621:                }
                    622:        }
                    623:        log_debug2("-- ch %zu: %hhu (%c)", ictx->off, ch, ch);
                    624:
                    625:        ictx->cell.data = ch;
                    626:        screen_write_cell(&ictx->ctx, &ictx->cell, ictx->utf8_buf);
                    627: }
                    628:
                    629: void
                    630: input_handle_c0_control(u_char ch, struct input_ctx *ictx)
                    631: {
                    632:        struct screen   *s = ictx->ctx.s;
                    633:
                    634:        log_debug2("-- c0 %zu: %hhu", ictx->off, ch);
                    635:
                    636:        switch (ch) {
                    637:        case '\0':      /* NUL */
                    638:                break;
                    639:        case '\n':      /* LF */
1.12      nicm      640:                screen_write_linefeed(&ictx->ctx, 0);
1.1       nicm      641:                break;
                    642:        case '\r':      /* CR */
                    643:                screen_write_carriagereturn(&ictx->ctx);
                    644:                break;
                    645:        case '\007':    /* BELL */
                    646:                ictx->wp->window->flags |= WINDOW_BELL;
                    647:                break;
                    648:        case '\010':    /* BS */
                    649:                screen_write_cursorleft(&ictx->ctx, 1);
                    650:                break;
                    651:        case '\011':    /* TAB */
1.7       nicm      652:                /* Don't tab beyond the end of the line. */
                    653:                if (s->cx >= screen_size_x(s) - 1)
                    654:                        break;
                    655:
                    656:                /* Find the next tab point, or use the last column if none. */
                    657:                do {
                    658:                        s->cx++;
                    659:                        if (bit_test(s->tabs, s->cx))
                    660:                                break;
                    661:                } while (s->cx < screen_size_x(s) - 1);
1.4       nicm      662:                break;
                    663:        case '\013':    /* VT */
1.12      nicm      664:                screen_write_linefeed(&ictx->ctx, 0);
1.1       nicm      665:                break;
                    666:        case '\016':    /* SO */
                    667:                ictx->cell.attr |= GRID_ATTR_CHARSET;
                    668:                break;
                    669:        case '\017':    /* SI */
                    670:                ictx->cell.attr &= ~GRID_ATTR_CHARSET;
                    671:                break;
                    672:        default:
                    673:                log_debug("unknown c0: %hhu", ch);
                    674:                break;
                    675:        }
                    676: }
                    677:
                    678: void
                    679: input_handle_c1_control(u_char ch, struct input_ctx *ictx)
                    680: {
1.7       nicm      681:        struct screen  *s = ictx->ctx.s;
                    682:
1.1       nicm      683:        log_debug2("-- c1 %zu: %hhu (%c)", ictx->off, ch, ch);
                    684:
                    685:        switch (ch) {
1.3       nicm      686:        case 'D':       /* IND */
1.12      nicm      687:                screen_write_linefeed(&ictx->ctx, 0);
1.3       nicm      688:                break;
1.1       nicm      689:        case 'E':       /* NEL */
                    690:                screen_write_carriagereturn(&ictx->ctx);
1.12      nicm      691:                screen_write_linefeed(&ictx->ctx, 0);
1.1       nicm      692:                break;
1.7       nicm      693:        case 'H':       /* HTS */
                    694:                if (s->cx < screen_size_x(s))
                    695:                        bit_set(s->tabs, s->cx);
                    696:                break;
1.1       nicm      697:        case 'M':       /* RI */
                    698:                screen_write_reverseindex(&ictx->ctx);
                    699:                break;
                    700:        default:
                    701:                log_debug("unknown c1: %hhu", ch);
                    702:                break;
                    703:        }
                    704: }
                    705:
                    706: void
                    707: input_handle_private_two(u_char ch, struct input_ctx *ictx)
                    708: {
                    709:        struct screen   *s = ictx->ctx.s;
                    710:
                    711:        log_debug2(
                    712:            "-- p2 %zu: %hhu (%c) %hhu", ictx->off, ch, ch, ictx->intermediate);
                    713:
                    714:        switch (ch) {
1.3       nicm      715:        case '0':       /* SCS */
1.1       nicm      716:                /*
                    717:                 * Not really supported, but fake it up enough for those that
                    718:                 * use it to switch character sets (by redefining G0 to
                    719:                 * graphics set, rather than switching to G1).
                    720:                 */
                    721:                switch (ictx->intermediate) {
                    722:                case '(':       /* G0 */
                    723:                        ictx->cell.attr |= GRID_ATTR_CHARSET;
                    724:                        break;
                    725:                }
                    726:                break;
                    727:        case '=':       /* DECKPAM */
1.3       nicm      728:                if (ictx->intermediate != '\0')
                    729:                        break;
1.1       nicm      730:                screen_write_kkeypadmode(&ictx->ctx, 1);
                    731:                log_debug("kkeypad on (application mode)");
                    732:                break;
                    733:        case '>':       /* DECKPNM */
1.3       nicm      734:                if (ictx->intermediate != '\0')
                    735:                        break;
1.1       nicm      736:                screen_write_kkeypadmode(&ictx->ctx, 0);
                    737:                log_debug("kkeypad off (number mode)");
                    738:                break;
                    739:        case '7':       /* DECSC */
1.3       nicm      740:                if (ictx->intermediate != '\0')
                    741:                        break;
1.1       nicm      742:                memcpy(&ictx->saved_cell, &ictx->cell, sizeof ictx->saved_cell);
                    743:                ictx->saved_cx = s->cx;
                    744:                ictx->saved_cy = s->cy;
                    745:                break;
1.3       nicm      746:        case '8':
                    747:                switch (ictx->intermediate) {
                    748:                case '\0':      /* DECRC */
                    749:                        memcpy(
                    750:                            &ictx->cell, &ictx->saved_cell, sizeof ictx->cell);
                    751:                        screen_write_cursormove(
                    752:                            &ictx->ctx, ictx->saved_cx, ictx->saved_cy);
                    753:                        break;
                    754:                case '#':       /* DECALN */
                    755:                        screen_write_alignmenttest(&ictx->ctx);
                    756:                        break;
                    757:                }
1.1       nicm      758:                break;
                    759:        default:
                    760:                log_debug("unknown p2: %hhu", ch);
                    761:                break;
                    762:        }
                    763: }
                    764:
                    765: void
                    766: input_handle_standard_two(u_char ch, struct input_ctx *ictx)
                    767: {
                    768:        log_debug2(
                    769:            "-- s2 %zu: %hhu (%c) %hhu", ictx->off, ch, ch, ictx->intermediate);
                    770:
                    771:        switch (ch) {
1.7       nicm      772:        case 'B':       /* SCS */
1.1       nicm      773:                /*
                    774:                 * Not really supported, but fake it up enough for those that
                    775:                 * use it to switch character sets (by redefining G0 to
                    776:                 * graphics set, rather than switching to G1).
                    777:                 */
                    778:                switch (ictx->intermediate) {
                    779:                case '(':       /* G0 */
                    780:                        ictx->cell.attr &= ~GRID_ATTR_CHARSET;
                    781:                        break;
                    782:                }
                    783:                break;
                    784:        case 'c':       /* RIS */
                    785:                memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell);
                    786:
                    787:                memcpy(&ictx->saved_cell, &ictx->cell, sizeof ictx->saved_cell);
                    788:                ictx->saved_cx = 0;
                    789:                ictx->saved_cy = 0;
                    790:
1.7       nicm      791:                screen_reset_tabs(ictx->ctx.s);
                    792:
1.1       nicm      793:                screen_write_scrollregion(
                    794:                    &ictx->ctx, 0, screen_size_y(ictx->ctx.s) - 1);
                    795:
                    796:                screen_write_insertmode(&ictx->ctx, 0);
                    797:                screen_write_kcursormode(&ictx->ctx, 0);
                    798:                screen_write_kkeypadmode(&ictx->ctx, 0);
                    799:                screen_write_mousemode(&ictx->ctx, 0);
                    800:
                    801:                screen_write_clearscreen(&ictx->ctx);
                    802:                screen_write_cursormove(&ictx->ctx, 0, 0);
                    803:                break;
                    804:        case 'k':
                    805:                input_start_string(ictx, STRING_NAME);
                    806:                input_state(ictx, input_state_string_next);
                    807:                break;
                    808:        default:
                    809:                log_debug("unknown s2: %hhu", ch);
                    810:                break;
                    811:        }
                    812: }
                    813:
                    814: void
                    815: input_handle_sequence(u_char ch, struct input_ctx *ictx)
                    816: {
                    817:        struct input_sequence_entry     *entry, find;
                    818:        struct screen                   *s = ictx->ctx.s;
                    819:        u_int                            i;
                    820:        struct input_arg                *iarg;
                    821:
                    822:        log_debug2("-- sq %zu: %hhu (%c): %u [sx=%u, sy=%u, cx=%u, cy=%u, "
                    823:            "ru=%u, rl=%u]", ictx->off, ch, ch, ARRAY_LENGTH(&ictx->args),
                    824:            screen_size_x(s), screen_size_y(s), s->cx, s->cy, s->rupper,
                    825:            s->rlower);
                    826:        for (i = 0; i < ARRAY_LENGTH(&ictx->args); i++) {
                    827:                iarg = &ARRAY_ITEM(&ictx->args, i);
                    828:                if (*iarg->data != '\0')
                    829:                        log_debug2("      ++ %u: %s", i, iarg->data);
                    830:        }
                    831:
                    832:        find.ch = ch;
                    833:        entry = bsearch(&find,
                    834:            input_sequence_table, nitems(input_sequence_table),
                    835:            sizeof input_sequence_table[0], input_sequence_cmp);
                    836:        if (entry != NULL)
                    837:                entry->fn(ictx);
                    838:        else
                    839:                log_debug("unknown sq: %c (%hhu %hhu)", ch, ch, ictx->private);
                    840: }
                    841:
                    842: void
                    843: input_handle_sequence_cuu(struct input_ctx *ictx)
                    844: {
                    845:        uint16_t        n;
                    846:
                    847:        if (ictx->private != '\0')
                    848:                return;
                    849:
                    850:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    851:                return;
                    852:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    853:                return;
                    854:        if (n == 0)
                    855:                n = 1;
                    856:
                    857:        screen_write_cursorup(&ictx->ctx, n);
                    858: }
                    859:
                    860: void
                    861: input_handle_sequence_cud(struct input_ctx *ictx)
                    862: {
                    863:        uint16_t        n;
                    864:
                    865:        if (ictx->private != '\0')
                    866:                return;
                    867:
                    868:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    869:                return;
                    870:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    871:                return;
                    872:        if (n == 0)
                    873:                n = 1;
                    874:
                    875:        screen_write_cursordown(&ictx->ctx, n);
                    876: }
                    877:
                    878: void
                    879: input_handle_sequence_cuf(struct input_ctx *ictx)
                    880: {
                    881:        uint16_t n;
                    882:
                    883:        if (ictx->private != '\0')
                    884:                return;
                    885:
                    886:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    887:                return;
                    888:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    889:                return;
                    890:        if (n == 0)
                    891:                n = 1;
                    892:
                    893:        screen_write_cursorright(&ictx->ctx, n);
                    894: }
                    895:
                    896: void
                    897: input_handle_sequence_cub(struct input_ctx *ictx)
                    898: {
                    899:        uint16_t        n;
                    900:
                    901:        if (ictx->private != '\0')
                    902:                return;
                    903:
                    904:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    905:                return;
                    906:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    907:                return;
                    908:        if (n == 0)
                    909:                n = 1;
                    910:
                    911:        screen_write_cursorleft(&ictx->ctx, n);
                    912: }
                    913:
                    914: void
                    915: input_handle_sequence_dch(struct input_ctx *ictx)
                    916: {
                    917:        uint16_t        n;
                    918:
                    919:        if (ictx->private != '\0')
                    920:                return;
                    921:
                    922:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    923:                return;
                    924:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    925:                return;
                    926:        if (n == 0)
                    927:                n = 1;
                    928:
                    929:        screen_write_deletecharacter(&ictx->ctx, n);
1.8       nicm      930: }
                    931:
                    932: void
                    933: input_handle_sequence_cbt(struct input_ctx *ictx)
                    934: {
                    935:        struct screen  *s = ictx->ctx.s;
                    936:        uint16_t        n;
                    937:
                    938:        if (ictx->private != '\0')
                    939:                return;
                    940:
                    941:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    942:                return;
                    943:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    944:                return;
                    945:        if (n == 0)
                    946:                n = 1;
                    947:
                    948:        /* Find the previous tab point, n times. */
                    949:        while (s->cx > 0 && n-- > 0) {
                    950:                do
                    951:                        s->cx--;
                    952:                while (s->cx > 0 && !bit_test(s->tabs, s->cx));
                    953:        }
1.14    ! nicm      954: }
        !           955:
        !           956: void
        !           957: input_handle_sequence_da(struct input_ctx *ictx)
        !           958: {
        !           959:        struct screen  *s = ictx->ctx.s;
        !           960:        uint16_t        n;
        !           961:
        !           962:        if (ictx->private != '\0')
        !           963:                return;
        !           964:
        !           965:        if (ARRAY_LENGTH(&ictx->args) > 1)
        !           966:                return;
        !           967:        if (input_get_argument(ictx, 0, &n, 0) != 0)
        !           968:                return;
        !           969:        if (n != 0)
        !           970:                return;
        !           971:
        !           972:        buffer_write(ictx->wp->out, "\033[?1;2c", (sizeof "\033[?1;2c") - 1);
1.1       nicm      973: }
                    974:
                    975: void
                    976: input_handle_sequence_dl(struct input_ctx *ictx)
                    977: {
                    978:        uint16_t        n;
                    979:
                    980:        if (ictx->private != '\0')
                    981:                return;
                    982:
                    983:        if (ARRAY_LENGTH(&ictx->args) > 1)
                    984:                return;
                    985:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                    986:                return;
                    987:        if (n == 0)
                    988:                n = 1;
                    989:
                    990:        screen_write_deleteline(&ictx->ctx, n);
                    991: }
                    992:
                    993: void
                    994: input_handle_sequence_ich(struct input_ctx *ictx)
                    995: {
                    996:        uint16_t        n;
                    997:
                    998:        if (ictx->private != '\0')
                    999:                return;
                   1000:
                   1001:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1002:                return;
                   1003:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1004:                return;
                   1005:        if (n == 0)
                   1006:                n = 1;
                   1007:
                   1008:        screen_write_insertcharacter(&ictx->ctx, n);
                   1009: }
                   1010:
                   1011: void
                   1012: input_handle_sequence_il(struct input_ctx *ictx)
                   1013: {
                   1014:        uint16_t        n;
                   1015:
                   1016:        if (ictx->private != '\0')
                   1017:                return;
                   1018:
                   1019:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1020:                return;
                   1021:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1022:                return;
                   1023:        if (n == 0)
                   1024:                n = 1;
                   1025:
                   1026:        screen_write_insertline(&ictx->ctx, n);
                   1027: }
                   1028:
                   1029: void
                   1030: input_handle_sequence_vpa(struct input_ctx *ictx)
                   1031: {
                   1032:        struct screen  *s = ictx->ctx.s;
                   1033:        uint16_t        n;
                   1034:
                   1035:        if (ictx->private != '\0')
                   1036:                return;
                   1037:
                   1038:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1039:                return;
                   1040:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1041:                return;
                   1042:        if (n == 0)
                   1043:                n = 1;
                   1044:
                   1045:        screen_write_cursormove(&ictx->ctx, s->cx, n - 1);
                   1046: }
                   1047:
                   1048: void
                   1049: input_handle_sequence_hpa(struct input_ctx *ictx)
                   1050: {
                   1051:        struct screen  *s = ictx->ctx.s;
                   1052:        uint16_t        n;
                   1053:
                   1054:        if (ictx->private != '\0')
                   1055:                return;
                   1056:
                   1057:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1058:                return;
                   1059:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1060:                return;
                   1061:        if (n == 0)
                   1062:                n = 1;
                   1063:
                   1064:        screen_write_cursormove(&ictx->ctx, n - 1, s->cy);
                   1065: }
                   1066:
                   1067: void
                   1068: input_handle_sequence_cup(struct input_ctx *ictx)
                   1069: {
                   1070:        uint16_t        n, m;
                   1071:
                   1072:        if (ictx->private != '\0')
                   1073:                return;
                   1074:
                   1075:        if (ARRAY_LENGTH(&ictx->args) > 2)
                   1076:                return;
                   1077:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1078:                return;
                   1079:        if (input_get_argument(ictx, 1, &m, 1) != 0)
                   1080:                return;
                   1081:        if (n == 0)
                   1082:                n = 1;
                   1083:        if (m == 0)
                   1084:                m = 1;
                   1085:
                   1086:        screen_write_cursormove(&ictx->ctx, m - 1, n - 1);
1.7       nicm     1087: }
                   1088:
                   1089: void
                   1090: input_handle_sequence_tbc(struct input_ctx *ictx)
                   1091: {
                   1092:        struct screen  *s = ictx->ctx.s;
                   1093:        uint16_t        n;
                   1094:
                   1095:        if (ictx->private != '\0')
                   1096:                return;
                   1097:
                   1098:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1099:                return;
                   1100:        if (input_get_argument(ictx, 0, &n, 1) != 0)
                   1101:                return;
                   1102:
                   1103:        switch (n) {
                   1104:        case 0:
                   1105:                if (s->cx < screen_size_x(s))
                   1106:                        bit_clear(s->tabs, s->cx);
                   1107:                break;
                   1108:        case 3:
                   1109:                bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
                   1110:                break;
                   1111:        }
1.1       nicm     1112: }
                   1113:
                   1114: void
                   1115: input_handle_sequence_ed(struct input_ctx *ictx)
                   1116: {
                   1117:        uint16_t        n;
                   1118:
                   1119:        if (ictx->private != '\0')
                   1120:                return;
                   1121:
                   1122:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1123:                return;
                   1124:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1125:                return;
                   1126:        if (n > 2)
                   1127:                return;
                   1128:
                   1129:        switch (n) {
                   1130:        case 0:
                   1131:                screen_write_clearendofscreen(&ictx->ctx);
                   1132:                break;
                   1133:        case 1:
                   1134:                screen_write_clearstartofscreen(&ictx->ctx);
                   1135:                break;
                   1136:        case 2:
                   1137:                screen_write_clearscreen(&ictx->ctx);
                   1138:                break;
                   1139:        }
                   1140: }
                   1141:
                   1142: void
                   1143: input_handle_sequence_el(struct input_ctx *ictx)
                   1144: {
                   1145:        uint16_t        n;
                   1146:
                   1147:        if (ictx->private != '\0')
                   1148:                return;
                   1149:
                   1150:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1151:                return;
                   1152:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1153:                return;
                   1154:        if (n > 2)
                   1155:                return;
                   1156:
                   1157:        switch (n) {
                   1158:        case 0:
                   1159:                screen_write_clearendofline(&ictx->ctx);
                   1160:                break;
                   1161:        case 1:
                   1162:                screen_write_clearstartofline(&ictx->ctx);
                   1163:                break;
                   1164:        case 2:
                   1165:                screen_write_clearline(&ictx->ctx);
                   1166:                break;
                   1167:        }
                   1168: }
                   1169:
                   1170: void
                   1171: input_handle_sequence_sm(struct input_ctx *ictx)
                   1172: {
1.9       nicm     1173:        struct window_pane      *wp = ictx->wp;
                   1174:        struct screen           *s = &wp->base;
                   1175:        u_int                    sx, sy;
                   1176:        uint16_t                 n;
1.1       nicm     1177:
                   1178:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1179:                return;
                   1180:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1181:                return;
                   1182:
                   1183:        if (ictx->private == '?') {
                   1184:                switch (n) {
                   1185:                case 1:         /* GATM */
                   1186:                        screen_write_kcursormode(&ictx->ctx, 1);
                   1187:                        log_debug("kcursor on");
                   1188:                        break;
                   1189:                case 25:        /* TCEM */
                   1190:                        screen_write_cursormode(&ictx->ctx, 1);
                   1191:                        log_debug("cursor on");
                   1192:                        break;
                   1193:                case 1000:
                   1194:                        screen_write_mousemode(&ictx->ctx, 1);
                   1195:                        log_debug("mouse on");
                   1196:                        break;
1.9       nicm     1197:                case 1049:
                   1198:                        if (wp->saved_grid != NULL)
                   1199:                                break;
                   1200:                        sx = screen_size_x(s);
                   1201:                        sy = screen_size_y(s);
                   1202:
                   1203:                        /*
                   1204:                         * Enter alternative screen mode. A copy of the visible
                   1205:                         * screen is saved and the history is not updated
                   1206:                         */
                   1207:
                   1208:                        wp->saved_grid = grid_create(sx, sy, 0);
                   1209:                        grid_duplicate_lines(
                   1210:                            wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
                   1211:                        wp->saved_cx = s->cx;
                   1212:                        wp->saved_cy = s->cy;
1.11      nicm     1213:                        memcpy(&wp->saved_cell,
                   1214:                            &ictx->cell, sizeof wp->saved_cell);
                   1215:
1.9       nicm     1216:                        grid_view_clear(s->grid, 0, 0, sx, sy);
                   1217:
                   1218:                        wp->base.grid->flags &= ~GRID_HISTORY;
                   1219:
                   1220:                        wp->flags |= PANE_REDRAW;
                   1221:                        break;
1.1       nicm     1222:                default:
                   1223:                        log_debug("unknown SM [%hhu]: %u", ictx->private, n);
                   1224:                        break;
                   1225:                }
                   1226:        } else {
                   1227:                switch (n) {
                   1228:                case 4:         /* IRM */
                   1229:                        screen_write_insertmode(&ictx->ctx, 1);
                   1230:                        log_debug("insert on");
                   1231:                        break;
                   1232:                case 34:
                   1233:                        /* Cursor high visibility not supported. */
                   1234:                        break;
                   1235:                default:
                   1236:                        log_debug("unknown SM [%hhu]: %u", ictx->private, n);
                   1237:                        break;
                   1238:                }
                   1239:        }
                   1240: }
                   1241:
                   1242: void
                   1243: input_handle_sequence_rm(struct input_ctx *ictx)
                   1244: {
1.9       nicm     1245:        struct window_pane      *wp = ictx->wp;
                   1246:        struct screen           *s = &wp->base;
                   1247:        u_int                    sx, sy;
                   1248:        uint16_t                 n;
1.1       nicm     1249:
                   1250:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1251:                return;
                   1252:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1253:                return;
                   1254:
                   1255:        if (ictx->private == '?') {
                   1256:                switch (n) {
                   1257:                case 1:         /* GATM */
                   1258:                        screen_write_kcursormode(&ictx->ctx, 0);
                   1259:                        log_debug("kcursor off");
                   1260:                        break;
                   1261:                case 25:        /* TCEM */
                   1262:                        screen_write_cursormode(&ictx->ctx, 0);
                   1263:                        log_debug("cursor off");
                   1264:                        break;
                   1265:                case 1000:
                   1266:                        screen_write_mousemode(&ictx->ctx, 0);
                   1267:                        log_debug("mouse off");
1.9       nicm     1268:                        break;
                   1269:                case 1049:
                   1270:                        if (wp->saved_grid == NULL)
                   1271:                                break;
                   1272:                        sx = screen_size_x(s);
                   1273:                        sy = screen_size_y(s);
                   1274:
                   1275:                        /*
                   1276:                         * Exit alternative screen mode and restore the copied
                   1277:                         * grid.
                   1278:                         */
                   1279:
                   1280:                        /*
                   1281:                         * If the current size is bigger, temporarily resize
                   1282:                         * to the old size before copying back.
                   1283:                         */
                   1284:                        if (sy > wp->saved_grid->sy)
                   1285:                                screen_resize(s, sx, wp->saved_grid->sy);
                   1286:
1.11      nicm     1287:                        /* Restore the grid, cursor position and cell. */
1.9       nicm     1288:                        grid_duplicate_lines(
                   1289:                            s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                   1290:                        s->cx = wp->saved_cx;
                   1291:                        if (s->cx > screen_size_x(s) - 1)
                   1292:                                s->cx = screen_size_x(s) - 1;
                   1293:                        s->cy = wp->saved_cy;
                   1294:                        if (s->cy > screen_size_y(s) - 1)
                   1295:                                s->cy = screen_size_y(s) - 1;
1.11      nicm     1296:                        memcpy(&ictx->cell, &wp->saved_cell, sizeof ictx->cell);
1.9       nicm     1297:
                   1298:                        /*
                   1299:                         * Turn history back on (so resize can use it) and then
                   1300:                         * resize back to the current size.
                   1301:                         */
                   1302:                        wp->base.grid->flags |= GRID_HISTORY;
                   1303:                        if (sy > wp->saved_grid->sy)
                   1304:                                screen_resize(s, sx, sy);
                   1305:
                   1306:                        grid_destroy(wp->saved_grid);
                   1307:                        wp->saved_grid = NULL;
                   1308:
                   1309:                        wp->flags |= PANE_REDRAW;
1.1       nicm     1310:                        break;
                   1311:                default:
                   1312:                        log_debug("unknown RM [%hhu]: %u", ictx->private, n);
                   1313:                        break;
                   1314:                }
                   1315:        } else if (ictx->private == '\0') {
                   1316:                switch (n) {
                   1317:                case 4:         /* IRM */
                   1318:                        screen_write_insertmode(&ictx->ctx, 0);
                   1319:                        log_debug("insert off");
                   1320:                        break;
                   1321:                case 34:
                   1322:                        /* Cursor high visibility not supported. */
                   1323:                        break;
                   1324:                default:
                   1325:                        log_debug("unknown RM [%hhu]: %u", ictx->private, n);
                   1326:                        break;
                   1327:                }
                   1328:        }
                   1329: }
                   1330:
                   1331: void
                   1332: input_handle_sequence_dsr(struct input_ctx *ictx)
                   1333: {
                   1334:        struct screen  *s = ictx->ctx.s;
                   1335:        uint16_t        n;
                   1336:        char            reply[32];
                   1337:
                   1338:        if (ARRAY_LENGTH(&ictx->args) > 1)
                   1339:                return;
                   1340:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1341:                return;
                   1342:
                   1343:        if (ictx->private == '\0') {
                   1344:                switch (n) {
                   1345:                case 6: /* cursor position */
                   1346:                        xsnprintf(reply, sizeof reply,
                   1347:                            "\033[%u;%uR", s->cy + 1, s->cx + 1);
                   1348:                        log_debug("cursor request, reply: %s", reply);
                   1349:                        buffer_write(ictx->wp->out, reply, strlen(reply));
                   1350:                        break;
                   1351:                }
                   1352:        }
                   1353: }
                   1354:
                   1355: void
                   1356: input_handle_sequence_decstbm(struct input_ctx *ictx)
                   1357: {
                   1358:        struct screen  *s = ictx->ctx.s;
                   1359:        uint16_t        n, m;
                   1360:
                   1361:        if (ictx->private != '\0')
                   1362:                return;
                   1363:
                   1364:        if (ARRAY_LENGTH(&ictx->args) > 2)
                   1365:                return;
                   1366:        if (input_get_argument(ictx, 0, &n, 0) != 0)
                   1367:                return;
                   1368:        if (input_get_argument(ictx, 1, &m, 0) != 0)
                   1369:                return;
                   1370:        if (n == 0)
                   1371:                n = 1;
                   1372:        if (m == 0)
                   1373:                m = screen_size_y(s);
                   1374:
                   1375:        screen_write_scrollregion(&ictx->ctx, n - 1, m - 1);
                   1376: }
                   1377:
                   1378: void
                   1379: input_handle_sequence_sgr(struct input_ctx *ictx)
                   1380: {
                   1381:        struct grid_cell       *gc = &ictx->cell;
                   1382:        u_int                   i;
                   1383:        uint16_t                m, o;
                   1384:        u_char                  attr;
                   1385:
                   1386:        if (ARRAY_LENGTH(&ictx->args) == 0) {
                   1387:                attr = gc->attr;
                   1388:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   1389:                gc->attr |= (attr & GRID_ATTR_CHARSET);
                   1390:                return;
                   1391:        }
                   1392:
                   1393:        for (i = 0; i < ARRAY_LENGTH(&ictx->args); i++) {
                   1394:                if (input_get_argument(ictx, i, &m, 0) != 0)
                   1395:                        return;
                   1396:
                   1397:                if (m == 38 || m == 48) {
                   1398:                        i++;
                   1399:                        if (input_get_argument(ictx, i, &o, 0) != 0)
                   1400:                                return;
                   1401:                        if (o != 5)
                   1402:                                continue;
                   1403:
                   1404:                        i++;
                   1405:                        if (input_get_argument(ictx, i, &o, 0) != 0)
                   1406:                                return;
                   1407:                        if (m == 38) {
                   1408:                                gc->flags |= GRID_FLAG_FG256;
                   1409:                                gc->fg = o;
                   1410:                        } else if (m == 48) {
                   1411:                                gc->flags |= GRID_FLAG_BG256;
                   1412:                                gc->bg = o;
                   1413:                        }
                   1414:                        continue;
                   1415:                }
                   1416:
                   1417:                switch (m) {
                   1418:                case 0:
                   1419:                case 10:
                   1420:                        attr = gc->attr;
                   1421:                        memcpy(gc, &grid_default_cell, sizeof *gc);
                   1422:                        gc->attr |= (attr & GRID_ATTR_CHARSET);
                   1423:                        break;
                   1424:                case 1:
                   1425:                        gc->attr |= GRID_ATTR_BRIGHT;
                   1426:                        break;
                   1427:                case 2:
                   1428:                        gc->attr |= GRID_ATTR_DIM;
                   1429:                        break;
                   1430:                case 3:
                   1431:                        gc->attr |= GRID_ATTR_ITALICS;
                   1432:                        break;
                   1433:                case 4:
                   1434:                        gc->attr |= GRID_ATTR_UNDERSCORE;
                   1435:                        break;
                   1436:                case 5:
                   1437:                        gc->attr |= GRID_ATTR_BLINK;
                   1438:                        break;
                   1439:                case 7:
                   1440:                        gc->attr |= GRID_ATTR_REVERSE;
                   1441:                        break;
                   1442:                case 8:
                   1443:                        gc->attr |= GRID_ATTR_HIDDEN;
                   1444:                        break;
                   1445:                case 22:
                   1446:                        gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
                   1447:                        break;
                   1448:                case 23:
                   1449:                        gc->attr &= ~GRID_ATTR_ITALICS;
                   1450:                        break;
                   1451:                case 24:
                   1452:                        gc->attr &= ~GRID_ATTR_UNDERSCORE;
                   1453:                        break;
                   1454:                case 25:
                   1455:                        gc->attr &= ~GRID_ATTR_BLINK;
                   1456:                        break;
                   1457:                case 27:
                   1458:                        gc->attr &= ~GRID_ATTR_REVERSE;
                   1459:                        break;
                   1460:                case 30:
                   1461:                case 31:
                   1462:                case 32:
                   1463:                case 33:
                   1464:                case 34:
                   1465:                case 35:
                   1466:                case 36:
                   1467:                case 37:
                   1468:                        gc->flags &= ~GRID_FLAG_FG256;
                   1469:                        gc->fg = m - 30;
                   1470:                        break;
                   1471:                case 39:
                   1472:                        gc->flags &= ~GRID_FLAG_FG256;
                   1473:                        gc->fg = 8;
                   1474:                        break;
                   1475:                case 40:
                   1476:                case 41:
                   1477:                case 42:
                   1478:                case 43:
                   1479:                case 44:
                   1480:                case 45:
                   1481:                case 46:
                   1482:                case 47:
                   1483:                        gc->flags &= ~GRID_FLAG_BG256;
                   1484:                        gc->bg = m - 40;
                   1485:                        break;
                   1486:                case 49:
                   1487:                        gc->flags &= ~GRID_FLAG_BG256;
                   1488:                        gc->bg = 8;
                   1489:                        break;
                   1490:                }
                   1491:        }
                   1492: }