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

1.82    ! nicm        1: /* $OpenBSD: input.c,v 1.81 2015/08/25 15:00:05 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:
        !           847:        wp->flags |= PANE_CHANGED;
1.30      nicm      848:
1.28      nicm      849:        wp->window->flags |= WINDOW_ACTIVITY;
1.30      nicm      850:        wp->window->flags &= ~WINDOW_SILENCE;
1.79      nicm      851:
                    852:        if (gettimeofday(&wp->window->activity_time, NULL) != 0)
                    853:                fatal("gettimeofday failed");
1.1       nicm      854:
1.28      nicm      855:        /*
                    856:         * Open the screen. Use NULL wp if there is a mode set as don't want to
                    857:         * update the tty.
                    858:         */
                    859:        if (wp->mode == NULL)
                    860:                screen_write_start(&ictx->ctx, wp, &wp->base);
                    861:        else
                    862:                screen_write_start(&ictx->ctx, NULL, &wp->base);
                    863:        ictx->wp = wp;
1.7       nicm      864:
1.28      nicm      865:        buf = EVBUFFER_DATA(evb);
                    866:        len = EVBUFFER_LENGTH(evb);
1.54      nicm      867:        notify_input(wp, evb);
1.28      nicm      868:        off = 0;
                    869:
                    870:        /* Parse the input. */
                    871:        while (off < len) {
                    872:                ictx->ch = buf[off++];
                    873:                log_debug("%s: '%c' %s", __func__, ictx->ch, ictx->state->name);
                    874:
                    875:                /* Find the transition. */
                    876:                itr = ictx->state->transitions;
                    877:                while (itr->first != -1 && itr->last != -1) {
                    878:                        if (ictx->ch >= itr->first && ictx->ch <= itr->last)
1.7       nicm      879:                                break;
1.28      nicm      880:                        itr++;
1.1       nicm      881:                }
1.28      nicm      882:                if (itr->first == -1 || itr->last == -1) {
                    883:                        /* No transition? Eh? */
                    884:                        fatalx("No transition from state!");
1.3       nicm      885:                }
1.1       nicm      886:
                    887:                /*
1.28      nicm      888:                 * Execute the handler, if any. Don't switch state if it
                    889:                 * returns non-zero.
1.1       nicm      890:                 */
1.31      nicm      891:                if (itr->handler != NULL && itr->handler(ictx) != 0)
1.28      nicm      892:                        continue;
                    893:
                    894:                /* And switch state, if necessary. */
1.52      nicm      895:                if (itr->state != NULL)
                    896:                        input_set_state(wp, itr);
                    897:
                    898:                /* If not in ground state, save input. */
                    899:                if (ictx->state != &input_state_ground)
                    900:                        evbuffer_add(ictx->since_ground, &ictx->ch, 1);
1.1       nicm      901:        }
                    902:
1.28      nicm      903:        /* Close the screen. */
                    904:        screen_write_stop(&ictx->ctx);
1.1       nicm      905:
1.28      nicm      906:        evbuffer_drain(evb, len);
1.1       nicm      907: }
                    908:
1.28      nicm      909: /* Split the parameter list (if any). */
                    910: int
                    911: input_split(struct input_ctx *ictx)
1.1       nicm      912:
                    913: {
1.28      nicm      914:        const char      *errstr;
                    915:        char            *ptr, *out;
                    916:        int              n;
1.1       nicm      917:
1.28      nicm      918:        ictx->param_list_len = 0;
                    919:        if (ictx->param_len == 0)
                    920:                return (0);
1.1       nicm      921:
1.28      nicm      922:        ptr = ictx->param_buf;
                    923:        while ((out = strsep(&ptr, ";")) != NULL) {
                    924:                if (*out == '\0')
                    925:                        n = -1;
                    926:                else {
                    927:                        n = strtonum(out, 0, INT_MAX, &errstr);
                    928:                        if (errstr != NULL)
                    929:                                return (-1);
                    930:                }
1.1       nicm      931:
1.28      nicm      932:                ictx->param_list[ictx->param_list_len++] = n;
                    933:                if (ictx->param_list_len == nitems(ictx->param_list))
                    934:                        return (-1);
                    935:        }
1.1       nicm      936:
1.28      nicm      937:        return (0);
1.1       nicm      938: }
                    939:
1.40      nicm      940: /* Get an argument or return default value. */
1.28      nicm      941: int
                    942: input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1.1       nicm      943: {
1.28      nicm      944:        int     retval;
1.1       nicm      945:
1.28      nicm      946:        if (validx >= ictx->param_list_len)
                    947:            return (defval);
1.1       nicm      948:
1.28      nicm      949:        retval = ictx->param_list[validx];
                    950:        if (retval == -1)
                    951:                return (defval);
                    952:        if (retval < minval)
                    953:                return (minval);
                    954:        return (retval);
1.1       nicm      955: }
                    956:
1.28      nicm      957: /* Reply to terminal query. */
1.1       nicm      958: void
1.28      nicm      959: input_reply(struct input_ctx *ictx, const char *fmt, ...)
1.1       nicm      960: {
1.28      nicm      961:        va_list ap;
                    962:        char   *reply;
1.1       nicm      963:
1.28      nicm      964:        va_start(ap, fmt);
                    965:        vasprintf(&reply, fmt, ap);
                    966:        va_end(ap);
1.1       nicm      967:
1.28      nicm      968:        bufferevent_write(ictx->wp->event, reply, strlen(reply));
1.53      nicm      969:        free(reply);
1.8       nicm      970: }
                    971:
1.28      nicm      972: /* Clear saved state. */
1.8       nicm      973: void
1.28      nicm      974: input_clear(struct input_ctx *ictx)
1.8       nicm      975: {
1.28      nicm      976:        *ictx->interm_buf = '\0';
                    977:        ictx->interm_len = 0;
1.8       nicm      978:
1.28      nicm      979:        *ictx->param_buf = '\0';
                    980:        ictx->param_len = 0;
1.8       nicm      981:
1.35      nicm      982:        *ictx->input_buf = '\0';
                    983:        ictx->input_len = 0;
                    984:
1.28      nicm      985:        ictx->flags &= ~INPUT_DISCARD;
1.14      nicm      986: }
                    987:
