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
{
140 INPUT_CSI_RM_PRIVATE
,
144 INPUT_CSI_SM_PRIVATE
,
149 /* Control (CSI) command table. */
150 const struct input_table_entry input_csi_table
[] = {
151 { '@', "", INPUT_CSI_ICH
},
152 { 'A', "", INPUT_CSI_CUU
},
153 { 'B', "", INPUT_CSI_CUD
},
154 { 'C', "", INPUT_CSI_CUF
},
155 { 'D', "", INPUT_CSI_CUB
},
156 { 'G', "", INPUT_CSI_HPA
},
157 { 'H', "", INPUT_CSI_CUP
},
158 { 'J', "", INPUT_CSI_ED
},
159 { 'K', "", INPUT_CSI_EL
},
160 { 'L', "", INPUT_CSI_IL
},
161 { 'M', "", INPUT_CSI_DL
},
162 { 'P', "", INPUT_CSI_DCH
},
163 { 'Z', "", INPUT_CSI_CBT
},
164 { 'c', "", INPUT_CSI_DA
},
165 { 'd', "", INPUT_CSI_VPA
},
166 { 'f', "", INPUT_CSI_CUP
},
167 { 'g', "", INPUT_CSI_TBC
},
168 { 'h', "", INPUT_CSI_SM
},
169 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
170 { 'l', "", INPUT_CSI_RM
},
171 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
172 { 'm', "", INPUT_CSI_SGR
},
173 { 'n', "", INPUT_CSI_DSR
},
174 { 'q', " ", INPUT_CSI_DECSCUSR
},
175 { 'r', "", INPUT_CSI_DECSTBM
},
176 { 's', "", INPUT_CSI_SCP
},
177 { 'u', "", INPUT_CSI_RCP
},
180 /* Input transition. */
181 struct input_transition
{
185 int (*handler
)(struct input_ctx
*);
186 const struct input_state
*state
;
192 void (*enter
)(struct input_ctx
*);
193 void (*exit
)(struct input_ctx
*);
194 const struct input_transition
*transitions
;
197 /* State transitions available from all states. */
198 #define INPUT_STATE_ANYWHERE \
199 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
200 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
201 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
203 /* Forward declarations of state tables. */
204 const struct input_transition input_state_ground_table
[];
205 const struct input_transition input_state_esc_enter_table
[];
206 const struct input_transition input_state_esc_intermediate_table
[];
207 const struct input_transition input_state_csi_enter_table
[];
208 const struct input_transition input_state_csi_parameter_table
[];
209 const struct input_transition input_state_csi_intermediate_table
[];
210 const struct input_transition input_state_csi_ignore_table
[];
211 const struct input_transition input_state_dcs_enter_table
[];
212 const struct input_transition input_state_dcs_parameter_table
[];
213 const struct input_transition input_state_dcs_intermediate_table
[];
214 const struct input_transition input_state_dcs_handler_table
[];
215 const struct input_transition input_state_dcs_escape_table
[];
216 const struct input_transition input_state_dcs_ignore_table
[];
217 const struct input_transition input_state_osc_string_table
[];
218 const struct input_transition input_state_apc_string_table
[];
219 const struct input_transition input_state_rename_string_table
[];
220 const struct input_transition input_state_consume_st_table
[];
221 const struct input_transition input_state_utf8_three_table
[];
222 const struct input_transition input_state_utf8_two_table
[];
223 const struct input_transition input_state_utf8_one_table
[];
225 /* ground state definition. */
226 const struct input_state input_state_ground
= {
229 input_state_ground_table
232 /* esc_enter state definition. */
233 const struct input_state input_state_esc_enter
= {
236 input_state_esc_enter_table
239 /* esc_intermediate state definition. */
240 const struct input_state input_state_esc_intermediate
= {
243 input_state_esc_intermediate_table
246 /* csi_enter state definition. */
247 const struct input_state input_state_csi_enter
= {
250 input_state_csi_enter_table
253 /* csi_parameter state definition. */
254 const struct input_state input_state_csi_parameter
= {
257 input_state_csi_parameter_table
260 /* csi_intermediate state definition. */
261 const struct input_state input_state_csi_intermediate
= {
264 input_state_csi_intermediate_table
267 /* csi_ignore state definition. */
268 const struct input_state input_state_csi_ignore
= {
271 input_state_csi_ignore_table
274 /* dcs_enter state definition. */
275 const struct input_state input_state_dcs_enter
= {
278 input_state_dcs_enter_table
281 /* dcs_parameter state definition. */
282 const struct input_state input_state_dcs_parameter
= {
285 input_state_dcs_parameter_table
288 /* dcs_intermediate state definition. */
289 const struct input_state input_state_dcs_intermediate
= {
292 input_state_dcs_intermediate_table
295 /* dcs_handler state definition. */
296 const struct input_state input_state_dcs_handler
= {
299 input_state_dcs_handler_table
302 /* dcs_escape state definition. */
303 const struct input_state input_state_dcs_escape
= {
306 input_state_dcs_escape_table
309 /* dcs_ignore state definition. */
310 const struct input_state input_state_dcs_ignore
= {
313 input_state_dcs_ignore_table
316 /* osc_string state definition. */
317 const struct input_state input_state_osc_string
= {
319 input_enter_osc
, input_exit_osc
,
320 input_state_osc_string_table
323 /* apc_string state definition. */
324 const struct input_state input_state_apc_string
= {
326 input_enter_apc
, input_exit_apc
,
327 input_state_apc_string_table
330 /* rename_string state definition. */
331 const struct input_state input_state_rename_string
= {
333 input_enter_rename
, input_exit_rename
,
334 input_state_rename_string_table
337 /* consume_st state definition. */
338 const struct input_state input_state_consume_st
= {
341 input_state_consume_st_table
344 /* utf8_three state definition. */
345 const struct input_state input_state_utf8_three
= {
348 input_state_utf8_three_table
351 /* utf8_two state definition. */
352 const struct input_state input_state_utf8_two
= {
355 input_state_utf8_two_table
358 /* utf8_one state definition. */
359 const struct input_state input_state_utf8_one
= {
362 input_state_utf8_one_table
365 /* ground state table. */
366 const struct input_transition input_state_ground_table
[] = {
367 INPUT_STATE_ANYWHERE
,
369 { 0x00, 0x17, input_c0_dispatch
, NULL
},
370 { 0x19, 0x19, input_c0_dispatch
, NULL
},
371 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
372 { 0x20, 0x7e, input_print
, NULL
},
373 { 0x7f, 0x7f, NULL
, NULL
},
374 { 0x80, 0xc1, input_print
, NULL
},
375 { 0xc2, 0xdf, input_utf8_open
, &input_state_utf8_one
},
376 { 0xe0, 0xef, input_utf8_open
, &input_state_utf8_two
},
377 { 0xf0, 0xf4, input_utf8_open
, &input_state_utf8_three
},
378 { 0xf5, 0xff, input_print
, NULL
},
380 { -1, -1, NULL
, NULL
}
383 /* esc_enter state table. */
384 const struct input_transition input_state_esc_enter_table
[] = {
385 INPUT_STATE_ANYWHERE
,
387 { 0x00, 0x17, input_c0_dispatch
, NULL
},
388 { 0x19, 0x19, input_c0_dispatch
, NULL
},
389 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
390 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
391 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
392 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
393 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
394 { 0x58, 0x58, NULL
, &input_state_consume_st
},
395 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
396 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
397 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
398 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
399 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
400 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
401 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
402 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
403 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
404 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
405 { 0x7f, 0xff, NULL
, NULL
},
407 { -1, -1, NULL
, NULL
}
410 /* esc_interm state table. */
411 const struct input_transition input_state_esc_intermediate_table
[] = {
412 INPUT_STATE_ANYWHERE
,
414 { 0x00, 0x17, input_c0_dispatch
, NULL
},
415 { 0x19, 0x19, input_c0_dispatch
, NULL
},
416 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
417 { 0x20, 0x2f, input_intermediate
, NULL
},
418 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
419 { 0x7f, 0xff, NULL
, NULL
},
421 { -1, -1, NULL
, NULL
}
424 /* csi_enter state table. */
425 const struct input_transition input_state_csi_enter_table
[] = {
426 INPUT_STATE_ANYWHERE
,
428 { 0x00, 0x17, input_c0_dispatch
, NULL
},
429 { 0x19, 0x19, input_c0_dispatch
, NULL
},
430 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
431 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
432 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
433 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
434 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
435 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
436 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
437 { 0x7f, 0xff, NULL
, NULL
},
439 { -1, -1, NULL
, NULL
}
442 /* csi_parameter state table. */
443 const struct input_transition input_state_csi_parameter_table
[] = {
444 INPUT_STATE_ANYWHERE
,
446 { 0x00, 0x17, input_c0_dispatch
, NULL
},
447 { 0x19, 0x19, input_c0_dispatch
, NULL
},
448 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
449 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
450 { 0x30, 0x39, input_parameter
, NULL
},
451 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
452 { 0x3b, 0x3b, input_parameter
, NULL
},
453 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
454 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
455 { 0x7f, 0xff, NULL
, NULL
},
457 { -1, -1, NULL
, NULL
}
460 /* csi_intermediate state table. */
461 const struct input_transition input_state_csi_intermediate_table
[] = {
462 INPUT_STATE_ANYWHERE
,
464 { 0x00, 0x17, input_c0_dispatch
, NULL
},
465 { 0x19, 0x19, input_c0_dispatch
, NULL
},
466 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
467 { 0x20, 0x2f, input_intermediate
, NULL
},
468 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
469 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
470 { 0x7f, 0xff, NULL
, NULL
},
472 { -1, -1, NULL
, NULL
}
475 /* csi_ignore state table. */
476 const struct input_transition input_state_csi_ignore_table
[] = {
477 INPUT_STATE_ANYWHERE
,
479 { 0x00, 0x17, input_c0_dispatch
, NULL
},
480 { 0x19, 0x19, input_c0_dispatch
, NULL
},
481 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
482 { 0x20, 0x3f, NULL
, NULL
},
483 { 0x40, 0x7e, NULL
, &input_state_ground
},
484 { 0x7f, 0xff, NULL
, NULL
},
486 { -1, -1, NULL
, NULL
}
489 /* dcs_enter state table. */
490 const struct input_transition input_state_dcs_enter_table
[] = {
491 INPUT_STATE_ANYWHERE
,
493 { 0x00, 0x17, NULL
, NULL
},
494 { 0x19, 0x19, NULL
, NULL
},
495 { 0x1c, 0x1f, NULL
, NULL
},
496 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
497 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
498 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
499 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
500 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
501 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
502 { 0x7f, 0xff, NULL
, NULL
},
504 { -1, -1, NULL
, NULL
}
507 /* dcs_parameter state table. */
508 const struct input_transition input_state_dcs_parameter_table
[] = {
509 INPUT_STATE_ANYWHERE
,
511 { 0x00, 0x17, NULL
, NULL
},
512 { 0x19, 0x19, NULL
, NULL
},
513 { 0x1c, 0x1f, NULL
, NULL
},
514 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
515 { 0x30, 0x39, input_parameter
, NULL
},
516 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
517 { 0x3b, 0x3b, input_parameter
, NULL
},
518 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
519 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
520 { 0x7f, 0xff, NULL
, NULL
},
522 { -1, -1, NULL
, NULL
}
525 /* dcs_interm state table. */
526 const struct input_transition input_state_dcs_intermediate_table
[] = {
527 INPUT_STATE_ANYWHERE
,
529 { 0x00, 0x17, NULL
, NULL
},
530 { 0x19, 0x19, NULL
, NULL
},
531 { 0x1c, 0x1f, NULL
, NULL
},
532 { 0x20, 0x2f, input_intermediate
, NULL
},
533 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
534 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
535 { 0x7f, 0xff, NULL
, NULL
},
537 { -1, -1, NULL
, NULL
}
540 /* dcs_handler state table. */
541 const struct input_transition input_state_dcs_handler_table
[] = {
542 /* No INPUT_STATE_ANYWHERE */
544 { 0x00, 0x1a, input_input
, NULL
},
545 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
546 { 0x1c, 0xff, input_input
, NULL
},
548 { -1, -1, NULL
, NULL
}
551 /* dcs_escape state table. */
552 const struct input_transition input_state_dcs_escape_table
[] = {
553 /* No INPUT_STATE_ANYWHERE */
555 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
556 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
557 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
559 { -1, -1, NULL
, NULL
}
562 /* dcs_ignore state table. */
563 const struct input_transition input_state_dcs_ignore_table
[] = {
564 INPUT_STATE_ANYWHERE
,
566 { 0x00, 0x17, NULL
, NULL
},
567 { 0x19, 0x19, NULL
, NULL
},
568 { 0x1c, 0x1f, NULL
, NULL
},
569 { 0x20, 0xff, NULL
, NULL
},
571 { -1, -1, NULL
, NULL
}
574 /* osc_string state table. */
575 const struct input_transition input_state_osc_string_table
[] = {
576 INPUT_STATE_ANYWHERE
,
578 { 0x00, 0x06, NULL
, NULL
},
579 { 0x07, 0x07, NULL
, &input_state_ground
},
580 { 0x08, 0x17, NULL
, NULL
},
581 { 0x19, 0x19, NULL
, NULL
},
582 { 0x1c, 0x1f, NULL
, NULL
},
583 { 0x20, 0xff, input_input
, NULL
},
585 { -1, -1, NULL
, NULL
}
588 /* apc_string state table. */
589 const struct input_transition input_state_apc_string_table
[] = {
590 INPUT_STATE_ANYWHERE
,
592 { 0x00, 0x17, NULL
, NULL
},
593 { 0x19, 0x19, NULL
, NULL
},
594 { 0x1c, 0x1f, NULL
, NULL
},
595 { 0x20, 0xff, input_input
, NULL
},
597 { -1, -1, NULL
, NULL
}
600 /* rename_string state table. */
601 const struct input_transition input_state_rename_string_table
[] = {
602 INPUT_STATE_ANYWHERE
,
604 { 0x00, 0x17, NULL
, NULL
},
605 { 0x19, 0x19, NULL
, NULL
},
606 { 0x1c, 0x1f, NULL
, NULL
},
607 { 0x20, 0xff, input_input
, NULL
},
609 { -1, -1, NULL
, NULL
}
612 /* consume_st state table. */
613 const struct input_transition input_state_consume_st_table
[] = {
614 INPUT_STATE_ANYWHERE
,
616 { 0x00, 0x17, NULL
, NULL
},
617 { 0x19, 0x19, NULL
, NULL
},
618 { 0x1c, 0x1f, NULL
, NULL
},
619 { 0x20, 0xff, NULL
, NULL
},
621 { -1, -1, NULL
, NULL
}
624 /* utf8_three state table. */
625 const struct input_transition input_state_utf8_three_table
[] = {
626 /* No INPUT_STATE_ANYWHERE */
628 { 0x00, 0x7f, NULL
, &input_state_ground
},
629 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_two
},
630 { 0xc0, 0xff, NULL
, &input_state_ground
},
632 { -1, -1, NULL
, NULL
}
635 /* utf8_two state table. */
636 const struct input_transition input_state_utf8_two_table
[] = {
637 /* No INPUT_STATE_ANYWHERE */
639 { 0x00, 0x7f, NULL
, &input_state_ground
},
640 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_one
},
641 { 0xc0, 0xff, NULL
, &input_state_ground
},
643 { -1, -1, NULL
, NULL
}
646 /* utf8_one state table. */
647 const struct input_transition input_state_utf8_one_table
[] = {
648 /* No INPUT_STATE_ANYWHERE */
650 { 0x00, 0x7f, NULL
, &input_state_ground
},
651 { 0x80, 0xbf, input_utf8_close
, &input_state_ground
},
652 { 0xc0, 0xff, NULL
, &input_state_ground
},
654 { -1, -1, NULL
, NULL
}
657 /* Input table compare. */
659 input_table_compare(const void *key
, const void *value
)
661 const struct input_ctx
*ictx
= key
;
662 const struct input_table_entry
*entry
= value
;
664 if (ictx
->ch
!= entry
->ch
)
665 return (ictx
->ch
- entry
->ch
);
666 return (strcmp(ictx
->interm_buf
, entry
->interm
));
669 /* Initialise input parser. */
671 input_init(struct window_pane
*wp
)
673 struct input_ctx
*ictx
= &wp
->ictx
;
675 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
677 memcpy(&ictx
->old_cell
, &grid_default_cell
, sizeof ictx
->old_cell
);
681 *ictx
->interm_buf
= '\0';
682 ictx
->interm_len
= 0;
684 *ictx
->param_buf
= '\0';
687 ictx
->state
= &input_state_ground
;
691 /* Destroy input parser. */
693 input_free(unused
struct window_pane
*wp
)
699 input_parse(struct window_pane
*wp
)
701 struct input_ctx
*ictx
= &wp
->ictx
;
702 const struct input_transition
*itr
;
703 struct evbuffer
*evb
= wp
->event
->input
;
707 if (EVBUFFER_LENGTH(evb
) == 0)
710 wp
->window
->flags
|= WINDOW_ACTIVITY
;
711 wp
->window
->flags
&= ~WINDOW_SILENCE
;
714 * Open the screen. Use NULL wp if there is a mode set as don't want to
717 if (wp
->mode
== NULL
)
718 screen_write_start(&ictx
->ctx
, wp
, &wp
->base
);
720 screen_write_start(&ictx
->ctx
, NULL
, &wp
->base
);
723 buf
= EVBUFFER_DATA(evb
);
724 len
= EVBUFFER_LENGTH(evb
);
727 /* Parse the input. */
729 ictx
->ch
= buf
[off
++];
730 log_debug("%s: '%c' %s", __func__
, ictx
->ch
, ictx
->state
->name
);
732 /* Find the transition. */
733 itr
= ictx
->state
->transitions
;
734 while (itr
->first
!= -1 && itr
->last
!= -1) {
735 if (ictx
->ch
>= itr
->first
&& ictx
->ch
<= itr
->last
)
739 if (itr
->first
== -1 || itr
->last
== -1) {
740 /* No transition? Eh? */
741 fatalx("No transition from state!");
745 * Execute the handler, if any. Don't switch state if it
748 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
751 /* And switch state, if necessary. */
752 if (itr
->state
!= NULL
) {
753 if (ictx
->state
->exit
!= NULL
)
754 ictx
->state
->exit(ictx
);
755 ictx
->state
= itr
->state
;
756 if (ictx
->state
->enter
!= NULL
)
757 ictx
->state
->enter(ictx
);
761 /* Close the screen. */
762 screen_write_stop(&ictx
->ctx
);
764 evbuffer_drain(evb
, len
);
767 /* Split the parameter list (if any). */
769 input_split(struct input_ctx
*ictx
)
776 ictx
->param_list_len
= 0;
777 if (ictx
->param_len
== 0)
780 ptr
= ictx
->param_buf
;
781 while ((out
= strsep(&ptr
, ";")) != NULL
) {
785 n
= strtonum(out
, 0, INT_MAX
, &errstr
);
790 ictx
->param_list
[ictx
->param_list_len
++] = n
;
791 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
798 /* Get an argument or return default value. */
800 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
804 if (validx
>= ictx
->param_list_len
)
807 retval
= ictx
->param_list
[validx
];
815 /* Reply to terminal query. */
817 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
823 vasprintf(&reply
, fmt
, ap
);
826 bufferevent_write(ictx
->wp
->event
, reply
, strlen(reply
));
830 /* Clear saved state. */
832 input_clear(struct input_ctx
*ictx
)
834 *ictx
->interm_buf
= '\0';
835 ictx
->interm_len
= 0;
837 *ictx
->param_buf
= '\0';
840 *ictx
->input_buf
= '\0';
843 ictx
->flags
&= ~INPUT_DISCARD
;
846 /* Output this character to the screen. */
848 input_print(struct input_ctx
*ictx
)
850 ictx
->cell
.data
= ictx
->ch
;
851 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, NULL
);
856 /* Collect intermediate string. */
858 input_intermediate(struct input_ctx
*ictx
)
860 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
861 ictx
->flags
|= INPUT_DISCARD
;
863 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
864 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
870 /* Collect parameter string. */
872 input_parameter(struct input_ctx
*ictx
)
874 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
875 ictx
->flags
|= INPUT_DISCARD
;
877 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
878 ictx
->param_buf
[ictx
->param_len
] = '\0';
884 /* Collect input string. */
886 input_input(struct input_ctx
*ictx
)
888 if (ictx
->input_len
== (sizeof ictx
->input_buf
) - 1)
889 ictx
->flags
|= INPUT_DISCARD
;
891 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
892 ictx
->input_buf
[ictx
->input_len
] = '\0';
898 /* Execute C0 control sequence. */
900 input_c0_dispatch(struct input_ctx
*ictx
)
902 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
903 struct window_pane
*wp
= ictx
->wp
;
904 struct screen
*s
= sctx
->s
;
906 log_debug("%s: '%c", __func__
, ictx
->ch
);
909 case '\000': /* NUL */
911 case '\007': /* BEL */
912 wp
->window
->flags
|= WINDOW_BELL
;
914 case '\010': /* BS */
915 screen_write_backspace(sctx
);
917 case '\011': /* HT */
918 /* Don't tab beyond the end of the line. */
919 if (s
->cx
>= screen_size_x(s
) - 1)
922 /* Find the next tab point, or use the last column if none. */
925 if (bit_test(s
->tabs
, s
->cx
))
927 } while (s
->cx
< screen_size_x(s
) - 1);
929 case '\012': /* LF */
930 case '\013': /* VT */
931 case '\014': /* FF */
932 screen_write_linefeed(sctx
, 0);
934 case '\015': /* CR */
935 screen_write_carriagereturn(sctx
);
937 case '\016': /* SO */
938 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
940 case '\017': /* SI */
941 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
944 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
951 /* Execute escape sequence. */
953 input_esc_dispatch(struct input_ctx
*ictx
)
955 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
956 struct screen
*s
= sctx
->s
;
957 struct input_table_entry
*entry
;
959 if (ictx
->flags
& INPUT_DISCARD
)
961 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
963 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
964 sizeof input_esc_table
[0], input_table_compare
);
966 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
970 switch (entry
->type
) {
972 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
973 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
977 screen_reset_tabs(sctx
->s
);
979 screen_write_scrollregion(sctx
, 0, screen_size_y(sctx
->s
) - 1);
981 screen_write_insertmode(sctx
, 0);
982 screen_write_kcursormode(sctx
, 0);
983 screen_write_kkeypadmode(sctx
, 0);
984 screen_write_mousemode_off(sctx
);
986 screen_write_clearscreen(sctx
);
987 screen_write_cursormove(sctx
, 0, 0);
990 screen_write_linefeed(sctx
, 0);
993 screen_write_carriagereturn(sctx
);
994 screen_write_linefeed(sctx
, 0);
997 if (s
->cx
< screen_size_x(s
))
998 bit_set(s
->tabs
, s
->cx
);
1001 screen_write_reverseindex(sctx
);
1003 case INPUT_ESC_DECKPAM
:
1004 screen_write_kkeypadmode(sctx
, 1);
1006 case INPUT_ESC_DECKPNM
:
1007 screen_write_kkeypadmode(sctx
, 0);
1009 case INPUT_ESC_DECSC
:
1010 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
1011 ictx
->old_cx
= s
->cx
;
1012 ictx
->old_cy
= s
->cy
;
1014 case INPUT_ESC_DECRC
:
1015 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
1016 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
);
1018 case INPUT_ESC_DECALN
:
1019 screen_write_alignmenttest(sctx
);
1021 case INPUT_ESC_SCSON_G0
:
1023 * Not really supported, but fake it up enough for those that
1024 * use it to switch character sets (by redefining G0 to
1025 * graphics set, rather than switching to G1).
1027 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
1029 case INPUT_ESC_SCSOFF_G0
:
1030 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
1037 /* Execute control sequence. */
1039 input_csi_dispatch(struct input_ctx
*ictx
)
1041 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1042 struct window_pane
*wp
= ictx
->wp
;
1043 struct screen
*s
= sctx
->s
;
1044 struct input_table_entry
*entry
;
1047 if (ictx
->flags
& INPUT_DISCARD
)
1049 if (input_split(ictx
) != 0)
1051 log_debug("%s: '%c' \"%s\" \"%s\"",
1052 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1054 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1055 sizeof input_csi_table
[0], input_table_compare
);
1056 if (entry
== NULL
) {
1057 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1061 switch (entry
->type
) {
1063 /* Find the previous tab point, n times. */
1064 n
= input_get(ictx
, 0, 1, 1);
1065 while (s
->cx
> 0 && n
-- > 0) {
1068 while (s
->cx
> 0 && !bit_test(s
->tabs
, s
->cx
));
1072 screen_write_cursorleft(sctx
, input_get(ictx
, 0, 1, 1));
1075 screen_write_cursordown(sctx
, input_get(ictx
, 0, 1, 1));
1078 screen_write_cursorright(sctx
, input_get(ictx
, 0, 1, 1));
1081 n
= input_get(ictx
, 0, 1, 1);
1082 m
= input_get(ictx
, 1, 1, 1);
1083 screen_write_cursormove(sctx
, m
- 1, n
- 1);
1086 screen_write_cursorup(sctx
, input_get(ictx
, 0, 1, 1));
1089 switch (input_get(ictx
, 0, 0, 0)) {
1091 input_reply(ictx
, "\033[?1;2c");
1094 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1099 screen_write_deletecharacter(sctx
, input_get(ictx
, 0, 1, 1));
1101 case INPUT_CSI_DECSTBM
:
1102 n
= input_get(ictx
, 0, 1, 1);
1103 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1104 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1107 screen_write_deleteline(sctx
, input_get(ictx
, 0, 1, 1));
1110 switch (input_get(ictx
, 0, 0, 0)) {
1112 input_reply(ictx
, "\033[0n");
1115 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1118 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1123 switch (input_get(ictx
, 0, 0, 0)) {
1125 screen_write_clearendofscreen(sctx
);
1128 screen_write_clearstartofscreen(sctx
);
1131 screen_write_clearscreen(sctx
);
1134 switch (input_get(ictx
, 1, 0, 0)) {
1137 * Linux console extension to clear history
1138 * (for example before locking the screen).
1140 screen_write_clearhistory(sctx
);
1145 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1150 switch (input_get(ictx
, 0, 0, 0)) {
1152 screen_write_clearendofline(sctx
);
1155 screen_write_clearstartofline(sctx
);
1158 screen_write_clearline(sctx
);
1161 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1166 n
= input_get(ictx
, 0, 1, 1);
1167 screen_write_cursormove(sctx
, n
- 1, s
->cy
);
1170 screen_write_insertcharacter(sctx
, input_get(ictx
, 0, 1, 1));
1173 screen_write_insertline(sctx
, input_get(ictx
, 0, 1, 1));
1176 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
1177 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
);
1180 switch (input_get(ictx
, 0, 0, -1)) {
1182 screen_write_insertmode(&ictx
->ctx
, 0);
1185 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1189 case INPUT_CSI_RM_PRIVATE
:
1190 switch (input_get(ictx
, 0, 0, -1)) {
1192 screen_write_kcursormode(&ictx
->ctx
, 0);
1194 case 3: /* DECCOLM */
1195 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1196 screen_write_clearscreen(&ictx
->ctx
);
1199 screen_write_cursormode(&ictx
->ctx
, 0);
1205 screen_write_mousemode_off(&ictx
->ctx
);
1208 screen_write_utf8mousemode(&ictx
->ctx
, 0);
1211 window_pane_alternate_off(wp
, &ictx
->cell
);
1214 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1219 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
1220 ictx
->old_cx
= s
->cx
;
1221 ictx
->old_cy
= s
->cy
;
1224 input_csi_dispatch_sgr(ictx
);
1227 switch (input_get(ictx
, 0, 0, -1)) {
1229 screen_write_insertmode(&ictx
->ctx
, 1);
1232 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1236 case INPUT_CSI_SM_PRIVATE
:
1237 switch (input_get(ictx
, 0, 0, -1)) {
1239 screen_write_kcursormode(&ictx
->ctx
, 1);
1241 case 3: /* DECCOLM */
1242 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1243 screen_write_clearscreen(&ictx
->ctx
);
1246 screen_write_cursormode(&ictx
->ctx
, 1);
1249 screen_write_mousemode_on(
1250 &ictx
->ctx
, MODE_MOUSE_STANDARD
);
1253 screen_write_mousemode_on(
1254 &ictx
->ctx
, MODE_MOUSE_BUTTON
);
1257 screen_write_mousemode_on(&ictx
->ctx
, MODE_MOUSE_ANY
);
1260 screen_write_utf8mousemode(&ictx
->ctx
, 1);
1263 window_pane_alternate_on(wp
, &ictx
->cell
);
1266 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1271 switch (input_get(ictx
, 0, 0, 0)) {
1273 if (s
->cx
< screen_size_x(s
))
1274 bit_clear(s
->tabs
, s
->cx
);
1277 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1280 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1285 n
= input_get(ictx
, 0, 1, 1);
1286 screen_write_cursormove(sctx
, s
->cx
, n
- 1);
1288 case INPUT_CSI_DECSCUSR
:
1289 n
= input_get(ictx
, 0, 0, 0);
1290 screen_set_cursor_style(s
, n
);
1297 /* Handle CSI SGR. */
1299 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
1301 struct grid_cell
*gc
= &ictx
->cell
;
1306 if (ictx
->param_list_len
== 0) {
1308 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1309 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1313 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1314 n
= input_get(ictx
, i
, 0, 0);
1316 if (n
== 38 || n
== 48) {
1318 if (input_get(ictx
, i
, 0, -1) != 5)
1322 m
= input_get(ictx
, i
, 0, -1);
1325 gc
->flags
&= ~GRID_FLAG_FG256
;
1327 } else if (n
== 48) {
1328 gc
->flags
&= ~GRID_FLAG_BG256
;
1334 gc
->flags
|= GRID_FLAG_FG256
;
1336 } else if (n
== 48) {
1337 gc
->flags
|= GRID_FLAG_BG256
;
1348 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1349 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1352 gc
->attr
|= GRID_ATTR_BRIGHT
;
1355 gc
->attr
|= GRID_ATTR_DIM
;
1358 gc
->attr
|= GRID_ATTR_ITALICS
;
1361 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
1364 gc
->attr
|= GRID_ATTR_BLINK
;
1367 gc
->attr
|= GRID_ATTR_REVERSE
;
1370 gc
->attr
|= GRID_ATTR_HIDDEN
;
1373 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
1376 gc
->attr
&= ~GRID_ATTR_ITALICS
;
1379 gc
->attr
&= ~GRID_ATTR_UNDERSCORE
;
1382 gc
->attr
&= ~GRID_ATTR_BLINK
;
1385 gc
->attr
&= ~GRID_ATTR_REVERSE
;
1395 gc
->flags
&= ~GRID_FLAG_FG256
;
1399 gc
->flags
&= ~GRID_FLAG_FG256
;
1410 gc
->flags
&= ~GRID_FLAG_BG256
;
1414 gc
->flags
&= ~GRID_FLAG_BG256
;
1425 gc
->flags
&= ~GRID_FLAG_FG256
;
1436 gc
->flags
&= ~GRID_FLAG_BG256
;
1443 /* DCS terminator (ST) received. */
1445 input_dcs_dispatch(struct input_ctx
*ictx
)
1447 const char prefix
[] = "tmux;";
1448 const u_int prefix_len
= (sizeof prefix
) - 1;
1450 if (ictx
->flags
& INPUT_DISCARD
)
1453 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1455 /* Check for tmux prefix. */
1456 if (ictx
->input_len
>= prefix_len
&&
1457 strncmp(ictx
->input_buf
, prefix
, prefix_len
) == 0) {
1458 screen_write_rawstring(&ictx
->ctx
,
1459 ictx
->input_buf
+ prefix_len
, ictx
->input_len
- prefix_len
);
1465 /* OSC string started. */
1467 input_enter_osc(struct input_ctx
*ictx
)
1469 log_debug("%s", __func__
);
1474 /* OSC terminator (ST) received. */
1476 input_exit_osc(struct input_ctx
*ictx
)
1478 u_char
*p
= ictx
->input_buf
;
1481 if (ictx
->flags
& INPUT_DISCARD
)
1483 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
1486 log_debug("%s: \"%s\"", __func__
, p
);
1489 while (*p
>= '0' && *p
<= '9')
1490 option
= option
* 10 + *p
++ - '0';
1497 screen_set_title(ictx
->ctx
.s
, p
);
1498 server_status_window(ictx
->wp
->window
);
1501 screen_set_cursor_colour(ictx
->ctx
.s
, p
);
1504 if (*p
== '\0') /* No arguments allowed. */
1505 screen_set_cursor_colour(ictx
->ctx
.s
, "");
1508 log_debug("%s: unknown '%u'", __func__
, option
);
1513 /* APC string started. */
1515 input_enter_apc(struct input_ctx
*ictx
)
1517 log_debug("%s", __func__
);
1522 /* APC terminator (ST) received. */
1524 input_exit_apc(struct input_ctx
*ictx
)
1526 if (ictx
->flags
& INPUT_DISCARD
)
1528 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1530 screen_set_title(ictx
->ctx
.s
, ictx
->input_buf
);
1531 server_status_window(ictx
->wp
->window
);
1534 /* Rename string started. */
1536 input_enter_rename(struct input_ctx
*ictx
)
1538 log_debug("%s", __func__
);
1543 /* Rename terminator (ST) received. */
1545 input_exit_rename(struct input_ctx
*ictx
)
1547 if (ictx
->flags
& INPUT_DISCARD
)
1549 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1551 xfree(ictx
->wp
->window
->name
);
1552 ictx
->wp
->window
->name
= xstrdup(ictx
->input_buf
);
1553 options_set_number(&ictx
->wp
->window
->options
, "automatic-rename", 0);
1555 server_status_window(ictx
->wp
->window
);
1558 /* Open UTF-8 character. */
1560 input_utf8_open(struct input_ctx
*ictx
)
1562 if (!options_get_number(&ictx
->wp
->window
->options
, "utf8")) {
1563 /* Print, and do not switch state. */
1567 log_debug("%s", __func__
);
1569 utf8_open(&ictx
->utf8data
, ictx
->ch
);
1573 /* Append to UTF-8 character. */
1575 input_utf8_add(struct input_ctx
*ictx
)
1577 log_debug("%s", __func__
);
1579 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1583 /* Close UTF-8 string. */
1585 input_utf8_close(struct input_ctx
*ictx
)
1587 log_debug("%s", __func__
);
1589 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1591 ictx
->cell
.flags
|= GRID_FLAG_UTF8
;
1592 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, &ictx
->utf8data
);
1593 ictx
->cell
.flags
&= ~GRID_FLAG_UTF8
;