4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
27 * Based on the description by Paul Williams at:
29 * http://vt100.net/emu/dec_ansi_parser
31 * With the following changes:
35 * - Support for UTF-8.
37 * - OSC (but not APC) may be terminated by \007 as well as ST.
39 * - A state for APC similar to OSC. Some terminals appear to use this to set
42 * - A state for the screen \033k...\033\\ sequence to rename a window. This is
43 * pretty stupid but not supporting it is more trouble than it is worth.
45 * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
46 * be passed to the underlying teminal(s).
49 /* Helper functions. */
50 int input_split(struct input_ctx
*);
51 int input_get(struct input_ctx
*, u_int
, int, int);
52 void input_reply(struct input_ctx
*, const char *, ...);
54 /* Transition entry/exit handlers. */
55 void input_clear(struct input_ctx
*);
56 void input_enter_osc(struct input_ctx
*);
57 void input_exit_osc(struct input_ctx
*);
58 void input_enter_apc(struct input_ctx
*);
59 void input_exit_apc(struct input_ctx
*);
60 void input_enter_rename(struct input_ctx
*);
61 void input_exit_rename(struct input_ctx
*);
63 /* Input state handlers. */
64 int input_print(struct input_ctx
*);
65 int input_intermediate(struct input_ctx
*);
66 int input_parameter(struct input_ctx
*);
67 int input_input(struct input_ctx
*);
68 int input_c0_dispatch(struct input_ctx
*);
69 int input_esc_dispatch(struct input_ctx
*);
70 int input_csi_dispatch(struct input_ctx
*);
71 void input_csi_dispatch_sgr(struct input_ctx
*);
72 int input_dcs_dispatch(struct input_ctx
*);
73 int input_utf8_open(struct input_ctx
*);
74 int input_utf8_add(struct input_ctx
*);
75 int input_utf8_close(struct input_ctx
*);
77 /* Command table comparison function. */
78 int input_table_compare(const void *, const void *);
80 /* Command table entry. */
81 struct input_table_entry
{
87 /* Escape commands. */
103 /* Escape command table. */
104 const struct input_table_entry input_esc_table
[] = {
105 { '0', "(", INPUT_ESC_SCSOFF_G0
},
106 { '7', "", INPUT_ESC_DECSC
},
107 { '8', "", INPUT_ESC_DECRC
},
108 { '8', "#", INPUT_ESC_DECALN
},
109 { '=', "", INPUT_ESC_DECKPAM
},
110 { '>', "", INPUT_ESC_DECKPNM
},
111 { 'B', "(", INPUT_ESC_SCSON_G0
},
112 { 'D', "", INPUT_ESC_IND
},
113 { 'E', "", INPUT_ESC_NEL
},
114 { 'H', "", INPUT_ESC_HTS
},
115 { 'M', "", INPUT_ESC_RI
},
116 { 'c', "", INPUT_ESC_RIS
},
119 /* Control (CSI) commands. */
120 enum input_csi_type
{
139 INPUT_CSI_RM_PRIVATE
,
142 INPUT_CSI_SM_PRIVATE
,
147 /* Control (CSI) command table. */
148 const struct input_table_entry input_csi_table
[] = {
149 { '@', "", INPUT_CSI_ICH
},
150 { 'A', "", INPUT_CSI_CUU
},
151 { 'B', "", INPUT_CSI_CUD
},
152 { 'C', "", INPUT_CSI_CUF
},
153 { 'D', "", INPUT_CSI_CUB
},
154 { 'G', "", INPUT_CSI_HPA
},
155 { 'H', "", INPUT_CSI_CUP
},
156 { 'J', "", INPUT_CSI_ED
},
157 { 'K', "", INPUT_CSI_EL
},
158 { 'L', "", INPUT_CSI_IL
},
159 { 'M', "", INPUT_CSI_DL
},
160 { 'P', "", INPUT_CSI_DCH
},
161 { 'Z', "", INPUT_CSI_CBT
},
162 { 'c', "", INPUT_CSI_DA
},
163 { 'd', "", INPUT_CSI_VPA
},
164 { 'f', "", INPUT_CSI_CUP
},
165 { 'g', "", INPUT_CSI_TBC
},
166 { 'h', "", INPUT_CSI_SM
},
167 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
168 { 'l', "", INPUT_CSI_RM
},
169 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
170 { 'm', "", INPUT_CSI_SGR
},
171 { 'n', "", INPUT_CSI_DSR
},
172 { 'q', " ", INPUT_CSI_DECSCUSR
},
173 { 'r', "", INPUT_CSI_DECSTBM
},
176 /* Input transition. */
177 struct input_transition
{
181 int (*handler
)(struct input_ctx
*);
182 const struct input_state
*state
;
188 void (*enter
)(struct input_ctx
*);
189 void (*exit
)(struct input_ctx
*);
190 const struct input_transition
*transitions
;
193 /* State transitions available from all states. */
194 #define INPUT_STATE_ANYWHERE \
195 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
196 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
197 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
199 /* Forward declarations of state tables. */
200 const struct input_transition input_state_ground_table
[];
201 const struct input_transition input_state_esc_enter_table
[];
202 const struct input_transition input_state_esc_intermediate_table
[];
203 const struct input_transition input_state_csi_enter_table
[];
204 const struct input_transition input_state_csi_parameter_table
[];
205 const struct input_transition input_state_csi_intermediate_table
[];
206 const struct input_transition input_state_csi_ignore_table
[];
207 const struct input_transition input_state_dcs_enter_table
[];
208 const struct input_transition input_state_dcs_parameter_table
[];
209 const struct input_transition input_state_dcs_intermediate_table
[];
210 const struct input_transition input_state_dcs_handler_table
[];
211 const struct input_transition input_state_dcs_escape_table
[];
212 const struct input_transition input_state_dcs_ignore_table
[];
213 const struct input_transition input_state_osc_string_table
[];
214 const struct input_transition input_state_apc_string_table
[];
215 const struct input_transition input_state_rename_string_table
[];
216 const struct input_transition input_state_consume_st_table
[];
217 const struct input_transition input_state_utf8_three_table
[];
218 const struct input_transition input_state_utf8_two_table
[];
219 const struct input_transition input_state_utf8_one_table
[];
221 /* ground state definition. */
222 const struct input_state input_state_ground
= {
225 input_state_ground_table
228 /* esc_enter state definition. */
229 const struct input_state input_state_esc_enter
= {
232 input_state_esc_enter_table
235 /* esc_intermediate state definition. */
236 const struct input_state input_state_esc_intermediate
= {
239 input_state_esc_intermediate_table
242 /* csi_enter state definition. */
243 const struct input_state input_state_csi_enter
= {
246 input_state_csi_enter_table
249 /* csi_parameter state definition. */
250 const struct input_state input_state_csi_parameter
= {
253 input_state_csi_parameter_table
256 /* csi_intermediate state definition. */
257 const struct input_state input_state_csi_intermediate
= {
260 input_state_csi_intermediate_table
263 /* csi_ignore state definition. */
264 const struct input_state input_state_csi_ignore
= {
267 input_state_csi_ignore_table
270 /* dcs_enter state definition. */
271 const struct input_state input_state_dcs_enter
= {
274 input_state_dcs_enter_table
277 /* dcs_parameter state definition. */
278 const struct input_state input_state_dcs_parameter
= {
281 input_state_dcs_parameter_table
284 /* dcs_intermediate state definition. */
285 const struct input_state input_state_dcs_intermediate
= {
288 input_state_dcs_intermediate_table
291 /* dcs_handler state definition. */
292 const struct input_state input_state_dcs_handler
= {
295 input_state_dcs_handler_table
298 /* dcs_escape state definition. */
299 const struct input_state input_state_dcs_escape
= {
302 input_state_dcs_escape_table
305 /* dcs_ignore state definition. */
306 const struct input_state input_state_dcs_ignore
= {
309 input_state_dcs_ignore_table
312 /* osc_string state definition. */
313 const struct input_state input_state_osc_string
= {
315 input_enter_osc
, input_exit_osc
,
316 input_state_osc_string_table
319 /* apc_string state definition. */
320 const struct input_state input_state_apc_string
= {
322 input_enter_apc
, input_exit_apc
,
323 input_state_apc_string_table
326 /* rename_string state definition. */
327 const struct input_state input_state_rename_string
= {
329 input_enter_rename
, input_exit_rename
,
330 input_state_rename_string_table
333 /* consume_st state definition. */
334 const struct input_state input_state_consume_st
= {
337 input_state_consume_st_table
340 /* utf8_three state definition. */
341 const struct input_state input_state_utf8_three
= {
344 input_state_utf8_three_table
347 /* utf8_two state definition. */
348 const struct input_state input_state_utf8_two
= {
351 input_state_utf8_two_table
354 /* utf8_one state definition. */
355 const struct input_state input_state_utf8_one
= {
358 input_state_utf8_one_table
361 /* ground state table. */
362 const struct input_transition input_state_ground_table
[] = {
363 INPUT_STATE_ANYWHERE
,
365 { 0x00, 0x17, input_c0_dispatch
, NULL
},
366 { 0x19, 0x19, input_c0_dispatch
, NULL
},
367 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
368 { 0x20, 0x7e, input_print
, NULL
},
369 { 0x7f, 0x7f, NULL
, NULL
},
370 { 0x80, 0xc1, input_print
, NULL
},
371 { 0xc2, 0xdf, input_utf8_open
, &input_state_utf8_one
},
372 { 0xe0, 0xef, input_utf8_open
, &input_state_utf8_two
},
373 { 0xf0, 0xf4, input_utf8_open
, &input_state_utf8_three
},
374 { 0xf5, 0xff, input_print
, NULL
},
376 { -1, -1, NULL
, NULL
}
379 /* esc_enter state table. */
380 const struct input_transition input_state_esc_enter_table
[] = {
381 INPUT_STATE_ANYWHERE
,
383 { 0x00, 0x17, input_c0_dispatch
, NULL
},
384 { 0x19, 0x19, input_c0_dispatch
, NULL
},
385 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
386 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
387 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
388 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
389 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
390 { 0x58, 0x58, NULL
, &input_state_consume_st
},
391 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
392 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
393 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
394 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
395 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
396 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
397 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
398 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
399 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
400 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
401 { 0x7f, 0xff, NULL
, NULL
},
403 { -1, -1, NULL
, NULL
}
406 /* esc_interm state table. */
407 const struct input_transition input_state_esc_intermediate_table
[] = {
408 INPUT_STATE_ANYWHERE
,
410 { 0x00, 0x17, input_c0_dispatch
, NULL
},
411 { 0x19, 0x19, input_c0_dispatch
, NULL
},
412 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
413 { 0x20, 0x2f, input_intermediate
, NULL
},
414 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
415 { 0x7f, 0xff, NULL
, NULL
},
417 { -1, -1, NULL
, NULL
}
420 /* csi_enter state table. */
421 const struct input_transition input_state_csi_enter_table
[] = {
422 INPUT_STATE_ANYWHERE
,
424 { 0x00, 0x17, input_c0_dispatch
, NULL
},
425 { 0x19, 0x19, input_c0_dispatch
, NULL
},
426 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
427 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
428 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
429 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
430 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
431 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
432 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
433 { 0x7f, 0xff, NULL
, NULL
},
435 { -1, -1, NULL
, NULL
}
438 /* csi_parameter state table. */
439 const struct input_transition input_state_csi_parameter_table
[] = {
440 INPUT_STATE_ANYWHERE
,
442 { 0x00, 0x17, input_c0_dispatch
, NULL
},
443 { 0x19, 0x19, input_c0_dispatch
, NULL
},
444 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
445 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
446 { 0x30, 0x39, input_parameter
, NULL
},
447 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
448 { 0x3b, 0x3b, input_parameter
, NULL
},
449 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
450 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
451 { 0x7f, 0xff, NULL
, NULL
},
453 { -1, -1, NULL
, NULL
}
456 /* csi_intermediate state table. */
457 const struct input_transition input_state_csi_intermediate_table
[] = {
458 INPUT_STATE_ANYWHERE
,
460 { 0x00, 0x17, input_c0_dispatch
, NULL
},
461 { 0x19, 0x19, input_c0_dispatch
, NULL
},
462 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
463 { 0x20, 0x2f, input_intermediate
, NULL
},
464 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
465 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
466 { 0x7f, 0xff, NULL
, NULL
},
468 { -1, -1, NULL
, NULL
}
471 /* csi_ignore state table. */
472 const struct input_transition input_state_csi_ignore_table
[] = {
473 INPUT_STATE_ANYWHERE
,
475 { 0x00, 0x17, input_c0_dispatch
, NULL
},
476 { 0x19, 0x19, input_c0_dispatch
, NULL
},
477 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
478 { 0x20, 0x3f, NULL
, NULL
},
479 { 0x40, 0x7e, NULL
, &input_state_ground
},
480 { 0x7f, 0xff, NULL
, NULL
},
482 { -1, -1, NULL
, NULL
}
485 /* dcs_enter state table. */
486 const struct input_transition input_state_dcs_enter_table
[] = {
487 INPUT_STATE_ANYWHERE
,
489 { 0x00, 0x17, NULL
, NULL
},
490 { 0x19, 0x19, NULL
, NULL
},
491 { 0x1c, 0x1f, NULL
, NULL
},
492 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
493 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
494 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
495 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
496 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
497 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
498 { 0x7f, 0xff, NULL
, NULL
},
500 { -1, -1, NULL
, NULL
}
503 /* dcs_parameter state table. */
504 const struct input_transition input_state_dcs_parameter_table
[] = {
505 INPUT_STATE_ANYWHERE
,
507 { 0x00, 0x17, NULL
, NULL
},
508 { 0x19, 0x19, NULL
, NULL
},
509 { 0x1c, 0x1f, NULL
, NULL
},
510 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
511 { 0x30, 0x39, input_parameter
, NULL
},
512 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
513 { 0x3b, 0x3b, input_parameter
, NULL
},
514 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
515 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
516 { 0x7f, 0xff, NULL
, NULL
},
518 { -1, -1, NULL
, NULL
}
521 /* dcs_interm state table. */
522 const struct input_transition input_state_dcs_intermediate_table
[] = {
523 INPUT_STATE_ANYWHERE
,
525 { 0x00, 0x17, NULL
, NULL
},
526 { 0x19, 0x19, NULL
, NULL
},
527 { 0x1c, 0x1f, NULL
, NULL
},
528 { 0x20, 0x2f, input_intermediate
, NULL
},
529 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
530 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
531 { 0x7f, 0xff, NULL
, NULL
},
533 { -1, -1, NULL
, NULL
}
536 /* dcs_handler state table. */
537 const struct input_transition input_state_dcs_handler_table
[] = {
538 /* No INPUT_STATE_ANYWHERE */
540 { 0x00, 0x1a, input_input
, NULL
},
541 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
542 { 0x1c, 0xff, input_input
, NULL
},
544 { -1, -1, NULL
, NULL
}
547 /* dcs_escape state table. */
548 const struct input_transition input_state_dcs_escape_table
[] = {
549 /* No INPUT_STATE_ANYWHERE */
551 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
552 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
553 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
555 { -1, -1, NULL
, NULL
}
558 /* dcs_ignore state table. */
559 const struct input_transition input_state_dcs_ignore_table
[] = {
560 INPUT_STATE_ANYWHERE
,
562 { 0x00, 0x17, NULL
, NULL
},
563 { 0x19, 0x19, NULL
, NULL
},
564 { 0x1c, 0x1f, NULL
, NULL
},
565 { 0x20, 0xff, NULL
, NULL
},
567 { -1, -1, NULL
, NULL
}
570 /* osc_string state table. */
571 const struct input_transition input_state_osc_string_table
[] = {
572 INPUT_STATE_ANYWHERE
,
574 { 0x00, 0x06, NULL
, NULL
},
575 { 0x07, 0x07, NULL
, &input_state_ground
},
576 { 0x08, 0x17, NULL
, NULL
},
577 { 0x19, 0x19, NULL
, NULL
},
578 { 0x1c, 0x1f, NULL
, NULL
},
579 { 0x20, 0xff, input_input
, NULL
},
581 { -1, -1, NULL
, NULL
}
584 /* apc_string state table. */
585 const struct input_transition input_state_apc_string_table
[] = {
586 INPUT_STATE_ANYWHERE
,
588 { 0x00, 0x17, NULL
, NULL
},
589 { 0x19, 0x19, NULL
, NULL
},
590 { 0x1c, 0x1f, NULL
, NULL
},
591 { 0x20, 0xff, input_input
, NULL
},
593 { -1, -1, NULL
, NULL
}
596 /* rename_string state table. */
597 const struct input_transition input_state_rename_string_table
[] = {
598 INPUT_STATE_ANYWHERE
,
600 { 0x00, 0x17, NULL
, NULL
},
601 { 0x19, 0x19, NULL
, NULL
},
602 { 0x1c, 0x1f, NULL
, NULL
},
603 { 0x20, 0xff, input_input
, NULL
},
605 { -1, -1, NULL
, NULL
}
608 /* consume_st state table. */
609 const struct input_transition input_state_consume_st_table
[] = {
610 INPUT_STATE_ANYWHERE
,
612 { 0x00, 0x17, NULL
, NULL
},
613 { 0x19, 0x19, NULL
, NULL
},
614 { 0x1c, 0x1f, NULL
, NULL
},
615 { 0x20, 0xff, NULL
, NULL
},
617 { -1, -1, NULL
, NULL
}
620 /* utf8_three state table. */
621 const struct input_transition input_state_utf8_three_table
[] = {
622 /* No INPUT_STATE_ANYWHERE */
624 { 0x00, 0x7f, NULL
, &input_state_ground
},
625 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_two
},
626 { 0xc0, 0xff, NULL
, &input_state_ground
},
628 { -1, -1, NULL
, NULL
}
631 /* utf8_two state table. */
632 const struct input_transition input_state_utf8_two_table
[] = {
633 /* No INPUT_STATE_ANYWHERE */
635 { 0x00, 0x7f, NULL
, &input_state_ground
},
636 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_one
},
637 { 0xc0, 0xff, NULL
, &input_state_ground
},
639 { -1, -1, NULL
, NULL
}
642 /* utf8_one state table. */
643 const struct input_transition input_state_utf8_one_table
[] = {
644 /* No INPUT_STATE_ANYWHERE */
646 { 0x00, 0x7f, NULL
, &input_state_ground
},
647 { 0x80, 0xbf, input_utf8_close
, &input_state_ground
},
648 { 0xc0, 0xff, NULL
, &input_state_ground
},
650 { -1, -1, NULL
, NULL
}
653 /* Input table compare. */
655 input_table_compare(const void *key
, const void *value
)
657 const struct input_ctx
*ictx
= key
;
658 const struct input_table_entry
*entry
= value
;
660 if (ictx
->ch
!= entry
->ch
)
661 return (ictx
->ch
- entry
->ch
);
662 return (strcmp(ictx
->interm_buf
, entry
->interm
));
665 /* Initialise input parser. */
667 input_init(struct window_pane
*wp
)
669 struct input_ctx
*ictx
= &wp
->ictx
;
671 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
673 memcpy(&ictx
->old_cell
, &grid_default_cell
, sizeof ictx
->old_cell
);
677 *ictx
->interm_buf
= '\0';
678 ictx
->interm_len
= 0;
680 *ictx
->param_buf
= '\0';
683 ictx
->state
= &input_state_ground
;
687 /* Destroy input parser. */
689 input_free(unused
struct window_pane
*wp
)
695 input_parse(struct window_pane
*wp
)
697 struct input_ctx
*ictx
= &wp
->ictx
;
698 const struct input_transition
*itr
;
699 struct evbuffer
*evb
= wp
->event
->input
;
703 if (EVBUFFER_LENGTH(evb
) == 0)
706 wp
->window
->flags
|= WINDOW_ACTIVITY
;
707 wp
->window
->flags
&= ~WINDOW_SILENCE
;
710 * Open the screen. Use NULL wp if there is a mode set as don't want to
713 if (wp
->mode
== NULL
)
714 screen_write_start(&ictx
->ctx
, wp
, &wp
->base
);
716 screen_write_start(&ictx
->ctx
, NULL
, &wp
->base
);
719 buf
= EVBUFFER_DATA(evb
);
720 len
= EVBUFFER_LENGTH(evb
);
723 /* Parse the input. */
725 ictx
->ch
= buf
[off
++];
726 log_debug("%s: '%c' %s", __func__
, ictx
->ch
, ictx
->state
->name
);
728 /* Find the transition. */
729 itr
= ictx
->state
->transitions
;
730 while (itr
->first
!= -1 && itr
->last
!= -1) {
731 if (ictx
->ch
>= itr
->first
&& ictx
->ch
<= itr
->last
)
735 if (itr
->first
== -1 || itr
->last
== -1) {
736 /* No transition? Eh? */
737 fatalx("No transition from state!");
741 * Execute the handler, if any. Don't switch state if it
744 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
747 /* And switch state, if necessary. */
748 if (itr
->state
!= NULL
) {
749 if (ictx
->state
->exit
!= NULL
)
750 ictx
->state
->exit(ictx
);
751 ictx
->state
= itr
->state
;
752 if (ictx
->state
->enter
!= NULL
)
753 ictx
->state
->enter(ictx
);
757 /* Close the screen. */
758 screen_write_stop(&ictx
->ctx
);
760 evbuffer_drain(evb
, len
);
763 /* Split the parameter list (if any). */
765 input_split(struct input_ctx
*ictx
)
772 ictx
->param_list_len
= 0;
773 if (ictx
->param_len
== 0)
776 ptr
= ictx
->param_buf
;
777 while ((out
= strsep(&ptr
, ";")) != NULL
) {
781 n
= strtonum(out
, 0, INT_MAX
, &errstr
);
786 ictx
->param_list
[ictx
->param_list_len
++] = n
;
787 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
794 /* Get an argument or return default value. */
796 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
800 if (validx
>= ictx
->param_list_len
)
803 retval
= ictx
->param_list
[validx
];
811 /* Reply to terminal query. */
813 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
819 vasprintf(&reply
, fmt
, ap
);
822 bufferevent_write(ictx
->wp
->event
, reply
, strlen(reply
));
826 /* Clear saved state. */
828 input_clear(struct input_ctx
*ictx
)
830 *ictx
->interm_buf
= '\0';
831 ictx
->interm_len
= 0;
833 *ictx
->param_buf
= '\0';
836 *ictx
->input_buf
= '\0';
839 ictx
->flags
&= ~INPUT_DISCARD
;
842 /* Output this character to the screen. */
844 input_print(struct input_ctx
*ictx
)
846 ictx
->cell
.data
= ictx
->ch
;
847 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, NULL
);
852 /* Collect intermediate string. */
854 input_intermediate(struct input_ctx
*ictx
)
856 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
857 ictx
->flags
|= INPUT_DISCARD
;
859 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
860 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
866 /* Collect parameter string. */
868 input_parameter(struct input_ctx
*ictx
)
870 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
871 ictx
->flags
|= INPUT_DISCARD
;
873 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
874 ictx
->param_buf
[ictx
->param_len
] = '\0';
880 /* Collect input string. */
882 input_input(struct input_ctx
*ictx
)
884 if (ictx
->input_len
== (sizeof ictx
->input_buf
) - 1)
885 ictx
->flags
|= INPUT_DISCARD
;
887 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
888 ictx
->input_buf
[ictx
->input_len
] = '\0';
894 /* Execute C0 control sequence. */
896 input_c0_dispatch(struct input_ctx
*ictx
)
898 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
899 struct window_pane
*wp
= ictx
->wp
;
900 struct screen
*s
= sctx
->s
;
902 log_debug("%s: '%c", __func__
, ictx
->ch
);
905 case '\000': /* NUL */
907 case '\007': /* BEL */
908 wp
->window
->flags
|= WINDOW_BELL
;
910 case '\010': /* BS */
911 screen_write_backspace(sctx
);
913 case '\011': /* HT */
914 /* Don't tab beyond the end of the line. */
915 if (s
->cx
>= screen_size_x(s
) - 1)
918 /* Find the next tab point, or use the last column if none. */
921 if (bit_test(s
->tabs
, s
->cx
))
923 } while (s
->cx
< screen_size_x(s
) - 1);
925 case '\012': /* LF */
926 case '\013': /* VT */
927 case '\014': /* FF */
928 screen_write_linefeed(sctx
, 0);
930 case '\015': /* CR */
931 screen_write_carriagereturn(sctx
);
933 case '\016': /* SO */
934 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
936 case '\017': /* SI */
937 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
940 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
947 /* Execute escape sequence. */
949 input_esc_dispatch(struct input_ctx
*ictx
)
951 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
952 struct screen
*s
= sctx
->s
;
953 struct input_table_entry
*entry
;
955 if (ictx
->flags
& INPUT_DISCARD
)
957 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
959 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
960 sizeof input_esc_table
[0], input_table_compare
);
962 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
966 switch (entry
->type
) {
968 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
969 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
973 screen_reset_tabs(sctx
->s
);
975 screen_write_scrollregion(sctx
, 0, screen_size_y(sctx
->s
) - 1);
977 screen_write_insertmode(sctx
, 0);
978 screen_write_kcursormode(sctx
, 0);
979 screen_write_kkeypadmode(sctx
, 0);
980 screen_write_mousemode_off(sctx
);
982 screen_write_clearscreen(sctx
);
983 screen_write_cursormove(sctx
, 0, 0);
986 screen_write_linefeed(sctx
, 0);
989 screen_write_carriagereturn(sctx
);
990 screen_write_linefeed(sctx
, 0);
993 if (s
->cx
< screen_size_x(s
))
994 bit_set(s
->tabs
, s
->cx
);
997 screen_write_reverseindex(sctx
);
999 case INPUT_ESC_DECKPAM
:
1000 screen_write_kkeypadmode(sctx
, 1);
1002 case INPUT_ESC_DECKPNM
:
1003 screen_write_kkeypadmode(sctx
, 0);
1005 case INPUT_ESC_DECSC
:
1006 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
1007 ictx
->old_cx
= s
->cx
;
1008 ictx
->old_cy
= s
->cy
;
1010 case INPUT_ESC_DECRC
:
1011 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
1012 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
);
1014 case INPUT_ESC_DECALN
:
1015 screen_write_alignmenttest(sctx
);
1017 case INPUT_ESC_SCSON_G0
:
1019 * Not really supported, but fake it up enough for those that
1020 * use it to switch character sets (by redefining G0 to
1021 * graphics set, rather than switching to G1).
1023 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
1025 case INPUT_ESC_SCSOFF_G0
:
1026 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
1033 /* Execute control sequence. */
1035 input_csi_dispatch(struct input_ctx
*ictx
)
1037 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1038 struct window_pane
*wp
= ictx
->wp
;
1039 struct screen
*s
= sctx
->s
;
1040 struct input_table_entry
*entry
;
1043 if (ictx
->flags
& INPUT_DISCARD
)
1045 if (input_split(ictx
) != 0)
1047 log_debug("%s: '%c' \"%s\" \"%s\"",
1048 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1050 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1051 sizeof input_csi_table
[0], input_table_compare
);
1052 if (entry
== NULL
) {
1053 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1057 switch (entry
->type
) {
1059 /* Find the previous tab point, n times. */
1060 n
= input_get(ictx
, 0, 1, 1);
1061 while (s
->cx
> 0 && n
-- > 0) {
1064 while (s
->cx
> 0 && !bit_test(s
->tabs
, s
->cx
));
1068 screen_write_cursorleft(sctx
, input_get(ictx
, 0, 1, 1));
1071 screen_write_cursordown(sctx
, input_get(ictx
, 0, 1, 1));
1074 screen_write_cursorright(sctx
, input_get(ictx
, 0, 1, 1));
1077 n
= input_get(ictx
, 0, 1, 1);
1078 m
= input_get(ictx
, 1, 1, 1);
1079 screen_write_cursormove(sctx
, m
- 1, n
- 1);
1082 screen_write_cursorup(sctx
, input_get(ictx
, 0, 1, 1));
1085 switch (input_get(ictx
, 0, 0, 0)) {
1087 input_reply(ictx
, "\033[?1;2c");
1090 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1095 screen_write_deletecharacter(sctx
, input_get(ictx
, 0, 1, 1));
1097 case INPUT_CSI_DECSTBM
:
1098 n
= input_get(ictx
, 0, 1, 1);
1099 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1100 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1103 screen_write_deleteline(sctx
, input_get(ictx
, 0, 1, 1));
1106 switch (input_get(ictx
, 0, 0, 0)) {
1108 input_reply(ictx
, "\033[0n");
1111 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1114 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1119 switch (input_get(ictx
, 0, 0, 0)) {
1121 screen_write_clearendofscreen(sctx
);
1124 screen_write_clearstartofscreen(sctx
);
1127 screen_write_clearscreen(sctx
);
1130 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1135 switch (input_get(ictx
, 0, 0, 0)) {
1137 screen_write_clearendofline(sctx
);
1140 screen_write_clearstartofline(sctx
);
1143 screen_write_clearline(sctx
);
1146 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1151 n
= input_get(ictx
, 0, 1, 1);
1152 screen_write_cursormove(sctx
, n
- 1, s
->cy
);
1155 screen_write_insertcharacter(sctx
, input_get(ictx
, 0, 1, 1));
1158 screen_write_insertline(sctx
, input_get(ictx
, 0, 1, 1));
1161 switch (input_get(ictx
, 0, 0, -1)) {
1163 screen_write_insertmode(&ictx
->ctx
, 0);
1166 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1170 case INPUT_CSI_RM_PRIVATE
:
1171 switch (input_get(ictx
, 0, 0, -1)) {
1173 screen_write_kcursormode(&ictx
->ctx
, 0);
1175 case 3: /* DECCOLM */
1176 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1177 screen_write_clearscreen(&ictx
->ctx
);
1180 screen_write_cursormode(&ictx
->ctx
, 0);
1186 screen_write_mousemode_off(&ictx
->ctx
);
1189 screen_write_utf8mousemode(&ictx
->ctx
, 0);
1192 window_pane_alternate_off(wp
, &ictx
->cell
);
1195 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1200 input_csi_dispatch_sgr(ictx
);
1203 switch (input_get(ictx
, 0, 0, -1)) {
1205 screen_write_insertmode(&ictx
->ctx
, 1);
1208 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1212 case INPUT_CSI_SM_PRIVATE
:
1213 switch (input_get(ictx
, 0, 0, -1)) {
1215 screen_write_kcursormode(&ictx
->ctx
, 1);
1217 case 3: /* DECCOLM */
1218 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1219 screen_write_clearscreen(&ictx
->ctx
);
1222 screen_write_cursormode(&ictx
->ctx
, 1);
1225 screen_write_mousemode_on(
1226 &ictx
->ctx
, MODE_MOUSE_STANDARD
);
1229 screen_write_mousemode_on(
1230 &ictx
->ctx
, MODE_MOUSE_BUTTON
);
1233 screen_write_mousemode_on(&ictx
->ctx
, MODE_MOUSE_ANY
);
1236 screen_write_utf8mousemode(&ictx
->ctx
, 1);
1239 window_pane_alternate_on(wp
, &ictx
->cell
);
1242 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1247 switch (input_get(ictx
, 0, 0, 0)) {
1249 if (s
->cx
< screen_size_x(s
))
1250 bit_clear(s
->tabs
, s
->cx
);
1253 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1256 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1261 n
= input_get(ictx
, 0, 1, 1);
1262 screen_write_cursormove(sctx
, s
->cx
, n
- 1);
1264 case INPUT_CSI_DECSCUSR
:
1265 n
= input_get(ictx
, 0, 0, 0);
1266 screen_set_cursor_style(s
, n
);
1273 /* Handle CSI SGR. */
1275 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
1277 struct grid_cell
*gc
= &ictx
->cell
;
1282 if (ictx
->param_list_len
== 0) {
1284 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1285 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1289 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1290 n
= input_get(ictx
, i
, 0, 0);
1292 if (n
== 38 || n
== 48) {
1294 if (input_get(ictx
, i
, 0, -1) != 5)
1298 m
= input_get(ictx
, i
, 0, -1);
1301 gc
->flags
&= ~GRID_FLAG_FG256
;
1303 } else if (n
== 48) {
1304 gc
->flags
&= ~GRID_FLAG_BG256
;
1310 gc
->flags
|= GRID_FLAG_FG256
;
1312 } else if (n
== 48) {
1313 gc
->flags
|= GRID_FLAG_BG256
;
1324 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1325 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1328 gc
->attr
|= GRID_ATTR_BRIGHT
;
1331 gc
->attr
|= GRID_ATTR_DIM
;
1334 gc
->attr
|= GRID_ATTR_ITALICS
;
1337 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
1340 gc
->attr
|= GRID_ATTR_BLINK
;
1343 gc
->attr
|= GRID_ATTR_REVERSE
;
1346 gc
->attr
|= GRID_ATTR_HIDDEN
;
1349 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
1352 gc
->attr
&= ~GRID_ATTR_ITALICS
;
1355 gc
->attr
&= ~GRID_ATTR_UNDERSCORE
;
1358 gc
->attr
&= ~GRID_ATTR_BLINK
;
1361 gc
->attr
&= ~GRID_ATTR_REVERSE
;
1371 gc
->flags
&= ~GRID_FLAG_FG256
;
1375 gc
->flags
&= ~GRID_FLAG_FG256
;
1386 gc
->flags
&= ~GRID_FLAG_BG256
;
1390 gc
->flags
&= ~GRID_FLAG_BG256
;
1401 gc
->flags
&= ~GRID_FLAG_FG256
;
1412 gc
->flags
&= ~GRID_FLAG_BG256
;
1419 /* DCS terminator (ST) received. */
1421 input_dcs_dispatch(struct input_ctx
*ictx
)
1423 const char prefix
[] = "tmux;";
1424 const u_int prefix_len
= (sizeof prefix
) - 1;
1426 if (ictx
->flags
& INPUT_DISCARD
)
1429 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1431 /* Check for tmux prefix. */
1432 if (ictx
->input_len
>= prefix_len
&&
1433 strncmp(ictx
->input_buf
, prefix
, prefix_len
) == 0) {
1434 screen_write_rawstring(&ictx
->ctx
,
1435 ictx
->input_buf
+ prefix_len
, ictx
->input_len
- prefix_len
);
1441 /* OSC string started. */
1443 input_enter_osc(struct input_ctx
*ictx
)
1445 log_debug("%s", __func__
);
1450 /* OSC terminator (ST) received. */
1452 input_exit_osc(struct input_ctx
*ictx
)
1454 u_char
*p
= ictx
->input_buf
;
1457 if (ictx
->flags
& INPUT_DISCARD
)
1459 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
1462 log_debug("%s: \"%s\"", __func__
, p
);
1465 while (*p
>= '0' && *p
<= '9')
1466 option
= option
* 10 + *p
++ - '0';
1473 screen_set_title(ictx
->ctx
.s
, p
);
1474 server_status_window(ictx
->wp
->window
);
1477 screen_set_cursor_colour(ictx
->ctx
.s
, p
);
1480 if (*p
== '\0') /* No arguments allowed. */
1481 screen_set_cursor_colour(ictx
->ctx
.s
, "");
1484 log_debug("%s: unknown '%u'", __func__
, option
);
1489 /* APC string started. */
1491 input_enter_apc(struct input_ctx
*ictx
)
1493 log_debug("%s", __func__
);
1498 /* APC terminator (ST) received. */
1500 input_exit_apc(struct input_ctx
*ictx
)
1502 if (ictx
->flags
& INPUT_DISCARD
)
1504 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1506 screen_set_title(ictx
->ctx
.s
, ictx
->input_buf
);
1507 server_status_window(ictx
->wp
->window
);
1510 /* Rename string started. */
1512 input_enter_rename(struct input_ctx
*ictx
)
1514 log_debug("%s", __func__
);
1519 /* Rename terminator (ST) received. */
1521 input_exit_rename(struct input_ctx
*ictx
)
1523 if (ictx
->flags
& INPUT_DISCARD
)
1525 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1527 xfree(ictx
->wp
->window
->name
);
1528 ictx
->wp
->window
->name
= xstrdup(ictx
->input_buf
);
1529 options_set_number(&ictx
->wp
->window
->options
, "automatic-rename", 0);
1531 server_status_window(ictx
->wp
->window
);
1534 /* Open UTF-8 character. */
1536 input_utf8_open(struct input_ctx
*ictx
)
1538 if (!options_get_number(&ictx
->wp
->window
->options
, "utf8")) {
1539 /* Print, and do not switch state. */
1543 log_debug("%s", __func__
);
1545 utf8_open(&ictx
->utf8data
, ictx
->ch
);
1549 /* Append to UTF-8 character. */
1551 input_utf8_add(struct input_ctx
*ictx
)
1553 log_debug("%s", __func__
);
1555 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1559 /* Close UTF-8 string. */
1561 input_utf8_close(struct input_ctx
*ictx
)
1563 log_debug("%s", __func__
);
1565 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1567 ictx
->cell
.flags
|= GRID_FLAG_UTF8
;
1568 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, &ictx
->utf8data
);
1569 ictx
->cell
.flags
&= ~GRID_FLAG_UTF8
;