1.67      nicm      988: /* Reset for ground state. */
                    989: void
                    990: input_ground(struct input_ctx *ictx)
                    991: {
                    992:        evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
                    993:
                    994:        if (ictx->input_space > INPUT_BUF_START) {
                    995:                ictx->input_space = INPUT_BUF_START;
1.71      nicm      996:                ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1.67      nicm      997:        }
                    998: }
                    999:
1.28      nicm     1000: /* Output this character to the screen. */
                   1001: int
                   1002: input_print(struct input_ctx *ictx)
1.14      nicm     1003: {
1.69      nicm     1004:        int     set;
                   1005:
                   1006:        set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
                   1007:        if (set == 1)
                   1008:                ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
                   1009:        else
                   1010:                ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
                   1011:
                   1012:        grid_cell_one(&ictx->cell.cell, ictx->ch);
                   1013:        screen_write_cell(&ictx->ctx, &ictx->cell.cell);
                   1014:
                   1015:        ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1.14      nicm     1016:
1.28      nicm     1017:        return (0);
1.1       nicm     1018: }
                   1019:
1.28      nicm     1020: /* Collect intermediate string. */
                   1021: int
                   1022: input_intermediate(struct input_ctx *ictx)
1.1       nicm     1023: {
1.28      nicm     1024:        if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
                   1025:                ictx->flags |= INPUT_DISCARD;
                   1026:        else {
                   1027:                ictx->interm_buf[ictx->interm_len++] = ictx->ch;
                   1028:                ictx->interm_buf[ictx->interm_len] = '\0';
                   1029:        }
1.1       nicm     1030:
1.28      nicm     1031:        return (0);
1.1       nicm     1032: }
                   1033:
1.28      nicm     1034: /* Collect parameter string. */
                   1035: int
                   1036: input_parameter(struct input_ctx *ictx)
1.1       nicm     1037: {
1.28      nicm     1038:        if (ictx->param_len == (sizeof ictx->param_buf) - 1)
                   1039:                ictx->flags |= INPUT_DISCARD;
                   1040:        else {
                   1041:                ictx->param_buf[ictx->param_len++] = ictx->ch;
                   1042:                ictx->param_buf[ictx->param_len] = '\0';
                   1043:        }
1.1       nicm     1044:
1.28      nicm     1045:        return (0);
1.1       nicm     1046: }
                   1047:
1.28      nicm     1048: /* Collect input string. */
                   1049: int
                   1050: input_input(struct input_ctx *ictx)
1.1       nicm     1051: {
1.67      nicm     1052:        size_t available;
                   1053:
                   1054:        available = ictx->input_space;
                   1055:        while (ictx->input_len + 1 >= available) {
                   1056:                available *= 2;
                   1057:                if (available > INPUT_BUF_LIMIT) {
                   1058:                        ictx->flags |= INPUT_DISCARD;
                   1059:                        return (0);
                   1060:                }
1.71      nicm     1061:                ictx->input_buf = xrealloc(ictx->input_buf, available);
1.67      nicm     1062:                ictx->input_space = available;
1.28      nicm     1063:        }
1.67      nicm     1064:        ictx->input_buf[ictx->input_len++] = ictx->ch;
                   1065:        ictx->input_buf[ictx->input_len] = '\0';
1.1       nicm     1066:
1.28      nicm     1067:        return (0);
1.1       nicm     1068: }
                   1069:
1.28      nicm     1070: /* Execute C0 control sequence. */
                   1071: int
                   1072: input_c0_dispatch(struct input_ctx *ictx)
1.1       nicm     1073: {
1.28      nicm     1074:        struct screen_write_ctx *sctx = &ictx->ctx;
                   1075:        struct window_pane      *wp = ictx->wp;
                   1076:        struct screen           *s = sctx->s;
1.1       nicm     1077:
1.28      nicm     1078:        log_debug("%s: '%c", __func__, ictx->ch);
1.1       nicm     1079:
1.28      nicm     1080:        switch (ictx->ch) {
                   1081:        case '\000':    /* NUL */
                   1082:                break;
                   1083:        case '\007':    /* BEL */
                   1084:                wp->window->flags |= WINDOW_BELL;
                   1085:                break;
                   1086:        case '\010':    /* BS */
                   1087:                screen_write_backspace(sctx);
1.75      nicm     1088:                break;
1.28      nicm     1089:        case '\011':    /* HT */
                   1090:                /* Don't tab beyond the end of the line. */
                   1091:                if (s->cx >= screen_size_x(s) - 1)
                   1092:                        break;
1.1       nicm     1093:
1.28      nicm     1094:                /* Find the next tab point, or use the last column if none. */
                   1095:                do {
                   1096:                        s->cx++;
                   1097:                        if (bit_test(s->tabs, s->cx))
                   1098:                                break;
                   1099:                } while (s->cx < screen_size_x(s) - 1);
                   1100:                break;
                   1101:        case '\012':    /* LF */
                   1102:        case '\013':    /* VT */
                   1103:        case '\014':    /* FF */
                   1104:                screen_write_linefeed(sctx, 0);
1.75      nicm     1105:                break;
1.28      nicm     1106:        case '\015':    /* CR */
                   1107:                screen_write_carriagereturn(sctx);
1.75      nicm     1108:                break;
1.28      nicm     1109:        case '\016':    /* SO */
1.69      nicm     1110:                ictx->cell.set = 1;
1.28      nicm     1111:                break;
                   1112:        case '\017':    /* SI */
1.69      nicm     1113:                ictx->cell.set = 0;
1.28      nicm     1114:                break;
                   1115:        default:
                   1116:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1117:                break;
                   1118:        }
1.1       nicm     1119:
1.28      nicm     1120:        return (0);
1.7       nicm     1121: }
                   1122:
