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

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