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

1.85    ! nicm        1: /* $OpenBSD: input.c,v 1.84 2015/09/01 19:16:00 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 <stdlib.h>
                     22: #include <string.h>
1.79      nicm       23: #include <time.h>
1.1       nicm       24:
                     25: #include "tmux.h"
                     26:
1.28      nicm       27: /*
                     28:  * Based on the description by Paul Williams at:
                     29:  *
                     30:  * http://vt100.net/emu/dec_ansi_parser
                     31:  *
                     32:  * With the following changes:
                     33:  *
                     34:  * - 7-bit only.
                     35:  *
                     36:  * - Support for UTF-8.
                     37:  *
                     38:  * - OSC (but not APC) may be terminated by \007 as well as ST.
                     39:  *
                     40:  * - A state for APC similar to OSC. Some terminals appear to use this to set
                     41:  *   the title.
                     42:  *
                     43:  * - A state for the screen \033k...\033\\ sequence to rename a window. This is
                     44:  *   pretty stupid but not supporting it is more trouble than it is worth.
1.37      nicm       45:  *
                     46:  * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
1.77      nicm       47:  *   be passed to the underlying terminals.
1.28      nicm       48:  */
                     49:
1.74      nicm       50: /* Input parser cell. */
                     51: struct input_cell {
                     52:        struct grid_cell        cell;
                     53:        int                     set;
                     54:        int                     g0set;  /* 1 if ACS */
                     55:        int                     g1set;  /* 1 if ACS */
                     56: };
                     57:
                     58: /* Input parser context. */
                     59: struct input_ctx {
                     60:        struct window_pane     *wp;
                     61:        struct screen_write_ctx ctx;
                     62:
                     63:        struct input_cell       cell;
                     64:
                     65:        struct input_cell       old_cell;
                     66:        u_int                   old_cx;
                     67:        u_int                   old_cy;
                     68:
                     69:        u_char                  interm_buf[4];
                     70:        size_t                  interm_len;
                     71:
                     72:        u_char                  param_buf[64];
                     73:        size_t                  param_len;
                     74:
                     75: #define INPUT_BUF_START 32
                     76: #define INPUT_BUF_LIMIT 1048576
                     77:        u_char                 *input_buf;
                     78:        size_t                  input_len;
                     79:        size_t                  input_space;
                     80:
                     81:        int                     param_list[24]; /* -1 not present */
                     82:        u_int                   param_list_len;
                     83:
                     84:        struct utf8_data        utf8data;
                     85:
                     86:        int                     ch;
                     87:        int                     flags;
                     88: #define INPUT_DISCARD 0x1
                     89:
                     90:        const struct input_state *state;
                     91:
                     92:        /*
                     93:         * All input received since we were last in the ground state. Sent to
                     94:         * control clients on connection.
                     95:         */
                     96:        struct evbuffer         *since_ground;
                     97: };
                     98:
1.28      nicm       99: /* Helper functions. */
1.52      nicm      100: struct input_transition;
1.28      nicm      101: int    input_split(struct input_ctx *);
                    102: int    input_get(struct input_ctx *, u_int, int, int);
                    103: void   input_reply(struct input_ctx *, const char *, ...);
1.52      nicm      104: void   input_set_state(struct window_pane *, const struct input_transition *);
1.69      nicm      105: void   input_reset_cell(struct input_ctx *);
1.28      nicm      106:
                    107: /* Transition entry/exit handlers. */
                    108: void   input_clear(struct input_ctx *);
1.67      nicm      109: void   input_ground(struct input_ctx *);
1.28      nicm      110: void   input_enter_osc(struct input_ctx *);
                    111: void   input_exit_osc(struct input_ctx *);
                    112: void   input_enter_apc(struct input_ctx *);
                    113: void   input_exit_apc(struct input_ctx *);
                    114: void   input_enter_rename(struct input_ctx *);
                    115: void   input_exit_rename(struct input_ctx *);
                    116:
                    117: /* Input state handlers. */
                    118: int    input_print(struct input_ctx *);
                    119: int    input_intermediate(struct input_ctx *);
                    120: int    input_parameter(struct input_ctx *);
                    121: int    input_input(struct input_ctx *);
                    122: int    input_c0_dispatch(struct input_ctx *);
                    123: int    input_esc_dispatch(struct input_ctx *);
                    124: int    input_csi_dispatch(struct input_ctx *);
1.64      nicm      125: void   input_csi_dispatch_rm(struct input_ctx *);
                    126: void   input_csi_dispatch_rm_private(struct input_ctx *);
                    127: void   input_csi_dispatch_sm(struct input_ctx *);
                    128: void   input_csi_dispatch_sm_private(struct input_ctx *);
1.65      nicm      129: void   input_csi_dispatch_winops(struct input_ctx *);
1.78      nicm      130: void   input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
                    131: void   input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
1.28      nicm      132: void   input_csi_dispatch_sgr(struct input_ctx *);
1.37      nicm      133: int    input_dcs_dispatch(struct input_ctx *);
1.28      nicm      134: int    input_utf8_open(struct input_ctx *);
                    135: int    input_utf8_add(struct input_ctx *);
                    136: int    input_utf8_close(struct input_ctx *);
                    137:
                    138: /* Command table comparison function. */
                    139: int    input_table_compare(const void *, const void *);
                    140:
                    141: /* Command table entry. */
                    142: struct input_table_entry {
                    143:        int             ch;
                    144:        const char     *interm;
                    145:        int             type;
1.1       nicm      146: };
                    147:
1.28      nicm      148: /* Escape commands. */
                    149: enum input_esc_type {
                    150:        INPUT_ESC_DECALN,
                    151:        INPUT_ESC_DECKPAM,
                    152:        INPUT_ESC_DECKPNM,
                    153:        INPUT_ESC_DECRC,
                    154:        INPUT_ESC_DECSC,
                    155:        INPUT_ESC_HTS,
                    156:        INPUT_ESC_IND,
                    157:        INPUT_ESC_NEL,
                    158:        INPUT_ESC_RI,
                    159:        INPUT_ESC_RIS,
1.69      nicm      160:        INPUT_ESC_SCSG0_OFF,
                    161:        INPUT_ESC_SCSG0_ON,
                    162:        INPUT_ESC_SCSG1_OFF,
                    163:        INPUT_ESC_SCSG1_ON,
1.28      nicm      164: };
1.1       nicm      165:
1.28      nicm      166: /* Escape command table. */
                    167: const struct input_table_entry input_esc_table[] = {
1.69      nicm      168:        { '0', "(", INPUT_ESC_SCSG0_ON },
                    169:        { '0', ")", INPUT_ESC_SCSG1_ON },
1.28      nicm      170:        { '7', "",  INPUT_ESC_DECSC },
                    171:        { '8', "",  INPUT_ESC_DECRC },
                    172:        { '8', "#", INPUT_ESC_DECALN },
                    173:        { '=', "",  INPUT_ESC_DECKPAM },
                    174:        { '>', "",  INPUT_ESC_DECKPNM },
1.69      nicm      175:        { 'B', "(", INPUT_ESC_SCSG0_OFF },
                    176:        { 'B', ")", INPUT_ESC_SCSG1_OFF },
1.28      nicm      177:        { 'D', "",  INPUT_ESC_IND },
                    178:        { 'E', "",  INPUT_ESC_NEL },
                    179:        { 'H', "",  INPUT_ESC_HTS },
                    180:        { 'M', "",  INPUT_ESC_RI },
                    181:        { 'c', "",  INPUT_ESC_RIS },
                    182: };
1.1       nicm      183:
1.28      nicm      184: /* Control (CSI) commands. */
                    185: enum input_csi_type {
                    186:        INPUT_CSI_CBT,
1.43      nicm      187:        INPUT_CSI_CNL,
                    188:        INPUT_CSI_CPL,
1.28      nicm      189:        INPUT_CSI_CUB,
                    190:        INPUT_CSI_CUD,
                    191:        INPUT_CSI_CUF,
                    192:        INPUT_CSI_CUP,
                    193:        INPUT_CSI_CUU,
                    194:        INPUT_CSI_DA,
1.50      nicm      195:        INPUT_CSI_DA_TWO,
1.28      nicm      196:        INPUT_CSI_DCH,
1.39      nicm      197:        INPUT_CSI_DECSCUSR,
1.28      nicm      198:        INPUT_CSI_DECSTBM,
                    199:        INPUT_CSI_DL,
                    200:        INPUT_CSI_DSR,
1.56      nicm      201:        INPUT_CSI_ECH,
1.28      nicm      202:        INPUT_CSI_ED,
                    203:        INPUT_CSI_EL,
                    204:        INPUT_CSI_HPA,
                    205:        INPUT_CSI_ICH,
                    206:        INPUT_CSI_IL,
1.42      nicm      207:        INPUT_CSI_RCP,
1.28      nicm      208:        INPUT_CSI_RM,
                    209:        INPUT_CSI_RM_PRIVATE,
1.42      nicm      210:        INPUT_CSI_SCP,
1.28      nicm      211:        INPUT_CSI_SGR,
                    212:        INPUT_CSI_SM,
                    213:        INPUT_CSI_SM_PRIVATE,
                    214:        INPUT_CSI_TBC,
                    215:        INPUT_CSI_VPA,
1.65      nicm      216:        INPUT_CSI_WINOPS,
1.28      nicm      217: };
1.1       nicm      218:
1.28      nicm      219: /* Control (CSI) command table. */
                    220: const struct input_table_entry input_csi_table[] = {
                    221:        { '@', "",  INPUT_CSI_ICH },
                    222:        { 'A', "",  INPUT_CSI_CUU },
                    223:        { 'B', "",  INPUT_CSI_CUD },
                    224:        { 'C', "",  INPUT_CSI_CUF },
                    225:        { 'D', "",  INPUT_CSI_CUB },
1.43      nicm      226:        { 'E', "",  INPUT_CSI_CNL },
                    227:        { 'F', "",  INPUT_CSI_CPL },
1.28      nicm      228:        { 'G', "",  INPUT_CSI_HPA },
                    229:        { 'H', "",  INPUT_CSI_CUP },
                    230:        { 'J', "",  INPUT_CSI_ED },
                    231:        { 'K', "",  INPUT_CSI_EL },
                    232:        { 'L', "",  INPUT_CSI_IL },
                    233:        { 'M', "",  INPUT_CSI_DL },
                    234:        { 'P', "",  INPUT_CSI_DCH },
1.56      nicm      235:        { 'X', "",  INPUT_CSI_ECH },
1.28      nicm      236:        { 'Z', "",  INPUT_CSI_CBT },
                    237:        { 'c', "",  INPUT_CSI_DA },
1.50      nicm      238:        { 'c', ">", INPUT_CSI_DA_TWO },
1.28      nicm      239:        { 'd', "",  INPUT_CSI_VPA },
                    240:        { 'f', "",  INPUT_CSI_CUP },
                    241:        { 'g', "",  INPUT_CSI_TBC },
                    242:        { 'h', "",  INPUT_CSI_SM },
                    243:        { 'h', "?", INPUT_CSI_SM_PRIVATE },
                    244:        { 'l', "",  INPUT_CSI_RM },
                    245:        { 'l', "?", INPUT_CSI_RM_PRIVATE },
                    246:        { 'm', "",  INPUT_CSI_SGR },
                    247:        { 'n', "",  INPUT_CSI_DSR },
1.39      nicm      248:        { 'q', " ", INPUT_CSI_DECSCUSR },
1.28      nicm      249:        { 'r', "",  INPUT_CSI_DECSTBM },
1.42      nicm      250:        { 's', "",  INPUT_CSI_SCP },
1.65      nicm      251:        { 't', "",  INPUT_CSI_WINOPS },
1.42      nicm      252:        { 'u', "",  INPUT_CSI_RCP },
1.28      nicm      253: };
1.1       nicm      254:
1.28      nicm      255: /* Input transition. */
                    256: struct input_transition {
                    257:        int                             first;
                    258:        int                             last;
1.1       nicm      259:
1.28      nicm      260:        int                             (*handler)(struct input_ctx *);
                    261:        const struct input_state       *state;
                    262: };
