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