1.28      nicm     1123: /* Execute escape sequence. */
                   1124: int
                   1125: input_esc_dispatch(struct input_ctx *ictx)
1.7       nicm     1126: {
1.31      nicm     1127:        struct screen_write_ctx         *sctx = &ictx->ctx;
                   1128:        struct screen                   *s = sctx->s;
                   1129:        struct input_table_entry        *entry;
1.7       nicm     1130:
1.28      nicm     1131:        if (ictx->flags & INPUT_DISCARD)
                   1132:                return (0);
                   1133:        log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1.7       nicm     1134:
1.28      nicm     1135:        entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
                   1136:            sizeof input_esc_table[0], input_table_compare);
                   1137:        if (entry == NULL) {
                   1138:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1139:                return (0);
                   1140:        }
1.7       nicm     1141:
1.28      nicm     1142:        switch (entry->type) {
                   1143:        case INPUT_ESC_RIS:
1.69      nicm     1144:                input_reset_cell(ictx);
1.46      nicm     1145:                screen_write_reset(sctx);
1.28      nicm     1146:                break;
                   1147:        case INPUT_ESC_IND:
                   1148:                screen_write_linefeed(sctx, 0);
                   1149:                break;
                   1150:        case INPUT_ESC_NEL:
                   1151:                screen_write_carriagereturn(sctx);
                   1152:                screen_write_linefeed(sctx, 0);
                   1153:                break;
                   1154:        case INPUT_ESC_HTS:
                   1155:                if (s->cx < screen_size_x(s))
                   1156:                        bit_set(s->tabs, s->cx);
                   1157:                break;
                   1158:        case INPUT_ESC_RI:
                   1159:                screen_write_reverseindex(sctx);
                   1160:                break;
                   1161:        case INPUT_ESC_DECKPAM:
1.59      nicm     1162:                screen_write_mode_set(sctx, MODE_KKEYPAD);
1.28      nicm     1163:                break;
                   1164:        case INPUT_ESC_DECKPNM:
1.59      nicm     1165:                screen_write_mode_clear(sctx, MODE_KKEYPAD);
1.1       nicm     1166:                break;
1.28      nicm     1167:        case INPUT_ESC_DECSC:
                   1168:                memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
                   1169:                ictx->old_cx = s->cx;
                   1170:                ictx->old_cy = s->cy;
                   1171:                break;
                   1172:        case INPUT_ESC_DECRC:
                   1173:                memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
                   1174:                screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
                   1175:                break;
                   1176:        case INPUT_ESC_DECALN:
                   1177:                screen_write_alignmenttest(sctx);
                   1178:                break;
1.69      nicm     1179:        case INPUT_ESC_SCSG0_ON:
                   1180:                ictx->cell.g0set = 1;
                   1181:                break;
                   1182:        case INPUT_ESC_SCSG0_OFF:
                   1183:                ictx->cell.g0set = 0;
                   1184:                break;
                   1185:        case INPUT_ESC_SCSG1_ON:
                   1186:                ictx->cell.g1set = 1;
1.1       nicm     1187:                break;
1.69      nicm     1188:        case INPUT_ESC_SCSG1_OFF:
                   1189:                ictx->cell.g1set = 0;
1.1       nicm     1190:                break;
                   1191:        }
1.28      nicm     1192:
                   1193:        return (0);
1.1       nicm     1194: }
                   1195:
1.28      nicm     1196: /* Execute control sequence. */
                   1197: int
                   1198: input_csi_dispatch(struct input_ctx *ictx)
1.1       nicm     1199: {
1.28      nicm     1200:        struct screen_write_ctx        *sctx = &ictx->ctx;
                   1201:        struct screen                  *s = sctx->s;
                   1202:        struct input_table_entry       *entry;
1.65      nicm     1203:        int                             n, m;
1.81      nicm     1204:        u_int                           cx;
1.1       nicm     1205:
1.28      nicm     1206:        if (ictx->flags & INPUT_DISCARD)
                   1207:                return (0);
                   1208:        if (input_split(ictx) != 0)
                   1209:                return (0);
                   1210:        log_debug("%s: '%c' \"%s\" \"%s\"",
                   1211:            __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1.1       nicm     1212:
1.28      nicm     1213:        entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
                   1214:            sizeof input_csi_table[0], input_table_compare);
                   1215:        if (entry == NULL) {
                   1216:                log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1217:                return (0);
                   1218:        }
1.1       nicm     1219:
1.28      nicm     1220:        switch (entry->type) {
                   1221:        case INPUT_CSI_CBT:
                   1222:                /* Find the previous tab point, n times. */
1.81      nicm     1223:                cx = s->cx;
                   1224:                if (cx > screen_size_x(s) - 1)
                   1225:                        cx = screen_size_x(s) - 1;
1.28      nicm     1226:                n = input_get(ictx, 0, 1, 1);
1.81      nicm     1227:                while (cx > 0 && n-- > 0) {
1.28      nicm     1228:                        do
1.81      nicm     1229:                                cx--;
                   1230:                        while (cx > 0 && !bit_test(s->tabs, cx));
1.28      nicm     1231:                }
1.81      nicm     1232:                s->cx = cx;
1.28      nicm     1233:                break;
                   1234:        case INPUT_CSI_CUB:
                   1235:                screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1));
                   1236:                break;
                   1237:        case INPUT_CSI_CUD:
                   1238:                screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
                   1239:                break;
                   1240:        case INPUT_CSI_CUF:
                   1241:                screen_write_cursorright(sctx, input_get(ictx, 0, 1, 1));
                   1242:                break;
                   1243:        case INPUT_CSI_CUP:
                   1244:                n = input_get(ictx, 0, 1, 1);
                   1245:                m = input_get(ictx, 1, 1, 1);
                   1246:                screen_write_cursormove(sctx, m - 1, n - 1);
                   1247:                break;
1.65      nicm     1248:        case INPUT_CSI_WINOPS:
                   1249:                input_csi_dispatch_winops(ictx);
                   1250:                break;
