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
{
143 INPUT_CSI_RM_PRIVATE
,
147 INPUT_CSI_SM_PRIVATE
,
152 /* Control (CSI) command table. */
153 const struct input_table_entry input_csi_table
[] = {
154 { '@', "", INPUT_CSI_ICH
},
155 { 'A', "", INPUT_CSI_CUU
},
156 { 'B', "", INPUT_CSI_CUD
},
157 { 'C', "", INPUT_CSI_CUF
},
158 { 'D', "", INPUT_CSI_CUB
},
159 { 'E', "", INPUT_CSI_CNL
},
160 { 'F', "", INPUT_CSI_CPL
},
161 { 'G', "", INPUT_CSI_HPA
},
162 { 'H', "", INPUT_CSI_CUP
},
163 { 'J', "", INPUT_CSI_ED
},
164 { 'K', "", INPUT_CSI_EL
},
165 { 'L', "", INPUT_CSI_IL
},
166 { 'M', "", INPUT_CSI_DL
},
167 { 'P', "", INPUT_CSI_DCH
},
168 { 'Z', "", INPUT_CSI_CBT
},
169 { 'c', "", INPUT_CSI_DA
},
170 { 'c', ">", INPUT_CSI_DA_TWO
},
171 { 'd', "", INPUT_CSI_VPA
},
172 { 'f', "", INPUT_CSI_CUP
},
173 { 'g', "", INPUT_CSI_TBC
},
174 { 'h', "", INPUT_CSI_SM
},
175 { 'h', "?", INPUT_CSI_SM_PRIVATE
},
176 { 'l', "", INPUT_CSI_RM
},
177 { 'l', "?", INPUT_CSI_RM_PRIVATE
},
178 { 'm', "", INPUT_CSI_SGR
},
179 { 'n', "", INPUT_CSI_DSR
},
180 { 'q', " ", INPUT_CSI_DECSCUSR
},
181 { 'r', "", INPUT_CSI_DECSTBM
},
182 { 's', "", INPUT_CSI_SCP
},
183 { 'u', "", INPUT_CSI_RCP
},
186 /* Input transition. */
187 struct input_transition
{
191 int (*handler
)(struct input_ctx
*);
192 const struct input_state
*state
;
198 void (*enter
)(struct input_ctx
*);
199 void (*exit
)(struct input_ctx
*);
200 const struct input_transition
*transitions
;
203 /* State transitions available from all states. */
204 #define INPUT_STATE_ANYWHERE \
205 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
206 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
207 { 0x1b, 0x1b, NULL, &input_state_esc_enter }
209 /* Forward declarations of state tables. */
210 const struct input_transition input_state_ground_table
[];
211 const struct input_transition input_state_esc_enter_table
[];
212 const struct input_transition input_state_esc_intermediate_table
[];
213 const struct input_transition input_state_csi_enter_table
[];
214 const struct input_transition input_state_csi_parameter_table
[];
215 const struct input_transition input_state_csi_intermediate_table
[];
216 const struct input_transition input_state_csi_ignore_table
[];
217 const struct input_transition input_state_dcs_enter_table
[];
218 const struct input_transition input_state_dcs_parameter_table
[];
219 const struct input_transition input_state_dcs_intermediate_table
[];
220 const struct input_transition input_state_dcs_handler_table
[];
221 const struct input_transition input_state_dcs_escape_table
[];
222 const struct input_transition input_state_dcs_ignore_table
[];
223 const struct input_transition input_state_osc_string_table
[];
224 const struct input_transition input_state_apc_string_table
[];
225 const struct input_transition input_state_rename_string_table
[];
226 const struct input_transition input_state_consume_st_table
[];
227 const struct input_transition input_state_utf8_three_table
[];
228 const struct input_transition input_state_utf8_two_table
[];
229 const struct input_transition input_state_utf8_one_table
[];
231 /* ground state definition. */
232 const struct input_state input_state_ground
= {
235 input_state_ground_table
238 /* esc_enter state definition. */
239 const struct input_state input_state_esc_enter
= {
242 input_state_esc_enter_table
245 /* esc_intermediate state definition. */
246 const struct input_state input_state_esc_intermediate
= {
249 input_state_esc_intermediate_table
252 /* csi_enter state definition. */
253 const struct input_state input_state_csi_enter
= {
256 input_state_csi_enter_table
259 /* csi_parameter state definition. */
260 const struct input_state input_state_csi_parameter
= {
263 input_state_csi_parameter_table
266 /* csi_intermediate state definition. */
267 const struct input_state input_state_csi_intermediate
= {
270 input_state_csi_intermediate_table
273 /* csi_ignore state definition. */
274 const struct input_state input_state_csi_ignore
= {
277 input_state_csi_ignore_table
280 /* dcs_enter state definition. */
281 const struct input_state input_state_dcs_enter
= {
284 input_state_dcs_enter_table
287 /* dcs_parameter state definition. */
288 const struct input_state input_state_dcs_parameter
= {
291 input_state_dcs_parameter_table
294 /* dcs_intermediate state definition. */
295 const struct input_state input_state_dcs_intermediate
= {
298 input_state_dcs_intermediate_table
301 /* dcs_handler state definition. */
302 const struct input_state input_state_dcs_handler
= {
305 input_state_dcs_handler_table
308 /* dcs_escape state definition. */
309 const struct input_state input_state_dcs_escape
= {
312 input_state_dcs_escape_table
315 /* dcs_ignore state definition. */
316 const struct input_state input_state_dcs_ignore
= {
319 input_state_dcs_ignore_table
322 /* osc_string state definition. */
323 const struct input_state input_state_osc_string
= {
325 input_enter_osc
, input_exit_osc
,
326 input_state_osc_string_table
329 /* apc_string state definition. */
330 const struct input_state input_state_apc_string
= {
332 input_enter_apc
, input_exit_apc
,
333 input_state_apc_string_table
336 /* rename_string state definition. */
337 const struct input_state input_state_rename_string
= {
339 input_enter_rename
, input_exit_rename
,
340 input_state_rename_string_table
343 /* consume_st state definition. */
344 const struct input_state input_state_consume_st
= {
347 input_state_consume_st_table
350 /* utf8_three state definition. */
351 const struct input_state input_state_utf8_three
= {
354 input_state_utf8_three_table
357 /* utf8_two state definition. */
358 const struct input_state input_state_utf8_two
= {
361 input_state_utf8_two_table
364 /* utf8_one state definition. */
365 const struct input_state input_state_utf8_one
= {
368 input_state_utf8_one_table
371 /* ground state table. */
372 const struct input_transition input_state_ground_table
[] = {
373 INPUT_STATE_ANYWHERE
,
375 { 0x00, 0x17, input_c0_dispatch
, NULL
},
376 { 0x19, 0x19, input_c0_dispatch
, NULL
},
377 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
378 { 0x20, 0x7e, input_print
, NULL
},
379 { 0x7f, 0x7f, NULL
, NULL
},
380 { 0x80, 0xc1, input_print
, NULL
},
381 { 0xc2, 0xdf, input_utf8_open
, &input_state_utf8_one
},
382 { 0xe0, 0xef, input_utf8_open
, &input_state_utf8_two
},
383 { 0xf0, 0xf4, input_utf8_open
, &input_state_utf8_three
},
384 { 0xf5, 0xff, input_print
, NULL
},
386 { -1, -1, NULL
, NULL
}
389 /* esc_enter state table. */
390 const struct input_transition input_state_esc_enter_table
[] = {
391 INPUT_STATE_ANYWHERE
,
393 { 0x00, 0x17, input_c0_dispatch
, NULL
},
394 { 0x19, 0x19, input_c0_dispatch
, NULL
},
395 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
396 { 0x20, 0x2f, input_intermediate
, &input_state_esc_intermediate
},
397 { 0x30, 0x4f, input_esc_dispatch
, &input_state_ground
},
398 { 0x50, 0x50, NULL
, &input_state_dcs_enter
},
399 { 0x51, 0x57, input_esc_dispatch
, &input_state_ground
},
400 { 0x58, 0x58, NULL
, &input_state_consume_st
},
401 { 0x59, 0x59, input_esc_dispatch
, &input_state_ground
},
402 { 0x5a, 0x5a, input_esc_dispatch
, &input_state_ground
},
403 { 0x5b, 0x5b, NULL
, &input_state_csi_enter
},
404 { 0x5c, 0x5c, input_esc_dispatch
, &input_state_ground
},
405 { 0x5d, 0x5d, NULL
, &input_state_osc_string
},
406 { 0x5e, 0x5e, NULL
, &input_state_consume_st
},
407 { 0x5f, 0x5f, NULL
, &input_state_apc_string
},
408 { 0x60, 0x6a, input_esc_dispatch
, &input_state_ground
},
409 { 0x6b, 0x6b, NULL
, &input_state_rename_string
},
410 { 0x6c, 0x7e, input_esc_dispatch
, &input_state_ground
},
411 { 0x7f, 0xff, NULL
, NULL
},
413 { -1, -1, NULL
, NULL
}
416 /* esc_interm state table. */
417 const struct input_transition input_state_esc_intermediate_table
[] = {
418 INPUT_STATE_ANYWHERE
,
420 { 0x00, 0x17, input_c0_dispatch
, NULL
},
421 { 0x19, 0x19, input_c0_dispatch
, NULL
},
422 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
423 { 0x20, 0x2f, input_intermediate
, NULL
},
424 { 0x30, 0x7e, input_esc_dispatch
, &input_state_ground
},
425 { 0x7f, 0xff, NULL
, NULL
},
427 { -1, -1, NULL
, NULL
}
430 /* csi_enter state table. */
431 const struct input_transition input_state_csi_enter_table
[] = {
432 INPUT_STATE_ANYWHERE
,
434 { 0x00, 0x17, input_c0_dispatch
, NULL
},
435 { 0x19, 0x19, input_c0_dispatch
, NULL
},
436 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
437 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
438 { 0x30, 0x39, input_parameter
, &input_state_csi_parameter
},
439 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
440 { 0x3b, 0x3b, input_parameter
, &input_state_csi_parameter
},
441 { 0x3c, 0x3f, input_intermediate
, &input_state_csi_parameter
},
442 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
443 { 0x7f, 0xff, NULL
, NULL
},
445 { -1, -1, NULL
, NULL
}
448 /* csi_parameter state table. */
449 const struct input_transition input_state_csi_parameter_table
[] = {
450 INPUT_STATE_ANYWHERE
,
452 { 0x00, 0x17, input_c0_dispatch
, NULL
},
453 { 0x19, 0x19, input_c0_dispatch
, NULL
},
454 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
455 { 0x20, 0x2f, input_intermediate
, &input_state_csi_intermediate
},
456 { 0x30, 0x39, input_parameter
, NULL
},
457 { 0x3a, 0x3a, NULL
, &input_state_csi_ignore
},
458 { 0x3b, 0x3b, input_parameter
, NULL
},
459 { 0x3c, 0x3f, NULL
, &input_state_csi_ignore
},
460 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
461 { 0x7f, 0xff, NULL
, NULL
},
463 { -1, -1, NULL
, NULL
}
466 /* csi_intermediate state table. */
467 const struct input_transition input_state_csi_intermediate_table
[] = {
468 INPUT_STATE_ANYWHERE
,
470 { 0x00, 0x17, input_c0_dispatch
, NULL
},
471 { 0x19, 0x19, input_c0_dispatch
, NULL
},
472 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
473 { 0x20, 0x2f, input_intermediate
, NULL
},
474 { 0x30, 0x3f, NULL
, &input_state_csi_ignore
},
475 { 0x40, 0x7e, input_csi_dispatch
, &input_state_ground
},
476 { 0x7f, 0xff, NULL
, NULL
},
478 { -1, -1, NULL
, NULL
}
481 /* csi_ignore state table. */
482 const struct input_transition input_state_csi_ignore_table
[] = {
483 INPUT_STATE_ANYWHERE
,
485 { 0x00, 0x17, input_c0_dispatch
, NULL
},
486 { 0x19, 0x19, input_c0_dispatch
, NULL
},
487 { 0x1c, 0x1f, input_c0_dispatch
, NULL
},
488 { 0x20, 0x3f, NULL
, NULL
},
489 { 0x40, 0x7e, NULL
, &input_state_ground
},
490 { 0x7f, 0xff, NULL
, NULL
},
492 { -1, -1, NULL
, NULL
}
495 /* dcs_enter state table. */
496 const struct input_transition input_state_dcs_enter_table
[] = {
497 INPUT_STATE_ANYWHERE
,
499 { 0x00, 0x17, NULL
, NULL
},
500 { 0x19, 0x19, NULL
, NULL
},
501 { 0x1c, 0x1f, NULL
, NULL
},
502 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
503 { 0x30, 0x39, input_parameter
, &input_state_dcs_parameter
},
504 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
505 { 0x3b, 0x3b, input_parameter
, &input_state_dcs_parameter
},
506 { 0x3c, 0x3f, input_intermediate
, &input_state_dcs_parameter
},
507 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
508 { 0x7f, 0xff, NULL
, NULL
},
510 { -1, -1, NULL
, NULL
}
513 /* dcs_parameter state table. */
514 const struct input_transition input_state_dcs_parameter_table
[] = {
515 INPUT_STATE_ANYWHERE
,
517 { 0x00, 0x17, NULL
, NULL
},
518 { 0x19, 0x19, NULL
, NULL
},
519 { 0x1c, 0x1f, NULL
, NULL
},
520 { 0x20, 0x2f, input_intermediate
, &input_state_dcs_intermediate
},
521 { 0x30, 0x39, input_parameter
, NULL
},
522 { 0x3a, 0x3a, NULL
, &input_state_dcs_ignore
},
523 { 0x3b, 0x3b, input_parameter
, NULL
},
524 { 0x3c, 0x3f, NULL
, &input_state_dcs_ignore
},
525 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
526 { 0x7f, 0xff, NULL
, NULL
},
528 { -1, -1, NULL
, NULL
}
531 /* dcs_interm state table. */
532 const struct input_transition input_state_dcs_intermediate_table
[] = {
533 INPUT_STATE_ANYWHERE
,
535 { 0x00, 0x17, NULL
, NULL
},
536 { 0x19, 0x19, NULL
, NULL
},
537 { 0x1c, 0x1f, NULL
, NULL
},
538 { 0x20, 0x2f, input_intermediate
, NULL
},
539 { 0x30, 0x3f, NULL
, &input_state_dcs_ignore
},
540 { 0x40, 0x7e, input_input
, &input_state_dcs_handler
},
541 { 0x7f, 0xff, NULL
, NULL
},
543 { -1, -1, NULL
, NULL
}
546 /* dcs_handler state table. */
547 const struct input_transition input_state_dcs_handler_table
[] = {
548 /* No INPUT_STATE_ANYWHERE */
550 { 0x00, 0x1a, input_input
, NULL
},
551 { 0x1b, 0x1b, NULL
, &input_state_dcs_escape
},
552 { 0x1c, 0xff, input_input
, NULL
},
554 { -1, -1, NULL
, NULL
}
557 /* dcs_escape state table. */
558 const struct input_transition input_state_dcs_escape_table
[] = {
559 /* No INPUT_STATE_ANYWHERE */
561 { 0x00, 0x5b, input_input
, &input_state_dcs_handler
},
562 { 0x5c, 0x5c, input_dcs_dispatch
, &input_state_ground
},
563 { 0x5d, 0xff, input_input
, &input_state_dcs_handler
},
565 { -1, -1, NULL
, NULL
}
568 /* dcs_ignore state table. */
569 const struct input_transition input_state_dcs_ignore_table
[] = {
570 INPUT_STATE_ANYWHERE
,
572 { 0x00, 0x17, NULL
, NULL
},
573 { 0x19, 0x19, NULL
, NULL
},
574 { 0x1c, 0x1f, NULL
, NULL
},
575 { 0x20, 0xff, NULL
, NULL
},
577 { -1, -1, NULL
, NULL
}
580 /* osc_string state table. */
581 const struct input_transition input_state_osc_string_table
[] = {
582 INPUT_STATE_ANYWHERE
,
584 { 0x00, 0x06, NULL
, NULL
},
585 { 0x07, 0x07, NULL
, &input_state_ground
},
586 { 0x08, 0x17, NULL
, NULL
},
587 { 0x19, 0x19, NULL
, NULL
},
588 { 0x1c, 0x1f, NULL
, NULL
},
589 { 0x20, 0xff, input_input
, NULL
},
591 { -1, -1, NULL
, NULL
}
594 /* apc_string state table. */
595 const struct input_transition input_state_apc_string_table
[] = {
596 INPUT_STATE_ANYWHERE
,
598 { 0x00, 0x17, NULL
, NULL
},
599 { 0x19, 0x19, NULL
, NULL
},
600 { 0x1c, 0x1f, NULL
, NULL
},
601 { 0x20, 0xff, input_input
, NULL
},
603 { -1, -1, NULL
, NULL
}
606 /* rename_string state table. */
607 const struct input_transition input_state_rename_string_table
[] = {
608 INPUT_STATE_ANYWHERE
,
610 { 0x00, 0x17, NULL
, NULL
},
611 { 0x19, 0x19, NULL
, NULL
},
612 { 0x1c, 0x1f, NULL
, NULL
},
613 { 0x20, 0xff, input_input
, NULL
},
615 { -1, -1, NULL
, NULL
}
618 /* consume_st state table. */
619 const struct input_transition input_state_consume_st_table
[] = {
620 INPUT_STATE_ANYWHERE
,
622 { 0x00, 0x17, NULL
, NULL
},
623 { 0x19, 0x19, NULL
, NULL
},
624 { 0x1c, 0x1f, NULL
, NULL
},
625 { 0x20, 0xff, NULL
, NULL
},
627 { -1, -1, NULL
, NULL
}
630 /* utf8_three state table. */
631 const struct input_transition input_state_utf8_three_table
[] = {
632 /* No INPUT_STATE_ANYWHERE */
634 { 0x00, 0x7f, NULL
, &input_state_ground
},
635 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_two
},
636 { 0xc0, 0xff, NULL
, &input_state_ground
},
638 { -1, -1, NULL
, NULL
}
641 /* utf8_two state table. */
642 const struct input_transition input_state_utf8_two_table
[] = {
643 /* No INPUT_STATE_ANYWHERE */
645 { 0x00, 0x7f, NULL
, &input_state_ground
},
646 { 0x80, 0xbf, input_utf8_add
, &input_state_utf8_one
},
647 { 0xc0, 0xff, NULL
, &input_state_ground
},
649 { -1, -1, NULL
, NULL
}
652 /* utf8_one state table. */
653 const struct input_transition input_state_utf8_one_table
[] = {
654 /* No INPUT_STATE_ANYWHERE */
656 { 0x00, 0x7f, NULL
, &input_state_ground
},
657 { 0x80, 0xbf, input_utf8_close
, &input_state_ground
},
658 { 0xc0, 0xff, NULL
, &input_state_ground
},
660 { -1, -1, NULL
, NULL
}
663 /* Input table compare. */
665 input_table_compare(const void *key
, const void *value
)
667 const struct input_ctx
*ictx
= key
;
668 const struct input_table_entry
*entry
= value
;
670 if (ictx
->ch
!= entry
->ch
)
671 return (ictx
->ch
- entry
->ch
);
672 return (strcmp(ictx
->interm_buf
, entry
->interm
));
675 /* Initialise input parser. */
677 input_init(struct window_pane
*wp
)
679 struct input_ctx
*ictx
= &wp
->ictx
;
681 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
683 memcpy(&ictx
->old_cell
, &grid_default_cell
, sizeof ictx
->old_cell
);
687 *ictx
->interm_buf
= '\0';
688 ictx
->interm_len
= 0;
690 *ictx
->param_buf
= '\0';
693 ictx
->state
= &input_state_ground
;
697 /* Destroy input parser. */
699 input_free(unused
struct window_pane
*wp
)
705 input_parse(struct window_pane
*wp
)
707 struct input_ctx
*ictx
= &wp
->ictx
;
708 const struct input_transition
*itr
;
709 struct evbuffer
*evb
= wp
->event
->input
;
713 if (EVBUFFER_LENGTH(evb
) == 0)
716 wp
->window
->flags
|= WINDOW_ACTIVITY
;
717 wp
->window
->flags
&= ~WINDOW_SILENCE
;
720 * Open the screen. Use NULL wp if there is a mode set as don't want to
723 if (wp
->mode
== NULL
)
724 screen_write_start(&ictx
->ctx
, wp
, &wp
->base
);
726 screen_write_start(&ictx
->ctx
, NULL
, &wp
->base
);
729 buf
= EVBUFFER_DATA(evb
);
730 len
= EVBUFFER_LENGTH(evb
);
733 /* Parse the input. */
735 ictx
->ch
= buf
[off
++];
736 log_debug("%s: '%c' %s", __func__
, ictx
->ch
, ictx
->state
->name
);
738 /* Find the transition. */
739 itr
= ictx
->state
->transitions
;
740 while (itr
->first
!= -1 && itr
->last
!= -1) {
741 if (ictx
->ch
>= itr
->first
&& ictx
->ch
<= itr
->last
)
745 if (itr
->first
== -1 || itr
->last
== -1) {
746 /* No transition? Eh? */
747 fatalx("No transition from state!");
751 * Execute the handler, if any. Don't switch state if it
754 if (itr
->handler
!= NULL
&& itr
->handler(ictx
) != 0)
757 /* And switch state, if necessary. */
758 if (itr
->state
!= NULL
) {
759 if (ictx
->state
->exit
!= NULL
)
760 ictx
->state
->exit(ictx
);
761 ictx
->state
= itr
->state
;
762 if (ictx
->state
->enter
!= NULL
)
763 ictx
->state
->enter(ictx
);
767 /* Close the screen. */
768 screen_write_stop(&ictx
->ctx
);
770 evbuffer_drain(evb
, len
);
773 /* Split the parameter list (if any). */
775 input_split(struct input_ctx
*ictx
)
782 ictx
->param_list_len
= 0;
783 if (ictx
->param_len
== 0)
786 ptr
= ictx
->param_buf
;
787 while ((out
= strsep(&ptr
, ";")) != NULL
) {
791 n
= strtonum(out
, 0, INT_MAX
, &errstr
);
796 ictx
->param_list
[ictx
->param_list_len
++] = n
;
797 if (ictx
->param_list_len
== nitems(ictx
->param_list
))
804 /* Get an argument or return default value. */
806 input_get(struct input_ctx
*ictx
, u_int validx
, int minval
, int defval
)
810 if (validx
>= ictx
->param_list_len
)
813 retval
= ictx
->param_list
[validx
];
821 /* Reply to terminal query. */
823 input_reply(struct input_ctx
*ictx
, const char *fmt
, ...)
829 vasprintf(&reply
, fmt
, ap
);
832 bufferevent_write(ictx
->wp
->event
, reply
, strlen(reply
));
836 /* Clear saved state. */
838 input_clear(struct input_ctx
*ictx
)
840 *ictx
->interm_buf
= '\0';
841 ictx
->interm_len
= 0;
843 *ictx
->param_buf
= '\0';
846 *ictx
->input_buf
= '\0';
849 ictx
->flags
&= ~INPUT_DISCARD
;
852 /* Output this character to the screen. */
854 input_print(struct input_ctx
*ictx
)
856 ictx
->cell
.data
= ictx
->ch
;
857 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, NULL
);
862 /* Collect intermediate string. */
864 input_intermediate(struct input_ctx
*ictx
)
866 if (ictx
->interm_len
== (sizeof ictx
->interm_buf
) - 1)
867 ictx
->flags
|= INPUT_DISCARD
;
869 ictx
->interm_buf
[ictx
->interm_len
++] = ictx
->ch
;
870 ictx
->interm_buf
[ictx
->interm_len
] = '\0';
876 /* Collect parameter string. */
878 input_parameter(struct input_ctx
*ictx
)
880 if (ictx
->param_len
== (sizeof ictx
->param_buf
) - 1)
881 ictx
->flags
|= INPUT_DISCARD
;
883 ictx
->param_buf
[ictx
->param_len
++] = ictx
->ch
;
884 ictx
->param_buf
[ictx
->param_len
] = '\0';
890 /* Collect input string. */
892 input_input(struct input_ctx
*ictx
)
894 if (ictx
->input_len
== (sizeof ictx
->input_buf
) - 1)
895 ictx
->flags
|= INPUT_DISCARD
;
897 ictx
->input_buf
[ictx
->input_len
++] = ictx
->ch
;
898 ictx
->input_buf
[ictx
->input_len
] = '\0';
904 /* Execute C0 control sequence. */
906 input_c0_dispatch(struct input_ctx
*ictx
)
908 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
909 struct window_pane
*wp
= ictx
->wp
;
910 struct screen
*s
= sctx
->s
;
913 log_debug("%s: '%c", __func__
, ictx
->ch
);
916 case '\000': /* NUL */
918 case '\007': /* BEL */
919 wp
->window
->flags
|= WINDOW_BELL
;
921 case '\010': /* BS */
922 screen_write_backspace(sctx
);
924 case '\011': /* HT */
925 /* Don't tab beyond the end of the line. */
926 if (s
->cx
>= screen_size_x(s
) - 1)
929 /* Find the next tab point, or use the last column if none. */
932 if (bit_test(s
->tabs
, s
->cx
))
934 } while (s
->cx
< screen_size_x(s
) - 1);
936 case '\012': /* LF */
937 case '\013': /* VT */
938 case '\014': /* FF */
939 screen_write_linefeed(sctx
, 0);
941 case '\015': /* CR */
942 screen_write_carriagereturn(sctx
);
944 case '\016': /* SO */
945 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
947 case '\017': /* SI */
948 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
951 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
958 trigger
= options_get_number(&wp
->window
->options
, "c0-change-trigger");
959 if (++wp
->changes
== trigger
) {
960 wp
->flags
|= PANE_DROP
;
961 window_pane_timer_start(wp
);
967 /* Execute escape sequence. */
969 input_esc_dispatch(struct input_ctx
*ictx
)
971 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
972 struct screen
*s
= sctx
->s
;
973 struct input_table_entry
*entry
;
975 if (ictx
->flags
& INPUT_DISCARD
)
977 log_debug("%s: '%c', %s", __func__
, ictx
->ch
, ictx
->interm_buf
);
979 entry
= bsearch(ictx
, input_esc_table
, nitems(input_esc_table
),
980 sizeof input_esc_table
[0], input_table_compare
);
982 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
986 switch (entry
->type
) {
988 memcpy(&ictx
->cell
, &grid_default_cell
, sizeof ictx
->cell
);
989 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
993 screen_write_reset(sctx
);
996 screen_write_linefeed(sctx
, 0);
999 screen_write_carriagereturn(sctx
);
1000 screen_write_linefeed(sctx
, 0);
1003 if (s
->cx
< screen_size_x(s
))
1004 bit_set(s
->tabs
, s
->cx
);
1007 screen_write_reverseindex(sctx
);
1009 case INPUT_ESC_DECKPAM
:
1010 screen_write_kkeypadmode(sctx
, 1);
1012 case INPUT_ESC_DECKPNM
:
1013 screen_write_kkeypadmode(sctx
, 0);
1015 case INPUT_ESC_DECSC
:
1016 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
1017 ictx
->old_cx
= s
->cx
;
1018 ictx
->old_cy
= s
->cy
;
1020 case INPUT_ESC_DECRC
:
1021 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
1022 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
);
1024 case INPUT_ESC_DECALN
:
1025 screen_write_alignmenttest(sctx
);
1027 case INPUT_ESC_SCSON_G0
:
1029 * Not really supported, but fake it up enough for those that
1030 * use it to switch character sets (by redefining G0 to
1031 * graphics set, rather than switching to G1).
1033 ictx
->cell
.attr
&= ~GRID_ATTR_CHARSET
;
1035 case INPUT_ESC_SCSOFF_G0
:
1036 ictx
->cell
.attr
|= GRID_ATTR_CHARSET
;
1043 /* Execute control sequence. */
1045 input_csi_dispatch(struct input_ctx
*ictx
)
1047 struct screen_write_ctx
*sctx
= &ictx
->ctx
;
1048 struct window_pane
*wp
= ictx
->wp
;
1049 struct screen
*s
= sctx
->s
;
1050 struct input_table_entry
*entry
;
1053 if (ictx
->flags
& INPUT_DISCARD
)
1055 if (input_split(ictx
) != 0)
1057 log_debug("%s: '%c' \"%s\" \"%s\"",
1058 __func__
, ictx
->ch
, ictx
->interm_buf
, ictx
->param_buf
);
1060 entry
= bsearch(ictx
, input_csi_table
, nitems(input_csi_table
),
1061 sizeof input_csi_table
[0], input_table_compare
);
1062 if (entry
== NULL
) {
1063 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1067 switch (entry
->type
) {
1069 /* Find the previous tab point, n times. */
1070 n
= input_get(ictx
, 0, 1, 1);
1071 while (s
->cx
> 0 && n
-- > 0) {
1074 while (s
->cx
> 0 && !bit_test(s
->tabs
, s
->cx
));
1078 screen_write_cursorleft(sctx
, input_get(ictx
, 0, 1, 1));
1081 screen_write_cursordown(sctx
, input_get(ictx
, 0, 1, 1));
1084 screen_write_cursorright(sctx
, input_get(ictx
, 0, 1, 1));
1087 n
= input_get(ictx
, 0, 1, 1);
1088 m
= input_get(ictx
, 1, 1, 1);
1089 screen_write_cursormove(sctx
, m
- 1, n
- 1);
1092 screen_write_cursorup(sctx
, input_get(ictx
, 0, 1, 1));
1095 screen_write_carriagereturn(sctx
);
1096 screen_write_cursordown(sctx
, input_get(ictx
, 0, 1, 1));
1099 screen_write_carriagereturn(sctx
);
1100 screen_write_cursorup(sctx
, input_get(ictx
, 0, 1, 1));
1103 switch (input_get(ictx
, 0, 0, 0)) {
1105 input_reply(ictx
, "\033[?1;2c");
1108 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1112 case INPUT_CSI_DA_TWO
:
1113 switch (input_get(ictx
, 0, 0, 0)) {
1115 input_reply(ictx
, "\033[>0;95;0c");
1118 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1123 screen_write_deletecharacter(sctx
, input_get(ictx
, 0, 1, 1));
1125 case INPUT_CSI_DECSTBM
:
1126 n
= input_get(ictx
, 0, 1, 1);
1127 m
= input_get(ictx
, 1, 1, screen_size_y(s
));
1128 screen_write_scrollregion(sctx
, n
- 1, m
- 1);
1131 screen_write_deleteline(sctx
, input_get(ictx
, 0, 1, 1));
1134 switch (input_get(ictx
, 0, 0, 0)) {
1136 input_reply(ictx
, "\033[0n");
1139 input_reply(ictx
, "\033[%u;%uR", s
->cy
+ 1, s
->cx
+ 1);
1142 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1147 switch (input_get(ictx
, 0, 0, 0)) {
1149 screen_write_clearendofscreen(sctx
);
1152 screen_write_clearstartofscreen(sctx
);
1155 screen_write_clearscreen(sctx
);
1158 switch (input_get(ictx
, 1, 0, 0)) {
1161 * Linux console extension to clear history
1162 * (for example before locking the screen).
1164 screen_write_clearhistory(sctx
);
1169 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1174 switch (input_get(ictx
, 0, 0, 0)) {
1176 screen_write_clearendofline(sctx
);
1179 screen_write_clearstartofline(sctx
);
1182 screen_write_clearline(sctx
);
1185 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1190 n
= input_get(ictx
, 0, 1, 1);
1191 screen_write_cursormove(sctx
, n
- 1, s
->cy
);
1194 screen_write_insertcharacter(sctx
, input_get(ictx
, 0, 1, 1));
1197 screen_write_insertline(sctx
, input_get(ictx
, 0, 1, 1));
1200 memcpy(&ictx
->cell
, &ictx
->old_cell
, sizeof ictx
->cell
);
1201 screen_write_cursormove(sctx
, ictx
->old_cx
, ictx
->old_cy
);
1204 switch (input_get(ictx
, 0, 0, -1)) {
1206 screen_write_insertmode(&ictx
->ctx
, 0);
1209 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1213 case INPUT_CSI_RM_PRIVATE
:
1214 switch (input_get(ictx
, 0, 0, -1)) {
1216 screen_write_kcursormode(&ictx
->ctx
, 0);
1218 case 3: /* DECCOLM */
1219 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1220 screen_write_clearscreen(&ictx
->ctx
);
1223 screen_write_cursormode(&ictx
->ctx
, 0);
1229 screen_write_mousemode_off(&ictx
->ctx
);
1232 screen_write_utf8mousemode(&ictx
->ctx
, 0);
1235 window_pane_alternate_off(wp
, &ictx
->cell
);
1238 screen_write_bracketpaste(&ictx
->ctx
, 0);
1241 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1246 memcpy(&ictx
->old_cell
, &ictx
->cell
, sizeof ictx
->old_cell
);
1247 ictx
->old_cx
= s
->cx
;
1248 ictx
->old_cy
= s
->cy
;
1251 input_csi_dispatch_sgr(ictx
);
1254 switch (input_get(ictx
, 0, 0, -1)) {
1256 screen_write_insertmode(&ictx
->ctx
, 1);
1259 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1263 case INPUT_CSI_SM_PRIVATE
:
1264 switch (input_get(ictx
, 0, 0, -1)) {
1266 screen_write_kcursormode(&ictx
->ctx
, 1);
1268 case 3: /* DECCOLM */
1269 screen_write_cursormove(&ictx
->ctx
, 0, 0);
1270 screen_write_clearscreen(&ictx
->ctx
);
1273 screen_write_cursormode(&ictx
->ctx
, 1);
1276 screen_write_mousemode_on(
1277 &ictx
->ctx
, MODE_MOUSE_STANDARD
);
1280 screen_write_mousemode_on(
1281 &ictx
->ctx
, MODE_MOUSE_BUTTON
);
1284 screen_write_mousemode_on(&ictx
->ctx
, MODE_MOUSE_ANY
);
1287 screen_write_utf8mousemode(&ictx
->ctx
, 1);
1290 window_pane_alternate_on(wp
, &ictx
->cell
);
1293 screen_write_bracketpaste(&ictx
->ctx
, 1);
1296 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1301 switch (input_get(ictx
, 0, 0, 0)) {
1303 if (s
->cx
< screen_size_x(s
))
1304 bit_clear(s
->tabs
, s
->cx
);
1307 bit_nclear(s
->tabs
, 0, screen_size_x(s
) - 1);
1310 log_debug("%s: unknown '%c'", __func__
, ictx
->ch
);
1315 n
= input_get(ictx
, 0, 1, 1);
1316 screen_write_cursormove(sctx
, s
->cx
, n
- 1);
1318 case INPUT_CSI_DECSCUSR
:
1319 n
= input_get(ictx
, 0, 0, 0);
1320 screen_set_cursor_style(s
, n
);
1327 /* Handle CSI SGR. */
1329 input_csi_dispatch_sgr(struct input_ctx
*ictx
)
1331 struct grid_cell
*gc
= &ictx
->cell
;
1336 if (ictx
->param_list_len
== 0) {
1338 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1339 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1343 for (i
= 0; i
< ictx
->param_list_len
; i
++) {
1344 n
= input_get(ictx
, i
, 0, 0);
1346 if (n
== 38 || n
== 48) {
1348 if (input_get(ictx
, i
, 0, -1) != 5)
1352 m
= input_get(ictx
, i
, 0, -1);
1355 gc
->flags
&= ~GRID_FLAG_FG256
;
1357 } else if (n
== 48) {
1358 gc
->flags
&= ~GRID_FLAG_BG256
;
1364 gc
->flags
|= GRID_FLAG_FG256
;
1366 } else if (n
== 48) {
1367 gc
->flags
|= GRID_FLAG_BG256
;
1378 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1379 gc
->attr
|= (attr
& GRID_ATTR_CHARSET
);
1382 gc
->attr
|= GRID_ATTR_BRIGHT
;
1385 gc
->attr
|= GRID_ATTR_DIM
;
1388 gc
->attr
|= GRID_ATTR_ITALICS
;
1391 gc
->attr
|= GRID_ATTR_UNDERSCORE
;
1394 gc
->attr
|= GRID_ATTR_BLINK
;
1397 gc
->attr
|= GRID_ATTR_REVERSE
;
1400 gc
->attr
|= GRID_ATTR_HIDDEN
;
1403 gc
->attr
&= ~(GRID_ATTR_BRIGHT
|GRID_ATTR_DIM
);
1406 gc
->attr
&= ~GRID_ATTR_ITALICS
;
1409 gc
->attr
&= ~GRID_ATTR_UNDERSCORE
;
1412 gc
->attr
&= ~GRID_ATTR_BLINK
;
1415 gc
->attr
&= ~GRID_ATTR_REVERSE
;
1425 gc
->flags
&= ~GRID_FLAG_FG256
;
1429 gc
->flags
&= ~GRID_FLAG_FG256
;
1440 gc
->flags
&= ~GRID_FLAG_BG256
;
1444 gc
->flags
&= ~GRID_FLAG_BG256
;
1455 gc
->flags
&= ~GRID_FLAG_FG256
;
1466 gc
->flags
&= ~GRID_FLAG_BG256
;
1473 /* DCS terminator (ST) received. */
1475 input_dcs_dispatch(struct input_ctx
*ictx
)
1477 const char prefix
[] = "tmux;";
1478 const u_int prefix_len
= (sizeof prefix
) - 1;
1480 if (ictx
->flags
& INPUT_DISCARD
)
1483 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1485 /* Check for tmux prefix. */
1486 if (ictx
->input_len
>= prefix_len
&&
1487 strncmp(ictx
->input_buf
, prefix
, prefix_len
) == 0) {
1488 screen_write_rawstring(&ictx
->ctx
,
1489 ictx
->input_buf
+ prefix_len
, ictx
->input_len
- prefix_len
);
1495 /* OSC string started. */
1497 input_enter_osc(struct input_ctx
*ictx
)
1499 log_debug("%s", __func__
);
1504 /* OSC terminator (ST) received. */
1506 input_exit_osc(struct input_ctx
*ictx
)
1508 u_char
*p
= ictx
->input_buf
;
1511 if (ictx
->flags
& INPUT_DISCARD
)
1513 if (ictx
->input_len
< 1 || *p
< '0' || *p
> '9')
1516 log_debug("%s: \"%s\"", __func__
, p
);
1519 while (*p
>= '0' && *p
<= '9')
1520 option
= option
* 10 + *p
++ - '0';
1527 screen_set_title(ictx
->ctx
.s
, p
);
1528 server_status_window(ictx
->wp
->window
);
1531 screen_set_cursor_colour(ictx
->ctx
.s
, p
);
1534 if (*p
== '\0') /* No arguments allowed. */
1535 screen_set_cursor_colour(ictx
->ctx
.s
, "");
1538 log_debug("%s: unknown '%u'", __func__
, option
);
1543 /* APC string started. */
1545 input_enter_apc(struct input_ctx
*ictx
)
1547 log_debug("%s", __func__
);
1552 /* APC terminator (ST) received. */
1554 input_exit_apc(struct input_ctx
*ictx
)
1556 if (ictx
->flags
& INPUT_DISCARD
)
1558 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1560 screen_set_title(ictx
->ctx
.s
, ictx
->input_buf
);
1561 server_status_window(ictx
->wp
->window
);
1564 /* Rename string started. */
1566 input_enter_rename(struct input_ctx
*ictx
)
1568 log_debug("%s", __func__
);
1573 /* Rename terminator (ST) received. */
1575 input_exit_rename(struct input_ctx
*ictx
)
1577 if (ictx
->flags
& INPUT_DISCARD
)
1579 if (!options_get_number(&ictx
->wp
->window
->options
, "allow-rename"))
1581 log_debug("%s: \"%s\"", __func__
, ictx
->input_buf
);
1583 window_set_name(ictx
->wp
->window
, ictx
->input_buf
);
1584 options_set_number(&ictx
->wp
->window
->options
, "automatic-rename", 0);
1586 server_status_window(ictx
->wp
->window
);
1589 /* Open UTF-8 character. */
1591 input_utf8_open(struct input_ctx
*ictx
)
1593 if (!options_get_number(&ictx
->wp
->window
->options
, "utf8")) {
1594 /* Print, and do not switch state. */
1598 log_debug("%s", __func__
);
1600 utf8_open(&ictx
->utf8data
, ictx
->ch
);
1604 /* Append to UTF-8 character. */
1606 input_utf8_add(struct input_ctx
*ictx
)
1608 log_debug("%s", __func__
);
1610 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1614 /* Close UTF-8 string. */
1616 input_utf8_close(struct input_ctx
*ictx
)
1618 log_debug("%s", __func__
);
1620 utf8_append(&ictx
->utf8data
, ictx
->ch
);
1622 ictx
->cell
.flags
|= GRID_FLAG_UTF8
;
1623 screen_write_cell(&ictx
->ctx
, &ictx
->cell
, &ictx
->utf8data
);
1624 ictx
->cell
.flags
&= ~GRID_FLAG_UTF8
;