1.1       nicm      263:
1.28      nicm      264: /* Input state. */
                    265: struct input_state {
                    266:        const char                      *name;
                    267:        void                            (*enter)(struct input_ctx *);
                    268:        void                            (*exit)(struct input_ctx *);
                    269:        const struct input_transition   *transitions;
                    270: };
1.1       nicm      271:
1.28      nicm      272: /* State transitions available from all states. */
                    273: #define INPUT_STATE_ANYWHERE \
                    274:        { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
                    275:        { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
                    276:        { 0x1b, 0x1b, NULL,              &input_state_esc_enter }
                    277:
                    278: /* Forward declarations of state tables. */
                    279: const struct input_transition input_state_ground_table[];
                    280: const struct input_transition input_state_esc_enter_table[];
                    281: const struct input_transition input_state_esc_intermediate_table[];
                    282: const struct input_transition input_state_csi_enter_table[];
                    283: const struct input_transition input_state_csi_parameter_table[];
                    284: const struct input_transition input_state_csi_intermediate_table[];
                    285: const struct input_transition input_state_csi_ignore_table[];
                    286: const struct input_transition input_state_dcs_enter_table[];
                    287: const struct input_transition input_state_dcs_parameter_table[];
                    288: const struct input_transition input_state_dcs_intermediate_table[];
                    289: const struct input_transition input_state_dcs_handler_table[];
1.37      nicm      290: const struct input_transition input_state_dcs_escape_table[];
1.28      nicm      291: const struct input_transition input_state_dcs_ignore_table[];
                    292: const struct input_transition input_state_osc_string_table[];
                    293: const struct input_transition input_state_apc_string_table[];
                    294: const struct input_transition input_state_rename_string_table[];
                    295: const struct input_transition input_state_consume_st_table[];
                    296: const struct input_transition input_state_utf8_three_table[];
                    297: const struct input_transition input_state_utf8_two_table[];
                    298: const struct input_transition input_state_utf8_one_table[];
                    299:
                    300: /* ground state definition. */
                    301: const struct input_state input_state_ground = {
                    302:        "ground",
1.67      nicm      303:        input_ground, NULL,
1.28      nicm      304:        input_state_ground_table
                    305: };
1.1       nicm      306:
1.28      nicm      307: /* esc_enter state definition. */
                    308: const struct input_state input_state_esc_enter = {
                    309:        "esc_enter",
                    310:        input_clear, NULL,
                    311:        input_state_esc_enter_table
                    312: };
1.1       nicm      313:
1.28      nicm      314: /* esc_intermediate state definition. */
                    315: const struct input_state input_state_esc_intermediate = {
                    316:        "esc_intermediate",
                    317:        NULL, NULL,
                    318:        input_state_esc_intermediate_table
                    319: };
1.1       nicm      320:
1.28      nicm      321: /* csi_enter state definition. */
                    322: const struct input_state input_state_csi_enter = {
                    323:        "csi_enter",
                    324:        input_clear, NULL,
                    325:        input_state_csi_enter_table
                    326: };
1.1       nicm      327:
1.28      nicm      328: /* csi_parameter state definition. */
                    329: const struct input_state input_state_csi_parameter = {
                    330:        "csi_parameter",
                    331:        NULL, NULL,
                    332:        input_state_csi_parameter_table
                    333: };
1.1       nicm      334:
1.28      nicm      335: /* csi_intermediate state definition. */
                    336: const struct input_state input_state_csi_intermediate = {
                    337:        "csi_intermediate",
                    338:        NULL, NULL,
                    339:        input_state_csi_intermediate_table
                    340: };
1.1       nicm      341:
1.28      nicm      342: /* csi_ignore state definition. */
                    343: const struct input_state input_state_csi_ignore = {
                    344:        "csi_ignore",
                    345:        NULL, NULL,
                    346:        input_state_csi_ignore_table
                    347: };
1.1       nicm      348:
1.28      nicm      349: /* dcs_enter state definition. */
                    350: const struct input_state input_state_dcs_enter = {
                    351:        "dcs_enter",
                    352:        input_clear, NULL,
                    353:        input_state_dcs_enter_table
                    354: };
1.1       nicm      355:
1.28      nicm      356: /* dcs_parameter state definition. */
                    357: const struct input_state input_state_dcs_parameter = {
                    358:        "dcs_parameter",
                    359:        NULL, NULL,
                    360:        input_state_dcs_parameter_table
                    361: };
1.1       nicm      362:
1.28      nicm      363: /* dcs_intermediate state definition. */
                    364: const struct input_state input_state_dcs_intermediate = {
                    365:        "dcs_intermediate",
                    366:        NULL, NULL,
                    367:        input_state_dcs_intermediate_table
                    368: };
1.1       nicm      369:
1.28      nicm      370: /* dcs_handler state definition. */
                    371: const struct input_state input_state_dcs_handler = {
                    372:        "dcs_handler",
1.37      nicm      373:        NULL, NULL,
1.28      nicm      374:        input_state_dcs_handler_table
                    375: };
1.1       nicm      376:
1.37      nicm      377: /* dcs_escape state definition. */
                    378: const struct input_state input_state_dcs_escape = {
                    379:        "dcs_escape",
                    380:        NULL, NULL,
                    381:        input_state_dcs_escape_table
                    382: };
                    383:
1.28      nicm      384: /* dcs_ignore state definition. */
                    385: const struct input_state input_state_dcs_ignore = {
                    386:        "dcs_ignore",
                    387:        NULL, NULL,
                    388:        input_state_dcs_ignore_table
                    389: };
1.1       nicm      390:
1.28      nicm      391: /* osc_string state definition. */
                    392: const struct input_state input_state_osc_string = {
                    393:        "osc_string",
                    394:        input_enter_osc, input_exit_osc,
                    395:        input_state_osc_string_table
                    396: };
1.1       nicm      397:
1.28      nicm      398: /* apc_string state definition. */
                    399: const struct input_state input_state_apc_string = {
                    400:        "apc_string",
                    401:        input_enter_apc, input_exit_apc,
                    402:        input_state_apc_string_table
                    403: };
1.1       nicm      404:
1.28      nicm      405: /* rename_string state definition. */
                    406: const struct input_state input_state_rename_string = {
                    407:        "rename_string",
                    408:        input_enter_rename, input_exit_rename,
                    409:        input_state_rename_string_table
                    410: };
1.1       nicm      411:
1.28      nicm      412: /* consume_st state definition. */
                    413: const struct input_state input_state_consume_st = {
                    414:        "consume_st",
                    415:        NULL, NULL,
                    416:        input_state_consume_st_table
                    417: };
1.1       nicm      418:
1.28      nicm      419: /* utf8_three state definition. */
                    420: const struct input_state input_state_utf8_three = {
                    421:        "utf8_three",
                    422:        NULL, NULL,
                    423:        input_state_utf8_three_table
                    424: };
1.1       nicm      425:
1.28      nicm      426: /* utf8_two state definition. */
                    427: const struct input_state input_state_utf8_two = {
                    428:        "utf8_two",
                    429:        NULL, NULL,
                    430:        input_state_utf8_two_table
                    431: };
1.1       nicm      432:
1.28      nicm      433: /* utf8_one state definition. */
                    434: const struct input_state input_state_utf8_one = {
                    435:        "utf8_one",
                    436:        NULL, NULL,
                    437:        input_state_utf8_one_table
                    438: };
1.1       nicm      439:
1.28      nicm      440: /* ground state table. */
                    441: const struct input_transition input_state_ground_table[] = {
                    442:        INPUT_STATE_ANYWHERE,
                    443:
                    444:        { 0x00, 0x17, input_c0_dispatch, NULL },
                    445:        { 0x19, 0x19, input_c0_dispatch, NULL },
                    446:        { 0x1c, 0x1f, input_c0_dispatch, NULL },
                    447:        { 0x20, 0x7e, input_print,       NULL },
                    448:        { 0x7f, 0x7f, NULL,              NULL },
                    449:        { 0x80, 0xc1, input_print,       NULL },
                    450:        { 0xc2, 0xdf, input_utf8_open,   &input_state_utf8_one },
                    451:        { 0xe0, 0xef, input_utf8_open,   &input_state_utf8_two },
                    452:        { 0xf0, 0xf4, input_utf8_open,   &input_state_utf8_three },
                    453:        { 0xf5, 0xff, input_print,       NULL },
1.1       nicm      454:
1.28      nicm      455:        { -1, -1, NULL, NULL }
                    456: };
1.13      nicm      457:
1.28      nicm      458: /* esc_enter state table. */
                    459: const struct input_transition input_state_esc_enter_table[] = {
                    460:        INPUT_STATE_ANYWHERE,
                    461:
                    462:        { 0x00, 0x17, input_c0_dispatch,  NULL },
                    463:        { 0x19, 0x19, input_c0_dispatch,  NULL },
                    464:        { 0x1c, 0x1f, input_c0_dispatch,  NULL },
                    465:        { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
                    466:        { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
                    467:        { 0x50, 0x50, NULL,               &input_state_dcs_enter },
                    468:        { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
                    469:        { 0x58, 0x58, NULL,               &input_state_consume_st },
                    470:        { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
                    471:        { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
                    472:        { 0x5b, 0x5b, NULL,               &input_state_csi_enter },
                    473:        { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
                    474:        { 0x5d, 0x5d, NULL,               &input_state_osc_string },
                    475:        { 0x5e, 0x5e, NULL,               &input_state_consume_st },
                    476:        { 0x5f, 0x5f, NULL,               &input_state_apc_string },
                    477:        { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
                    478:        { 0x6b, 0x6b, NULL,               &input_state_rename_string },
1.29      nicm      479:        { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
1.28      nicm      480:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      481:
1.28      nicm      482:        { -1, -1, NULL, NULL }
                    483: };
1.1       nicm      484:
1.28      nicm      485: /* esc_interm state table. */
                    486: const struct input_transition input_state_esc_intermediate_table[] = {
                    487:        INPUT_STATE_ANYWHERE,
                    488:
                    489:        { 0x00, 0x17, input_c0_dispatch,  NULL },
                    490:        { 0x19, 0x19, input_c0_dispatch,  NULL },
                    491:        { 0x1c, 0x1f, input_c0_dispatch,  NULL },
                    492:        { 0x20, 0x2f, input_intermediate, NULL },
                    493:        { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
                    494:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      495:
1.28      nicm      496:        { -1, -1, NULL, NULL }
                    497: };
1.1       nicm      498:
1.28      nicm      499: /* csi_enter state table. */
                    500: const struct input_transition input_state_csi_enter_table[] = {
                    501:        INPUT_STATE_ANYWHERE,
                    502:
                    503:        { 0x00, 0x17, input_c0_dispatch,  NULL },
                    504:        { 0x19, 0x19, input_c0_dispatch,  NULL },
                    505:        { 0x1c, 0x1f, input_c0_dispatch,  NULL },
                    506:        { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
                    507:        { 0x30, 0x39, input_parameter,    &input_state_csi_parameter },
                    508:        { 0x3a, 0x3a, NULL,               &input_state_csi_ignore },
                    509:        { 0x3b, 0x3b, input_parameter,    &input_state_csi_parameter },
                    510:        { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
                    511:        { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
                    512:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      513:
1.28      nicm      514:        { -1, -1, NULL, NULL }
                    515: };
1.1       nicm      516:
1.28      nicm      517: /* csi_parameter state table. */
                    518: const struct input_transition input_state_csi_parameter_table[] = {
                    519:        INPUT_STATE_ANYWHERE,
                    520:
                    521:        { 0x00, 0x17, input_c0_dispatch,  NULL },
                    522:        { 0x19, 0x19, input_c0_dispatch,  NULL },
                    523:        { 0x1c, 0x1f, input_c0_dispatch,  NULL },
                    524:        { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
                    525:        { 0x30, 0x39, input_parameter,    NULL },
                    526:        { 0x3a, 0x3a, NULL,               &input_state_csi_ignore },
                    527:        { 0x3b, 0x3b, input_parameter,    NULL },
                    528:        { 0x3c, 0x3f, NULL,               &input_state_csi_ignore },
                    529:        { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
                    530:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      531:
1.28      nicm      532:        { -1, -1, NULL, NULL }
                    533: };
1.1       nicm      534:
1.28      nicm      535: /* csi_intermediate state table. */
                    536: const struct input_transition input_state_csi_intermediate_table[] = {
                    537:        INPUT_STATE_ANYWHERE,
                    538:
                    539:        { 0x00, 0x17, input_c0_dispatch,  NULL },
                    540:        { 0x19, 0x19, input_c0_dispatch,  NULL },
                    541:        { 0x1c, 0x1f, input_c0_dispatch,  NULL },
                    542:        { 0x20, 0x2f, input_intermediate, NULL },
                    543:        { 0x30, 0x3f, NULL,               &input_state_csi_ignore },
                    544:        { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
                    545:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      546:
1.28      nicm      547:        { -1, -1, NULL, NULL }
                    548: };
1.1       nicm      549:
1.28      nicm      550: /* csi_ignore state table. */
                    551: const struct input_transition input_state_csi_ignore_table[] = {
                    552:        INPUT_STATE_ANYWHERE,
                    553:
                    554:        { 0x00, 0x17, input_c0_dispatch, NULL },
                    555:        { 0x19, 0x19, input_c0_dispatch, NULL },
                    556:        { 0x1c, 0x1f, input_c0_dispatch, NULL },
                    557:        { 0x20, 0x3f, NULL,              NULL },
                    558:        { 0x40, 0x7e, NULL,              &input_state_ground },
                    559:        { 0x7f, 0xff, NULL,              NULL },
1.1       nicm      560:
1.28      nicm      561:        { -1, -1, NULL, NULL }
                    562: };
1.1       nicm      563:
1.28      nicm      564: /* dcs_enter state table. */
                    565: const struct input_transition input_state_dcs_enter_table[] = {
                    566:        INPUT_STATE_ANYWHERE,
                    567:
                    568:        { 0x00, 0x17, NULL,               NULL },
                    569:        { 0x19, 0x19, NULL,               NULL },
                    570:        { 0x1c, 0x1f, NULL,               NULL },
                    571:        { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
                    572:        { 0x30, 0x39, input_parameter,    &input_state_dcs_parameter },
                    573:        { 0x3a, 0x3a, NULL,               &input_state_dcs_ignore },
                    574:        { 0x3b, 0x3b, input_parameter,    &input_state_dcs_parameter },
                    575:        { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
1.37      nicm      576:        { 0x40, 0x7e, input_input,        &input_state_dcs_handler },
1.28      nicm      577:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      578:
1.28      nicm      579:        { -1, -1, NULL, NULL }
                    580: };
1.1       nicm      581:
1.28      nicm      582: /* dcs_parameter state table. */
                    583: const struct input_transition input_state_dcs_parameter_table[] = {
                    584:        INPUT_STATE_ANYWHERE,
                    585:
                    586:        { 0x00, 0x17, NULL,               NULL },
                    587:        { 0x19, 0x19, NULL,               NULL },
                    588:        { 0x1c, 0x1f, NULL,               NULL },
                    589:        { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
                    590:        { 0x30, 0x39, input_parameter,    NULL },
                    591:        { 0x3a, 0x3a, NULL,               &input_state_dcs_ignore },
                    592:        { 0x3b, 0x3b, input_parameter,    NULL },
                    593:        { 0x3c, 0x3f, NULL,               &input_state_dcs_ignore },
1.37      nicm      594:        { 0x40, 0x7e, input_input,        &input_state_dcs_handler },
1.28      nicm      595:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      596:
1.28      nicm      597:        { -1, -1, NULL, NULL }
                    598: };
1.1       nicm      599:
1.28      nicm      600: /* dcs_interm state table. */
                    601: const struct input_transition input_state_dcs_intermediate_table[] = {
                    602:        INPUT_STATE_ANYWHERE,
                    603:
                    604:        { 0x00, 0x17, NULL,               NULL },
                    605:        { 0x19, 0x19, NULL,               NULL },
                    606:        { 0x1c, 0x1f, NULL,               NULL },
                    607:        { 0x20, 0x2f, input_intermediate, NULL },
                    608:        { 0x30, 0x3f, NULL,               &input_state_dcs_ignore },
1.37      nicm      609:        { 0x40, 0x7e, input_input,        &input_state_dcs_handler },
1.28      nicm      610:        { 0x7f, 0xff, NULL,               NULL },
1.1       nicm      611:
1.28      nicm      612:        { -1, -1, NULL, NULL }
                    613: };
1.1       nicm      614:
1.28      nicm      615: /* dcs_handler state table. */
                    616: const struct input_transition input_state_dcs_handler_table[] = {
1.37      nicm      617:        /* No INPUT_STATE_ANYWHERE */
                    618:
                    619:        { 0x00, 0x1a, input_input,  NULL },
                    620:        { 0x1b, 0x1b, NULL,         &input_state_dcs_escape },
                    621:        { 0x1c, 0xff, input_input,  NULL },
                    622:
                    623:        { -1, -1, NULL, NULL }
                    624: };
                    625:
                    626: /* dcs_escape state table. */
                    627: const struct input_transition input_state_dcs_escape_table[] = {
                    628:        /* No INPUT_STATE_ANYWHERE */
1.28      nicm      629:
1.37      nicm      630:        { 0x00, 0x5b, input_input,        &input_state_dcs_handler },
                    631:        { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
                    632:        { 0x5d, 0xff, input_input,        &input_state_dcs_handler },
1.1       nicm      633:
1.28      nicm      634:        { -1, -1, NULL, NULL }
                    635: };
1.1       nicm      636:
1.40      nicm      637: /* dcs_ignore state table. */
1.28      nicm      638: const struct input_transition input_state_dcs_ignore_table[] = {
                    639:        INPUT_STATE_ANYWHERE,
                    640:
                    641:        { 0x00, 0x17, NULL,         NULL },
                    642:        { 0x19, 0x19, NULL,         NULL },
                    643:        { 0x1c, 0x1f, NULL,         NULL },
                    644:        { 0x20, 0xff, NULL,         NULL },
1.1       nicm      645:
1.28      nicm      646:        { -1, -1, NULL, NULL }
                    647: };
1.1       nicm      648:
1.28      nicm      649: /* osc_string state table. */
                    650: const struct input_transition input_state_osc_string_table[] = {
                    651:        INPUT_STATE_ANYWHERE,
                    652:
                    653:        { 0x00, 0x06, NULL,         NULL },
                    654:        { 0x07, 0x07, NULL,         &input_state_ground },
                    655:        { 0x08, 0x17, NULL,         NULL },
                    656:        { 0x19, 0x19, NULL,         NULL },
                    657:        { 0x1c, 0x1f, NULL,         NULL },
                    658:        { 0x20, 0xff, input_input,  NULL },
1.1       nicm      659:
1.28      nicm      660:        { -1, -1, NULL, NULL }
                    661: };
1.1       nicm      662:
1.28      nicm      663: /* apc_string state table. */
                    664: const struct input_transition input_state_apc_string_table[] = {
                    665:        INPUT_STATE_ANYWHERE,
                    666:
                    667:        { 0x00, 0x17, NULL,         NULL },
                    668:        { 0x19, 0x19, NULL,         NULL },
                    669:        { 0x1c, 0x1f, NULL,         NULL },
                    670:        { 0x20, 0xff, input_input,  NULL },
1.1       nicm      671:
1.28      nicm      672:        { -1, -1, NULL, NULL }
                    673: };
1.1       nicm      674:
1.28      nicm      675: /* rename_string state table. */
                    676: const struct input_transition input_state_rename_string_table[] = {
                    677:        INPUT_STATE_ANYWHERE,
                    678:
                    679:        { 0x00, 0x17, NULL,         NULL },
                    680:        { 0x19, 0x19, NULL,         NULL },
                    681:        { 0x1c, 0x1f, NULL,         NULL },
                    682:        { 0x20, 0xff, input_input,  NULL },
1.1       nicm      683:
1.28      nicm      684:        { -1, -1, NULL, NULL }
                    685: };
1.1       nicm      686:
1.28      nicm      687: /* consume_st state table. */
                    688: const struct input_transition input_state_consume_st_table[] = {
                    689:        INPUT_STATE_ANYWHERE,
                    690:
                    691:        { 0x00, 0x17, NULL,         NULL },
                    692:        { 0x19, 0x19, NULL,         NULL },
                    693:        { 0x1c, 0x1f, NULL,         NULL },
                    694:        { 0x20, 0xff, NULL,         NULL },
1.6       nicm      695:
1.28      nicm      696:        { -1, -1, NULL, NULL }
                    697: };
1.6       nicm      698:
1.28      nicm      699: /* utf8_three state table. */
                    700: const struct input_transition input_state_utf8_three_table[] = {
                    701:        /* No INPUT_STATE_ANYWHERE */
                    702:
                    703:        { 0x00, 0x7f, NULL,             &input_state_ground },
                    704:        { 0x80, 0xbf, input_utf8_add,   &input_state_utf8_two },
                    705:        { 0xc0, 0xff, NULL,             &input_state_ground },
1.4       nicm      706:
1.28      nicm      707:        { -1, -1, NULL, NULL }
                    708: };
1.1       nicm      709:
1.28      nicm      710: /* utf8_two state table. */
                    711: const struct input_transition input_state_utf8_two_table[] = {
                    712:        /* No INPUT_STATE_ANYWHERE */
                    713:
                    714:        { 0x00, 0x7f, NULL,           &input_state_ground },
                    715:        { 0x80, 0xbf, input_utf8_add, &input_state_utf8_one },
                    716:        { 0xc0, 0xff, NULL,           &input_state_ground },
1.1       nicm      717:
1.28      nicm      718:        { -1, -1, NULL, NULL }
                    719: };
1.1       nicm      720:
1.28      nicm      721: /* utf8_one state table. */
                    722: const struct input_transition input_state_utf8_one_table[] = {
                    723:        /* No INPUT_STATE_ANYWHERE */
                    724:
                    725:        { 0x00, 0x7f, NULL,             &input_state_ground },
                    726:        { 0x80, 0xbf, input_utf8_close, &input_state_ground },
                    727:        { 0xc0, 0xff, NULL,             &input_state_ground },
1.1       nicm      728:
1.28      nicm      729:        { -1, -1, NULL, NULL }
                    730: };
1.1       nicm      731:
1.28      nicm      732: /* Input table compare. */
                    733: int
                    734: input_table_compare(const void *key, const void *value)
1.1       nicm      735: {
1.28      nicm      736:        const struct input_ctx          *ictx = key;
                    737:        const struct input_table_entry  *entry = value;
1.1       nicm      738:
1.28      nicm      739:        if (ictx->ch != entry->ch)
                    740:                return (ictx->ch - entry->ch);
                    741:        return (strcmp(ictx->interm_buf, entry->interm));
1.1       nicm      742: }
                    743:
1.69      nicm      744: /* Reset cell state to default. */
                    745: void
                    746: input_reset_cell(struct input_ctx *ictx)
                    747: {
                    748:        memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
                    749:        ictx->cell.set = 0;
                    750:        ictx->cell.g0set = ictx->cell.g1set = 0;
                    751:
                    752:        memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
                    753:        ictx->old_cx = 0;
                    754:        ictx->old_cy = 0;
                    755: }
                    756:
1.28      nicm      757: /* Initialise input parser. */
1.1       nicm      758: void
1.28      nicm      759: input_init(struct window_pane *wp)
1.1       nicm      760: {
1.74      nicm      761:        struct input_ctx        *ictx;
                    762:
                    763:        ictx = wp->ictx = xcalloc(1, sizeof *ictx);
1.1       nicm      764:
1.69      nicm      765:        input_reset_cell(ictx);
1.1       nicm      766:
1.28      nicm      767:        *ictx->interm_buf = '\0';
                    768:        ictx->interm_len = 0;
1.1       nicm      769:
1.28      nicm      770:        *ictx->param_buf = '\0';
                    771:        ictx->param_len = 0;
1.1       nicm      772:
1.67      nicm      773:        ictx->input_space = INPUT_BUF_START;
                    774:        ictx->input_buf = xmalloc(INPUT_BUF_START);
                    775:
                    776:        *ictx->input_buf = '\0';
                    777:        ictx->input_len = 0;
                    778:
1.28      nicm      779:        ictx->state = &input_state_ground;
                    780:        ictx->flags = 0;
1.52      nicm      781:
                    782:        ictx->since_ground = evbuffer_new();
1.1       nicm      783: }
                    784:
1.28      nicm      785: /* Destroy input parser. */
1.1       nicm      786: void
1.52      nicm      787: input_free(struct window_pane *wp)
1.1       nicm      788: {
1.74      nicm      789:        struct input_ctx        *ictx = wp->ictx;
                    790:
                    791:        free(ictx->input_buf);
                    792:        evbuffer_free(ictx->since_ground);
                    793:
                    794:        free (ictx);
                    795:        wp->ictx = NULL;
                    796: }
                    797:
                    798: /* Reset input state and clear screen. */
                    799: void
                    800: input_reset(struct window_pane *wp)
                    801: {
                    802:        struct input_ctx        *ictx = wp->ictx;
1.67      nicm      803:
1.80      nicm      804:        input_reset_cell(ictx);
1.74      nicm      805:
                    806:        if (wp->mode == NULL)
                    807:                screen_write_start(&ictx->ctx, wp, &wp->base);
                    808:        else
                    809:                screen_write_start(&ictx->ctx, NULL, &wp->base);
                    810:        screen_write_reset(&ictx->ctx);
                    811:        screen_write_stop(&ictx->ctx);
                    812: }
                    813:
                    814: /* Return pending data. */
                    815: struct evbuffer *
                    816: input_pending(struct window_pane *wp)
                    817: {
                    818:        return (wp->ictx->since_ground);
1.52      nicm      819: }
                    820:
                    821: /* Change input state. */
                    822: void
                    823: input_set_state(struct window_pane *wp, const struct input_transition *itr)
                    824: {
1.74      nicm      825:        struct input_ctx        *ictx = wp->ictx;
1.52      nicm      826:
                    827:        if (ictx->state->exit != NULL)
                    828:                ictx->state->exit(ictx);
                    829:        ictx->state = itr->state;
                    830:        if (ictx->state->enter != NULL)
                    831:                ictx->state->enter(ictx);
1.1       nicm      832: }
                    833:
1.28      nicm      834: /* Parse input. */
1.1       nicm      835: void
1.28      nicm      836: input_parse(struct window_pane *wp)
1.1       nicm      837: {
1.74      nicm      838:        struct input_ctx                *ictx = wp->ictx;
1.28      nicm      839:        const struct input_transition   *itr;
                    840:        struct evbuffer                 *evb = wp->event->input;
                    841:        u_char                          *buf;
                    842:        size_t                           len, off;
1.1       nicm      843:
1.28      nicm      844:        if (EVBUFFER_LENGTH(evb) == 0)
                    845:                return;
1.82      nicm      846:
1.83      nicm      847:        window_update_activity(wp->window);
1.82      nicm      848:        wp->flags |= PANE_CHANGED;
1.30      nicm      849:
1.28      nicm      850:        /*
                    851:         * Open the screen. Use NULL wp if there is a mode set as don't want to
                    852:         * update the tty.
                    853:         */
                    854:        if (wp->mode == NULL)
                    855:                screen_write_start(&ictx->ctx, wp, &wp->base);
                    856:        else
                    857:                screen_write_start(&ictx->ctx, NULL, &wp->base);
                    858:        ictx->wp = wp;
1.7       nicm      859:
1.28      nicm      860:        buf = EVBUFFER_DATA(evb);
                    861:        len = EVBUFFER_LENGTH(evb);
1.54      nicm      862:        notify_input(wp, evb);
1.28      nicm      863:        off = 0;
                    864:
1.84      nicm      865:        log_debug("%s: %s, %zu bytes: %.*s", __func__, ictx->state->name, len,
                    866:            (int)len, buf);
                    867:
1.28      nicm      868:        /* Parse the input. */
                    869:        while (off < len) {
                    870:                ictx->ch = buf[off++];
                    871:
                    872:                /* Find the transition. */
                    873:                itr = ictx->state->transitions;
                    874:                while (itr->first != -1 && itr->last != -1) {
                    875:                        if (ictx->ch >= itr->first && ictx->ch <= itr->last)
1.7       nicm      876:                                break;
1.28      nicm      877:                        itr++;
1.1       nicm      878:                }
1.28      nicm      879:                if (itr->first == -1 || itr->last == -1) {
                    880:                        /* No transition? Eh? */
1.85    ! nicm      881:                        fatalx("no transition from state");
1.3       nicm      882:                }
1.1       nicm      883:
                    884:                /*
1.28      nicm      885:                 * Execute the handler, if any. Don't switch state if it
                    886:                 * returns non-zero.
1.1       nicm      887:                 */
1.31      nicm      888:                if (itr->handler != NULL && itr->handler(ictx) != 0)
1.28      nicm      889:                        continue;
                    890:
                    891:                /* And switch state, if necessary. */
1.52      nicm      892:                if (itr->state != NULL)
                    893:                        input_set_state(wp, itr);
                    894:
                    895:                /* If not in ground state, save input. */
                    896:                if (ictx->state != &input_state_ground)
                    897:                        evbuffer_add(ictx->since_ground, &ictx->ch, 1);
1.1       nicm      898:        }
                    899:
1.28      nicm      900:        /* Close the screen. */
                    901:        screen_write_stop(&ictx->ctx);
1.1       nicm      902:
1.28      nicm      903:        evbuffer_drain(evb, len);
1.1       nicm      904: }
                    905:
1.28      nicm      906: /* Split the parameter list (if any). */
                    907: int
                    908: input_split(struct input_ctx *ictx)
1.1       nicm      909:
                    910: {
1.28      nicm      911:        const char      *errstr;
                    912:        char            *ptr, *out;
                    913:        int              n;
1.1       nicm      914:
1.28      nicm      915:        ictx->param_list_len = 0;
                    916:        if (ictx->param_len == 0)
                    917:                return (0);
1.1       nicm      918:
1.28      nicm      919:        ptr = ictx->param_buf;
                    920:        while ((out = strsep(&ptr, ";")) != NULL) {
                    921:                if (*out == '\0')
                    922:                        n = -1;
                    923:                else {
                    924:                        n = strtonum(out, 0, INT_MAX, &errstr);
                    925:                        if (errstr != NULL)
                    926:                                return (-1);
                    927:                }
1.1       nicm      928:
1.28      nicm      929:                ictx->param_list[ictx->param_list_len++] = n;
                    930:                if (ictx->param_list_len == nitems(ictx->param_list))
                    931:                        return (-1);
                    932:        }
1.1       nicm      933:
1.28      nicm      934:        return (0);
1.1       nicm      935: }
                    936:
1.40      nicm      937: /* Get an argument or return default value. */
1.28      nicm      938: int
                    939: input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1.1       nicm      940: {
1.28      nicm      941:        int     retval;
1.1       nicm      942:
1.28      nicm      943:        if (validx >= ictx->param_list_len)
                    944:            return (defval);
1.1       nicm      945:
1.28      nicm      946:        retval = ictx->param_list[validx];
                    947:        if (retval == -1)
                    948:                return (defval);
                    949:        if (retval < minval)
                    950:                return (minval);
                    951:        return (retval);
1.1       nicm      952: }
                    953:
1.28      nicm      954: /* Reply to terminal query. */
1.1       nicm      955: void
1.28      nicm      956: input_reply(struct input_ctx *ictx, const char *fmt, ...)
1.1       nicm      957: {
1.28      nicm      958:        va_list ap;
                    959:        char   *reply;
1.1       nicm      960:
1.28      nicm      961:        va_start(ap, fmt);
                    962:        vasprintf(&reply, fmt, ap);
                    963:        va_end(ap);
1.1       nicm      964:
1.28      nicm      965:        bufferevent_write(ictx->wp->event, reply, strlen(reply));
1.53      nicm      966:        free(reply);
1.8       nicm      967: }
                    968:
1.28      nicm      969: /* Clear saved state. */
1.8       nicm      970: void
1.28      nicm      971: input_clear(struct input_ctx *ictx)
1.8       nicm      972: {
1.28      nicm      973:        *ictx->interm_buf = '\0';
                    974:        ictx->interm_len = 0;
1.8       nicm      975:
1.28      nicm      976:        *ictx->param_buf = '\0';
                    977:        ictx->param_len = 0;
1.8       nicm      978:
1.35      nicm      979:        *ictx->input_buf = '\0';
                    980:        ictx->input_len = 0;
                    981:
1.28      nicm      982:        ictx->flags &= ~INPUT_DISCARD;
1.14      nicm      983: }
                    984:
1.67      nicm      985: /* Reset for ground state. */
                    986: void
                    987: input_ground(struct input_ctx *ictx)
                    988: {
                    989:        evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
                    990:
                    991:        if (ictx->input_space > INPUT_BUF_START) {
                    992:                ictx->input_space = INPUT_BUF_START;
1.71      nicm      993:                ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1.67      nicm      994:        }
                    995: }
                    996:
1.28      nicm      997: /* Output this character to the screen. */
                    998: int
                    999: input_print(struct input_ctx *ictx)
1.14      nicm     1000: {
1.69      nicm     1001:        int     set;
                   1002:
                   1003:        set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
                   1004:        if (set == 1)
                   1005:                ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
                   1006:        else
                   1007:                ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
                   1008:
                   1009:        grid_cell_one(&ictx->cell.cell, ictx->ch);
                   1010:        screen_write_cell(&ictx->ctx, &ictx->cell.cell);
                   1011:
                   1012:        ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1.14      nicm     1013:
1.28      nicm     1014:        return (0);
1.1       nicm     1015: }
                   1016:
1.28      nicm     1017: /* Collect intermediate string. */
                   1018: int
                   1019: input_intermediate(struct input_ctx *ictx)
1.1       nicm     1020: {
1.28      nicm     1021:        if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
                   1022:                ictx->flags |= INPUT_DISCARD;
                   1023:        else {
                   1024:                ictx->interm_buf[ictx->interm_len++] = ictx->ch;
                   1025:                ictx->interm_buf[ictx->interm_len] = '\0';
                   1026:        }
1.1       nicm     1027:
1.28      nicm     1028:        return (0);
1.1       nicm     1029: }
                   1030:
1.28      nicm     1031: /* Collect parameter string. */
                   1032: int
                   1033: input_parameter(struct input_ctx *ictx)
1.1       nicm     1034: {
1.28      nicm     1035:        if (ictx->param_len == (sizeof ictx->param_buf) - 1)
                   1036:                ictx->flags |= INPUT_DISCARD;
                   1037:        else {
                   1038:                ictx->param_buf[ictx->param_len++] = ictx->ch;
                   1039:                ictx->param_buf[ictx->param_len] = '\0';
                   1040:        }
1.1       nicm     1041:
1.28      nicm     1042:        return (0);
1.1       nicm     1043: }
                   1044:
1.28      nicm     1045: /* Collect input string. */
                   1046: int
                   1047: input_input(struct input_ctx *ictx)
1.1       nicm     1048: {
1.67      nicm     1049:        size_t available;
                   1050:
                   1051:        available = ictx->input_space;
                   1052:        while (ictx->input_len + 1 >= available) {
                   1053:                available *= 2;
                   1054:                if (available > INPUT_BUF_LIMIT) {
                   1055:                        ictx->flags |= INPUT_DISCARD;
                   1056:                        return (0);
                   1057:                }
1.71      nicm     1058:                ictx->input_buf = xrealloc(ictx->input_buf, available);
1.67      nicm     1059:                ictx->input_space = available;
1.28      nicm     1060:        }
1.67      nicm     1061:        ictx->input_buf[ictx->input_len++] = ictx->ch;
                   1062:        ictx->input_buf[ictx->input_len] = '\0';
1.1       nicm     1063:
1.28      nicm     1064:        return (0);
1.1       nicm     1065: }
                   1066:
1.28      nicm     1067: /* Execute C0 control sequence. */
                   1068: int
                   1069: input_c0_dispatch(struct input_ctx *ictx)
1.1       nicm     1070: {
1.28      nicm     1071:        struct screen_write_ctx *sctx = &ictx->ctx;
                   1072:        struct window_pane      *wp = ictx->wp;
                   1073:        struct screen           *s = sctx->s;
1.1       nicm     1074:
1.84      nicm     1075:        log_debug("%s: '%c'", __func__, ictx->ch);
1.1       nicm     1076:
1.28      nicm     1077:        switch (ictx->ch) {
                   1078:        case '\000':    /* NUL */
                   1079:                break;
                   1080:        case '\007':    /* BEL */
1.83      nicm     1081:                alerts_queue(wp->window, WINDOW_BELL);
1.28      nicm     1082:                break;
                   1083:        case '\010':    /* BS */
                   1084:                screen_write_backspace(sctx);
1.75      nicm     1085:                break;
1.28      nicm     1086:        case '\011':    /* HT */
                   1087:                /* Don't tab beyond the end of the line. */
                   1088:                if (s->cx >= screen_size_x(s) - 1)
                   1089:                        break;
1.1       nicm     1090:
1.28      nicm     1091:                /* Find the next tab point, or use the last column if none. */
                   1092:                do {
                   1093:                        s->cx++;
                   1094:                        if (bit_test(s->tabs, s->cx))
                   1095:                                break;
                   1096:                } while (s->cx < screen_size_x(s) - 1);
                   1097:                break;
                   1098:        case '\012':    /* LF */
                   1099:        case '\013':    /* VT */
                   1100:        case '\014':    /* FF */
                   1101:                screen_write_linefeed(sctx, 0);
1.75      nicm     1102:                break;
1.28      nicm     1103:        case '\015':    /* CR */
                   1104:                screen_write_carriagereturn(sctx);
1.75      nicm     1105:                break;
1.28      nicm     1106:        case '\016':    /* SO */
1.69      nicm     1107:                ictx->cell.set = 1;
1.28      nicm     1108:                break;
                   1109:        case '\017':    /* SI */
1.69      nicm     1110:                ictx->cell.set = 0;
1.28      nicm     1111:                break;
                   1112:        default:
                   1113:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1114:                break;
                   1115:        }
1.1       nicm     1116:
1.28      nicm     1117:        return (0);
1.7       nicm     1118: }
                   1119:
1.28      nicm     1120: /* Execute escape sequence. */
                   1121: int
                   1122: input_esc_dispatch(struct input_ctx *ictx)
1.7       nicm     1123: {
1.31      nicm     1124:        struct screen_write_ctx         *sctx = &ictx->ctx;
                   1125:        struct screen                   *s = sctx->s;
                   1126:        struct input_table_entry        *entry;
1.7       nicm     1127:
1.28      nicm     1128:        if (ictx->flags & INPUT_DISCARD)
                   1129:                return (0);
                   1130:        log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1.7       nicm     1131:
1.28      nicm     1132:        entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
                   1133:            sizeof input_esc_table[0], input_table_compare);
                   1134:        if (entry == NULL) {
                   1135:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1136:                return (0);
                   1137:        }
1.7       nicm     1138:
1.28      nicm     1139:        switch (entry->type) {
                   1140:        case INPUT_ESC_RIS:
1.69      nicm     1141:                input_reset_cell(ictx);
1.46      nicm     1142:                screen_write_reset(sctx);
1.28      nicm     1143:                break;
                   1144:        case INPUT_ESC_IND:
                   1145:                screen_write_linefeed(sctx, 0);
                   1146:                break;
                   1147:        case INPUT_ESC_NEL:
                   1148:                screen_write_carriagereturn(sctx);
                   1149:                screen_write_linefeed(sctx, 0);
                   1150:                break;
                   1151:        case INPUT_ESC_HTS:
                   1152:                if (s->cx < screen_size_x(s))
                   1153:                        bit_set(s->tabs, s->cx);
                   1154:                break;
                   1155:        case INPUT_ESC_RI:
                   1156:                screen_write_reverseindex(sctx);
                   1157:                break;
                   1158:        case INPUT_ESC_DECKPAM:
1.59      nicm     1159:                screen_write_mode_set(sctx, MODE_KKEYPAD);
1.28      nicm     1160:                break;
                   1161:        case INPUT_ESC_DECKPNM:
1.59      nicm     1162:                screen_write_mode_clear(sctx, MODE_KKEYPAD);
1.1       nicm     1163:                break;
1.28      nicm     1164:        case INPUT_ESC_DECSC:
                   1165:                memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
                   1166:                ictx->old_cx = s->cx;
                   1167:                ictx->old_cy = s->cy;
                   1168:                break;
                   1169:        case INPUT_ESC_DECRC:
                   1170:                memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
                   1171:                screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
                   1172:                break;
                   1173:        case INPUT_ESC_DECALN:
                   1174:                screen_write_alignmenttest(sctx);
                   1175:                break;
1.69      nicm     1176:        case INPUT_ESC_SCSG0_ON:
                   1177:                ictx->cell.g0set = 1;
                   1178:                break;
                   1179:        case INPUT_ESC_SCSG0_OFF:
                   1180:                ictx->cell.g0set = 0;
                   1181:                break;
                   1182:        case INPUT_ESC_SCSG1_ON:
                   1183:                ictx->cell.g1set = 1;
1.1       nicm     1184:                break;
1.69      nicm     1185:        case INPUT_ESC_SCSG1_OFF:
                   1186:                ictx->cell.g1set = 0;
1.1       nicm     1187:                break;
                   1188:        }
1.28      nicm     1189:
                   1190:        return (0);
1.1       nicm     1191: }
                   1192:
1.28      nicm     1193: /* Execute control sequence. */
                   1194: int
                   1195: input_csi_dispatch(struct input_ctx *ictx)
1.1       nicm     1196: {
1.28      nicm     1197:        struct screen_write_ctx        *sctx = &ictx->ctx;
                   1198:        struct screen                  *s = sctx->s;
                   1199:        struct input_table_entry       *entry;
1.65      nicm     1200:        int                             n, m;
1.81      nicm     1201:        u_int                           cx;
1.1       nicm     1202:
1.28      nicm     1203:        if (ictx->flags & INPUT_DISCARD)
                   1204:                return (0);
                   1205:        if (input_split(ictx) != 0)
                   1206:                return (0);
                   1207:        log_debug("%s: '%c' \"%s\" \"%s\"",
                   1208:            __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1.1       nicm     1209:
1.28      nicm     1210:        entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
                   1211:            sizeof input_csi_table[0], input_table_compare);
                   1212:        if (entry == NULL) {
                   1213:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1214:                return (0);
                   1215:        }
1.1       nicm     1216:
1.28      nicm     1217:        switch (entry->type) {
                   1218:        case INPUT_CSI_CBT:
                   1219:                /* Find the previous tab point, n times. */
1.81      nicm     1220:                cx = s->cx;
                   1221:                if (cx > screen_size_x(s) - 1)
                   1222:                        cx = screen_size_x(s) - 1;
1.28      nicm     1223:                n = input_get(ictx, 0, 1, 1);
1.81      nicm     1224:                while (cx > 0 && n-- > 0) {
1.28      nicm     1225:                        do
1.81      nicm     1226:                                cx--;
                   1227:                        while (cx > 0 && !bit_test(s->tabs, cx));
1.28      nicm     1228:                }
1.81      nicm     1229:                s->cx = cx;
1.28      nicm     1230:                break;
                   1231:        case INPUT_CSI_CUB:
                   1232:                screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1));
                   1233:                break;
                   1234:        case INPUT_CSI_CUD:
                   1235:                screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
                   1236:                break;
                   1237:        case INPUT_CSI_CUF:
                   1238:                screen_write_cursorright(sctx, input_get(ictx, 0, 1, 1));
                   1239:                break;
                   1240:        case INPUT_CSI_CUP:
                   1241:                n = input_get(ictx, 0, 1, 1);
                   1242:                m = input_get(ictx, 1, 1, 1);
                   1243:                screen_write_cursormove(sctx, m - 1, n - 1);
                   1244:                break;
1.65      nicm     1245:        case INPUT_CSI_WINOPS:
                   1246:                input_csi_dispatch_winops(ictx);
                   1247:                break;
1.28      nicm     1248:        case INPUT_CSI_CUU:
1.43      nicm     1249:                screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
                   1250:                break;
                   1251:        case INPUT_CSI_CNL:
                   1252:                screen_write_carriagereturn(sctx);
                   1253:                screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
                   1254:                break;
                   1255:        case INPUT_CSI_CPL:
                   1256:                screen_write_carriagereturn(sctx);
1.28      nicm     1257:                screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
                   1258:                break;
                   1259:        case INPUT_CSI_DA:
                   1260:                switch (input_get(ictx, 0, 0, 0)) {
                   1261:                case 0:
                   1262:                        input_reply(ictx, "\033[?1;2c");
1.50      nicm     1263:                        break;
                   1264:                default:
                   1265:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1266:                        break;
                   1267:                }
                   1268:                break;
                   1269:        case INPUT_CSI_DA_TWO:
                   1270:                switch (input_get(ictx, 0, 0, 0)) {
                   1271:                case 0:
1.66      nicm     1272:                        input_reply(ictx, "\033[>84;0;0c");
1.28      nicm     1273:                        break;
                   1274:                default:
                   1275:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1276:                        break;
                   1277:                }
1.56      nicm     1278:                break;
                   1279:        case INPUT_CSI_ECH:
                   1280:                screen_write_clearcharacter(sctx, input_get(ictx, 0, 1, 1));
1.28      nicm     1281:                break;
                   1282:        case INPUT_CSI_DCH:
                   1283:                screen_write_deletecharacter(sctx, input_get(ictx, 0, 1, 1));
                   1284:                break;
                   1285:        case INPUT_CSI_DECSTBM:
                   1286:                n = input_get(ictx, 0, 1, 1);
                   1287:                m = input_get(ictx, 1, 1, screen_size_y(s));
                   1288:                screen_write_scrollregion(sctx, n - 1, m - 1);
1.1       nicm     1289:                break;
1.28      nicm     1290:        case INPUT_CSI_DL:
                   1291:                screen_write_deleteline(sctx, input_get(ictx, 0, 1, 1));
1.1       nicm     1292:                break;
1.28      nicm     1293:        case INPUT_CSI_DSR:
                   1294:                switch (input_get(ictx, 0, 0, 0)) {
                   1295:                case 5:
                   1296:                        input_reply(ictx, "\033[0n");
                   1297:                        break;
                   1298:                case 6:
                   1299:                        input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
                   1300:                        break;
                   1301:                default:
                   1302:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1303:                        break;
                   1304:                }
                   1305:                break;
                   1306:        case INPUT_CSI_ED:
                   1307:                switch (input_get(ictx, 0, 0, 0)) {
                   1308:                case 0:
                   1309:                        screen_write_clearendofscreen(sctx);
                   1310:                        break;
                   1311:                case 1:
                   1312:                        screen_write_clearstartofscreen(sctx);
                   1313:                        break;
                   1314:                case 2:
                   1315:                        screen_write_clearscreen(sctx);
1.41      nicm     1316:                        break;
                   1317:                case 3:
                   1318:                        switch (input_get(ictx, 1, 0, 0)) {
                   1319:                        case 0:
                   1320:                                /*
                   1321:                                 * Linux console extension to clear history
                   1322:                                 * (for example before locking the screen).
                   1323:                                 */
                   1324:                                screen_write_clearhistory(sctx);
                   1325:                                break;
                   1326:                        }
1.28      nicm     1327:                        break;
                   1328:                default:
                   1329:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1330:                        break;
                   1331:                }
                   1332:                break;
                   1333:        case INPUT_CSI_EL:
                   1334:                switch (input_get(ictx, 0, 0, 0)) {
                   1335:                case 0:
                   1336:                        screen_write_clearendofline(sctx);
                   1337:                        break;
                   1338:                case 1:
                   1339:                        screen_write_clearstartofline(sctx);
                   1340:                        break;
                   1341:                case 2:
                   1342:                        screen_write_clearline(sctx);
                   1343:                        break;
                   1344:                default:
                   1345:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1346:                        break;
                   1347:                }
                   1348:                break;
                   1349:        case INPUT_CSI_HPA:
                   1350:                n = input_get(ictx, 0, 1, 1);
                   1351:                screen_write_cursormove(sctx, n - 1, s->cy);
                   1352:                break;
                   1353:        case INPUT_CSI_ICH:
                   1354:                screen_write_insertcharacter(sctx, input_get(ictx, 0, 1, 1));
                   1355:                break;
                   1356:        case INPUT_CSI_IL:
                   1357:                screen_write_insertline(sctx, input_get(ictx, 0, 1, 1));
                   1358:                break;
1.42      nicm     1359:        case INPUT_CSI_RCP:
                   1360:                memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
                   1361:                screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
                   1362:                break;
1.28      nicm     1363:        case INPUT_CSI_RM:
1.64      nicm     1364:                input_csi_dispatch_rm(ictx);
                   1365:                break;
                   1366:        case INPUT_CSI_RM_PRIVATE:
                   1367:                input_csi_dispatch_rm_private(ictx);
                   1368:                break;
                   1369:        case INPUT_CSI_SCP:
                   1370:                memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
                   1371:                ictx->old_cx = s->cx;
                   1372:                ictx->old_cy = s->cy;
                   1373:                break;
                   1374:        case INPUT_CSI_SGR:
                   1375:                input_csi_dispatch_sgr(ictx);
                   1376:                break;
                   1377:        case INPUT_CSI_SM:
                   1378:                input_csi_dispatch_sm(ictx);
                   1379:                break;
                   1380:        case INPUT_CSI_SM_PRIVATE:
                   1381:                input_csi_dispatch_sm_private(ictx);
                   1382:                break;
                   1383:        case INPUT_CSI_TBC:
                   1384:                switch (input_get(ictx, 0, 0, 0)) {
                   1385:                case 0:
                   1386:                        if (s->cx < screen_size_x(s))
                   1387:                                bit_clear(s->tabs, s->cx);
                   1388:                        break;
                   1389:                case 3:
                   1390:                        bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
                   1391:                        break;
                   1392:                default:
                   1393:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1394:                        break;
                   1395:                }
                   1396:                break;
                   1397:        case INPUT_CSI_VPA:
                   1398:                n = input_get(ictx, 0, 1, 1);
                   1399:                screen_write_cursormove(sctx, s->cx, n - 1);
                   1400:                break;
                   1401:        case INPUT_CSI_DECSCUSR:
                   1402:                n = input_get(ictx, 0, 0, 0);
                   1403:                screen_set_cursor_style(s, n);
                   1404:                break;
                   1405:        }
                   1406:
                   1407:        return (0);
                   1408: }
                   1409:
                   1410: /* Handle CSI RM. */
                   1411: void
                   1412: input_csi_dispatch_rm(struct input_ctx *ictx)
                   1413: {
                   1414:        u_int   i;
                   1415:
                   1416:        for (i = 0; i < ictx->param_list_len; i++) {
                   1417:                switch (input_get(ictx, i, 0, -1)) {
1.28      nicm     1418:                case 4:         /* IRM */
1.59      nicm     1419:                        screen_write_mode_clear(&ictx->ctx, MODE_INSERT);
1.28      nicm     1420:                        break;
1.72      nicm     1421:                case 34:
                   1422:                        screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
                   1423:                        break;
1.28      nicm     1424:                default:
                   1425:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1426:                        break;
                   1427:                }
1.64      nicm     1428:        }
                   1429: }
                   1430:
                   1431: /* Handle CSI private RM. */
                   1432: void
                   1433: input_csi_dispatch_rm_private(struct input_ctx *ictx)
                   1434: {
1.69      nicm     1435:        struct window_pane      *wp = ictx->wp;
                   1436:        u_int                    i;
1.64      nicm     1437:
                   1438:        for (i = 0; i < ictx->param_list_len; i++) {
                   1439:                switch (input_get(ictx, i, 0, -1)) {
                   1440:                case 1:         /* DECCKM */
1.59      nicm     1441:                        screen_write_mode_clear(&ictx->ctx, MODE_KCURSOR);
1.1       nicm     1442:                        break;
1.17      nicm     1443:                case 3:         /* DECCOLM */
1.24      nicm     1444:                        screen_write_cursormove(&ictx->ctx, 0, 0);
1.17      nicm     1445:                        screen_write_clearscreen(&ictx->ctx);
                   1446:                        break;
1.61      nicm     1447:                case 7:         /* DECAWM */
                   1448:                        screen_write_mode_clear(&ictx->ctx, MODE_WRAP);
                   1449:                        break;
1.72      nicm     1450:                case 12:
                   1451:                        screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
                   1452:                        break;
1.1       nicm     1453:                case 25:        /* TCEM */
1.59      nicm     1454:                        screen_write_mode_clear(&ictx->ctx, MODE_CURSOR);
1.1       nicm     1455:                        break;
                   1456:                case 1000:
1.32      nicm     1457:                case 1001:
                   1458:                case 1002:
1.59      nicm     1459:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
1.1       nicm     1460:                        break;
1.62      nicm     1461:                case 1004:
                   1462:                        screen_write_mode_clear(&ictx->ctx, MODE_FOCUSON);
                   1463:                        break;
1.33      nicm     1464:                case 1005:
1.59      nicm     1465:                        screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_UTF8);
1.33      nicm     1466:                        break;
1.60      nicm     1467:                case 1006:
                   1468:                        screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_SGR);
                   1469:                        break;
1.55      nicm     1470:                case 47:
                   1471:                case 1047:
1.69      nicm     1472:                        window_pane_alternate_off(wp, &ictx->cell.cell, 0);
1.55      nicm     1473:                        break;
1.9       nicm     1474:                case 1049:
1.69      nicm     1475:                        window_pane_alternate_off(wp, &ictx->cell.cell, 1);
1.9       nicm     1476:                        break;
1.49      nicm     1477:                case 2004:
1.59      nicm     1478:                        screen_write_mode_clear(&ictx->ctx, MODE_BRACKETPASTE);
1.49      nicm     1479:                        break;
1.1       nicm     1480:                default:
1.28      nicm     1481:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1482:                        break;
                   1483:                }
1.64      nicm     1484:        }
                   1485: }
                   1486:
                   1487: /* Handle CSI SM. */
                   1488: void
                   1489: input_csi_dispatch_sm(struct input_ctx *ictx)
                   1490: {
                   1491:        u_int   i;
                   1492:
                   1493:        for (i = 0; i < ictx->param_list_len; i++) {
                   1494:                switch (input_get(ictx, i, 0, -1)) {
1.1       nicm     1495:                case 4:         /* IRM */
1.59      nicm     1496:                        screen_write_mode_set(&ictx->ctx, MODE_INSERT);
1.1       nicm     1497:                        break;
1.72      nicm     1498:                case 34:
                   1499:                        screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
                   1500:                        break;
1.1       nicm     1501:                default:
1.28      nicm     1502:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1503:                        break;
                   1504:                }
1.64      nicm     1505:        }
                   1506: }
                   1507:
                   1508: /* Handle CSI private SM. */
                   1509: void
                   1510: input_csi_dispatch_sm_private(struct input_ctx *ictx)
                   1511: {
1.69      nicm     1512:        struct window_pane      *wp = ictx->wp;
                   1513:        u_int                    i;
1.64      nicm     1514:
                   1515:        for (i = 0; i < ictx->param_list_len; i++) {
                   1516:                switch (input_get(ictx, i, 0, -1)) {
                   1517:                case 1:         /* DECCKM */
1.59      nicm     1518:                        screen_write_mode_set(&ictx->ctx, MODE_KCURSOR);
1.17      nicm     1519:                        break;
                   1520:                case 3:         /* DECCOLM */
1.24      nicm     1521:                        screen_write_cursormove(&ictx->ctx, 0, 0);
1.17      nicm     1522:                        screen_write_clearscreen(&ictx->ctx);
1.61      nicm     1523:                        break;
                   1524:                case 7:         /* DECAWM */
                   1525:                        screen_write_mode_set(&ictx->ctx, MODE_WRAP);
1.72      nicm     1526:                        break;
                   1527:                case 12:
                   1528:                        screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
1.1       nicm     1529:                        break;
                   1530:                case 25:        /* TCEM */
1.59      nicm     1531:                        screen_write_mode_set(&ictx->ctx, MODE_CURSOR);
1.1       nicm     1532:                        break;
                   1533:                case 1000:
1.59      nicm     1534:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
                   1535:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_STANDARD);
1.32      nicm     1536:                        break;
                   1537:                case 1002:
1.59      nicm     1538:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
                   1539:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_BUTTON);
1.62      nicm     1540:                        break;
                   1541:                case 1004:
1.64      nicm     1542:                        if (ictx->ctx.s->mode & MODE_FOCUSON)
1.62      nicm     1543:                                break;
                   1544:                        screen_write_mode_set(&ictx->ctx, MODE_FOCUSON);
1.69      nicm     1545:                        wp->flags |= PANE_FOCUSPUSH; /* force update */
1.33      nicm     1546:                        break;
                   1547:                case 1005:
1.59      nicm     1548:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_UTF8);
1.60      nicm     1549:                        break;
                   1550:                case 1006:
                   1551:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_SGR);
1.9       nicm     1552:                        break;
1.55      nicm     1553:                case 47:
                   1554:                case 1047:
1.69      nicm     1555:                        window_pane_alternate_on(wp, &ictx->cell.cell, 0);
1.55      nicm     1556:                        break;
1.9       nicm     1557:                case 1049:
1.69      nicm     1558:                        window_pane_alternate_on(wp, &ictx->cell.cell, 1);
1.49      nicm     1559:                        break;
                   1560:                case 2004:
1.59      nicm     1561:                        screen_write_mode_set(&ictx->ctx, MODE_BRACKETPASTE);
1.1       nicm     1562:                        break;
                   1563:                default:
1.28      nicm     1564:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1565:                        break;
                   1566:                }
1.65      nicm     1567:        }
                   1568: }
                   1569:
                   1570: /* Handle CSI window operations. */
                   1571: void
                   1572: input_csi_dispatch_winops(struct input_ctx *ictx)
                   1573: {
                   1574:        struct window_pane      *wp = ictx->wp;
                   1575:        int                      n, m;
                   1576:
                   1577:        m = 0;
                   1578:        while ((n = input_get(ictx, m, 0, -1)) != -1) {
                   1579:                switch (n) {
                   1580:                case 1:
                   1581:                case 2:
                   1582:                case 5:
                   1583:                case 6:
                   1584:                case 7:
                   1585:                case 11:
                   1586:                case 13:
                   1587:                case 14:
                   1588:                case 19:
                   1589:                case 20:
                   1590:                case 21:
                   1591:                case 24:
                   1592:                        break;
                   1593:                case 3:
                   1594:                case 4:
                   1595:                case 8:
                   1596:                        m++;
                   1597:                        if (input_get(ictx, m, 0, -1) == -1)
                   1598:                                return;
                   1599:                        /* FALLTHROUGH */
                   1600:                case 9:
                   1601:                case 10:
                   1602:                case 22:
                   1603:                case 23:
                   1604:                        m++;
                   1605:                        if (input_get(ictx, m, 0, -1) == -1)
                   1606:                                return;
                   1607:                        break;
                   1608:                case 18:
1.76      nicm     1609:                        input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx);
1.65      nicm     1610:                        break;
                   1611:                default:
                   1612:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1613:                        break;
                   1614:                }
                   1615:                m++;
1.1       nicm     1616:        }
                   1617: }
                   1618:
1.78      nicm     1619: /* Handle CSI SGR for 256 colours. */
                   1620: void
                   1621: input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
                   1622: {
                   1623:        struct grid_cell        *gc = &ictx->cell.cell;
                   1624:        int                      c;
                   1625:
                   1626:        (*i)++;
                   1627:        c = input_get(ictx, *i, 0, -1);
                   1628:        if (c == -1) {
                   1629:                if (fgbg == 38) {
                   1630:                        gc->flags &= ~GRID_FLAG_FG256;
                   1631:                        gc->fg = 8;
                   1632:                } else if (fgbg == 48) {
                   1633:                        gc->flags &= ~GRID_FLAG_BG256;
                   1634:                        gc->bg = 8;
                   1635:                }
                   1636:        } else {
                   1637:                if (fgbg == 38) {
                   1638:                        gc->flags |= GRID_FLAG_FG256;
                   1639:                        gc->fg = c;
                   1640:                } else if (fgbg == 48) {
                   1641:                        gc->flags |= GRID_FLAG_BG256;
                   1642:                        gc->bg = c;
                   1643:                }
                   1644:        }
                   1645: }
                   1646:
                   1647: /* Handle CSI SGR for RGB colours. */
                   1648: void
                   1649: input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
                   1650: {
                   1651:        struct grid_cell        *gc = &ictx->cell.cell;
                   1652:        int                      c, r, g, b;
                   1653:
                   1654:        (*i)++;
                   1655:        r = input_get(ictx, *i, 0, -1);
                   1656:        if (r == -1 || r > 255)
                   1657:                return;
                   1658:        (*i)++;
                   1659:        g = input_get(ictx, *i, 0, -1);
                   1660:        if (g == -1 || g > 255)
                   1661:                return;
                   1662:        (*i)++;
                   1663:        b = input_get(ictx, *i, 0, -1);
                   1664:        if (b == -1 || b > 255)
                   1665:                return;
                   1666:
                   1667:        c = colour_find_rgb(r, g, b);
                   1668:        if (fgbg == 38) {
                   1669:                gc->flags |= GRID_FLAG_FG256;
                   1670:                gc->fg = c;
                   1671:        } else if (fgbg == 48) {
                   1672:                gc->flags |= GRID_FLAG_BG256;
                   1673:                gc->bg = c;
                   1674:        }
                   1675: }
                   1676:
1.28      nicm     1677: /* Handle CSI SGR. */
1.1       nicm     1678: void
1.28      nicm     1679: input_csi_dispatch_sgr(struct input_ctx *ictx)
1.1       nicm     1680: {
1.69      nicm     1681:        struct grid_cell        *gc = &ictx->cell.cell;
1.28      nicm     1682:        u_int                    i;
1.78      nicm     1683:        int                      n;
1.1       nicm     1684:
1.28      nicm     1685:        if (ictx->param_list_len == 0) {
1.1       nicm     1686:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   1687:                return;
                   1688:        }
                   1689:
1.28      nicm     1690:        for (i = 0; i < ictx->param_list_len; i++) {
                   1691:                n = input_get(ictx, i, 0, 0);
1.1       nicm     1692:
1.28      nicm     1693:                if (n == 38 || n == 48) {
1.1       nicm     1694:                        i++;
1.78      nicm     1695:                        switch (input_get(ictx, i, 0, -1)) {
                   1696:                        case 2:
                   1697:                                input_csi_dispatch_sgr_rgb(ictx, n, &i);
                   1698:                                break;
                   1699:                        case 5:
                   1700:                                input_csi_dispatch_sgr_256(ictx, n, &i);
                   1701:                                break;
1.1       nicm     1702:                        }
                   1703:                        continue;
                   1704:                }
                   1705:
1.28      nicm     1706:                switch (n) {
1.1       nicm     1707:                case 0:
                   1708:                case 10:
                   1709:                        memcpy(gc, &grid_default_cell, sizeof *gc);
                   1710:                        break;
                   1711:                case 1:
                   1712:                        gc->attr |= GRID_ATTR_BRIGHT;
                   1713:                        break;
                   1714:                case 2:
                   1715:                        gc->attr |= GRID_ATTR_DIM;
                   1716:                        break;
                   1717:                case 3:
                   1718:                        gc->attr |= GRID_ATTR_ITALICS;
                   1719:                        break;
                   1720:                case 4:
                   1721:                        gc->attr |= GRID_ATTR_UNDERSCORE;
                   1722:                        break;
                   1723:                case 5:
                   1724:                        gc->attr |= GRID_ATTR_BLINK;
                   1725:                        break;
                   1726:                case 7:
                   1727:                        gc->attr |= GRID_ATTR_REVERSE;
                   1728:                        break;
                   1729:                case 8:
                   1730:                        gc->attr |= GRID_ATTR_HIDDEN;
                   1731:                        break;
                   1732:                case 22:
                   1733:                        gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
                   1734:                        break;
                   1735:                case 23:
                   1736:                        gc->attr &= ~GRID_ATTR_ITALICS;
                   1737:                        break;
                   1738:                case 24:
                   1739:                        gc->attr &= ~GRID_ATTR_UNDERSCORE;
                   1740:                        break;
                   1741:                case 25:
                   1742:                        gc->attr &= ~GRID_ATTR_BLINK;
                   1743:                        break;
                   1744:                case 27:
                   1745:                        gc->attr &= ~GRID_ATTR_REVERSE;
                   1746:                        break;
                   1747:                case 30:
                   1748:                case 31:
                   1749:                case 32:
                   1750:                case 33:
                   1751:                case 34:
                   1752:                case 35:
                   1753:                case 36:
                   1754:                case 37:
                   1755:                        gc->flags &= ~GRID_FLAG_FG256;
1.28      nicm     1756:                        gc->fg = n - 30;
1.1       nicm     1757:                        break;
                   1758:                case 39:
                   1759:                        gc->flags &= ~GRID_FLAG_FG256;
                   1760:                        gc->fg = 8;
                   1761:                        break;
                   1762:                case 40:
                   1763:                case 41:
                   1764:                case 42:
                   1765:                case 43:
                   1766:                case 44:
                   1767:                case 45:
                   1768:                case 46:
                   1769:                case 47:
                   1770:                        gc->flags &= ~GRID_FLAG_BG256;
1.28      nicm     1771:                        gc->bg = n - 40;
1.1       nicm     1772:                        break;
                   1773:                case 49:
                   1774:                        gc->flags &= ~GRID_FLAG_BG256;
                   1775:                        gc->bg = 8;
1.20      nicm     1776:                        break;
                   1777:                case 90:
                   1778:                case 91:
                   1779:                case 92:
                   1780:                case 93:
                   1781:                case 94:
                   1782:                case 95:
                   1783:                case 96:
                   1784:                case 97:
1.26      nicm     1785:                        gc->flags &= ~GRID_FLAG_FG256;
1.28      nicm     1786:                        gc->fg = n;
1.20      nicm     1787:                        break;
                   1788:                case 100:
                   1789:                case 101:
                   1790:                case 102:
                   1791:                case 103:
                   1792:                case 104:
                   1793:                case 105:
                   1794:                case 106:
                   1795:                case 107:
1.26      nicm     1796:                        gc->flags &= ~GRID_FLAG_BG256;
1.47      nicm     1797:                        gc->bg = n - 10;
1.1       nicm     1798:                        break;
                   1799:                }
                   1800:        }
1.28      nicm     1801: }
                   1802:
1.37      nicm     1803: /* DCS terminator (ST) received. */
                   1804: int
                   1805: input_dcs_dispatch(struct input_ctx *ictx)
1.28      nicm     1806: {
1.37      nicm     1807:        const char      prefix[] = "tmux;";
                   1808:        const u_int     prefix_len = (sizeof prefix) - 1;
                   1809:
                   1810:        if (ictx->flags & INPUT_DISCARD)
                   1811:                return (0);
                   1812:
                   1813:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
1.28      nicm     1814:
1.37      nicm     1815:        /* Check for tmux prefix. */
                   1816:        if (ictx->input_len >= prefix_len &&
                   1817:            strncmp(ictx->input_buf, prefix, prefix_len) == 0) {
                   1818:                screen_write_rawstring(&ictx->ctx,
                   1819:                    ictx->input_buf + prefix_len, ictx->input_len - prefix_len);
                   1820:        }
1.28      nicm     1821:
1.37      nicm     1822:        return (0);
1.28      nicm     1823: }
                   1824:
                   1825: /* OSC string started. */
                   1826: void
                   1827: input_enter_osc(struct input_ctx *ictx)
                   1828: {
                   1829:        log_debug("%s", __func__);
                   1830:
1.35      nicm     1831:        input_clear(ictx);
1.28      nicm     1832: }
                   1833:
                   1834: /* OSC terminator (ST) received. */
                   1835: void
                   1836: input_exit_osc(struct input_ctx *ictx)
                   1837: {
1.67      nicm     1838:        u_char  *p = ictx->input_buf;
1.73      nicm     1839:        u_int    option;
1.38      nicm     1840:
1.28      nicm     1841:        if (ictx->flags & INPUT_DISCARD)
                   1842:                return;
1.38      nicm     1843:        if (ictx->input_len < 1 || *p < '0' || *p > '9')
                   1844:                return;
1.28      nicm     1845:
1.38      nicm     1846:        log_debug("%s: \"%s\"", __func__, p);
1.28      nicm     1847:
1.38      nicm     1848:        option = 0;
                   1849:        while (*p >= '0' && *p <= '9')
                   1850:                option = option * 10 + *p++ - '0';
                   1851:        if (*p == ';')
                   1852:                p++;
                   1853:
                   1854:        switch (option) {
                   1855:        case 0:
                   1856:        case 2:
                   1857:                screen_set_title(ictx->ctx.s, p);
                   1858:                server_status_window(ictx->wp->window);
                   1859:                break;
                   1860:        case 12:
1.57      nicm     1861:                if (*p != '?') /* ? is colour request */
                   1862:                        screen_set_cursor_colour(ictx->ctx.s, p);
1.38      nicm     1863:                break;
                   1864:        case 112:
1.57      nicm     1865:                if (*p == '\0') /* no arguments allowed */
1.38      nicm     1866:                        screen_set_cursor_colour(ictx->ctx.s, "");
                   1867:                break;
                   1868:        default:
                   1869:                log_debug("%s: unknown '%u'", __func__, option);
                   1870:                break;
                   1871:        }
1.28      nicm     1872: }
                   1873:
                   1874: /* APC string started. */
                   1875: void
                   1876: input_enter_apc(struct input_ctx *ictx)
                   1877: {
                   1878:        log_debug("%s", __func__);
                   1879:
1.35      nicm     1880:        input_clear(ictx);
1.28      nicm     1881: }
                   1882:
                   1883: /* APC terminator (ST) received. */
                   1884: void
                   1885: input_exit_apc(struct input_ctx *ictx)
                   1886: {
                   1887:        if (ictx->flags & INPUT_DISCARD)
                   1888:                return;
                   1889:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
                   1890:
                   1891:        screen_set_title(ictx->ctx.s, ictx->input_buf);
                   1892:        server_status_window(ictx->wp->window);
                   1893: }
                   1894:
                   1895: /* Rename string started. */
                   1896: void
                   1897: input_enter_rename(struct input_ctx *ictx)
                   1898: {
                   1899:        log_debug("%s", __func__);
                   1900:
1.35      nicm     1901:        input_clear(ictx);
1.28      nicm     1902: }
                   1903:
                   1904: /* Rename terminator (ST) received. */
                   1905: void
                   1906: input_exit_rename(struct input_ctx *ictx)
                   1907: {
                   1908:        if (ictx->flags & INPUT_DISCARD)
1.44      nicm     1909:                return;
                   1910:        if (!options_get_number(&ictx->wp->window->options, "allow-rename"))
1.28      nicm     1911:                return;
                   1912:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
                   1913:
1.48      nicm     1914:        window_set_name(ictx->wp->window, ictx->input_buf);
1.28      nicm     1915:        options_set_number(&ictx->wp->window->options, "automatic-rename", 0);
                   1916:
                   1917:        server_status_window(ictx->wp->window);
                   1918: }
                   1919:
                   1920: /* Open UTF-8 character. */
                   1921: int
                   1922: input_utf8_open(struct input_ctx *ictx)
                   1923: {
                   1924:        if (!options_get_number(&ictx->wp->window->options, "utf8")) {
                   1925:                /* Print, and do not switch state. */
                   1926:                input_print(ictx);
                   1927:                return (-1);
                   1928:        }
                   1929:        log_debug("%s", __func__);
                   1930:
                   1931:        utf8_open(&ictx->utf8data, ictx->ch);
                   1932:        return (0);
                   1933: }
                   1934:
                   1935: /* Append to UTF-8 character. */
                   1936: int
                   1937: input_utf8_add(struct input_ctx *ictx)
                   1938: {
                   1939:        log_debug("%s", __func__);
                   1940:
                   1941:        utf8_append(&ictx->utf8data, ictx->ch);
                   1942:        return (0);
                   1943: }
                   1944:
                   1945: /* Close UTF-8 string. */
                   1946: int
                   1947: input_utf8_close(struct input_ctx *ictx)
                   1948: {
                   1949:        log_debug("%s", __func__);
                   1950:
                   1951:        utf8_append(&ictx->utf8data, ictx->ch);
                   1952:
1.69      nicm     1953:        grid_cell_set(&ictx->cell.cell, &ictx->utf8data);
                   1954:        screen_write_cell(&ictx->ctx, &ictx->cell.cell);
1.28      nicm     1955:
                   1956:        return (0);
1.1       nicm     1957: }