1.28      nicm     1251:        case INPUT_CSI_CUU:
1.43      nicm     1252:                screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
                   1253:                break;
                   1254:        case INPUT_CSI_CNL:
                   1255:                screen_write_carriagereturn(sctx);
                   1256:                screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
                   1257:                break;
                   1258:        case INPUT_CSI_CPL:
                   1259:                screen_write_carriagereturn(sctx);
1.28      nicm     1260:                screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
                   1261:                break;
                   1262:        case INPUT_CSI_DA:
                   1263:                switch (input_get(ictx, 0, 0, 0)) {
                   1264:                case 0:
                   1265:                        input_reply(ictx, "\033[?1;2c");
1.50      nicm     1266:                        break;
                   1267:                default:
                   1268:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1269:                        break;
                   1270:                }
                   1271:                break;
                   1272:        case INPUT_CSI_DA_TWO:
                   1273:                switch (input_get(ictx, 0, 0, 0)) {
                   1274:                case 0:
1.66      nicm     1275:                        input_reply(ictx, "\033[>84;0;0c");
1.28      nicm     1276:                        break;
                   1277:                default:
                   1278:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1279:                        break;
                   1280:                }
1.56      nicm     1281:                break;
                   1282:        case INPUT_CSI_ECH:
                   1283:                screen_write_clearcharacter(sctx, input_get(ictx, 0, 1, 1));
1.28      nicm     1284:                break;
                   1285:        case INPUT_CSI_DCH:
                   1286:                screen_write_deletecharacter(sctx, input_get(ictx, 0, 1, 1));
                   1287:                break;
                   1288:        case INPUT_CSI_DECSTBM:
                   1289:                n = input_get(ictx, 0, 1, 1);
                   1290:                m = input_get(ictx, 1, 1, screen_size_y(s));
                   1291:                screen_write_scrollregion(sctx, n - 1, m - 1);
1.1       nicm     1292:                break;
1.28      nicm     1293:        case INPUT_CSI_DL:
                   1294:                screen_write_deleteline(sctx, input_get(ictx, 0, 1, 1));
1.1       nicm     1295:                break;
1.28      nicm     1296:        case INPUT_CSI_DSR:
                   1297:                switch (input_get(ictx, 0, 0, 0)) {
                   1298:                case 5:
                   1299:                        input_reply(ictx, "\033[0n");
                   1300:                        break;
                   1301:                case 6:
                   1302:                        input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
                   1303:                        break;
                   1304:                default:
                   1305:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1306:                        break;
                   1307:                }
                   1308:                break;
                   1309:        case INPUT_CSI_ED:
                   1310:                switch (input_get(ictx, 0, 0, 0)) {
                   1311:                case 0:
                   1312:                        screen_write_clearendofscreen(sctx);
                   1313:                        break;
                   1314:                case 1:
                   1315:                        screen_write_clearstartofscreen(sctx);
                   1316:                        break;
                   1317:                case 2:
                   1318:                        screen_write_clearscreen(sctx);
1.41      nicm     1319:                        break;
                   1320:                case 3:
                   1321:                        switch (input_get(ictx, 1, 0, 0)) {
                   1322:                        case 0:
                   1323:                                /*
                   1324:                                 * Linux console extension to clear history
                   1325:                                 * (for example before locking the screen).
                   1326:                                 */
                   1327:                                screen_write_clearhistory(sctx);
                   1328:                                break;
                   1329:                        }
1.28      nicm     1330:                        break;
                   1331:                default:
                   1332:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1333:                        break;
                   1334:                }
                   1335:                break;
                   1336:        case INPUT_CSI_EL:
                   1337:                switch (input_get(ictx, 0, 0, 0)) {
                   1338:                case 0:
                   1339:                        screen_write_clearendofline(sctx);
                   1340:                        break;
                   1341:                case 1:
                   1342:                        screen_write_clearstartofline(sctx);
                   1343:                        break;
                   1344:                case 2:
                   1345:                        screen_write_clearline(sctx);
                   1346:                        break;
                   1347:                default:
                   1348:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1349:                        break;
                   1350:                }
                   1351:                break;
                   1352:        case INPUT_CSI_HPA:
                   1353:                n = input_get(ictx, 0, 1, 1);
                   1354:                screen_write_cursormove(sctx, n - 1, s->cy);
                   1355:                break;
                   1356:        case INPUT_CSI_ICH:
                   1357:                screen_write_insertcharacter(sctx, input_get(ictx, 0, 1, 1));
                   1358:                break;
                   1359:        case INPUT_CSI_IL:
                   1360:                screen_write_insertline(sctx, input_get(ictx, 0, 1, 1));
                   1361:                break;
1.42      nicm     1362:        case INPUT_CSI_RCP:
                   1363:                memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
                   1364:                screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
                   1365:                break;
