Annotation of src/usr.bin/tmux/input.c, Revision 1.79
1.79 ! nicm 1: /* $OpenBSD: input.c,v 1.78 2015/06/05 22:33:39 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.74 nicm 804: memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell);
805: memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
806: ictx->old_cx = 0;
807: ictx->old_cy = 0;
808:
809: if (wp->mode == NULL)
810: screen_write_start(&ictx->ctx, wp, &wp->base);
811: else
812: screen_write_start(&ictx->ctx, NULL, &wp->base);
813: screen_write_reset(&ictx->ctx);
814: screen_write_stop(&ictx->ctx);
815: }
816:
817: /* Return pending data. */
818: struct evbuffer *
819: input_pending(struct window_pane *wp)
820: {
821: return (wp->ictx->since_ground);
1.52 nicm 822: }
823:
824: /* Change input state. */
825: void
826: input_set_state(struct window_pane *wp, const struct input_transition *itr)
827: {
1.74 nicm 828: struct input_ctx *ictx = wp->ictx;
1.52 nicm 829:
830: if (ictx->state->exit != NULL)
831: ictx->state->exit(ictx);
832: ictx->state = itr->state;
833: if (ictx->state->enter != NULL)
834: ictx->state->enter(ictx);
1.1 nicm 835: }
836:
1.28 nicm 837: /* Parse input. */
1.1 nicm 838: void
1.28 nicm 839: input_parse(struct window_pane *wp)
1.1 nicm 840: {
1.74 nicm 841: struct input_ctx *ictx = wp->ictx;
1.28 nicm 842: const struct input_transition *itr;
843: struct evbuffer *evb = wp->event->input;
844: u_char *buf;
845: size_t len, off;
1.1 nicm 846:
1.28 nicm 847: if (EVBUFFER_LENGTH(evb) == 0)
848: return;
1.30 nicm 849:
1.28 nicm 850: wp->window->flags |= WINDOW_ACTIVITY;
1.30 nicm 851: wp->window->flags &= ~WINDOW_SILENCE;
1.79 ! nicm 852:
! 853: if (gettimeofday(&wp->window->activity_time, NULL) != 0)
! 854: fatal("gettimeofday failed");
1.1 nicm 855:
1.28 nicm 856: /*
857: * Open the screen. Use NULL wp if there is a mode set as don't want to
858: * update the tty.
859: */
860: if (wp->mode == NULL)
861: screen_write_start(&ictx->ctx, wp, &wp->base);
862: else
863: screen_write_start(&ictx->ctx, NULL, &wp->base);
864: ictx->wp = wp;
1.7 nicm 865:
1.28 nicm 866: buf = EVBUFFER_DATA(evb);
867: len = EVBUFFER_LENGTH(evb);
1.54 nicm 868: notify_input(wp, evb);
1.28 nicm 869: off = 0;
870:
871: /* Parse the input. */
872: while (off < len) {
873: ictx->ch = buf[off++];
874: log_debug("%s: '%c' %s", __func__, ictx->ch, ictx->state->name);
875:
876: /* Find the transition. */
877: itr = ictx->state->transitions;
878: while (itr->first != -1 && itr->last != -1) {
879: if (ictx->ch >= itr->first && ictx->ch <= itr->last)
1.7 nicm 880: break;
1.28 nicm 881: itr++;
1.1 nicm 882: }
1.28 nicm 883: if (itr->first == -1 || itr->last == -1) {
884: /* No transition? Eh? */
885: fatalx("No transition from state!");
1.3 nicm 886: }
1.1 nicm 887:
888: /*
1.28 nicm 889: * Execute the handler, if any. Don't switch state if it
890: * returns non-zero.
1.1 nicm 891: */
1.31 nicm 892: if (itr->handler != NULL && itr->handler(ictx) != 0)
1.28 nicm 893: continue;
894:
895: /* And switch state, if necessary. */
1.52 nicm 896: if (itr->state != NULL)
897: input_set_state(wp, itr);
898:
899: /* If not in ground state, save input. */
900: if (ictx->state != &input_state_ground)
901: evbuffer_add(ictx->since_ground, &ictx->ch, 1);
1.1 nicm 902: }
903:
1.28 nicm 904: /* Close the screen. */
905: screen_write_stop(&ictx->ctx);
1.1 nicm 906:
1.28 nicm 907: evbuffer_drain(evb, len);
1.1 nicm 908: }
909:
1.28 nicm 910: /* Split the parameter list (if any). */
911: int
912: input_split(struct input_ctx *ictx)
1.1 nicm 913:
914: {
1.28 nicm 915: const char *errstr;
916: char *ptr, *out;
917: int n;
1.1 nicm 918:
1.28 nicm 919: ictx->param_list_len = 0;
920: if (ictx->param_len == 0)
921: return (0);
1.1 nicm 922:
1.28 nicm 923: ptr = ictx->param_buf;
924: while ((out = strsep(&ptr, ";")) != NULL) {
925: if (*out == '\0')
926: n = -1;
927: else {
928: n = strtonum(out, 0, INT_MAX, &errstr);
929: if (errstr != NULL)
930: return (-1);
931: }
1.1 nicm 932:
1.28 nicm 933: ictx->param_list[ictx->param_list_len++] = n;
934: if (ictx->param_list_len == nitems(ictx->param_list))
935: return (-1);
936: }
1.1 nicm 937:
1.28 nicm 938: return (0);
1.1 nicm 939: }
940:
1.40 nicm 941: /* Get an argument or return default value. */
1.28 nicm 942: int
943: input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1.1 nicm 944: {
1.28 nicm 945: int retval;
1.1 nicm 946:
1.28 nicm 947: if (validx >= ictx->param_list_len)
948: return (defval);
1.1 nicm 949:
1.28 nicm 950: retval = ictx->param_list[validx];
951: if (retval == -1)
952: return (defval);
953: if (retval < minval)
954: return (minval);
955: return (retval);
1.1 nicm 956: }
957:
1.28 nicm 958: /* Reply to terminal query. */
1.1 nicm 959: void
1.28 nicm 960: input_reply(struct input_ctx *ictx, const char *fmt, ...)
1.1 nicm 961: {
1.28 nicm 962: va_list ap;
963: char *reply;
1.1 nicm 964:
1.28 nicm 965: va_start(ap, fmt);
966: vasprintf(&reply, fmt, ap);
967: va_end(ap);
1.1 nicm 968:
1.28 nicm 969: bufferevent_write(ictx->wp->event, reply, strlen(reply));
1.53 nicm 970: free(reply);
1.8 nicm 971: }
972:
1.28 nicm 973: /* Clear saved state. */
1.8 nicm 974: void
1.28 nicm 975: input_clear(struct input_ctx *ictx)
1.8 nicm 976: {
1.28 nicm 977: *ictx->interm_buf = '\0';
978: ictx->interm_len = 0;
1.8 nicm 979:
1.28 nicm 980: *ictx->param_buf = '\0';
981: ictx->param_len = 0;
1.8 nicm 982:
1.35 nicm 983: *ictx->input_buf = '\0';
984: ictx->input_len = 0;
985:
1.28 nicm 986: ictx->flags &= ~INPUT_DISCARD;
1.14 nicm 987: }
988:
1.67 nicm 989: /* Reset for ground state. */
990: void
991: input_ground(struct input_ctx *ictx)
992: {
993: evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground));
994:
995: if (ictx->input_space > INPUT_BUF_START) {
996: ictx->input_space = INPUT_BUF_START;
1.71 nicm 997: ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
1.67 nicm 998: }
999: }
1000:
1.28 nicm 1001: /* Output this character to the screen. */
1002: int
1003: input_print(struct input_ctx *ictx)
1.14 nicm 1004: {
1.69 nicm 1005: int set;
1006:
1007: set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1008: if (set == 1)
1009: ictx->cell.cell.attr |= GRID_ATTR_CHARSET;
1010: else
1011: ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1012:
1013: grid_cell_one(&ictx->cell.cell, ictx->ch);
1014: screen_write_cell(&ictx->ctx, &ictx->cell.cell);
1015:
1016: ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET;
1.14 nicm 1017:
1.28 nicm 1018: return (0);
1.1 nicm 1019: }
1020:
1.28 nicm 1021: /* Collect intermediate string. */
1022: int
1023: input_intermediate(struct input_ctx *ictx)
1.1 nicm 1024: {
1.28 nicm 1025: if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1026: ictx->flags |= INPUT_DISCARD;
1027: else {
1028: ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1029: ictx->interm_buf[ictx->interm_len] = '\0';
1030: }
1.1 nicm 1031:
1.28 nicm 1032: return (0);
1.1 nicm 1033: }
1034:
1.28 nicm 1035: /* Collect parameter string. */
1036: int
1037: input_parameter(struct input_ctx *ictx)
1.1 nicm 1038: {
1.28 nicm 1039: if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1040: ictx->flags |= INPUT_DISCARD;
1041: else {
1042: ictx->param_buf[ictx->param_len++] = ictx->ch;
1043: ictx->param_buf[ictx->param_len] = '\0';
1044: }
1.1 nicm 1045:
1.28 nicm 1046: return (0);
1.1 nicm 1047: }
1048:
1.28 nicm 1049: /* Collect input string. */
1050: int
1051: input_input(struct input_ctx *ictx)
1.1 nicm 1052: {
1.67 nicm 1053: size_t available;
1054:
1055: available = ictx->input_space;
1056: while (ictx->input_len + 1 >= available) {
1057: available *= 2;
1058: if (available > INPUT_BUF_LIMIT) {
1059: ictx->flags |= INPUT_DISCARD;
1060: return (0);
1061: }
1.71 nicm 1062: ictx->input_buf = xrealloc(ictx->input_buf, available);
1.67 nicm 1063: ictx->input_space = available;
1.28 nicm 1064: }
1.67 nicm 1065: ictx->input_buf[ictx->input_len++] = ictx->ch;
1066: ictx->input_buf[ictx->input_len] = '\0';
1.1 nicm 1067:
1.28 nicm 1068: return (0);
1.1 nicm 1069: }
1070:
1.28 nicm 1071: /* Execute C0 control sequence. */
1072: int
1073: input_c0_dispatch(struct input_ctx *ictx)
1.1 nicm 1074: {
1.28 nicm 1075: struct screen_write_ctx *sctx = &ictx->ctx;
1076: struct window_pane *wp = ictx->wp;
1077: struct screen *s = sctx->s;
1.1 nicm 1078:
1.28 nicm 1079: log_debug("%s: '%c", __func__, ictx->ch);
1.1 nicm 1080:
1.28 nicm 1081: switch (ictx->ch) {
1082: case '\000': /* NUL */
1083: break;
1084: case '\007': /* BEL */
1085: wp->window->flags |= WINDOW_BELL;
1086: break;
1087: case '\010': /* BS */
1088: screen_write_backspace(sctx);
1.75 nicm 1089: break;
1.28 nicm 1090: case '\011': /* HT */
1091: /* Don't tab beyond the end of the line. */
1092: if (s->cx >= screen_size_x(s) - 1)
1093: break;
1.1 nicm 1094:
1.28 nicm 1095: /* Find the next tab point, or use the last column if none. */
1096: do {
1097: s->cx++;
1098: if (bit_test(s->tabs, s->cx))
1099: break;
1100: } while (s->cx < screen_size_x(s) - 1);
1101: break;
1102: case '\012': /* LF */
1103: case '\013': /* VT */
1104: case '\014': /* FF */
1105: screen_write_linefeed(sctx, 0);
1.75 nicm 1106: break;
1.28 nicm 1107: case '\015': /* CR */
1108: screen_write_carriagereturn(sctx);
1.75 nicm 1109: break;
1.28 nicm 1110: case '\016': /* SO */
1.69 nicm 1111: ictx->cell.set = 1;
1.28 nicm 1112: break;
1113: case '\017': /* SI */
1.69 nicm 1114: ictx->cell.set = 0;
1.28 nicm 1115: break;
1116: default:
1117: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1118: break;
1119: }
1.1 nicm 1120:
1.28 nicm 1121: return (0);
1.7 nicm 1122: }
1123:
1.28 nicm 1124: /* Execute escape sequence. */
1125: int
1126: input_esc_dispatch(struct input_ctx *ictx)
1.7 nicm 1127: {
1.31 nicm 1128: struct screen_write_ctx *sctx = &ictx->ctx;
1129: struct screen *s = sctx->s;
1130: struct input_table_entry *entry;
1.7 nicm 1131:
1.28 nicm 1132: if (ictx->flags & INPUT_DISCARD)
1133: return (0);
1134: log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1.7 nicm 1135:
1.28 nicm 1136: entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
1137: sizeof input_esc_table[0], input_table_compare);
1138: if (entry == NULL) {
1139: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1140: return (0);
1141: }
1.7 nicm 1142:
1.28 nicm 1143: switch (entry->type) {
1144: case INPUT_ESC_RIS:
1.69 nicm 1145: input_reset_cell(ictx);
1.46 nicm 1146: screen_write_reset(sctx);
1.28 nicm 1147: break;
1148: case INPUT_ESC_IND:
1149: screen_write_linefeed(sctx, 0);
1150: break;
1151: case INPUT_ESC_NEL:
1152: screen_write_carriagereturn(sctx);
1153: screen_write_linefeed(sctx, 0);
1154: break;
1155: case INPUT_ESC_HTS:
1156: if (s->cx < screen_size_x(s))
1157: bit_set(s->tabs, s->cx);
1158: break;
1159: case INPUT_ESC_RI:
1160: screen_write_reverseindex(sctx);
1161: break;
1162: case INPUT_ESC_DECKPAM:
1.59 nicm 1163: screen_write_mode_set(sctx, MODE_KKEYPAD);
1.28 nicm 1164: break;
1165: case INPUT_ESC_DECKPNM:
1.59 nicm 1166: screen_write_mode_clear(sctx, MODE_KKEYPAD);
1.1 nicm 1167: break;
1.28 nicm 1168: case INPUT_ESC_DECSC:
1169: memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
1170: ictx->old_cx = s->cx;
1171: ictx->old_cy = s->cy;
1172: break;
1173: case INPUT_ESC_DECRC:
1174: memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
1175: screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
1176: break;
1177: case INPUT_ESC_DECALN:
1178: screen_write_alignmenttest(sctx);
1179: break;
1.69 nicm 1180: case INPUT_ESC_SCSG0_ON:
1181: ictx->cell.g0set = 1;
1182: break;
1183: case INPUT_ESC_SCSG0_OFF:
1184: ictx->cell.g0set = 0;
1185: break;
1186: case INPUT_ESC_SCSG1_ON:
1187: ictx->cell.g1set = 1;
1.1 nicm 1188: break;
1.69 nicm 1189: case INPUT_ESC_SCSG1_OFF:
1190: ictx->cell.g1set = 0;
1.1 nicm 1191: break;
1192: }
1.28 nicm 1193:
1194: return (0);
1.1 nicm 1195: }
1196:
1.28 nicm 1197: /* Execute control sequence. */
1198: int
1199: input_csi_dispatch(struct input_ctx *ictx)
1.1 nicm 1200: {
1.28 nicm 1201: struct screen_write_ctx *sctx = &ictx->ctx;
1202: struct screen *s = sctx->s;
1203: struct input_table_entry *entry;
1.65 nicm 1204: int n, m;
1.1 nicm 1205:
1.28 nicm 1206: if (ictx->flags & INPUT_DISCARD)
1207: return (0);
1208: if (input_split(ictx) != 0)
1209: return (0);
1210: log_debug("%s: '%c' \"%s\" \"%s\"",
1211: __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1.1 nicm 1212:
1.28 nicm 1213: entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
1214: sizeof input_csi_table[0], input_table_compare);
1215: if (entry == NULL) {
1216: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1217: return (0);
1218: }
1.1 nicm 1219:
1.28 nicm 1220: switch (entry->type) {
1221: case INPUT_CSI_CBT:
1222: /* Find the previous tab point, n times. */
1223: n = input_get(ictx, 0, 1, 1);
1224: while (s->cx > 0 && n-- > 0) {
1225: do
1226: s->cx--;
1227: while (s->cx > 0 && !bit_test(s->tabs, s->cx));
1228: }
1229: break;
1230: case INPUT_CSI_CUB:
1231: screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1));
1232: break;
1233: case INPUT_CSI_CUD:
1234: screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
1235: break;
1236: case INPUT_CSI_CUF:
1237: screen_write_cursorright(sctx, input_get(ictx, 0, 1, 1));
1238: break;
1239: case INPUT_CSI_CUP:
1240: n = input_get(ictx, 0, 1, 1);
1241: m = input_get(ictx, 1, 1, 1);
1242: screen_write_cursormove(sctx, m - 1, n - 1);
1243: break;
1.65 nicm 1244: case INPUT_CSI_WINOPS:
1245: input_csi_dispatch_winops(ictx);
1246: break;
1.28 nicm 1247: case INPUT_CSI_CUU:
1.43 nicm 1248: screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
1249: break;
1250: case INPUT_CSI_CNL:
1251: screen_write_carriagereturn(sctx);
1252: screen_write_cursordown(sctx, input_get(ictx, 0, 1, 1));
1253: break;
1254: case INPUT_CSI_CPL:
1255: screen_write_carriagereturn(sctx);
1.28 nicm 1256: screen_write_cursorup(sctx, input_get(ictx, 0, 1, 1));
1257: break;
1258: case INPUT_CSI_DA:
1259: switch (input_get(ictx, 0, 0, 0)) {
1260: case 0:
1261: input_reply(ictx, "\033[?1;2c");
1.50 nicm 1262: break;
1263: default:
1264: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1265: break;
1266: }
1267: break;
1268: case INPUT_CSI_DA_TWO:
1269: switch (input_get(ictx, 0, 0, 0)) {
1270: case 0:
1.66 nicm 1271: input_reply(ictx, "\033[>84;0;0c");
1.28 nicm 1272: break;
1273: default:
1274: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1275: break;
1276: }
1.56 nicm 1277: break;
1278: case INPUT_CSI_ECH:
1279: screen_write_clearcharacter(sctx, input_get(ictx, 0, 1, 1));
1.28 nicm 1280: break;
1281: case INPUT_CSI_DCH:
1282: screen_write_deletecharacter(sctx, input_get(ictx, 0, 1, 1));
1283: break;
1284: case INPUT_CSI_DECSTBM:
1285: n = input_get(ictx, 0, 1, 1);
1286: m = input_get(ictx, 1, 1, screen_size_y(s));
1287: screen_write_scrollregion(sctx, n - 1, m - 1);
1.1 nicm 1288: break;
1.28 nicm 1289: case INPUT_CSI_DL:
1290: screen_write_deleteline(sctx, input_get(ictx, 0, 1, 1));
1.1 nicm 1291: break;
1.28 nicm 1292: case INPUT_CSI_DSR:
1293: switch (input_get(ictx, 0, 0, 0)) {
1294: case 5:
1295: input_reply(ictx, "\033[0n");
1296: break;
1297: case 6:
1298: input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1299: break;
1300: default:
1301: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1302: break;
1303: }
1304: break;
1305: case INPUT_CSI_ED:
1306: switch (input_get(ictx, 0, 0, 0)) {
1307: case 0:
1308: screen_write_clearendofscreen(sctx);
1309: break;
1310: case 1:
1311: screen_write_clearstartofscreen(sctx);
1312: break;
1313: case 2:
1314: screen_write_clearscreen(sctx);
1.41 nicm 1315: break;
1316: case 3:
1317: switch (input_get(ictx, 1, 0, 0)) {
1318: case 0:
1319: /*
1320: * Linux console extension to clear history
1321: * (for example before locking the screen).
1322: */
1323: screen_write_clearhistory(sctx);
1324: break;
1325: }
1.28 nicm 1326: break;
1327: default:
1328: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1329: break;
1330: }
1331: break;
1332: case INPUT_CSI_EL:
1333: switch (input_get(ictx, 0, 0, 0)) {
1334: case 0:
1335: screen_write_clearendofline(sctx);
1336: break;
1337: case 1:
1338: screen_write_clearstartofline(sctx);
1339: break;
1340: case 2:
1341: screen_write_clearline(sctx);
1342: break;
1343: default:
1344: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1345: break;
1346: }
1347: break;
1348: case INPUT_CSI_HPA:
1349: n = input_get(ictx, 0, 1, 1);
1350: screen_write_cursormove(sctx, n - 1, s->cy);
1351: break;
1352: case INPUT_CSI_ICH:
1353: screen_write_insertcharacter(sctx, input_get(ictx, 0, 1, 1));
1354: break;
1355: case INPUT_CSI_IL:
1356: screen_write_insertline(sctx, input_get(ictx, 0, 1, 1));
1357: break;
1.42 nicm 1358: case INPUT_CSI_RCP:
1359: memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
1360: screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy);
1361: break;
1.28 nicm 1362: case INPUT_CSI_RM:
1.64 nicm 1363: input_csi_dispatch_rm(ictx);
1364: break;
1365: case INPUT_CSI_RM_PRIVATE:
1366: input_csi_dispatch_rm_private(ictx);
1367: break;
1368: case INPUT_CSI_SCP:
1369: memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
1370: ictx->old_cx = s->cx;
1371: ictx->old_cy = s->cy;
1372: break;
1373: case INPUT_CSI_SGR:
1374: input_csi_dispatch_sgr(ictx);
1375: break;
1376: case INPUT_CSI_SM:
1377: input_csi_dispatch_sm(ictx);
1378: break;
1379: case INPUT_CSI_SM_PRIVATE:
1380: input_csi_dispatch_sm_private(ictx);
1381: break;
1382: case INPUT_CSI_TBC:
1383: switch (input_get(ictx, 0, 0, 0)) {
1384: case 0:
1385: if (s->cx < screen_size_x(s))
1386: bit_clear(s->tabs, s->cx);
1387: break;
1388: case 3:
1389: bit_nclear(s->tabs, 0, screen_size_x(s) - 1);
1390: break;
1391: default:
1392: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1393: break;
1394: }
1395: break;
1396: case INPUT_CSI_VPA:
1397: n = input_get(ictx, 0, 1, 1);
1398: screen_write_cursormove(sctx, s->cx, n - 1);
1399: break;
1400: case INPUT_CSI_DECSCUSR:
1401: n = input_get(ictx, 0, 0, 0);
1402: screen_set_cursor_style(s, n);
1403: break;
1404: }
1405:
1406: return (0);
1407: }
1408:
1409: /* Handle CSI RM. */
1410: void
1411: input_csi_dispatch_rm(struct input_ctx *ictx)
1412: {
1413: u_int i;
1414:
1415: for (i = 0; i < ictx->param_list_len; i++) {
1416: switch (input_get(ictx, i, 0, -1)) {
1.28 nicm 1417: case 4: /* IRM */
1.59 nicm 1418: screen_write_mode_clear(&ictx->ctx, MODE_INSERT);
1.28 nicm 1419: break;
1.72 nicm 1420: case 34:
1421: screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
1422: break;
1.28 nicm 1423: default:
1424: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1425: break;
1426: }
1.64 nicm 1427: }
1428: }
1429:
1430: /* Handle CSI private RM. */
1431: void
1432: input_csi_dispatch_rm_private(struct input_ctx *ictx)
1433: {
1.69 nicm 1434: struct window_pane *wp = ictx->wp;
1435: u_int i;
1.64 nicm 1436:
1437: for (i = 0; i < ictx->param_list_len; i++) {
1438: switch (input_get(ictx, i, 0, -1)) {
1439: case 1: /* DECCKM */
1.59 nicm 1440: screen_write_mode_clear(&ictx->ctx, MODE_KCURSOR);
1.1 nicm 1441: break;
1.17 nicm 1442: case 3: /* DECCOLM */
1.24 nicm 1443: screen_write_cursormove(&ictx->ctx, 0, 0);
1.17 nicm 1444: screen_write_clearscreen(&ictx->ctx);
1445: break;
1.61 nicm 1446: case 7: /* DECAWM */
1447: screen_write_mode_clear(&ictx->ctx, MODE_WRAP);
1448: break;
1.72 nicm 1449: case 12:
1450: screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
1451: break;
1.1 nicm 1452: case 25: /* TCEM */
1.59 nicm 1453: screen_write_mode_clear(&ictx->ctx, MODE_CURSOR);
1.1 nicm 1454: break;
1455: case 1000:
1.32 nicm 1456: case 1001:
1457: case 1002:
1.59 nicm 1458: screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
1.1 nicm 1459: break;
1.62 nicm 1460: case 1004:
1461: screen_write_mode_clear(&ictx->ctx, MODE_FOCUSON);
1462: break;
1.33 nicm 1463: case 1005:
1.59 nicm 1464: screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_UTF8);
1.33 nicm 1465: break;
1.60 nicm 1466: case 1006:
1467: screen_write_mode_clear(&ictx->ctx, MODE_MOUSE_SGR);
1468: break;
1.55 nicm 1469: case 47:
1470: case 1047:
1.69 nicm 1471: window_pane_alternate_off(wp, &ictx->cell.cell, 0);
1.55 nicm 1472: break;
1.9 nicm 1473: case 1049:
1.69 nicm 1474: window_pane_alternate_off(wp, &ictx->cell.cell, 1);
1.9 nicm 1475: break;
1.49 nicm 1476: case 2004:
1.59 nicm 1477: screen_write_mode_clear(&ictx->ctx, MODE_BRACKETPASTE);
1.49 nicm 1478: break;
1.1 nicm 1479: default:
1.28 nicm 1480: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1 nicm 1481: break;
1482: }
1.64 nicm 1483: }
1484: }
1485:
1486: /* Handle CSI SM. */
1487: void
1488: input_csi_dispatch_sm(struct input_ctx *ictx)
1489: {
1490: u_int i;
1491:
1492: for (i = 0; i < ictx->param_list_len; i++) {
1493: switch (input_get(ictx, i, 0, -1)) {
1.1 nicm 1494: case 4: /* IRM */
1.59 nicm 1495: screen_write_mode_set(&ictx->ctx, MODE_INSERT);
1.1 nicm 1496: break;
1.72 nicm 1497: case 34:
1498: screen_write_mode_clear(&ictx->ctx, MODE_BLINKING);
1499: break;
1.1 nicm 1500: default:
1.28 nicm 1501: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1 nicm 1502: break;
1503: }
1.64 nicm 1504: }
1505: }
1506:
1507: /* Handle CSI private SM. */
1508: void
1509: input_csi_dispatch_sm_private(struct input_ctx *ictx)
1510: {
1.69 nicm 1511: struct window_pane *wp = ictx->wp;
1512: u_int i;
1.64 nicm 1513:
1514: for (i = 0; i < ictx->param_list_len; i++) {
1515: switch (input_get(ictx, i, 0, -1)) {
1516: case 1: /* DECCKM */
1.59 nicm 1517: screen_write_mode_set(&ictx->ctx, MODE_KCURSOR);
1.17 nicm 1518: break;
1519: case 3: /* DECCOLM */
1.24 nicm 1520: screen_write_cursormove(&ictx->ctx, 0, 0);
1.17 nicm 1521: screen_write_clearscreen(&ictx->ctx);
1.61 nicm 1522: break;
1523: case 7: /* DECAWM */
1524: screen_write_mode_set(&ictx->ctx, MODE_WRAP);
1.72 nicm 1525: break;
1526: case 12:
1527: screen_write_mode_set(&ictx->ctx, MODE_BLINKING);
1.1 nicm 1528: break;
1529: case 25: /* TCEM */
1.59 nicm 1530: screen_write_mode_set(&ictx->ctx, MODE_CURSOR);
1.1 nicm 1531: break;
1532: case 1000:
1.59 nicm 1533: screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
1534: screen_write_mode_set(&ictx->ctx, MODE_MOUSE_STANDARD);
1.32 nicm 1535: break;
1536: case 1002:
1.59 nicm 1537: screen_write_mode_clear(&ictx->ctx, ALL_MOUSE_MODES);
1538: screen_write_mode_set(&ictx->ctx, MODE_MOUSE_BUTTON);
1.62 nicm 1539: break;
1540: case 1004:
1.64 nicm 1541: if (ictx->ctx.s->mode & MODE_FOCUSON)
1.62 nicm 1542: break;
1543: screen_write_mode_set(&ictx->ctx, MODE_FOCUSON);
1.69 nicm 1544: wp->flags |= PANE_FOCUSPUSH; /* force update */
1.33 nicm 1545: break;
1546: case 1005:
1.59 nicm 1547: screen_write_mode_set(&ictx->ctx, MODE_MOUSE_UTF8);
1.60 nicm 1548: break;
1549: case 1006:
1550: screen_write_mode_set(&ictx->ctx, MODE_MOUSE_SGR);
1.9 nicm 1551: break;
1.55 nicm 1552: case 47:
1553: case 1047:
1.69 nicm 1554: window_pane_alternate_on(wp, &ictx->cell.cell, 0);
1.55 nicm 1555: break;
1.9 nicm 1556: case 1049:
1.69 nicm 1557: window_pane_alternate_on(wp, &ictx->cell.cell, 1);
1.49 nicm 1558: break;
1559: case 2004:
1.59 nicm 1560: screen_write_mode_set(&ictx->ctx, MODE_BRACKETPASTE);
1.1 nicm 1561: break;
1562: default:
1.28 nicm 1563: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1.1 nicm 1564: break;
1565: }
1.65 nicm 1566: }
1567: }
1568:
1569: /* Handle CSI window operations. */
1570: void
1571: input_csi_dispatch_winops(struct input_ctx *ictx)
1572: {
1573: struct window_pane *wp = ictx->wp;
1574: int n, m;
1575:
1576: m = 0;
1577: while ((n = input_get(ictx, m, 0, -1)) != -1) {
1578: switch (n) {
1579: case 1:
1580: case 2:
1581: case 5:
1582: case 6:
1583: case 7:
1584: case 11:
1585: case 13:
1586: case 14:
1587: case 19:
1588: case 20:
1589: case 21:
1590: case 24:
1591: break;
1592: case 3:
1593: case 4:
1594: case 8:
1595: m++;
1596: if (input_get(ictx, m, 0, -1) == -1)
1597: return;
1598: /* FALLTHROUGH */
1599: case 9:
1600: case 10:
1601: case 22:
1602: case 23:
1603: m++;
1604: if (input_get(ictx, m, 0, -1) == -1)
1605: return;
1606: break;
1607: case 18:
1.76 nicm 1608: input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx);
1.65 nicm 1609: break;
1610: default:
1611: log_debug("%s: unknown '%c'", __func__, ictx->ch);
1612: break;
1613: }
1614: m++;
1.1 nicm 1615: }
1616: }
1617:
1.78 nicm 1618: /* Handle CSI SGR for 256 colours. */
1619: void
1620: input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1621: {
1622: struct grid_cell *gc = &ictx->cell.cell;
1623: int c;
1624:
1625: (*i)++;
1626: c = input_get(ictx, *i, 0, -1);
1627: if (c == -1) {
1628: if (fgbg == 38) {
1629: gc->flags &= ~GRID_FLAG_FG256;
1630: gc->fg = 8;
1631: } else if (fgbg == 48) {
1632: gc->flags &= ~GRID_FLAG_BG256;
1633: gc->bg = 8;
1634: }
1635: } else {
1636: if (fgbg == 38) {
1637: gc->flags |= GRID_FLAG_FG256;
1638: gc->fg = c;
1639: } else if (fgbg == 48) {
1640: gc->flags |= GRID_FLAG_BG256;
1641: gc->bg = c;
1642: }
1643: }
1644: }
1645:
1646: /* Handle CSI SGR for RGB colours. */
1647: void
1648: input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1649: {
1650: struct grid_cell *gc = &ictx->cell.cell;
1651: int c, r, g, b;
1652:
1653: (*i)++;
1654: r = input_get(ictx, *i, 0, -1);
1655: if (r == -1 || r > 255)
1656: return;
1657: (*i)++;
1658: g = input_get(ictx, *i, 0, -1);
1659: if (g == -1 || g > 255)
1660: return;
1661: (*i)++;
1662: b = input_get(ictx, *i, 0, -1);
1663: if (b == -1 || b > 255)
1664: return;
1665:
1666: c = colour_find_rgb(r, g, b);
1667: if (fgbg == 38) {
1668: gc->flags |= GRID_FLAG_FG256;
1669: gc->fg = c;
1670: } else if (fgbg == 48) {
1671: gc->flags |= GRID_FLAG_BG256;
1672: gc->bg = c;
1673: }
1674: }
1675:
1.28 nicm 1676: /* Handle CSI SGR. */
1.1 nicm 1677: void
1.28 nicm 1678: input_csi_dispatch_sgr(struct input_ctx *ictx)
1.1 nicm 1679: {
1.69 nicm 1680: struct grid_cell *gc = &ictx->cell.cell;
1.28 nicm 1681: u_int i;
1.78 nicm 1682: int n;
1.1 nicm 1683:
1.28 nicm 1684: if (ictx->param_list_len == 0) {
1.1 nicm 1685: memcpy(gc, &grid_default_cell, sizeof *gc);
1686: return;
1687: }
1688:
1.28 nicm 1689: for (i = 0; i < ictx->param_list_len; i++) {
1690: n = input_get(ictx, i, 0, 0);
1.1 nicm 1691:
1.28 nicm 1692: if (n == 38 || n == 48) {
1.1 nicm 1693: i++;
1.78 nicm 1694: switch (input_get(ictx, i, 0, -1)) {
1695: case 2:
1696: input_csi_dispatch_sgr_rgb(ictx, n, &i);
1697: break;
1698: case 5:
1699: input_csi_dispatch_sgr_256(ictx, n, &i);
1700: break;
1.1 nicm 1701: }
1702: continue;
1703: }
1704:
1.28 nicm 1705: switch (n) {
1.1 nicm 1706: case 0:
1707: case 10:
1708: memcpy(gc, &grid_default_cell, sizeof *gc);
1709: break;
1710: case 1:
1711: gc->attr |= GRID_ATTR_BRIGHT;
1712: break;
1713: case 2:
1714: gc->attr |= GRID_ATTR_DIM;
1715: break;
1716: case 3:
1717: gc->attr |= GRID_ATTR_ITALICS;
1718: break;
1719: case 4:
1720: gc->attr |= GRID_ATTR_UNDERSCORE;
1721: break;
1722: case 5:
1723: gc->attr |= GRID_ATTR_BLINK;
1724: break;
1725: case 7:
1726: gc->attr |= GRID_ATTR_REVERSE;
1727: break;
1728: case 8:
1729: gc->attr |= GRID_ATTR_HIDDEN;
1730: break;
1731: case 22:
1732: gc->attr &= ~(GRID_ATTR_BRIGHT|GRID_ATTR_DIM);
1733: break;
1734: case 23:
1735: gc->attr &= ~GRID_ATTR_ITALICS;
1736: break;
1737: case 24:
1738: gc->attr &= ~GRID_ATTR_UNDERSCORE;
1739: break;
1740: case 25:
1741: gc->attr &= ~GRID_ATTR_BLINK;
1742: break;
1743: case 27:
1744: gc->attr &= ~GRID_ATTR_REVERSE;
1745: break;
1746: case 30:
1747: case 31:
1748: case 32:
1749: case 33:
1750: case 34:
1751: case 35:
1752: case 36:
1753: case 37:
1754: gc->flags &= ~GRID_FLAG_FG256;
1.28 nicm 1755: gc->fg = n - 30;
1.1 nicm 1756: break;
1757: case 39:
1758: gc->flags &= ~GRID_FLAG_FG256;
1759: gc->fg = 8;
1760: break;
1761: case 40:
1762: case 41:
1763: case 42:
1764: case 43:
1765: case 44:
1766: case 45:
1767: case 46:
1768: case 47:
1769: gc->flags &= ~GRID_FLAG_BG256;
1.28 nicm 1770: gc->bg = n - 40;
1.1 nicm 1771: break;
1772: case 49:
1773: gc->flags &= ~GRID_FLAG_BG256;
1774: gc->bg = 8;
1.20 nicm 1775: break;
1776: case 90:
1777: case 91:
1778: case 92:
1779: case 93:
1780: case 94:
1781: case 95:
1782: case 96:
1783: case 97:
1.26 nicm 1784: gc->flags &= ~GRID_FLAG_FG256;
1.28 nicm 1785: gc->fg = n;
1.20 nicm 1786: break;
1787: case 100:
1788: case 101:
1789: case 102:
1790: case 103:
1791: case 104:
1792: case 105:
1793: case 106:
1794: case 107:
1.26 nicm 1795: gc->flags &= ~GRID_FLAG_BG256;
1.47 nicm 1796: gc->bg = n - 10;
1.1 nicm 1797: break;
1798: }
1799: }
1.28 nicm 1800: }
1801:
1.37 nicm 1802: /* DCS terminator (ST) received. */
1803: int
1804: input_dcs_dispatch(struct input_ctx *ictx)
1.28 nicm 1805: {
1.37 nicm 1806: const char prefix[] = "tmux;";
1807: const u_int prefix_len = (sizeof prefix) - 1;
1808:
1809: if (ictx->flags & INPUT_DISCARD)
1810: return (0);
1811:
1812: log_debug("%s: \"%s\"", __func__, ictx->input_buf);
1.28 nicm 1813:
1.37 nicm 1814: /* Check for tmux prefix. */
1815: if (ictx->input_len >= prefix_len &&
1816: strncmp(ictx->input_buf, prefix, prefix_len) == 0) {
1817: screen_write_rawstring(&ictx->ctx,
1818: ictx->input_buf + prefix_len, ictx->input_len - prefix_len);
1819: }
1.28 nicm 1820:
1.37 nicm 1821: return (0);
1.28 nicm 1822: }
1823:
1824: /* OSC string started. */
1825: void
1826: input_enter_osc(struct input_ctx *ictx)
1827: {
1828: log_debug("%s", __func__);
1829:
1.35 nicm 1830: input_clear(ictx);
1.28 nicm 1831: }
1832:
1833: /* OSC terminator (ST) received. */
1834: void
1835: input_exit_osc(struct input_ctx *ictx)
1836: {
1.67 nicm 1837: u_char *p = ictx->input_buf;
1.73 nicm 1838: u_int option;
1.38 nicm 1839:
1.28 nicm 1840: if (ictx->flags & INPUT_DISCARD)
1841: return;
1.38 nicm 1842: if (ictx->input_len < 1 || *p < '0' || *p > '9')
1843: return;
1.28 nicm 1844:
1.38 nicm 1845: log_debug("%s: \"%s\"", __func__, p);
1.28 nicm 1846:
1.38 nicm 1847: option = 0;
1848: while (*p >= '0' && *p <= '9')
1849: option = option * 10 + *p++ - '0';
1850: if (*p == ';')
1851: p++;
1852:
1853: switch (option) {
1854: case 0:
1855: case 2:
1856: screen_set_title(ictx->ctx.s, p);
1857: server_status_window(ictx->wp->window);
1858: break;
1859: case 12:
1.57 nicm 1860: if (*p != '?') /* ? is colour request */
1861: screen_set_cursor_colour(ictx->ctx.s, p);
1.38 nicm 1862: break;
1863: case 112:
1.57 nicm 1864: if (*p == '\0') /* no arguments allowed */
1.38 nicm 1865: screen_set_cursor_colour(ictx->ctx.s, "");
1866: break;
1867: default:
1868: log_debug("%s: unknown '%u'", __func__, option);
1869: break;
1870: }
1.28 nicm 1871: }
1872:
1873: /* APC string started. */
1874: void
1875: input_enter_apc(struct input_ctx *ictx)
1876: {
1877: log_debug("%s", __func__);
1878:
1.35 nicm 1879: input_clear(ictx);
1.28 nicm 1880: }
1881:
1882: /* APC terminator (ST) received. */
1883: void
1884: input_exit_apc(struct input_ctx *ictx)
1885: {
1886: if (ictx->flags & INPUT_DISCARD)
1887: return;
1888: log_debug("%s: \"%s\"", __func__, ictx->input_buf);
1889:
1890: screen_set_title(ictx->ctx.s, ictx->input_buf);
1891: server_status_window(ictx->wp->window);
1892: }
1893:
1894: /* Rename string started. */
1895: void
1896: input_enter_rename(struct input_ctx *ictx)
1897: {
1898: log_debug("%s", __func__);
1899:
1.35 nicm 1900: input_clear(ictx);
1.28 nicm 1901: }
1902:
1903: /* Rename terminator (ST) received. */
1904: void
1905: input_exit_rename(struct input_ctx *ictx)
1906: {
1907: if (ictx->flags & INPUT_DISCARD)
1.44 nicm 1908: return;
1909: if (!options_get_number(&ictx->wp->window->options, "allow-rename"))
1.28 nicm 1910: return;
1911: log_debug("%s: \"%s\"", __func__, ictx->input_buf);
1912:
1.48 nicm 1913: window_set_name(ictx->wp->window, ictx->input_buf);
1.28 nicm 1914: options_set_number(&ictx->wp->window->options, "automatic-rename", 0);
1915:
1916: server_status_window(ictx->wp->window);
1917: }
1918:
1919: /* Open UTF-8 character. */
1920: int
1921: input_utf8_open(struct input_ctx *ictx)
1922: {
1923: if (!options_get_number(&ictx->wp->window->options, "utf8")) {
1924: /* Print, and do not switch state. */
1925: input_print(ictx);
1926: return (-1);
1927: }
1928: log_debug("%s", __func__);
1929:
1930: utf8_open(&ictx->utf8data, ictx->ch);
1931: return (0);
1932: }
1933:
1934: /* Append to UTF-8 character. */
1935: int
1936: input_utf8_add(struct input_ctx *ictx)
1937: {
1938: log_debug("%s", __func__);
1939:
1940: utf8_append(&ictx->utf8data, ictx->ch);
1941: return (0);
1942: }
1943:
1944: /* Close UTF-8 string. */
1945: int
1946: input_utf8_close(struct input_ctx *ictx)
1947: {
1948: log_debug("%s", __func__);
1949:
1950: utf8_append(&ictx->utf8data, ictx->ch);
1951:
1.69 nicm 1952: grid_cell_set(&ictx->cell.cell, &ictx->utf8data);
1953: screen_write_cell(&ictx->ctx, &ictx->cell.cell);
1.28 nicm 1954:
1955: return (0);
1.1 nicm 1956: }