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