1.28      nicm     1366:        case INPUT_CSI_RM:
1.64      nicm     1367:                input_csi_dispatch_rm(ictx);
                   1368:                break;
                   1369:        case INPUT_CSI_RM_PRIVATE:
                   1370:                input_csi_dispatch_rm_private(ictx);
                   1371:                break;
                   1372:        case INPUT_CSI_SCP:
                   1373:                memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
                   1374:                ictx->old_cx = s->cx;
                   1375:                ictx->old_cy = s->cy;
                   1376:                break;
                   1377:        case INPUT_CSI_SGR:
                   1378:                input_csi_dispatch_sgr(ictx);
                   1379:                break;
                   1380:        case INPUT_CSI_SM:
                   1381:                input_csi_dispatch_sm(ictx);
                   1382:                break;
                   1383:        case INPUT_CSI_SM_PRIVATE:
                   1384:                input_csi_dispatch_sm_private(ictx);
                   1385:                break;
                   1386:        case INPUT_CSI_TBC:
                   1387:                switch (input_get(ictx, 0, 0, 0)) {
                   1388:                case 0:
                   1389:                        if (s->cx < screen_size_x(s))
                   1390:                                bit_clear(s->tabs, s->cx);
                   1391:                        break;
                   1392:                case 3:
                   1393:                        bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
                   1394:                        break;
                   1395:                default:
                   1396:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1397:                        break;
                   1398:                }
                   1399:                break;
                   1400:        case INPUT_CSI_VPA:
                   1401:                n = input_get(ictx, 0, 1, 1);
                   1402:                screen_write_cursormove(sctx, s->cx, n - 1);
                   1403:                break;
                   1404:        case INPUT_CSI_DECSCUSR:
                   1405:                n = input_get(ictx, 0, 0, 0);
                   1406:                screen_set_cursor_style(s, n);
                   1407:                break;
                   1408:        }
                   1409:
                   1410:        return (0);
                   1411: }
                   1412:
                   1413: /* Handle CSI RM. */
                   1414: void
                   1415: input_csi_dispatch_rm(struct input_ctx *ictx)
                   1416: {
                   1417:        u_int   i;
                   1418:
                   1419:        for (i = 0; i < ictx->param_list_len; i++) {
                   1420:                switch (input_get(ictx, i, 0, -1)) {
1.28      nicm     1421:                case 4:         /* IRM */
1.59      nicm     1422:                        screen_write_mode_clear(&ictx->ctx, MODE_INSERT);
1.28      nicm     1423:                        break;
1.72      nicm     1424:                case 34:
                   1425:                        screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
                   1426:                        break;
1.28      nicm     1427:                default:
                   1428:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1429:                        break;
                   1430:                }
1.64      nicm     1431:        }
                   1432: }
                   1433:
                   1434: /* Handle CSI private RM. */
                   1435: void
                   1436: input_csi_dispatch_rm_private(struct input_ctx *ictx)
                   1437: {
1.69      nicm     1438:        struct window_pane      *wp = ictx->wp;
                   1439:        u_int                    i;
1.64      nicm     1440:
                   1441:        for (i = 0; i < ictx->param_list_len; i++) {
                   1442:                switch (input_get(ictx, i, 0, -1)) {
                   1443:                case 1:         /* DECCKM */
1.59      nicm     1444:                        screen_write_mode_clear(&ictx->ctx, MODE_KCURSOR);
1.1       nicm     1445:                        break;
1.17      nicm     1446:                case 3:         /* DECCOLM */
1.24      nicm     1447:                        screen_write_cursormove(&ictx->ctx, 0, 0);
1.17      nicm     1448:                        screen_write_clearscreen(&ictx->ctx);
                   1449:                        break;
1.61      nicm     1450:                case 7:         /* DECAWM */
                   1451:                        screen_write_mode_clear(&ictx->ctx, MODE_WRAP);
                   1452:                        break;
1.72      nicm     1453:                case 12:
                   1454:                        screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
                   1455:                        break;
1.1       nicm     1456:                case 25:        /* TCEM */
1.59      nicm     1457:                        screen_write_mode_clear(&ictx->ctx, MODE_CURSOR);
1.1       nicm     1458:                        break;
                   1459:                case 1000:
1.32      nicm     1460:                case 1001:
                   1461:                case 1002:
1.59      nicm     1462:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
1.1       nicm     1463:                        break;
1.62      nicm     1464:                case 1004:
                   1465:                        screen_write_mode_clear(&ictx->ctx, MODE_FOCUSON);
                   1466:                        break;
1.33      nicm     1467:                case 1005:
1.59      nicm     1468:                        screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_UTF8);
1.33      nicm     1469:                        break;
1.60      nicm     1470:                case 1006:
                   1471:                        screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_SGR);
                   1472:                        break;
1.55      nicm     1473:                case 47:
                   1474:                case 1047:
1.69      nicm     1475:                        window_pane_alternate_off(wp, &ictx->cell.cell, 0);
1.55      nicm     1476:                        break;
1.9       nicm     1477:                case 1049:
1.69      nicm     1478:                        window_pane_alternate_off(wp, &ictx->cell.cell, 1);
1.9       nicm     1479:                        break;
1.49      nicm     1480:                case 2004:
1.59      nicm     1481:                        screen_write_mode_clear(&ictx->ctx, MODE_BRACKETPASTE);
1.49      nicm     1482:                        break;
1.1       nicm     1483:                default:
1.28      nicm     1484:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1485:                        break;
                   1486:                }
1.64      nicm     1487:        }
                   1488: }
                   1489:
                   1490: /* Handle CSI SM. */
                   1491: void
                   1492: input_csi_dispatch_sm(struct input_ctx *ictx)
                   1493: {
                   1494:        u_int   i;
                   1495:
                   1496:        for (i = 0; i < ictx->param_list_len; i++) {
                   1497:                switch (input_get(ictx, i, 0, -1)) {
1.1       nicm     1498:                case 4:         /* IRM */
1.59      nicm     1499:                        screen_write_mode_set(&ictx->ctx, MODE_INSERT);
1.1       nicm     1500:                        break;
1.72      nicm     1501:                case 34:
                   1502:                        screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
                   1503:                        break;
1.1       nicm     1504:                default:
1.28      nicm     1505:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1506:                        break;
                   1507:                }
1.64      nicm     1508:        }
                   1509: }
                   1510:
                   1511: /* Handle CSI private SM. */
                   1512: void
                   1513: input_csi_dispatch_sm_private(struct input_ctx *ictx)
                   1514: {
1.69      nicm     1515:        struct window_pane      *wp = ictx->wp;
                   1516:        u_int                    i;
1.64      nicm     1517:
                   1518:        for (i = 0; i < ictx->param_list_len; i++) {
                   1519:                switch (input_get(ictx, i, 0, -1)) {
                   1520:                case 1:         /* DECCKM */
1.59      nicm     1521:                        screen_write_mode_set(&ictx->ctx, MODE_KCURSOR);
1.17      nicm     1522:                        break;
                   1523:                case 3:         /* DECCOLM */
1.24      nicm     1524:                        screen_write_cursormove(&ictx->ctx, 0, 0);
1.17      nicm     1525:                        screen_write_clearscreen(&ictx->ctx);
1.61      nicm     1526:                        break;
                   1527:                case 7:         /* DECAWM */
                   1528:                        screen_write_mode_set(&ictx->ctx, MODE_WRAP);
1.72      nicm     1529:                        break;
                   1530:                case 12:
                   1531:                        screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
1.1       nicm     1532:                        break;
                   1533:                case 25:        /* TCEM */
1.59      nicm     1534:                        screen_write_mode_set(&ictx->ctx, MODE_CURSOR);
1.1       nicm     1535:                        break;
                   1536:                case 1000:
1.59      nicm     1537:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
                   1538:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_STANDARD);
1.32      nicm     1539:                        break;
                   1540:                case 1002:
1.59      nicm     1541:                        screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
                   1542:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_BUTTON);
1.62      nicm     1543:                        break;
                   1544:                case 1004:
1.64      nicm     1545:                        if (ictx->ctx.s->mode & MODE_FOCUSON)
1.62      nicm     1546:                                break;
                   1547:                        screen_write_mode_set(&ictx->ctx, MODE_FOCUSON);
1.69      nicm     1548:                        wp->flags |= PANE_FOCUSPUSH; /* force update */
1.33      nicm     1549:                        break;
                   1550:                case 1005:
1.59      nicm     1551:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_UTF8);
1.60      nicm     1552:                        break;
                   1553:                case 1006:
                   1554:                        screen_write_mode_set(&ictx->ctx, MODE_MOUSE_SGR);
