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