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

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