1.9       nicm     1555:                        break;
1.55      nicm     1556:                case 47:
                   1557:                case 1047:
1.69      nicm     1558:                        window_pane_alternate_on(wp, &ictx->cell.cell, 0);
1.55      nicm     1559:                        break;
1.9       nicm     1560:                case 1049:
1.69      nicm     1561:                        window_pane_alternate_on(wp, &ictx->cell.cell, 1);
1.49      nicm     1562:                        break;
                   1563:                case 2004:
1.59      nicm     1564:                        screen_write_mode_set(&ictx->ctx, MODE_BRACKETPASTE);
1.1       nicm     1565:                        break;
                   1566:                default:
1.28      nicm     1567:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1       nicm     1568:                        break;
                   1569:                }
1.65      nicm     1570:        }
                   1571: }
                   1572:
                   1573: /* Handle CSI window operations. */
                   1574: void
                   1575: input_csi_dispatch_winops(struct input_ctx *ictx)
                   1576: {
                   1577:        struct window_pane      *wp = ictx->wp;
                   1578:        int                      n, m;
                   1579:
                   1580:        m = 0;
                   1581:        while ((n = input_get(ictx, m, 0, -1)) != -1) {
                   1582:                switch (n) {
                   1583:                case 1:
                   1584:                case 2:
                   1585:                case 5:
                   1586:                case 6:
                   1587:                case 7:
                   1588:                case 11:
                   1589:                case 13:
                   1590:                case 14:
                   1591:                case 19:
                   1592:                case 20:
                   1593:                case 21:
                   1594:                case 24:
                   1595:                        break;
                   1596:                case 3:
                   1597:                case 4:
                   1598:                case 8:
                   1599:                        m++;
                   1600:                        if (input_get(ictx, m, 0, -1) == -1)
                   1601:                                return;
                   1602:                        /* FALLTHROUGH */
                   1603:                case 9:
                   1604:                case 10:
                   1605:                case 22:
                   1606:                case 23:
                   1607:                        m++;
                   1608:                        if (input_get(ictx, m, 0, -1) == -1)
                   1609:                                return;
                   1610:                        break;
                   1611:                case 18:
1.76      nicm     1612:                        input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx);
1.65      nicm     1613:                        break;
                   1614:                default:
                   1615:                        log_debug("%s: unknown '%c'", __func__, ictx->ch);
                   1616:                        break;
                   1617:                }
                   1618:                m++;
1.1       nicm     1619:        }
                   1620: }
                   1621:
1.78      nicm     1622: /* Handle CSI SGR for 256 colours. */
                   1623: void
                   1624: input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
                   1625: {
                   1626:        struct grid_cell        *gc = &ictx->cell.cell;
                   1627:        int                      c;
                   1628:
                   1629:        (*i)++;
                   1630:        c = input_get(ictx, *i, 0, -1);
                   1631:        if (c == -1) {
                   1632:                if (fgbg == 38) {
                   1633:                        gc->flags &= ~GRID_FLAG_FG256;
                   1634:                        gc->fg = 8;
                   1635:                } else if (fgbg == 48) {
                   1636:                        gc->flags &= ~GRID_FLAG_BG256;
                   1637:                        gc->bg = 8;
                   1638:                }
                   1639:        } else {
                   1640:                if (fgbg == 38) {
                   1641:                        gc->flags |= GRID_FLAG_FG256;
                   1642:                        gc->fg = c;
                   1643:                } else if (fgbg == 48) {
                   1644:                        gc->flags |= GRID_FLAG_BG256;
                   1645:                        gc->bg = c;
                   1646:                }
                   1647:        }
                   1648: }
                   1649:
                   1650: /* Handle CSI SGR for RGB colours. */
                   1651: void
                   1652: input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
                   1653: {
                   1654:        struct grid_cell        *gc = &ictx->cell.cell;
                   1655:        int                      c, r, g, b;
                   1656:
                   1657:        (*i)++;
                   1658:        r = input_get(ictx, *i, 0, -1);
                   1659:        if (r == -1 || r > 255)
                   1660:                return;
                   1661:        (*i)++;
                   1662:        g = input_get(ictx, *i, 0, -1);
                   1663:        if (g == -1 || g > 255)
                   1664:                return;
                   1665:        (*i)++;
                   1666:        b = input_get(ictx, *i, 0, -1);
                   1667:        if (b == -1 || b > 255)
                   1668:                return;
                   1669:
                   1670:        c = colour_find_rgb(r, g, b);
                   1671:        if (fgbg == 38) {
                   1672:                gc->flags |= GRID_FLAG_FG256;
                   1673:                gc->fg = c;
                   1674:        } else if (fgbg == 48) {
                   1675:                gc->flags |= GRID_FLAG_BG256;
                   1676:                gc->bg = c;
                   1677:        }
                   1678: }
                   1679:
1.28      nicm     1680: /* Handle CSI SGR. */
1.1       nicm     1681: void
1.28      nicm     1682: input_csi_dispatch_sgr(struct input_ctx *ictx)
1.1       nicm     1683: {
1.69      nicm     1684:        struct grid_cell        *gc = &ictx->cell.cell;
1.28      nicm     1685:        u_int                    i;
1.78      nicm     1686:        int                      n;
1.1       nicm     1687:
1.28      nicm     1688:        if (ictx->param_list_len == 0) {
1.1       nicm     1689:                memcpy(gc, &grid_default_cell, sizeof *gc);
                   1690:                return;
                   1691:        }
                   1692:
1.28      nicm     1693:        for (i = 0; i < ictx->param_list_len; i++) {
                   1694:                n = input_get(ictx, i, 0, 0);
1.1       nicm     1695:
1.28      nicm     1696:                if (n == 38 || n == 48) {
1.1       nicm     1697:                        i++;
1.78      nicm     1698:                        switch (input_get(ictx, i, 0, -1)) {
                   1699:                        case 2:
                   1700:                                input_csi_dispatch_sgr_rgb(ictx, n, &i);
                   1701:                                break;
                   1702:                        case 5:
                   1703:                                input_csi_dispatch_sgr_256(ictx, n, &i);
                   1704:                                break;
1.1       nicm     1705:                        }
                   1706:                        continue;
                   1707:                }
                   1708:
1.28      nicm     1709:                switch (n) {
1.1       nicm     1710:                case 0:
                   1711:                case 10:
                   1712:                        memcpy(gc, &grid_default_cell, sizeof *gc);
                   1713:                        break;
                   1714:                case 1:
                   1715:                        gc->attr |= GRID_ATTR_BRIGHT;
                   1716:                        break;
                   1717:                case 2:
                   1718:                        gc->attr |= GRID_ATTR_DIM;
                   1719:                        break;
                   1720:                case 3:
                   1721:                        gc->attr |= GRID_ATTR_ITALICS;
                   1722:                        break;
                   1723:                case 4:
                   1724:                        gc->attr |= GRID_ATTR_UNDERSCORE;
                   1725:                        break;
                   1726:                case 5:
                   1727:                        gc->attr |= GRID_ATTR_BLINK;
                   1728:                        break;
                   1729:                case 7:
                   1730:                        gc->attr |= GRID_ATTR_REVERSE;
                   1731:                        break;
                   1732:                case 8:
                   1733:                        gc->attr |= GRID_ATTR_HIDDEN;
                   1734:                        break;
                   1735:                case 22:
                   1736:                        gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
                   1737:                        break;
                   1738:                case 23:
                   1739:                        gc->attr &= ~GRID_ATTR_ITALICS;
                   1740:                        break;
                   1741:                case 24:
                   1742:                        gc->attr &= ~GRID_ATTR_UNDERSCORE;
                   1743:                        break;
                   1744:                case 25:
                   1745:                        gc->attr &= ~GRID_ATTR_BLINK;
                   1746:                        break;
                   1747:                case 27:
                   1748:                        gc->attr &= ~GRID_ATTR_REVERSE;
                   1749:                        break;
                   1750:                case 30:
                   1751:                case 31:
                   1752:                case 32:
                   1753:                case 33:
                   1754:                case 34:
                   1755:                case 35:
                   1756:                case 36:
                   1757:                case 37:
                   1758:                        gc->flags &= ~GRID_FLAG_FG256;
1.28      nicm     1759:                        gc->fg = n - 30;
1.1       nicm     1760:                        break;
                   1761:                case 39:
                   1762:                        gc->flags &= ~GRID_FLAG_FG256;
                   1763:                        gc->fg = 8;
                   1764:                        break;
                   1765:                case 40:
                   1766:                case 41:
                   1767:                case 42:
                   1768:                case 43:
                   1769:                case 44:
                   1770:                case 45:
                   1771:                case 46:
                   1772:                case 47:
                   1773:                        gc->flags &= ~GRID_FLAG_BG256;
1.28      nicm     1774:                        gc->bg = n - 40;
1.1       nicm     1775:                        break;
                   1776:                case 49:
                   1777:                        gc->flags &= ~GRID_FLAG_BG256;
                   1778:                        gc->bg = 8;
1.20      nicm     1779:                        break;
                   1780:                case 90:
                   1781:                case 91:
                   1782:                case 92:
                   1783:                case 93:
                   1784:                case 94:
                   1785:                case 95:
                   1786:                case 96:
                   1787:                case 97:
1.26      nicm     1788:                        gc->flags &= ~GRID_FLAG_FG256;
1.28      nicm     1789:                        gc->fg = n;
1.20      nicm     1790:                        break;
                   1791:                case 100:
                   1792:                case 101:
                   1793:                case 102:
                   1794:                case 103:
                   1795:                case 104:
                   1796:                case 105:
                   1797:                case 106:
                   1798:                case 107:
1.26      nicm     1799:                        gc->flags &= ~GRID_FLAG_BG256;
1.47      nicm     1800:                        gc->bg = n - 10;
1.1       nicm     1801:                        break;
                   1802:                }
                   1803:        }
1.28      nicm     1804: }
                   1805:
1.37      nicm     1806: /* DCS terminator (ST) received. */
                   1807: int
                   1808: input_dcs_dispatch(struct input_ctx *ictx)
1.28      nicm     1809: {
1.37      nicm     1810:        const char      prefix[] = "tmux;";
                   1811:        const u_int     prefix_len = (sizeof prefix) - 1;
                   1812:
                   1813:        if (ictx->flags & INPUT_DISCARD)
                   1814:                return (0);
                   1815:
                   1816:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
1.28      nicm     1817:
1.37      nicm     1818:        /* Check for tmux prefix. */
                   1819:        if (ictx->input_len >= prefix_len &&
                   1820:            strncmp(ictx->input_buf, prefix, prefix_len) == 0) {
                   1821:                screen_write_rawstring(&ictx->ctx,
                   1822:                    ictx->input_buf + prefix_len, ictx->input_len - prefix_len);
                   1823:        }
1.28      nicm     1824:
1.37      nicm     1825:        return (0);
1.28      nicm     1826: }
                   1827:
                   1828: /* OSC string started. */
                   1829: void
                   1830: input_enter_osc(struct input_ctx *ictx)
                   1831: {
                   1832:        log_debug("%s", __func__);
                   1833:
1.35      nicm     1834:        input_clear(ictx);
1.28      nicm     1835: }
                   1836:
                   1837: /* OSC terminator (ST) received. */
                   1838: void
                   1839: input_exit_osc(struct input_ctx *ictx)
                   1840: {
1.67      nicm     1841:        u_char  *p = ictx->input_buf;
1.73      nicm     1842:        u_int    option;
1.38      nicm     1843:
1.28      nicm     1844:        if (ictx->flags & INPUT_DISCARD)
                   1845:                return;
1.38      nicm     1846:        if (ictx->input_len < 1 || *p < '0' || *p > '9')
                   1847:                return;
1.28      nicm     1848:
1.38      nicm     1849:        log_debug("%s: \"%s\"", __func__, p);
1.28      nicm     1850:
1.38      nicm     1851:        option = 0;
                   1852:        while (*p >= '0' && *p <= '9')
                   1853:                option = option * 10 + *p++ - '0';
                   1854:        if (*p == ';')
                   1855:                p++;
                   1856:
                   1857:        switch (option) {
                   1858:        case 0:
                   1859:        case 2:
                   1860:                screen_set_title(ictx->ctx.s, p);
                   1861:                server_status_window(ictx->wp->window);
                   1862:                break;
                   1863:        case 12:
1.57      nicm     1864:                if (*p != '?') /* ? is colour request */
                   1865:                        screen_set_cursor_colour(ictx->ctx.s, p);
1.38      nicm     1866:                break;
                   1867:        case 112:
1.57      nicm     1868:                if (*p == '\0') /* no arguments allowed */
1.38      nicm     1869:                        screen_set_cursor_colour(ictx->ctx.s, "");
                   1870:                break;
                   1871:        default:
                   1872:                log_debug("%s: unknown '%u'", __func__, option);
                   1873:                break;
                   1874:        }
1.28      nicm     1875: }
                   1876:
                   1877: /* APC string started. */
                   1878: void
                   1879: input_enter_apc(struct input_ctx *ictx)
                   1880: {
                   1881:        log_debug("%s", __func__);
                   1882:
1.35      nicm     1883:        input_clear(ictx);
1.28      nicm     1884: }
                   1885:
                   1886: /* APC terminator (ST) received. */
                   1887: void
                   1888: input_exit_apc(struct input_ctx *ictx)
                   1889: {
                   1890:        if (ictx->flags & INPUT_DISCARD)
                   1891:                return;
                   1892:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
                   1893:
                   1894:        screen_set_title(ictx->ctx.s, ictx->input_buf);
                   1895:        server_status_window(ictx->wp->window);
                   1896: }
                   1897:
                   1898: /* Rename string started. */
                   1899: void
                   1900: input_enter_rename(struct input_ctx *ictx)
                   1901: {
                   1902:        log_debug("%s", __func__);
                   1903:
1.35      nicm     1904:        input_clear(ictx);
1.28      nicm     1905: }
                   1906:
                   1907: /* Rename terminator (ST) received. */
                   1908: void
                   1909: input_exit_rename(struct input_ctx *ictx)
                   1910: {
                   1911:        if (ictx->flags & INPUT_DISCARD)
1.44      nicm     1912:                return;
                   1913:        if (!options_get_number(&ictx->wp->window->options, "allow-rename"))
1.28      nicm     1914:                return;
                   1915:        log_debug("%s: \"%s\"", __func__, ictx->input_buf);
                   1916:
1.48      nicm     1917:        window_set_name(ictx->wp->window, ictx->input_buf);
1.28      nicm     1918:        options_set_number(&ictx->wp->window->options, "automatic-rename", 0);
                   1919:
                   1920:        server_status_window(ictx->wp->window);
                   1921: }
                   1922:
                   1923: /* Open UTF-8 character. */
                   1924: int
                   1925: input_utf8_open(struct input_ctx *ictx)
                   1926: {
                   1927:        if (!options_get_number(&ictx->wp->window->options, "utf8")) {
                   1928:                /* Print, and do not switch state. */
                   1929:                input_print(ictx);
                   1930:                return (-1);
                   1931:        }
                   1932:        log_debug("%s", __func__);
                   1933:
                   1934:        utf8_open(&ictx->utf8data, ictx->ch);
                   1935:        return (0);
                   1936: }
                   1937:
                   1938: /* Append to UTF-8 character. */
                   1939: int
                   1940: input_utf8_add(struct input_ctx *ictx)
                   1941: {
                   1942:        log_debug("%s", __func__);
                   1943:
                   1944:        utf8_append(&ictx->utf8data, ictx->ch);
                   1945:        return (0);
                   1946: }
                   1947:
                   1948: /* Close UTF-8 string. */
                   1949: int
                   1950: input_utf8_close(struct input_ctx *ictx)
                   1951: {
                   1952:        log_debug("%s", __func__);
                   1953:
                   1954:        utf8_append(&ictx->utf8data, ictx->ch);
                   1955:
1.69      nicm     1956:        grid_cell_set(&ictx->cell.cell, &ictx->utf8data);
                   1957:        screen_write_cell(&ictx->ctx, &ictx->cell.cell);
1.28      nicm     1958:
                   1959:        return (0);
1.1       nicm     1960: }