Bug Summary

File:src/usr.bin/tmux/input.c
Warning:line 907, column 10
Access to field 'transitions' results in a dereference of a null pointer (loaded from field 'state')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name input.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/tmux/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/usr.bin/tmux -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/tmux/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/tmux/input.c
1/* $OpenBSD: input.c,v 1.198 2021/12/07 07:28:44 nicm Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <netinet/in.h>
22
23#include <ctype.h>
24#include <resolv.h>
25#include <stdlib.h>
26#include <string.h>
27#include <time.h>
28
29#include "tmux.h"
30
31/*
32 * Based on the description by Paul Williams at:
33 *
34 * https://vt100.net/emu/dec_ansi_parser
35 *
36 * With the following changes:
37 *
38 * - 7-bit only.
39 *
40 * - Support for UTF-8.
41 *
42 * - OSC (but not APC) may be terminated by \007 as well as ST.
43 *
44 * - A state for APC similar to OSC. Some terminals appear to use this to set
45 * the title.
46 *
47 * - A state for the screen \033k...\033\\ sequence to rename a window. This is
48 * pretty stupid but not supporting it is more trouble than it is worth.
49 *
50 * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to
51 * be passed to the underlying terminals.
52 */
53
54/* Input parser cell. */
55struct input_cell {
56 struct grid_cell cell;
57 int set;
58 int g0set; /* 1 if ACS */
59 int g1set; /* 1 if ACS */
60};
61
62/* Input parser argument. */
63struct input_param {
64 enum {
65 INPUT_MISSING,
66 INPUT_NUMBER,
67 INPUT_STRING
68 } type;
69 union {
70 int num;
71 char *str;
72 };
73};
74
75/* Input parser context. */
76struct input_ctx {
77 struct window_pane *wp;
78 struct bufferevent *event;
79 struct screen_write_ctx ctx;
80 struct colour_palette *palette;
81
82 struct input_cell cell;
83
84 struct input_cell old_cell;
85 u_int old_cx;
86 u_int old_cy;
87 int old_mode;
88
89 u_char interm_buf[4];
90 size_t interm_len;
91
92 u_char param_buf[64];
93 size_t param_len;
94
95#define INPUT_BUF_START32 32
96#define INPUT_BUF_LIMIT1048576 1048576
97 u_char *input_buf;
98 size_t input_len;
99 size_t input_space;
100 enum {
101 INPUT_END_ST,
102 INPUT_END_BEL
103 } input_end;
104
105 struct input_param param_list[24];
106 u_int param_list_len;
107
108 struct utf8_data utf8data;
109 int utf8started;
110
111 int ch;
112 int last;
113
114 int flags;
115#define INPUT_DISCARD0x1 0x1
116
117 const struct input_state *state;
118
119 struct event timer;
120
121 /*
122 * All input received since we were last in the ground state. Sent to
123 * control clients on connection.
124 */
125 struct evbuffer *since_ground;
126};
127
128/* Helper functions. */
129struct input_transition;
130static int input_split(struct input_ctx *);
131static int input_get(struct input_ctx *, u_int, int, int);
132static void printflike(2, 3)__attribute__ ((format (printf, 2, 3))) input_reply(struct input_ctx *, const char *, ...);
133static void input_set_state(struct input_ctx *,
134 const struct input_transition *);
135static void input_reset_cell(struct input_ctx *);
136
137static void input_osc_4(struct input_ctx *, const char *);
138static void input_osc_10(struct input_ctx *, const char *);
139static void input_osc_11(struct input_ctx *, const char *);
140static void input_osc_12(struct input_ctx *, const char *);
141static void input_osc_52(struct input_ctx *, const char *);
142static void input_osc_104(struct input_ctx *, const char *);
143static void input_osc_110(struct input_ctx *, const char *);
144static void input_osc_111(struct input_ctx *, const char *);
145static void input_osc_112(struct input_ctx *, const char *);
146
147/* Transition entry/exit handlers. */
148static void input_clear(struct input_ctx *);
149static void input_ground(struct input_ctx *);
150static void input_enter_dcs(struct input_ctx *);
151static void input_enter_osc(struct input_ctx *);
152static void input_exit_osc(struct input_ctx *);
153static void input_enter_apc(struct input_ctx *);
154static void input_exit_apc(struct input_ctx *);
155static void input_enter_rename(struct input_ctx *);
156static void input_exit_rename(struct input_ctx *);
157
158/* Input state handlers. */
159static int input_print(struct input_ctx *);
160static int input_intermediate(struct input_ctx *);
161static int input_parameter(struct input_ctx *);
162static int input_input(struct input_ctx *);
163static int input_c0_dispatch(struct input_ctx *);
164static int input_esc_dispatch(struct input_ctx *);
165static int input_csi_dispatch(struct input_ctx *);
166static void input_csi_dispatch_rm(struct input_ctx *);
167static void input_csi_dispatch_rm_private(struct input_ctx *);
168static void input_csi_dispatch_sm(struct input_ctx *);
169static void input_csi_dispatch_sm_private(struct input_ctx *);
170static void input_csi_dispatch_winops(struct input_ctx *);
171static void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
172static void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
173static void input_csi_dispatch_sgr(struct input_ctx *);
174static int input_dcs_dispatch(struct input_ctx *);
175static int input_top_bit_set(struct input_ctx *);
176static int input_end_bel(struct input_ctx *);
177
178/* Command table comparison function. */
179static int input_table_compare(const void *, const void *);
180
181/* Command table entry. */
182struct input_table_entry {
183 int ch;
184 const char *interm;
185 int type;
186};
187
188/* Escape commands. */
189enum input_esc_type {
190 INPUT_ESC_DECALN,
191 INPUT_ESC_DECKPAM,
192 INPUT_ESC_DECKPNM,
193 INPUT_ESC_DECRC,
194 INPUT_ESC_DECSC,
195 INPUT_ESC_HTS,
196 INPUT_ESC_IND,
197 INPUT_ESC_NEL,
198 INPUT_ESC_RI,
199 INPUT_ESC_RIS,
200 INPUT_ESC_SCSG0_OFF,
201 INPUT_ESC_SCSG0_ON,
202 INPUT_ESC_SCSG1_OFF,
203 INPUT_ESC_SCSG1_ON,
204 INPUT_ESC_ST,
205};
206
207/* Escape command table. */
208static const struct input_table_entry input_esc_table[] = {
209 { '0', "(", INPUT_ESC_SCSG0_ON },
210 { '0', ")", INPUT_ESC_SCSG1_ON },
211 { '7', "", INPUT_ESC_DECSC },
212 { '8', "", INPUT_ESC_DECRC },
213 { '8', "#", INPUT_ESC_DECALN },
214 { '=', "", INPUT_ESC_DECKPAM },
215 { '>', "", INPUT_ESC_DECKPNM },
216 { 'B', "(", INPUT_ESC_SCSG0_OFF },
217 { 'B', ")", INPUT_ESC_SCSG1_OFF },
218 { 'D', "", INPUT_ESC_IND },
219 { 'E', "", INPUT_ESC_NEL },
220 { 'H', "", INPUT_ESC_HTS },
221 { 'M', "", INPUT_ESC_RI },
222 { '\\', "", INPUT_ESC_ST },
223 { 'c', "", INPUT_ESC_RIS },
224};
225
226/* Control (CSI) commands. */
227enum input_csi_type {
228 INPUT_CSI_CBT,
229 INPUT_CSI_CNL,
230 INPUT_CSI_CPL,
231 INPUT_CSI_CUB,
232 INPUT_CSI_CUD,
233 INPUT_CSI_CUF,
234 INPUT_CSI_CUP,
235 INPUT_CSI_CUU,
236 INPUT_CSI_DA,
237 INPUT_CSI_DA_TWO,
238 INPUT_CSI_DCH,
239 INPUT_CSI_DECSCUSR,
240 INPUT_CSI_DECSTBM,
241 INPUT_CSI_DL,
242 INPUT_CSI_DSR,
243 INPUT_CSI_ECH,
244 INPUT_CSI_ED,
245 INPUT_CSI_EL,
246 INPUT_CSI_HPA,
247 INPUT_CSI_ICH,
248 INPUT_CSI_IL,
249 INPUT_CSI_MODOFF,
250 INPUT_CSI_MODSET,
251 INPUT_CSI_RCP,
252 INPUT_CSI_REP,
253 INPUT_CSI_RM,
254 INPUT_CSI_RM_PRIVATE,
255 INPUT_CSI_SCP,
256 INPUT_CSI_SD,
257 INPUT_CSI_SGR,
258 INPUT_CSI_SM,
259 INPUT_CSI_SM_PRIVATE,
260 INPUT_CSI_SU,
261 INPUT_CSI_TBC,
262 INPUT_CSI_VPA,
263 INPUT_CSI_WINOPS,
264 INPUT_CSI_XDA,
265};
266
267/* Control (CSI) command table. */
268static const struct input_table_entry input_csi_table[] = {
269 { '@', "", INPUT_CSI_ICH },
270 { 'A', "", INPUT_CSI_CUU },
271 { 'B', "", INPUT_CSI_CUD },
272 { 'C', "", INPUT_CSI_CUF },
273 { 'D', "", INPUT_CSI_CUB },
274 { 'E', "", INPUT_CSI_CNL },
275 { 'F', "", INPUT_CSI_CPL },
276 { 'G', "", INPUT_CSI_HPA },
277 { 'H', "", INPUT_CSI_CUP },
278 { 'J', "", INPUT_CSI_ED },
279 { 'K', "", INPUT_CSI_EL },
280 { 'L', "", INPUT_CSI_IL },
281 { 'M', "", INPUT_CSI_DL },
282 { 'P', "", INPUT_CSI_DCH },
283 { 'S', "", INPUT_CSI_SU },
284 { 'T', "", INPUT_CSI_SD },
285 { 'X', "", INPUT_CSI_ECH },
286 { 'Z', "", INPUT_CSI_CBT },
287 { '`', "", INPUT_CSI_HPA },
288 { 'b', "", INPUT_CSI_REP },
289 { 'c', "", INPUT_CSI_DA },
290 { 'c', ">", INPUT_CSI_DA_TWO },
291 { 'd', "", INPUT_CSI_VPA },
292 { 'f', "", INPUT_CSI_CUP },
293 { 'g', "", INPUT_CSI_TBC },
294 { 'h', "", INPUT_CSI_SM },
295 { 'h', "?", INPUT_CSI_SM_PRIVATE },
296 { 'l', "", INPUT_CSI_RM },
297 { 'l', "?", INPUT_CSI_RM_PRIVATE },
298 { 'm', "", INPUT_CSI_SGR },
299 { 'm', ">", INPUT_CSI_MODSET },
300 { 'n', "", INPUT_CSI_DSR },
301 { 'n', ">", INPUT_CSI_MODOFF },
302 { 'q', " ", INPUT_CSI_DECSCUSR },
303 { 'q', ">", INPUT_CSI_XDA },
304 { 'r', "", INPUT_CSI_DECSTBM },
305 { 's', "", INPUT_CSI_SCP },
306 { 't', "", INPUT_CSI_WINOPS },
307 { 'u', "", INPUT_CSI_RCP },
308};
309
310/* Input transition. */
311struct input_transition {
312 int first;
313 int last;
314
315 int (*handler)(struct input_ctx *);
316 const struct input_state *state;
317};
318
319/* Input state. */
320struct input_state {
321 const char *name;
322 void (*enter)(struct input_ctx *);
323 void (*exit)(struct input_ctx *);
324 const struct input_transition *transitions;
325};
326
327/* State transitions available from all states. */
328#define INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
\
329 { 0x18, 0x18, input_c0_dispatch, &input_state_ground }, \
330 { 0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, \
331 { 0x1b, 0x1b, NULL((void *)0), &input_state_esc_enter }
332
333/* Forward declarations of state tables. */
334static const struct input_transition input_state_ground_table[];
335static const struct input_transition input_state_esc_enter_table[];
336static const struct input_transition input_state_esc_intermediate_table[];
337static const struct input_transition input_state_csi_enter_table[];
338static const struct input_transition input_state_csi_parameter_table[];
339static const struct input_transition input_state_csi_intermediate_table[];
340static const struct input_transition input_state_csi_ignore_table[];
341static const struct input_transition input_state_dcs_enter_table[];
342static const struct input_transition input_state_dcs_parameter_table[];
343static const struct input_transition input_state_dcs_intermediate_table[];
344static const struct input_transition input_state_dcs_handler_table[];
345static const struct input_transition input_state_dcs_escape_table[];
346static const struct input_transition input_state_dcs_ignore_table[];
347static const struct input_transition input_state_osc_string_table[];
348static const struct input_transition input_state_apc_string_table[];
349static const struct input_transition input_state_rename_string_table[];
350static const struct input_transition input_state_consume_st_table[];
351
352/* ground state definition. */
353static const struct input_state input_state_ground = {
354 "ground",
355 input_ground, NULL((void *)0),
356 input_state_ground_table
357};
358
359/* esc_enter state definition. */
360static const struct input_state input_state_esc_enter = {
361 "esc_enter",
362 input_clear, NULL((void *)0),
363 input_state_esc_enter_table
364};
365
366/* esc_intermediate state definition. */
367static const struct input_state input_state_esc_intermediate = {
368 "esc_intermediate",
369 NULL((void *)0), NULL((void *)0),
370 input_state_esc_intermediate_table
371};
372
373/* csi_enter state definition. */
374static const struct input_state input_state_csi_enter = {
375 "csi_enter",
376 input_clear, NULL((void *)0),
377 input_state_csi_enter_table
378};
379
380/* csi_parameter state definition. */
381static const struct input_state input_state_csi_parameter = {
382 "csi_parameter",
383 NULL((void *)0), NULL((void *)0),
384 input_state_csi_parameter_table
385};
386
387/* csi_intermediate state definition. */
388static const struct input_state input_state_csi_intermediate = {
389 "csi_intermediate",
390 NULL((void *)0), NULL((void *)0),
391 input_state_csi_intermediate_table
392};
393
394/* csi_ignore state definition. */
395static const struct input_state input_state_csi_ignore = {
396 "csi_ignore",
397 NULL((void *)0), NULL((void *)0),
398 input_state_csi_ignore_table
399};
400
401/* dcs_enter state definition. */
402static const struct input_state input_state_dcs_enter = {
403 "dcs_enter",
404 input_enter_dcs, NULL((void *)0),
405 input_state_dcs_enter_table
406};
407
408/* dcs_parameter state definition. */
409static const struct input_state input_state_dcs_parameter = {
410 "dcs_parameter",
411 NULL((void *)0), NULL((void *)0),
412 input_state_dcs_parameter_table
413};
414
415/* dcs_intermediate state definition. */
416static const struct input_state input_state_dcs_intermediate = {
417 "dcs_intermediate",
418 NULL((void *)0), NULL((void *)0),
419 input_state_dcs_intermediate_table
420};
421
422/* dcs_handler state definition. */
423static const struct input_state input_state_dcs_handler = {
424 "dcs_handler",
425 NULL((void *)0), NULL((void *)0),
426 input_state_dcs_handler_table
427};
428
429/* dcs_escape state definition. */
430static const struct input_state input_state_dcs_escape = {
431 "dcs_escape",
432 NULL((void *)0), NULL((void *)0),
433 input_state_dcs_escape_table
434};
435
436/* dcs_ignore state definition. */
437static const struct input_state input_state_dcs_ignore = {
438 "dcs_ignore",
439 NULL((void *)0), NULL((void *)0),
440 input_state_dcs_ignore_table
441};
442
443/* osc_string state definition. */
444static const struct input_state input_state_osc_string = {
445 "osc_string",
446 input_enter_osc, input_exit_osc,
447 input_state_osc_string_table
448};
449
450/* apc_string state definition. */
451static const struct input_state input_state_apc_string = {
452 "apc_string",
453 input_enter_apc, input_exit_apc,
454 input_state_apc_string_table
455};
456
457/* rename_string state definition. */
458static const struct input_state input_state_rename_string = {
459 "rename_string",
460 input_enter_rename, input_exit_rename,
461 input_state_rename_string_table
462};
463
464/* consume_st state definition. */
465static const struct input_state input_state_consume_st = {
466 "consume_st",
467 input_enter_rename, NULL((void *)0), /* rename also waits for ST */
468 input_state_consume_st_table
469};
470
471/* ground state table. */
472static const struct input_transition input_state_ground_table[] = {
473 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
474
475 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
476 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
477 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
478 { 0x20, 0x7e, input_print, NULL((void *)0) },
479 { 0x7f, 0x7f, NULL((void *)0), NULL((void *)0) },
480 { 0x80, 0xff, input_top_bit_set, NULL((void *)0) },
481
482 { -1, -1, NULL((void *)0), NULL((void *)0) }
483};
484
485/* esc_enter state table. */
486static const struct input_transition input_state_esc_enter_table[] = {
487 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
488
489 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
490 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
491 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
492 { 0x20, 0x2f, input_intermediate, &input_state_esc_intermediate },
493 { 0x30, 0x4f, input_esc_dispatch, &input_state_ground },
494 { 0x50, 0x50, NULL((void *)0), &input_state_dcs_enter },
495 { 0x51, 0x57, input_esc_dispatch, &input_state_ground },
496 { 0x58, 0x58, NULL((void *)0), &input_state_consume_st },
497 { 0x59, 0x59, input_esc_dispatch, &input_state_ground },
498 { 0x5a, 0x5a, input_esc_dispatch, &input_state_ground },
499 { 0x5b, 0x5b, NULL((void *)0), &input_state_csi_enter },
500 { 0x5c, 0x5c, input_esc_dispatch, &input_state_ground },
501 { 0x5d, 0x5d, NULL((void *)0), &input_state_osc_string },
502 { 0x5e, 0x5e, NULL((void *)0), &input_state_consume_st },
503 { 0x5f, 0x5f, NULL((void *)0), &input_state_apc_string },
504 { 0x60, 0x6a, input_esc_dispatch, &input_state_ground },
505 { 0x6b, 0x6b, NULL((void *)0), &input_state_rename_string },
506 { 0x6c, 0x7e, input_esc_dispatch, &input_state_ground },
507 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
508
509 { -1, -1, NULL((void *)0), NULL((void *)0) }
510};
511
512/* esc_intermediate state table. */
513static const struct input_transition input_state_esc_intermediate_table[] = {
514 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
515
516 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
517 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
518 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
519 { 0x20, 0x2f, input_intermediate, NULL((void *)0) },
520 { 0x30, 0x7e, input_esc_dispatch, &input_state_ground },
521 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
522
523 { -1, -1, NULL((void *)0), NULL((void *)0) }
524};
525
526/* csi_enter state table. */
527static const struct input_transition input_state_csi_enter_table[] = {
528 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
529
530 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
531 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
532 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
533 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
534 { 0x30, 0x39, input_parameter, &input_state_csi_parameter },
535 { 0x3a, 0x3a, input_parameter, &input_state_csi_parameter },
536 { 0x3b, 0x3b, input_parameter, &input_state_csi_parameter },
537 { 0x3c, 0x3f, input_intermediate, &input_state_csi_parameter },
538 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
539 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
540
541 { -1, -1, NULL((void *)0), NULL((void *)0) }
542};
543
544/* csi_parameter state table. */
545static const struct input_transition input_state_csi_parameter_table[] = {
546 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
547
548 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
549 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
550 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
551 { 0x20, 0x2f, input_intermediate, &input_state_csi_intermediate },
552 { 0x30, 0x39, input_parameter, NULL((void *)0) },
553 { 0x3a, 0x3a, input_parameter, NULL((void *)0) },
554 { 0x3b, 0x3b, input_parameter, NULL((void *)0) },
555 { 0x3c, 0x3f, NULL((void *)0), &input_state_csi_ignore },
556 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
557 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
558
559 { -1, -1, NULL((void *)0), NULL((void *)0) }
560};
561
562/* csi_intermediate state table. */
563static const struct input_transition input_state_csi_intermediate_table[] = {
564 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
565
566 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
567 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
568 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
569 { 0x20, 0x2f, input_intermediate, NULL((void *)0) },
570 { 0x30, 0x3f, NULL((void *)0), &input_state_csi_ignore },
571 { 0x40, 0x7e, input_csi_dispatch, &input_state_ground },
572 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
573
574 { -1, -1, NULL((void *)0), NULL((void *)0) }
575};
576
577/* csi_ignore state table. */
578static const struct input_transition input_state_csi_ignore_table[] = {
579 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
580
581 { 0x00, 0x17, input_c0_dispatch, NULL((void *)0) },
582 { 0x19, 0x19, input_c0_dispatch, NULL((void *)0) },
583 { 0x1c, 0x1f, input_c0_dispatch, NULL((void *)0) },
584 { 0x20, 0x3f, NULL((void *)0), NULL((void *)0) },
585 { 0x40, 0x7e, NULL((void *)0), &input_state_ground },
586 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
587
588 { -1, -1, NULL((void *)0), NULL((void *)0) }
589};
590
591/* dcs_enter state table. */
592static const struct input_transition input_state_dcs_enter_table[] = {
593 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
594
595 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
596 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
597 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
598 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
599 { 0x30, 0x39, input_parameter, &input_state_dcs_parameter },
600 { 0x3a, 0x3a, NULL((void *)0), &input_state_dcs_ignore },
601 { 0x3b, 0x3b, input_parameter, &input_state_dcs_parameter },
602 { 0x3c, 0x3f, input_intermediate, &input_state_dcs_parameter },
603 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
604 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
605
606 { -1, -1, NULL((void *)0), NULL((void *)0) }
607};
608
609/* dcs_parameter state table. */
610static const struct input_transition input_state_dcs_parameter_table[] = {
611 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
612
613 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
614 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
615 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
616 { 0x20, 0x2f, input_intermediate, &input_state_dcs_intermediate },
617 { 0x30, 0x39, input_parameter, NULL((void *)0) },
618 { 0x3a, 0x3a, NULL((void *)0), &input_state_dcs_ignore },
619 { 0x3b, 0x3b, input_parameter, NULL((void *)0) },
620 { 0x3c, 0x3f, NULL((void *)0), &input_state_dcs_ignore },
621 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
622 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
623
624 { -1, -1, NULL((void *)0), NULL((void *)0) }
625};
626
627/* dcs_intermediate state table. */
628static const struct input_transition input_state_dcs_intermediate_table[] = {
629 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
630
631 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
632 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
633 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
634 { 0x20, 0x2f, input_intermediate, NULL((void *)0) },
635 { 0x30, 0x3f, NULL((void *)0), &input_state_dcs_ignore },
636 { 0x40, 0x7e, input_input, &input_state_dcs_handler },
637 { 0x7f, 0xff, NULL((void *)0), NULL((void *)0) },
638
639 { -1, -1, NULL((void *)0), NULL((void *)0) }
640};
641
642/* dcs_handler state table. */
643static const struct input_transition input_state_dcs_handler_table[] = {
644 /* No INPUT_STATE_ANYWHERE */
645
646 { 0x00, 0x1a, input_input, NULL((void *)0) },
647 { 0x1b, 0x1b, NULL((void *)0), &input_state_dcs_escape },
648 { 0x1c, 0xff, input_input, NULL((void *)0) },
649
650 { -1, -1, NULL((void *)0), NULL((void *)0) }
651};
652
653/* dcs_escape state table. */
654static const struct input_transition input_state_dcs_escape_table[] = {
655 /* No INPUT_STATE_ANYWHERE */
656
657 { 0x00, 0x5b, input_input, &input_state_dcs_handler },
658 { 0x5c, 0x5c, input_dcs_dispatch, &input_state_ground },
659 { 0x5d, 0xff, input_input, &input_state_dcs_handler },
660
661 { -1, -1, NULL((void *)0), NULL((void *)0) }
662};
663
664/* dcs_ignore state table. */
665static const struct input_transition input_state_dcs_ignore_table[] = {
666 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
667
668 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
669 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
670 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
671 { 0x20, 0xff, NULL((void *)0), NULL((void *)0) },
672
673 { -1, -1, NULL((void *)0), NULL((void *)0) }
674};
675
676/* osc_string state table. */
677static const struct input_transition input_state_osc_string_table[] = {
678 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
679
680 { 0x00, 0x06, NULL((void *)0), NULL((void *)0) },
681 { 0x07, 0x07, input_end_bel, &input_state_ground },
682 { 0x08, 0x17, NULL((void *)0), NULL((void *)0) },
683 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
684 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
685 { 0x20, 0xff, input_input, NULL((void *)0) },
686
687 { -1, -1, NULL((void *)0), NULL((void *)0) }
688};
689
690/* apc_string state table. */
691static const struct input_transition input_state_apc_string_table[] = {
692 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
693
694 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
695 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
696 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
697 { 0x20, 0xff, input_input, NULL((void *)0) },
698
699 { -1, -1, NULL((void *)0), NULL((void *)0) }
700};
701
702/* rename_string state table. */
703static const struct input_transition input_state_rename_string_table[] = {
704 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
705
706 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
707 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
708 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
709 { 0x20, 0xff, input_input, NULL((void *)0) },
710
711 { -1, -1, NULL((void *)0), NULL((void *)0) }
712};
713
714/* consume_st state table. */
715static const struct input_transition input_state_consume_st_table[] = {
716 INPUT_STATE_ANYWHERE{ 0x18, 0x18, input_c0_dispatch, &input_state_ground }, {
0x1a, 0x1a, input_c0_dispatch, &input_state_ground }, { 0x1b
, 0x1b, ((void *)0), &input_state_esc_enter }
,
717
718 { 0x00, 0x17, NULL((void *)0), NULL((void *)0) },
719 { 0x19, 0x19, NULL((void *)0), NULL((void *)0) },
720 { 0x1c, 0x1f, NULL((void *)0), NULL((void *)0) },
721 { 0x20, 0xff, NULL((void *)0), NULL((void *)0) },
722
723 { -1, -1, NULL((void *)0), NULL((void *)0) }
724};
725
726/* Input table compare. */
727static int
728input_table_compare(const void *key, const void *value)
729{
730 const struct input_ctx *ictx = key;
731 const struct input_table_entry *entry = value;
732
733 if (ictx->ch != entry->ch)
734 return (ictx->ch - entry->ch);
735 return (strcmp(ictx->interm_buf, entry->interm));
736}
737
738/*
739 * Timer - if this expires then have been waiting for a terminator for too
740 * long, so reset to ground.
741 */
742static void
743input_timer_callback(__unused__attribute__((__unused__)) int fd, __unused__attribute__((__unused__)) short events, void *arg)
744{
745 struct input_ctx *ictx = arg;
746
747 log_debug("%s: %s expired" , __func__, ictx->state->name);
748 input_reset(ictx, 0);
749}
750
751/* Start the timer. */
752static void
753input_start_timer(struct input_ctx *ictx)
754{
755 struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
756
757 event_del(&ictx->timer);
758 event_add(&ictx->timer, &tv);
759}
760
761/* Reset cell state to default. */
762static void
763input_reset_cell(struct input_ctx *ictx)
764{
765 memcpy(&ictx->cell.cell, &grid_default_cell, sizeof ictx->cell.cell);
766 ictx->cell.set = 0;
767 ictx->cell.g0set = ictx->cell.g1set = 0;
768
769 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
770 ictx->old_cx = 0;
771 ictx->old_cy = 0;
772}
773
774/* Save screen state. */
775static void
776input_save_state(struct input_ctx *ictx)
777{
778 struct screen_write_ctx *sctx = &ictx->ctx;
779 struct screen *s = sctx->s;
780
781 memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell);
782 ictx->old_cx = s->cx;
783 ictx->old_cy = s->cy;
784 ictx->old_mode = s->mode;
785}
786
787/* Restore screen state. */
788static void
789input_restore_state(struct input_ctx *ictx)
790{
791 struct screen_write_ctx *sctx = &ictx->ctx;
792
793 memcpy(&ictx->cell, &ictx->old_cell, sizeof ictx->cell);
794 if (ictx->old_mode & MODE_ORIGIN0x2000)
795 screen_write_mode_set(sctx, MODE_ORIGIN0x2000);
796 else
797 screen_write_mode_clear(sctx, MODE_ORIGIN0x2000);
798 screen_write_cursormove(sctx, ictx->old_cx, ictx->old_cy, 0);
799}
800
801/* Initialise input parser. */
802struct input_ctx *
803input_init(struct window_pane *wp, struct bufferevent *bev,
804 struct colour_palette *palette)
805{
806 struct input_ctx *ictx;
807
808 ictx = xcalloc(1, sizeof *ictx);
809 ictx->wp = wp;
810 ictx->event = bev;
811 ictx->palette = palette;
812
813 ictx->input_space = INPUT_BUF_START32;
814 ictx->input_buf = xmalloc(INPUT_BUF_START32);
815
816 ictx->since_ground = evbuffer_new();
817 if (ictx->since_ground == NULL((void *)0))
818 fatalx("out of memory");
819
820 evtimer_set(&ictx->timer, input_timer_callback, ictx)event_set(&ictx->timer, -1, 0, input_timer_callback, ictx
)
;
821
822 input_reset(ictx, 0);
823 return (ictx);
824}
825
826/* Destroy input parser. */
827void
828input_free(struct input_ctx *ictx)
829{
830 u_int i;
831
832 for (i = 0; i < ictx->param_list_len; i++) {
833 if (ictx->param_list[i].type == INPUT_STRING)
834 free(ictx->param_list[i].str);
835 }
836
837 event_del(&ictx->timer);
838
839 free(ictx->input_buf);
840 evbuffer_free(ictx->since_ground);
841
842 free(ictx);
843}
844
845/* Reset input state and clear screen. */
846void
847input_reset(struct input_ctx *ictx, int clear)
848{
849 struct screen_write_ctx *sctx = &ictx->ctx;
850 struct window_pane *wp = ictx->wp;
851
852 input_reset_cell(ictx);
853
854 if (clear && wp != NULL((void *)0)) {
855 if (TAILQ_EMPTY(&wp->modes)(((&wp->modes)->tqh_first) == ((void *)0)))
856 screen_write_start_pane(sctx, wp, &wp->base);
857 else
858 screen_write_start(sctx, &wp->base);
859 screen_write_reset(sctx);
860 screen_write_stop(sctx);
861 }
862
863 input_clear(ictx);
864
865 ictx->last = -1;
866
867 ictx->state = &input_state_ground;
868 ictx->flags = 0;
869}
870
871/* Return pending data. */
872struct evbuffer *
873input_pending(struct input_ctx *ictx)
874{
875 return (ictx->since_ground);
876}
877
878/* Change input state. */
879static void
880input_set_state(struct input_ctx *ictx, const struct input_transition *itr)
881{
882 if (ictx->state->exit != NULL((void *)0))
883 ictx->state->exit(ictx);
884 ictx->state = itr->state;
885 if (ictx->state->enter != NULL((void *)0))
886 ictx->state->enter(ictx);
887}
888
889/* Parse data. */
890static void
891input_parse(struct input_ctx *ictx, u_char *buf, size_t len)
892{
893 struct screen_write_ctx *sctx = &ictx->ctx;
894 const struct input_state *state = NULL((void *)0);
895 const struct input_transition *itr = NULL((void *)0);
896 size_t off = 0;
897
898 /* Parse the input. */
899 while (off
4.1
'off' is < 'len'
< len) {
5
Loop condition is true. Entering loop body
900 ictx->ch = buf[off++];
901
902 /* Find the transition. */
903 if (ictx->state != state ||
6
Assuming 'state' is equal to field 'state'
904 itr
6.1
'itr' is equal to NULL
== NULL((void *)0) ||
905 ictx->ch < itr->first ||
906 ictx->ch > itr->last) {
907 itr = ictx->state->transitions;
7
Access to field 'transitions' results in a dereference of a null pointer (loaded from field 'state')
908 while (itr->first != -1 && itr->last != -1) {
909 if (ictx->ch >= itr->first &&
910 ictx->ch <= itr->last)
911 break;
912 itr++;
913 }
914 if (itr->first == -1 || itr->last == -1) {
915 /* No transition? Eh? */
916 fatalx("no transition from state");
917 }
918 }
919 state = ictx->state;
920
921 /*
922 * Any state except print stops the current collection. This is
923 * an optimization to avoid checking if the attributes have
924 * changed for every character. It will stop unnecessarily for
925 * sequences that don't make a terminal change, but they should
926 * be the minority.
927 */
928 if (itr->handler != input_print)
929 screen_write_collect_end(sctx);
930
931 /*
932 * Execute the handler, if any. Don't switch state if it
933 * returns non-zero.
934 */
935 if (itr->handler != NULL((void *)0) && itr->handler(ictx) != 0)
936 continue;
937
938 /* And switch state, if necessary. */
939 if (itr->state != NULL((void *)0))
940 input_set_state(ictx, itr);
941
942 /* If not in ground state, save input. */
943 if (ictx->state != &input_state_ground)
944 evbuffer_add(ictx->since_ground, &ictx->ch, 1);
945 }
946}
947
948/* Parse input from pane. */
949void
950input_parse_pane(struct window_pane *wp)
951{
952 void *new_data;
953 size_t new_size;
954
955 new_data = window_pane_get_new_data(wp, &wp->offset, &new_size);
956 input_parse_buffer(wp, new_data, new_size);
957 window_pane_update_used_data(wp, &wp->offset, new_size);
958}
959
960/* Parse given input. */
961void
962input_parse_buffer(struct window_pane *wp, u_char *buf, size_t len)
963{
964 struct input_ctx *ictx = wp->ictx;
965 struct screen_write_ctx *sctx = &ictx->ctx;
966
967 if (len == 0)
968 return;
969
970 window_update_activity(wp->window);
971 wp->flags |= PANE_CHANGED0x80;
972
973 /* NULL wp if there is a mode set as don't want to update the tty. */
974 if (TAILQ_EMPTY(&wp->modes)(((&wp->modes)->tqh_first) == ((void *)0)))
975 screen_write_start_pane(sctx, wp, &wp->base);
976 else
977 screen_write_start(sctx, &wp->base);
978
979 log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id,
980 ictx->state->name, len, (int)len, buf);
981
982 input_parse(ictx, buf, len);
983 screen_write_stop(sctx);
984}
985
986/* Parse given input for screen. */
987void
988input_parse_screen(struct input_ctx *ictx, struct screen *s,
989 screen_write_init_ctx_cb cb, void *arg, u_char *buf, size_t len)
990{
991 struct screen_write_ctx *sctx = &ictx->ctx;
992
993 if (len == 0)
1
Assuming 'len' is not equal to 0
2
Taking false branch
994 return;
995
996 screen_write_start_callback(sctx, s, cb, arg);
3
Value assigned to field 'state'
997 input_parse(ictx, buf, len);
4
Calling 'input_parse'
998 screen_write_stop(sctx);
999}
1000
1001/* Split the parameter list (if any). */
1002static int
1003input_split(struct input_ctx *ictx)
1004{
1005 const char *errstr;
1006 char *ptr, *out;
1007 struct input_param *ip;
1008 u_int i;
1009
1010 for (i = 0; i < ictx->param_list_len; i++) {
1011 if (ictx->param_list[i].type == INPUT_STRING)
1012 free(ictx->param_list[i].str);
1013 }
1014 ictx->param_list_len = 0;
1015
1016 if (ictx->param_len == 0)
1017 return (0);
1018 ip = &ictx->param_list[0];
1019
1020 ptr = ictx->param_buf;
1021 while ((out = strsep(&ptr, ";")) != NULL((void *)0)) {
1022 if (*out == '\0')
1023 ip->type = INPUT_MISSING;
1024 else {
1025 if (strchr(out, ':') != NULL((void *)0)) {
1026 ip->type = INPUT_STRING;
1027 ip->str = xstrdup(out);
1028 } else {
1029 ip->type = INPUT_NUMBER;
1030 ip->num = strtonum(out, 0, INT_MAX2147483647, &errstr);
1031 if (errstr != NULL((void *)0))
1032 return (-1);
1033 }
1034 }
1035 ip = &ictx->param_list[++ictx->param_list_len];
1036 if (ictx->param_list_len == nitems(ictx->param_list)(sizeof((ictx->param_list)) / sizeof((ictx->param_list)
[0]))
)
1037 return (-1);
1038 }
1039
1040 for (i = 0; i < ictx->param_list_len; i++) {
1041 ip = &ictx->param_list[i];
1042 if (ip->type == INPUT_MISSING)
1043 log_debug("parameter %u: missing", i);
1044 else if (ip->type == INPUT_STRING)
1045 log_debug("parameter %u: string %s", i, ip->str);
1046 else if (ip->type == INPUT_NUMBER)
1047 log_debug("parameter %u: number %d", i, ip->num);
1048 }
1049
1050 return (0);
1051}
1052
1053/* Get an argument or return default value. */
1054static int
1055input_get(struct input_ctx *ictx, u_int validx, int minval, int defval)
1056{
1057 struct input_param *ip;
1058 int retval;
1059
1060 if (validx >= ictx->param_list_len)
1061 return (defval);
1062 ip = &ictx->param_list[validx];
1063 if (ip->type == INPUT_MISSING)
1064 return (defval);
1065 if (ip->type == INPUT_STRING)
1066 return (-1);
1067 retval = ip->num;
1068 if (retval < minval)
1069 return (minval);
1070 return (retval);
1071}
1072
1073/* Reply to terminal query. */
1074static void
1075input_reply(struct input_ctx *ictx, const char *fmt, ...)
1076{
1077 struct bufferevent *bev = ictx->event;
1078 va_list ap;
1079 char *reply;
1080
1081 va_start(ap, fmt)__builtin_va_start(ap, fmt);
1082 xvasprintf(&reply, fmt, ap);
1083 va_end(ap)__builtin_va_end(ap);
1084
1085 bufferevent_write(bev, reply, strlen(reply));
1086 free(reply);
1087}
1088
1089/* Clear saved state. */
1090static void
1091input_clear(struct input_ctx *ictx)
1092{
1093 event_del(&ictx->timer);
1094
1095 *ictx->interm_buf = '\0';
1096 ictx->interm_len = 0;
1097
1098 *ictx->param_buf = '\0';
1099 ictx->param_len = 0;
1100
1101 *ictx->input_buf = '\0';
1102 ictx->input_len = 0;
1103
1104 ictx->input_end = INPUT_END_ST;
1105
1106 ictx->flags &= ~INPUT_DISCARD0x1;
1107}
1108
1109/* Reset for ground state. */
1110static void
1111input_ground(struct input_ctx *ictx)
1112{
1113 event_del(&ictx->timer);
1114 evbuffer_drain(ictx->since_ground, EVBUFFER_LENGTH(ictx->since_ground)(ictx->since_ground)->off);
1115
1116 if (ictx->input_space > INPUT_BUF_START32) {
1117 ictx->input_space = INPUT_BUF_START32;
1118 ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START32);
1119 }
1120}
1121
1122/* Output this character to the screen. */
1123static int
1124input_print(struct input_ctx *ictx)
1125{
1126 struct screen_write_ctx *sctx = &ictx->ctx;
1127 int set;
1128
1129 ictx->utf8started = 0; /* can't be valid UTF-8 */
1130
1131 set = ictx->cell.set == 0 ? ictx->cell.g0set : ictx->cell.g1set;
1132 if (set == 1)
1133 ictx->cell.cell.attr |= GRID_ATTR_CHARSET0x80;
1134 else
1135 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET0x80;
1136
1137 utf8_set(&ictx->cell.cell.data, ictx->ch);
1138 screen_write_collect_add(sctx, &ictx->cell.cell);
1139 ictx->last = ictx->ch;
1140
1141 ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET0x80;
1142
1143 return (0);
1144}
1145
1146/* Collect intermediate string. */
1147static int
1148input_intermediate(struct input_ctx *ictx)
1149{
1150 if (ictx->interm_len == (sizeof ictx->interm_buf) - 1)
1151 ictx->flags |= INPUT_DISCARD0x1;
1152 else {
1153 ictx->interm_buf[ictx->interm_len++] = ictx->ch;
1154 ictx->interm_buf[ictx->interm_len] = '\0';
1155 }
1156
1157 return (0);
1158}
1159
1160/* Collect parameter string. */
1161static int
1162input_parameter(struct input_ctx *ictx)
1163{
1164 if (ictx->param_len == (sizeof ictx->param_buf) - 1)
1165 ictx->flags |= INPUT_DISCARD0x1;
1166 else {
1167 ictx->param_buf[ictx->param_len++] = ictx->ch;
1168 ictx->param_buf[ictx->param_len] = '\0';
1169 }
1170
1171 return (0);
1172}
1173
1174/* Collect input string. */
1175static int
1176input_input(struct input_ctx *ictx)
1177{
1178 size_t available;
1179
1180 available = ictx->input_space;
1181 while (ictx->input_len + 1 >= available) {
1182 available *= 2;
1183 if (available > INPUT_BUF_LIMIT1048576) {
1184 ictx->flags |= INPUT_DISCARD0x1;
1185 return (0);
1186 }
1187 ictx->input_buf = xrealloc(ictx->input_buf, available);
1188 ictx->input_space = available;
1189 }
1190 ictx->input_buf[ictx->input_len++] = ictx->ch;
1191 ictx->input_buf[ictx->input_len] = '\0';
1192
1193 return (0);
1194}
1195
1196/* Execute C0 control sequence. */
1197static int
1198input_c0_dispatch(struct input_ctx *ictx)
1199{
1200 struct screen_write_ctx *sctx = &ictx->ctx;
1201 struct window_pane *wp = ictx->wp;
1202 struct screen *s = sctx->s;
1203
1204 ictx->utf8started = 0; /* can't be valid UTF-8 */
1205
1206 log_debug("%s: '%c'", __func__, ictx->ch);
1207
1208 switch (ictx->ch) {
1209 case '\000': /* NUL */
1210 break;
1211 case '\007': /* BEL */
1212 if (wp != NULL((void *)0))
1213 alerts_queue(wp->window, WINDOW_BELL0x1);
1214 break;
1215 case '\010': /* BS */
1216 screen_write_backspace(sctx);
1217 break;
1218 case '\011': /* HT */
1219 /* Don't tab beyond the end of the line. */
1220 if (s->cx >= screen_size_x(s)((s)->grid->sx) - 1)
1221 break;
1222
1223 /* Find the next tab point, or use the last column if none. */
1224 do {
1225 s->cx++;
1226 if (bit_test(s->tabs, s->cx)((s->tabs)[((s->cx) >> 3)] & (1 << ((s->
cx)&0x7)))
)
1227 break;
1228 } while (s->cx < screen_size_x(s)((s)->grid->sx) - 1);
1229 break;
1230 case '\012': /* LF */
1231 case '\013': /* VT */
1232 case '\014': /* FF */
1233 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1234 if (s->mode & MODE_CRLF0x4000)
1235 screen_write_carriagereturn(sctx);
1236 break;
1237 case '\015': /* CR */
1238 screen_write_carriagereturn(sctx);
1239 break;
1240 case '\016': /* SO */
1241 ictx->cell.set = 1;
1242 break;
1243 case '\017': /* SI */
1244 ictx->cell.set = 0;
1245 break;
1246 default:
1247 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1248 break;
1249 }
1250
1251 ictx->last = -1;
1252 return (0);
1253}
1254
1255/* Execute escape sequence. */
1256static int
1257input_esc_dispatch(struct input_ctx *ictx)
1258{
1259 struct screen_write_ctx *sctx = &ictx->ctx;
1260 struct screen *s = sctx->s;
1261 struct input_table_entry *entry;
1262
1263 if (ictx->flags & INPUT_DISCARD0x1)
1264 return (0);
1265 log_debug("%s: '%c', %s", __func__, ictx->ch, ictx->interm_buf);
1266
1267 entry = bsearch(ictx, input_esc_table, nitems(input_esc_table)(sizeof((input_esc_table)) / sizeof((input_esc_table)[0])),
1268 sizeof input_esc_table[0], input_table_compare);
1269 if (entry == NULL((void *)0)) {
1270 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1271 return (0);
1272 }
1273
1274 switch (entry->type) {
1275 case INPUT_ESC_RIS:
1276 colour_palette_clear(ictx->palette);
1277 input_reset_cell(ictx);
1278 screen_write_reset(sctx);
1279 screen_write_fullredraw(sctx);
1280 break;
1281 case INPUT_ESC_IND:
1282 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1283 break;
1284 case INPUT_ESC_NEL:
1285 screen_write_carriagereturn(sctx);
1286 screen_write_linefeed(sctx, 0, ictx->cell.cell.bg);
1287 break;
1288 case INPUT_ESC_HTS:
1289 if (s->cx < screen_size_x(s)((s)->grid->sx))
1290 bit_set(s->tabs, s->cx)((s->tabs)[((s->cx) >> 3)] |= (1 << ((s->
cx)&0x7)))
;
1291 break;
1292 case INPUT_ESC_RI:
1293 screen_write_reverseindex(sctx, ictx->cell.cell.bg);
1294 break;
1295 case INPUT_ESC_DECKPAM:
1296 screen_write_mode_set(sctx, MODE_KKEYPAD0x8);
1297 break;
1298 case INPUT_ESC_DECKPNM:
1299 screen_write_mode_clear(sctx, MODE_KKEYPAD0x8);
1300 break;
1301 case INPUT_ESC_DECSC:
1302 input_save_state(ictx);
1303 break;
1304 case INPUT_ESC_DECRC:
1305 input_restore_state(ictx);
1306 break;
1307 case INPUT_ESC_DECALN:
1308 screen_write_alignmenttest(sctx);
1309 break;
1310 case INPUT_ESC_SCSG0_ON:
1311 ictx->cell.g0set = 1;
1312 break;
1313 case INPUT_ESC_SCSG0_OFF:
1314 ictx->cell.g0set = 0;
1315 break;
1316 case INPUT_ESC_SCSG1_ON:
1317 ictx->cell.g1set = 1;
1318 break;
1319 case INPUT_ESC_SCSG1_OFF:
1320 ictx->cell.g1set = 0;
1321 break;
1322 case INPUT_ESC_ST:
1323 /* ST terminates OSC but the state transition already did it. */
1324 break;
1325 }
1326
1327 ictx->last = -1;
1328 return (0);
1329}
1330
1331/* Execute control sequence. */
1332static int
1333input_csi_dispatch(struct input_ctx *ictx)
1334{
1335 struct screen_write_ctx *sctx = &ictx->ctx;
1336 struct screen *s = sctx->s;
1337 struct input_table_entry *entry;
1338 int i, n, m;
1339 u_int cx, bg = ictx->cell.cell.bg;
1340
1341 if (ictx->flags & INPUT_DISCARD0x1)
1342 return (0);
1343
1344 log_debug("%s: '%c' \"%s\" \"%s\"",
1345 __func__, ictx->ch, ictx->interm_buf, ictx->param_buf);
1346
1347 if (input_split(ictx) != 0)
1348 return (0);
1349
1350 entry = bsearch(ictx, input_csi_table, nitems(input_csi_table)(sizeof((input_csi_table)) / sizeof((input_csi_table)[0])),
1351 sizeof input_csi_table[0], input_table_compare);
1352 if (entry == NULL((void *)0)) {
1353 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1354 return (0);
1355 }
1356
1357 switch (entry->type) {
1358 case INPUT_CSI_CBT:
1359 /* Find the previous tab point, n times. */
1360 cx = s->cx;
1361 if (cx > screen_size_x(s)((s)->grid->sx) - 1)
1362 cx = screen_size_x(s)((s)->grid->sx) - 1;
1363 n = input_get(ictx, 0, 1, 1);
1364 if (n == -1)
1365 break;
1366 while (cx > 0 && n-- > 0) {
1367 do
1368 cx--;
1369 while (cx > 0 && !bit_test(s->tabs, cx)((s->tabs)[((cx) >> 3)] & (1 << ((cx)&
0x7)))
);
1370 }
1371 s->cx = cx;
1372 break;
1373 case INPUT_CSI_CUB:
1374 n = input_get(ictx, 0, 1, 1);
1375 if (n != -1)
1376 screen_write_cursorleft(sctx, n);
1377 break;
1378 case INPUT_CSI_CUD:
1379 n = input_get(ictx, 0, 1, 1);
1380 if (n != -1)
1381 screen_write_cursordown(sctx, n);
1382 break;
1383 case INPUT_CSI_CUF:
1384 n = input_get(ictx, 0, 1, 1);
1385 if (n != -1)
1386 screen_write_cursorright(sctx, n);
1387 break;
1388 case INPUT_CSI_CUP:
1389 n = input_get(ictx, 0, 1, 1);
1390 m = input_get(ictx, 1, 1, 1);
1391 if (n != -1 && m != -1)
1392 screen_write_cursormove(sctx, m - 1, n - 1, 1);
1393 break;
1394 case INPUT_CSI_MODSET:
1395 n = input_get(ictx, 0, 0, 0);
1396 m = input_get(ictx, 1, 0, 0);
1397 if (options_get_number(global_options, "extended-keys") == 2)
1398 break;
1399 if (n == 0 || (n == 4 && m == 0))
1400 screen_write_mode_clear(sctx, MODE_KEXTENDED0x8000);
1401 else if (n == 4 && (m == 1 || m == 2))
1402 screen_write_mode_set(sctx, MODE_KEXTENDED0x8000);
1403 break;
1404 case INPUT_CSI_MODOFF:
1405 n = input_get(ictx, 0, 0, 0);
1406 if (n == 4)
1407 screen_write_mode_clear(sctx, MODE_KEXTENDED0x8000);
1408 break;
1409 case INPUT_CSI_WINOPS:
1410 input_csi_dispatch_winops(ictx);
1411 break;
1412 case INPUT_CSI_CUU:
1413 n = input_get(ictx, 0, 1, 1);
1414 if (n != -1)
1415 screen_write_cursorup(sctx, n);
1416 break;
1417 case INPUT_CSI_CNL:
1418 n = input_get(ictx, 0, 1, 1);
1419 if (n != -1) {
1420 screen_write_carriagereturn(sctx);
1421 screen_write_cursordown(sctx, n);
1422 }
1423 break;
1424 case INPUT_CSI_CPL:
1425 n = input_get(ictx, 0, 1, 1);
1426 if (n != -1) {
1427 screen_write_carriagereturn(sctx);
1428 screen_write_cursorup(sctx, n);
1429 }
1430 break;
1431 case INPUT_CSI_DA:
1432 switch (input_get(ictx, 0, 0, 0)) {
1433 case -1:
1434 break;
1435 case 0:
1436 input_reply(ictx, "\033[?1;2c");
1437 break;
1438 default:
1439 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1440 break;
1441 }
1442 break;
1443 case INPUT_CSI_DA_TWO:
1444 switch (input_get(ictx, 0, 0, 0)) {
1445 case -1:
1446 break;
1447 case 0:
1448 input_reply(ictx, "\033[>84;0;0c");
1449 break;
1450 default:
1451 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1452 break;
1453 }
1454 break;
1455 case INPUT_CSI_ECH:
1456 n = input_get(ictx, 0, 1, 1);
1457 if (n != -1)
1458 screen_write_clearcharacter(sctx, n, bg);
1459 break;
1460 case INPUT_CSI_DCH:
1461 n = input_get(ictx, 0, 1, 1);
1462 if (n != -1)
1463 screen_write_deletecharacter(sctx, n, bg);
1464 break;
1465 case INPUT_CSI_DECSTBM:
1466 n = input_get(ictx, 0, 1, 1);
1467 m = input_get(ictx, 1, 1, screen_size_y(s)((s)->grid->sy));
1468 if (n != -1 && m != -1)
1469 screen_write_scrollregion(sctx, n - 1, m - 1);
1470 break;
1471 case INPUT_CSI_DL:
1472 n = input_get(ictx, 0, 1, 1);
1473 if (n != -1)
1474 screen_write_deleteline(sctx, n, bg);
1475 break;
1476 case INPUT_CSI_DSR:
1477 switch (input_get(ictx, 0, 0, 0)) {
1478 case -1:
1479 break;
1480 case 5:
1481 input_reply(ictx, "\033[0n");
1482 break;
1483 case 6:
1484 input_reply(ictx, "\033[%u;%uR", s->cy + 1, s->cx + 1);
1485 break;
1486 default:
1487 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1488 break;
1489 }
1490 break;
1491 case INPUT_CSI_ED:
1492 switch (input_get(ictx, 0, 0, 0)) {
1493 case -1:
1494 break;
1495 case 0:
1496 screen_write_clearendofscreen(sctx, bg);
1497 break;
1498 case 1:
1499 screen_write_clearstartofscreen(sctx, bg);
1500 break;
1501 case 2:
1502 screen_write_clearscreen(sctx, bg);
1503 break;
1504 case 3:
1505 if (input_get(ictx, 1, 0, 0) == 0) {
1506 /*
1507 * Linux console extension to clear history
1508 * (for example before locking the screen).
1509 */
1510 screen_write_clearhistory(sctx);
1511 }
1512 break;
1513 default:
1514 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1515 break;
1516 }
1517 break;
1518 case INPUT_CSI_EL:
1519 switch (input_get(ictx, 0, 0, 0)) {
1520 case -1:
1521 break;
1522 case 0:
1523 screen_write_clearendofline(sctx, bg);
1524 break;
1525 case 1:
1526 screen_write_clearstartofline(sctx, bg);
1527 break;
1528 case 2:
1529 screen_write_clearline(sctx, bg);
1530 break;
1531 default:
1532 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1533 break;
1534 }
1535 break;
1536 case INPUT_CSI_HPA:
1537 n = input_get(ictx, 0, 1, 1);
1538 if (n != -1)
1539 screen_write_cursormove(sctx, n - 1, -1, 1);
1540 break;
1541 case INPUT_CSI_ICH:
1542 n = input_get(ictx, 0, 1, 1);
1543 if (n != -1)
1544 screen_write_insertcharacter(sctx, n, bg);
1545 break;
1546 case INPUT_CSI_IL:
1547 n = input_get(ictx, 0, 1, 1);
1548 if (n != -1)
1549 screen_write_insertline(sctx, n, bg);
1550 break;
1551 case INPUT_CSI_REP:
1552 n = input_get(ictx, 0, 1, 1);
1553 if (n == -1)
1554 break;
1555
1556 m = screen_size_x(s)((s)->grid->sx) - s->cx;
1557 if (n > m)
1558 n = m;
1559
1560 if (ictx->last == -1)
1561 break;
1562 ictx->ch = ictx->last;
1563
1564 for (i = 0; i < n; i++)
1565 input_print(ictx);
1566 break;
1567 case INPUT_CSI_RCP:
1568 input_restore_state(ictx);
1569 break;
1570 case INPUT_CSI_RM:
1571 input_csi_dispatch_rm(ictx);
1572 break;
1573 case INPUT_CSI_RM_PRIVATE:
1574 input_csi_dispatch_rm_private(ictx);
1575 break;
1576 case INPUT_CSI_SCP:
1577 input_save_state(ictx);
1578 break;
1579 case INPUT_CSI_SGR:
1580 input_csi_dispatch_sgr(ictx);
1581 break;
1582 case INPUT_CSI_SM:
1583 input_csi_dispatch_sm(ictx);
1584 break;
1585 case INPUT_CSI_SM_PRIVATE:
1586 input_csi_dispatch_sm_private(ictx);
1587 break;
1588 case INPUT_CSI_SU:
1589 n = input_get(ictx, 0, 1, 1);
1590 if (n != -1)
1591 screen_write_scrollup(sctx, n, bg);
1592 break;
1593 case INPUT_CSI_SD:
1594 n = input_get(ictx, 0, 1, 1);
1595 if (n != -1)
1596 screen_write_scrolldown(sctx, n, bg);
1597 break;
1598 case INPUT_CSI_TBC:
1599 switch (input_get(ictx, 0, 0, 0)) {
1600 case -1:
1601 break;
1602 case 0:
1603 if (s->cx < screen_size_x(s)((s)->grid->sx))
1604 bit_clear(s->tabs, s->cx)((s->tabs)[((s->cx) >> 3)] &= ~(1 << ((
s->cx)&0x7)))
;
1605 break;
1606 case 3:
1607 bit_nclear(s->tabs, 0, screen_size_x(s) - 1)do { register bitstr_t *__name = (s->tabs); register int __start
= (0), __stop = (((s)->grid->sx) - 1); while (__start <=
__stop) { ((__name)[((__start) >> 3)] &= ~(1 <<
((__start)&0x7))); __start++; } } while(0)
;
1608 break;
1609 default:
1610 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1611 break;
1612 }
1613 break;
1614 case INPUT_CSI_VPA:
1615 n = input_get(ictx, 0, 1, 1);
1616 if (n != -1)
1617 screen_write_cursormove(sctx, -1, n - 1, 1);
1618 break;
1619 case INPUT_CSI_DECSCUSR:
1620 n = input_get(ictx, 0, 0, 0);
1621 if (n != -1)
1622 screen_set_cursor_style(n, &s->cstyle, &s->mode);
1623 break;
1624 case INPUT_CSI_XDA:
1625 n = input_get(ictx, 0, 0, 0);
1626 if (n == 0)
1627 input_reply(ictx, "\033P>|tmux %s\033\\", getversion());
1628 break;
1629
1630 }
1631
1632 ictx->last = -1;
1633 return (0);
1634}
1635
1636/* Handle CSI RM. */
1637static void
1638input_csi_dispatch_rm(struct input_ctx *ictx)
1639{
1640 struct screen_write_ctx *sctx = &ictx->ctx;
1641 u_int i;
1642
1643 for (i = 0; i < ictx->param_list_len; i++) {
1644 switch (input_get(ictx, i, 0, -1)) {
1645 case -1:
1646 break;
1647 case 4: /* IRM */
1648 screen_write_mode_clear(sctx, MODE_INSERT0x2);
1649 break;
1650 case 34:
1651 screen_write_mode_set(sctx, MODE_CURSOR_VERY_VISIBLE0x10000);
1652 break;
1653 default:
1654 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1655 break;
1656 }
1657 }
1658}
1659
1660/* Handle CSI private RM. */
1661static void
1662input_csi_dispatch_rm_private(struct input_ctx *ictx)
1663{
1664 struct screen_write_ctx *sctx = &ictx->ctx;
1665 struct grid_cell *gc = &ictx->cell.cell;
1666 u_int i;
1667
1668 for (i = 0; i < ictx->param_list_len; i++) {
1669 switch (input_get(ictx, i, 0, -1)) {
1670 case -1:
1671 break;
1672 case 1: /* DECCKM */
1673 screen_write_mode_clear(sctx, MODE_KCURSOR0x4);
1674 break;
1675 case 3: /* DECCOLM */
1676 screen_write_cursormove(sctx, 0, 0, 1);
1677 screen_write_clearscreen(sctx, gc->bg);
1678 break;
1679 case 6: /* DECOM */
1680 screen_write_mode_clear(sctx, MODE_ORIGIN0x2000);
1681 screen_write_cursormove(sctx, 0, 0, 1);
1682 break;
1683 case 7: /* DECAWM */
1684 screen_write_mode_clear(sctx, MODE_WRAP0x10);
1685 break;
1686 case 12:
1687 screen_write_mode_clear(sctx, MODE_CURSOR_BLINKING0x80);
1688 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET0x20000);
1689 break;
1690 case 25: /* TCEM */
1691 screen_write_mode_clear(sctx, MODE_CURSOR0x1);
1692 break;
1693 case 1000:
1694 case 1001:
1695 case 1002:
1696 case 1003:
1697 screen_write_mode_clear(sctx, ALL_MOUSE_MODES(0x20|0x40|0x1000));
1698 break;
1699 case 1004:
1700 screen_write_mode_clear(sctx, MODE_FOCUSON0x800);
1701 break;
1702 case 1005:
1703 screen_write_mode_clear(sctx, MODE_MOUSE_UTF80x100);
1704 break;
1705 case 1006:
1706 screen_write_mode_clear(sctx, MODE_MOUSE_SGR0x200);
1707 break;
1708 case 47:
1709 case 1047:
1710 screen_write_alternateoff(sctx, gc, 0);
1711 break;
1712 case 1049:
1713 screen_write_alternateoff(sctx, gc, 1);
1714 break;
1715 case 2004:
1716 screen_write_mode_clear(sctx, MODE_BRACKETPASTE0x400);
1717 break;
1718 default:
1719 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1720 break;
1721 }
1722 }
1723}
1724
1725/* Handle CSI SM. */
1726static void
1727input_csi_dispatch_sm(struct input_ctx *ictx)
1728{
1729 struct screen_write_ctx *sctx = &ictx->ctx;
1730 u_int i;
1731
1732 for (i = 0; i < ictx->param_list_len; i++) {
1733 switch (input_get(ictx, i, 0, -1)) {
1734 case -1:
1735 break;
1736 case 4: /* IRM */
1737 screen_write_mode_set(sctx, MODE_INSERT0x2);
1738 break;
1739 case 34:
1740 screen_write_mode_clear(sctx, MODE_CURSOR_VERY_VISIBLE0x10000);
1741 break;
1742 default:
1743 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1744 break;
1745 }
1746 }
1747}
1748
1749/* Handle CSI private SM. */
1750static void
1751input_csi_dispatch_sm_private(struct input_ctx *ictx)
1752{
1753 struct screen_write_ctx *sctx = &ictx->ctx;
1754 struct window_pane *wp = ictx->wp;
1755 struct grid_cell *gc = &ictx->cell.cell;
1756 u_int i;
1757
1758 for (i = 0; i < ictx->param_list_len; i++) {
1759 switch (input_get(ictx, i, 0, -1)) {
1760 case -1:
1761 break;
1762 case 1: /* DECCKM */
1763 screen_write_mode_set(sctx, MODE_KCURSOR0x4);
1764 break;
1765 case 3: /* DECCOLM */
1766 screen_write_cursormove(sctx, 0, 0, 1);
1767 screen_write_clearscreen(sctx, ictx->cell.cell.bg);
1768 break;
1769 case 6: /* DECOM */
1770 screen_write_mode_set(sctx, MODE_ORIGIN0x2000);
1771 screen_write_cursormove(sctx, 0, 0, 1);
1772 break;
1773 case 7: /* DECAWM */
1774 screen_write_mode_set(sctx, MODE_WRAP0x10);
1775 break;
1776 case 12:
1777 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING0x80);
1778 screen_write_mode_set(sctx, MODE_CURSOR_BLINKING_SET0x20000);
1779 break;
1780 case 25: /* TCEM */
1781 screen_write_mode_set(sctx, MODE_CURSOR0x1);
1782 break;
1783 case 1000:
1784 screen_write_mode_clear(sctx, ALL_MOUSE_MODES(0x20|0x40|0x1000));
1785 screen_write_mode_set(sctx, MODE_MOUSE_STANDARD0x20);
1786 break;
1787 case 1002:
1788 screen_write_mode_clear(sctx, ALL_MOUSE_MODES(0x20|0x40|0x1000));
1789 screen_write_mode_set(sctx, MODE_MOUSE_BUTTON0x40);
1790 break;
1791 case 1003:
1792 screen_write_mode_clear(sctx, ALL_MOUSE_MODES(0x20|0x40|0x1000));
1793 screen_write_mode_set(sctx, MODE_MOUSE_ALL0x1000);
1794 break;
1795 case 1004:
1796 if (sctx->s->mode & MODE_FOCUSON0x800)
1797 break;
1798 screen_write_mode_set(sctx, MODE_FOCUSON0x800);
1799 if (wp == NULL((void *)0))
1800 break;
1801 if (wp->flags & PANE_FOCUSED0x4)
1802 bufferevent_write(wp->event, "\033[I", 3);
1803 else
1804 bufferevent_write(wp->event, "\033[O", 3);
1805 break;
1806 case 1005:
1807 screen_write_mode_set(sctx, MODE_MOUSE_UTF80x100);
1808 break;
1809 case 1006:
1810 screen_write_mode_set(sctx, MODE_MOUSE_SGR0x200);
1811 break;
1812 case 47:
1813 case 1047:
1814 screen_write_alternateon(sctx, gc, 0);
1815 break;
1816 case 1049:
1817 screen_write_alternateon(sctx, gc, 1);
1818 break;
1819 case 2004:
1820 screen_write_mode_set(sctx, MODE_BRACKETPASTE0x400);
1821 break;
1822 default:
1823 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1824 break;
1825 }
1826 }
1827}
1828
1829/* Handle CSI window operations. */
1830static void
1831input_csi_dispatch_winops(struct input_ctx *ictx)
1832{
1833 struct screen_write_ctx *sctx = &ictx->ctx;
1834 struct screen *s = sctx->s;
1835 struct window_pane *wp = ictx->wp;
1836 u_int x = screen_size_x(s)((s)->grid->sx), y = screen_size_y(s)((s)->grid->sy);
1837 int n, m;
1838
1839 m = 0;
1840 while ((n = input_get(ictx, m, 0, -1)) != -1) {
1841 switch (n) {
1842 case 1:
1843 case 2:
1844 case 5:
1845 case 6:
1846 case 7:
1847 case 11:
1848 case 13:
1849 case 14:
1850 case 19:
1851 case 20:
1852 case 21:
1853 case 24:
1854 break;
1855 case 3:
1856 case 4:
1857 case 8:
1858 m++;
1859 if (input_get(ictx, m, 0, -1) == -1)
1860 return;
1861 /* FALLTHROUGH */
1862 case 9:
1863 case 10:
1864 m++;
1865 if (input_get(ictx, m, 0, -1) == -1)
1866 return;
1867 break;
1868 case 22:
1869 m++;
1870 switch (input_get(ictx, m, 0, -1)) {
1871 case -1:
1872 return;
1873 case 0:
1874 case 2:
1875 screen_push_title(sctx->s);
1876 break;
1877 }
1878 break;
1879 case 23:
1880 m++;
1881 switch (input_get(ictx, m, 0, -1)) {
1882 case -1:
1883 return;
1884 case 0:
1885 case 2:
1886 screen_pop_title(sctx->s);
1887 if (wp == NULL((void *)0))
1888 break;
1889 notify_pane("pane-title-changed", wp);
1890 server_redraw_window_borders(wp->window);
1891 server_status_window(wp->window);
1892 break;
1893 }
1894 break;
1895 case 18:
1896 input_reply(ictx, "\033[8;%u;%ut", x, y);
1897 break;
1898 default:
1899 log_debug("%s: unknown '%c'", __func__, ictx->ch);
1900 break;
1901 }
1902 m++;
1903 }
1904}
1905
1906/* Helper for 256 colour SGR. */
1907static int
1908input_csi_dispatch_sgr_256_do(struct input_ctx *ictx, int fgbg, int c)
1909{
1910 struct grid_cell *gc = &ictx->cell.cell;
1911
1912 if (c == -1 || c > 255) {
1913 if (fgbg == 38)
1914 gc->fg = 8;
1915 else if (fgbg == 48)
1916 gc->bg = 8;
1917 } else {
1918 if (fgbg == 38)
1919 gc->fg = c | COLOUR_FLAG_2560x01000000;
1920 else if (fgbg == 48)
1921 gc->bg = c | COLOUR_FLAG_2560x01000000;
1922 else if (fgbg == 58)
1923 gc->us = c | COLOUR_FLAG_2560x01000000;
1924 }
1925 return (1);
1926}
1927
1928/* Handle CSI SGR for 256 colours. */
1929static void
1930input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
1931{
1932 int c;
1933
1934 c = input_get(ictx, (*i) + 1, 0, -1);
1935 if (input_csi_dispatch_sgr_256_do(ictx, fgbg, c))
1936 (*i)++;
1937}
1938
1939/* Helper for RGB colour SGR. */
1940static int
1941input_csi_dispatch_sgr_rgb_do(struct input_ctx *ictx, int fgbg, int r, int g,
1942 int b)
1943{
1944 struct grid_cell *gc = &ictx->cell.cell;
1945
1946 if (r == -1 || r > 255)
1947 return (0);
1948 if (g == -1 || g > 255)
1949 return (0);
1950 if (b == -1 || b > 255)
1951 return (0);
1952
1953 if (fgbg == 38)
1954 gc->fg = colour_join_rgb(r, g, b);
1955 else if (fgbg == 48)
1956 gc->bg = colour_join_rgb(r, g, b);
1957 else if (fgbg == 58)
1958 gc->us = colour_join_rgb(r, g, b);
1959 return (1);
1960}
1961
1962/* Handle CSI SGR for RGB colours. */
1963static void
1964input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
1965{
1966 int r, g, b;
1967
1968 r = input_get(ictx, (*i) + 1, 0, -1);
1969 g = input_get(ictx, (*i) + 2, 0, -1);
1970 b = input_get(ictx, (*i) + 3, 0, -1);
1971 if (input_csi_dispatch_sgr_rgb_do(ictx, fgbg, r, g, b))
1972 (*i) += 3;
1973}
1974
1975/* Handle CSI SGR with a ISO parameter. */
1976static void
1977input_csi_dispatch_sgr_colon(struct input_ctx *ictx, u_int i)
1978{
1979 struct grid_cell *gc = &ictx->cell.cell;
1980 char *s = ictx->param_list[i].str, *copy, *ptr, *out;
1981 int p[8];
1982 u_int n;
1983 const char *errstr;
1984
1985 for (n = 0; n < nitems(p)(sizeof((p)) / sizeof((p)[0])); n++)
1986 p[n] = -1;
1987 n = 0;
1988
1989 ptr = copy = xstrdup(s);
1990 while ((out = strsep(&ptr, ":")) != NULL((void *)0)) {
1991 if (*out != '\0') {
1992 p[n++] = strtonum(out, 0, INT_MAX2147483647, &errstr);
1993 if (errstr != NULL((void *)0) || n == nitems(p)(sizeof((p)) / sizeof((p)[0]))) {
1994 free(copy);
1995 return;
1996 }
1997 } else {
1998 n++;
1999 if (n == nitems(p)(sizeof((p)) / sizeof((p)[0]))) {
2000 free(copy);
2001 return;
2002 }
2003 }
2004 log_debug("%s: %u = %d", __func__, n - 1, p[n - 1]);
2005 }
2006 free(copy);
2007
2008 if (n == 0)
2009 return;
2010 if (p[0] == 4) {
2011 if (n != 2)
2012 return;
2013 switch (p[1]) {
2014 case 0:
2015 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2016 break;
2017 case 1:
2018 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2019 gc->attr |= GRID_ATTR_UNDERSCORE0x4;
2020 break;
2021 case 2:
2022 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2023 gc->attr |= GRID_ATTR_UNDERSCORE_20x200;
2024 break;
2025 case 3:
2026 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2027 gc->attr |= GRID_ATTR_UNDERSCORE_30x400;
2028 break;
2029 case 4:
2030 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2031 gc->attr |= GRID_ATTR_UNDERSCORE_40x800;
2032 break;
2033 case 5:
2034 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2035 gc->attr |= GRID_ATTR_UNDERSCORE_50x1000;
2036 break;
2037 }
2038 return;
2039 }
2040 if (n < 2 || (p[0] != 38 && p[0] != 48 && p[0] != 58))
2041 return;
2042 switch (p[1]) {
2043 case 2:
2044 if (n < 3)
2045 break;
2046 if (n == 5)
2047 i = 2;
2048 else
2049 i = 3;
2050 if (n < i + 3)
2051 break;
2052 input_csi_dispatch_sgr_rgb_do(ictx, p[0], p[i], p[i + 1],
2053 p[i + 2]);
2054 break;
2055 case 5:
2056 if (n < 3)
2057 break;
2058 input_csi_dispatch_sgr_256_do(ictx, p[0], p[2]);
2059 break;
2060 }
2061}
2062
2063/* Handle CSI SGR. */
2064static void
2065input_csi_dispatch_sgr(struct input_ctx *ictx)
2066{
2067 struct grid_cell *gc = &ictx->cell.cell;
2068 u_int i;
2069 int n;
2070
2071 if (ictx->param_list_len == 0) {
2072 memcpy(gc, &grid_default_cell, sizeof *gc);
2073 return;
2074 }
2075
2076 for (i = 0; i < ictx->param_list_len; i++) {
2077 if (ictx->param_list[i].type == INPUT_STRING) {
2078 input_csi_dispatch_sgr_colon(ictx, i);
2079 continue;
2080 }
2081 n = input_get(ictx, i, 0, 0);
2082 if (n == -1)
2083 continue;
2084
2085 if (n == 38 || n == 48 || n == 58) {
2086 i++;
2087 switch (input_get(ictx, i, 0, -1)) {
2088 case 2:
2089 input_csi_dispatch_sgr_rgb(ictx, n, &i);
2090 break;
2091 case 5:
2092 input_csi_dispatch_sgr_256(ictx, n, &i);
2093 break;
2094 }
2095 continue;
2096 }
2097
2098 switch (n) {
2099 case 0:
2100 memcpy(gc, &grid_default_cell, sizeof *gc);
2101 break;
2102 case 1:
2103 gc->attr |= GRID_ATTR_BRIGHT0x1;
2104 break;
2105 case 2:
2106 gc->attr |= GRID_ATTR_DIM0x2;
2107 break;
2108 case 3:
2109 gc->attr |= GRID_ATTR_ITALICS0x40;
2110 break;
2111 case 4:
2112 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2113 gc->attr |= GRID_ATTR_UNDERSCORE0x4;
2114 break;
2115 case 5:
2116 case 6:
2117 gc->attr |= GRID_ATTR_BLINK0x8;
2118 break;
2119 case 7:
2120 gc->attr |= GRID_ATTR_REVERSE0x10;
2121 break;
2122 case 8:
2123 gc->attr |= GRID_ATTR_HIDDEN0x20;
2124 break;
2125 case 9:
2126 gc->attr |= GRID_ATTR_STRIKETHROUGH0x100;
2127 break;
2128 case 21:
2129 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2130 gc->attr |= GRID_ATTR_UNDERSCORE_20x200;
2131 break;
2132 case 22:
2133 gc->attr &= ~(GRID_ATTR_BRIGHT0x1|GRID_ATTR_DIM0x2);
2134 break;
2135 case 23:
2136 gc->attr &= ~GRID_ATTR_ITALICS0x40;
2137 break;
2138 case 24:
2139 gc->attr &= ~GRID_ATTR_ALL_UNDERSCORE(0x4| 0x200| 0x400| 0x800| 0x1000);
2140 break;
2141 case 25:
2142 gc->attr &= ~GRID_ATTR_BLINK0x8;
2143 break;
2144 case 27:
2145 gc->attr &= ~GRID_ATTR_REVERSE0x10;
2146 break;
2147 case 28:
2148 gc->attr &= ~GRID_ATTR_HIDDEN0x20;
2149 break;
2150 case 29:
2151 gc->attr &= ~GRID_ATTR_STRIKETHROUGH0x100;
2152 break;
2153 case 30:
2154 case 31:
2155 case 32:
2156 case 33:
2157 case 34:
2158 case 35:
2159 case 36:
2160 case 37:
2161 gc->fg = n - 30;
2162 break;
2163 case 39:
2164 gc->fg = 8;
2165 break;
2166 case 40:
2167 case 41:
2168 case 42:
2169 case 43:
2170 case 44:
2171 case 45:
2172 case 46:
2173 case 47:
2174 gc->bg = n - 40;
2175 break;
2176 case 49:
2177 gc->bg = 8;
2178 break;
2179 case 53:
2180 gc->attr |= GRID_ATTR_OVERLINE0x2000;
2181 break;
2182 case 55:
2183 gc->attr &= ~GRID_ATTR_OVERLINE0x2000;
2184 break;
2185 case 59:
2186 gc->us = 0;
2187 break;
2188 case 90:
2189 case 91:
2190 case 92:
2191 case 93:
2192 case 94:
2193 case 95:
2194 case 96:
2195 case 97:
2196 gc->fg = n;
2197 break;
2198 case 100:
2199 case 101:
2200 case 102:
2201 case 103:
2202 case 104:
2203 case 105:
2204 case 106:
2205 case 107:
2206 gc->bg = n - 10;
2207 break;
2208 }
2209 }
2210}
2211
2212/* End of input with BEL. */
2213static int
2214input_end_bel(struct input_ctx *ictx)
2215{
2216 log_debug("%s", __func__);
2217
2218 ictx->input_end = INPUT_END_BEL;
2219
2220 return (0);
2221}
2222
2223/* DCS string started. */
2224static void
2225input_enter_dcs(struct input_ctx *ictx)
2226{
2227 log_debug("%s", __func__);
2228
2229 input_clear(ictx);
2230 input_start_timer(ictx);
2231 ictx->last = -1;
2232}
2233
2234/* DCS terminator (ST) received. */
2235static int
2236input_dcs_dispatch(struct input_ctx *ictx)
2237{
2238 struct screen_write_ctx *sctx = &ictx->ctx;
2239 u_char *buf = ictx->input_buf;
2240 size_t len = ictx->input_len;
2241 const char prefix[] = "tmux;";
2242 const u_int prefixlen = (sizeof prefix) - 1;
2243
2244 if (ictx->flags & INPUT_DISCARD0x1)
2245 return (0);
2246
2247 log_debug("%s: \"%s\"", __func__, buf);
2248
2249 if (len >= prefixlen && strncmp(buf, prefix, prefixlen) == 0)
2250 screen_write_rawstring(sctx, buf + prefixlen, len - prefixlen);
2251
2252 return (0);
2253}
2254
2255/* OSC string started. */
2256static void
2257input_enter_osc(struct input_ctx *ictx)
2258{
2259 log_debug("%s", __func__);
2260
2261 input_clear(ictx);
2262 input_start_timer(ictx);
2263 ictx->last = -1;
2264}
2265
2266/* OSC terminator (ST) received. */
2267static void
2268input_exit_osc(struct input_ctx *ictx)
2269{
2270 struct screen_write_ctx *sctx = &ictx->ctx;
2271 struct window_pane *wp = ictx->wp;
2272 u_char *p = ictx->input_buf;
2273 u_int option;
2274
2275 if (ictx->flags & INPUT_DISCARD0x1)
2276 return;
2277 if (ictx->input_len < 1 || *p < '0' || *p > '9')
2278 return;
2279
2280 log_debug("%s: \"%s\" (end %s)", __func__, p,
2281 ictx->input_end == INPUT_END_ST ? "ST" : "BEL");
2282
2283 option = 0;
2284 while (*p >= '0' && *p <= '9')
2285 option = option * 10 + *p++ - '0';
2286 if (*p == ';')
2287 p++;
2288
2289 switch (option) {
2290 case 0:
2291 case 2:
2292 if (screen_set_title(sctx->s, p) && wp != NULL((void *)0)) {
2293 notify_pane("pane-title-changed", wp);
2294 server_redraw_window_borders(wp->window);
2295 server_status_window(wp->window);
2296 }
2297 break;
2298 case 4:
2299 input_osc_4(ictx, p);
2300 break;
2301 case 7:
2302 if (utf8_isvalid(p)) {
2303 screen_set_path(sctx->s, p);
2304 if (wp != NULL((void *)0)) {
2305 server_redraw_window_borders(wp->window);
2306 server_status_window(wp->window);
2307 }
2308 }
2309 break;
2310 case 10:
2311 input_osc_10(ictx, p);
2312 break;
2313 case 11:
2314 input_osc_11(ictx, p);
2315 break;
2316 case 12:
2317 input_osc_12(ictx, p);
2318 break;
2319 case 52:
2320 input_osc_52(ictx, p);
2321 break;
2322 case 104:
2323 input_osc_104(ictx, p);
2324 break;
2325 case 110:
2326 input_osc_110(ictx, p);
2327 break;
2328 case 111:
2329 input_osc_111(ictx, p);
2330 break;
2331 case 112:
2332 input_osc_112(ictx, p);
2333 break;
2334 default:
2335 log_debug("%s: unknown '%u'", __func__, option);
2336 break;
2337 }
2338}
2339
2340/* APC string started. */
2341static void
2342input_enter_apc(struct input_ctx *ictx)
2343{
2344 log_debug("%s", __func__);
2345
2346 input_clear(ictx);
2347 input_start_timer(ictx);
2348 ictx->last = -1;
2349}
2350
2351/* APC terminator (ST) received. */
2352static void
2353input_exit_apc(struct input_ctx *ictx)
2354{
2355 struct screen_write_ctx *sctx = &ictx->ctx;
2356 struct window_pane *wp = ictx->wp;
2357
2358 if (ictx->flags & INPUT_DISCARD0x1)
2359 return;
2360 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2361
2362 if (screen_set_title(sctx->s, ictx->input_buf) && wp != NULL((void *)0)) {
2363 notify_pane("pane-title-changed", wp);
2364 server_redraw_window_borders(wp->window);
2365 server_status_window(wp->window);
2366 }
2367}
2368
2369/* Rename string started. */
2370static void
2371input_enter_rename(struct input_ctx *ictx)
2372{
2373 log_debug("%s", __func__);
2374
2375 input_clear(ictx);
2376 input_start_timer(ictx);
2377 ictx->last = -1;
2378}
2379
2380/* Rename terminator (ST) received. */
2381static void
2382input_exit_rename(struct input_ctx *ictx)
2383{
2384 struct window_pane *wp = ictx->wp;
2385 struct window *w;
2386 struct options_entry *o;
2387
2388 if (wp == NULL((void *)0))
2389 return;
2390 if (ictx->flags & INPUT_DISCARD0x1)
2391 return;
2392 if (!options_get_number(ictx->wp->options, "allow-rename"))
2393 return;
2394 log_debug("%s: \"%s\"", __func__, ictx->input_buf);
2395
2396 if (!utf8_isvalid(ictx->input_buf))
2397 return;
2398 w = wp->window;
2399
2400 if (ictx->input_len == 0) {
2401 o = options_get_only(w->options, "automatic-rename");
2402 if (o != NULL((void *)0))
2403 options_remove_or_default(o, -1, NULL((void *)0));
2404 if (!options_get_number(w->options, "automatic-rename"))
2405 window_set_name(w, "");
2406 } else {
2407 options_set_number(w->options, "automatic-rename", 0);
2408 window_set_name(w, ictx->input_buf);
2409 }
2410 server_redraw_window_borders(w);
2411 server_status_window(w);
2412}
2413
2414/* Open UTF-8 character. */
2415static int
2416input_top_bit_set(struct input_ctx *ictx)
2417{
2418 struct screen_write_ctx *sctx = &ictx->ctx;
2419 struct utf8_data *ud = &ictx->utf8data;
2420
2421 ictx->last = -1;
2422
2423 if (!ictx->utf8started) {
2424 if (utf8_open(ud, ictx->ch) != UTF8_MORE)
2425 return (0);
2426 ictx->utf8started = 1;
2427 return (0);
2428 }
2429
2430 switch (utf8_append(ud, ictx->ch)) {
2431 case UTF8_MORE:
2432 return (0);
2433 case UTF8_ERROR:
2434 ictx->utf8started = 0;
2435 return (0);
2436 case UTF8_DONE:
2437 break;
2438 }
2439 ictx->utf8started = 0;
2440
2441 log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
2442 (int)ud->size, ud->data, ud->width);
2443
2444 utf8_copy(&ictx->cell.cell.data, ud);
2445 screen_write_collect_add(sctx, &ictx->cell.cell);
2446
2447 return (0);
2448}
2449
2450/* Parse colour from OSC. */
2451static int
2452input_osc_parse_colour(const char *p)
2453{
2454 double c, m, y, k = 0;
2455 u_int r, g, b;
2456 size_t len = strlen(p);
2457 int colour = -1;
2458 char *copy;
2459
2460 if ((len == 12 && sscanf(p, "rgb:%02x/%02x/%02x", &r, &g, &b) == 3) ||
2461 (len == 7 && sscanf(p, "#%02x%02x%02x", &r, &g, &b) == 3) ||
2462 sscanf(p, "%d,%d,%d", &r, &g, &b) == 3)
2463 colour = colour_join_rgb(r, g, b);
2464 else if ((len == 18 &&
2465 sscanf(p, "rgb:%04x/%04x/%04x", &r, &g, &b) == 3) ||
2466 (len == 13 && sscanf(p, "#%04x%04x%04x", &r, &g, &b) == 3))
2467 colour = colour_join_rgb(r >> 8, g >> 8, b >> 8);
2468 else if ((sscanf(p, "cmyk:%lf/%lf/%lf/%lf", &c, &m, &y, &k) == 4 ||
2469 sscanf(p, "cmy:%lf/%lf/%lf", &c, &m, &y) == 3) &&
2470 c >= 0 && c <= 1 && m >= 0 && m <= 1 &&
2471 y >= 0 && y <= 1 && k >= 0 && k <= 1) {
2472 colour = colour_join_rgb(
2473 (1 - c) * (1 - k) * 255,
2474 (1 - m) * (1 - k) * 255,
2475 (1 - y) * (1 - k) * 255);
2476 } else {
2477 while (len != 0 && *p == ' ') {
2478 p++;
2479 len--;
2480 }
2481 while (len != 0 && p[len - 1] == ' ')
2482 len--;
2483 copy = xstrndup(p, len);
2484 colour = colour_byname(copy);
2485 free(copy);
2486 }
2487 log_debug("%s: %s = %s", __func__, p, colour_tostring(colour));
2488 return (colour);
2489}
2490
2491/* Reply to a colour request. */
2492static void
2493input_osc_colour_reply(struct input_ctx *ictx, u_int n, int c)
2494{
2495 u_char r, g, b;
2496 const char *end;
2497
2498 if (c != -1)
2499 c = colour_force_rgb(c);
2500 if (c == -1)
2501 return;
2502 colour_split_rgb(c, &r, &g, &b);
2503
2504 if (ictx->input_end == INPUT_END_BEL)
2505 end = "\007";
2506 else
2507 end = "\033\\";
2508 input_reply(ictx, "\033]%u;rgb:%02hhx%02hhx/%02hhx%02hhx/%02hhx%02hhx%s",
2509 n, r, r, g, g, b, b, end);
2510}
2511
2512/* Handle the OSC 4 sequence for setting (multiple) palette entries. */
2513static void
2514input_osc_4(struct input_ctx *ictx, const char *p)
2515{
2516 char *copy, *s, *next = NULL((void *)0);
2517 long idx;
2518 int c, bad = 0, redraw = 0;
2519
2520 copy = s = xstrdup(p);
2521 while (s != NULL((void *)0) && *s != '\0') {
2522 idx = strtol(s, &next, 10);
2523 if (*next++ != ';') {
2524 bad = 1;
2525 break;
2526 }
2527 if (idx < 0 || idx >= 256) {
2528 bad = 1;
2529 break;
2530 }
2531
2532 s = strsep(&next, ";");
2533 if (strcmp(s, "?") == 0) {
2534 c = colour_palette_get(ictx->palette, idx);
2535 if (c != -1)
2536 input_osc_colour_reply(ictx, 4, c);
2537 continue;
2538 }
2539 if ((c = input_osc_parse_colour(s)) == -1) {
2540 s = next;
2541 continue;
2542 }
2543 if (colour_palette_set(ictx->palette, idx, c))
2544 redraw = 1;
2545 s = next;
2546 }
2547 if (bad)
2548 log_debug("bad OSC 4: %s", p);
2549 if (redraw)
2550 screen_write_fullredraw(&ictx->ctx);
2551 free(copy);
2552}
2553
2554/* Handle the OSC 10 sequence for setting and querying foreground colour. */
2555static void
2556input_osc_10(struct input_ctx *ictx, const char *p)
2557{
2558 struct window_pane *wp = ictx->wp;
2559 struct grid_cell defaults;
2560 int c;
2561
2562 if (strcmp(p, "?") == 0) {
2563 if (wp != NULL((void *)0)) {
2564 tty_default_colours(&defaults, wp);
2565 input_osc_colour_reply(ictx, 10, defaults.fg);
2566 }
2567 return;
2568 }
2569
2570 if ((c = input_osc_parse_colour(p)) == -1) {
2571 log_debug("bad OSC 10: %s", p);
2572 return;
2573 }
2574 if (ictx->palette != NULL((void *)0)) {
2575 ictx->palette->fg = c;
2576 if (wp != NULL((void *)0))
2577 wp->flags |= PANE_STYLECHANGED0x1000;
2578 screen_write_fullredraw(&ictx->ctx);
2579 }
2580}
2581
2582/* Handle the OSC 110 sequence for resetting foreground colour. */
2583static void
2584input_osc_110(struct input_ctx *ictx, const char *p)
2585{
2586 struct window_pane *wp = ictx->wp;
2587
2588 if (*p != '\0')
2589 return;
2590 if (ictx->palette != NULL((void *)0)) {
2591 ictx->palette->fg = 8;
2592 if (wp != NULL((void *)0))
2593 wp->flags |= PANE_STYLECHANGED0x1000;
2594 screen_write_fullredraw(&ictx->ctx);
2595 }
2596}
2597
2598/* Handle the OSC 11 sequence for setting and querying background colour. */
2599static void
2600input_osc_11(struct input_ctx *ictx, const char *p)
2601{
2602 struct window_pane *wp = ictx->wp;
2603 struct grid_cell defaults;
2604 int c;
2605
2606 if (strcmp(p, "?") == 0) {
2607 if (wp != NULL((void *)0)) {
2608 tty_default_colours(&defaults, wp);
2609 input_osc_colour_reply(ictx, 11, defaults.bg);
2610 }
2611 return;
2612 }
2613
2614 if ((c = input_osc_parse_colour(p)) == -1) {
2615 log_debug("bad OSC 11: %s", p);
2616 return;
2617 }
2618 if (ictx->palette != NULL((void *)0)) {
2619 ictx->palette->bg = c;
2620 if (wp != NULL((void *)0))
2621 wp->flags |= PANE_STYLECHANGED0x1000;
2622 screen_write_fullredraw(&ictx->ctx);
2623 }
2624}
2625
2626/* Handle the OSC 111 sequence for resetting background colour. */
2627static void
2628input_osc_111(struct input_ctx *ictx, const char *p)
2629{
2630 struct window_pane *wp = ictx->wp;
2631
2632 if (*p != '\0')
2633 return;
2634 if (ictx->palette != NULL((void *)0)) {
2635 ictx->palette->bg = 8;
2636 if (wp != NULL((void *)0))
2637 wp->flags |= PANE_STYLECHANGED0x1000;
2638 screen_write_fullredraw(&ictx->ctx);
2639 }
2640}
2641
2642/* Handle the OSC 12 sequence for setting and querying cursor colour. */
2643static void
2644input_osc_12(struct input_ctx *ictx, const char *p)
2645{
2646 struct window_pane *wp = ictx->wp;
2647 int c;
2648
2649 if (strcmp(p, "?") == 0) {
2650 if (wp != NULL((void *)0)) {
2651 c = ictx->ctx.s->ccolour;
2652 if (c == -1)
2653 c = ictx->ctx.s->default_ccolour;
2654 input_osc_colour_reply(ictx, 12, c);
2655 }
2656 return;
2657 }
2658
2659 if ((c = input_osc_parse_colour(p)) == -1) {
2660 log_debug("bad OSC 12: %s", p);
2661 return;
2662 }
2663 screen_set_cursor_colour(ictx->ctx.s, c);
2664}
2665
2666/* Handle the OSC 112 sequence for resetting cursor colour. */
2667static void
2668input_osc_112(struct input_ctx *ictx, const char *p)
2669{
2670 if (*p == '\0') /* no arguments allowed */
2671 screen_set_cursor_colour(ictx->ctx.s, -1);
2672}
2673
2674
2675/* Handle the OSC 52 sequence for setting the clipboard. */
2676static void
2677input_osc_52(struct input_ctx *ictx, const char *p)
2678{
2679 struct window_pane *wp = ictx->wp;
2680 char *end;
2681 const char *buf;
2682 size_t len;
2683 u_char *out;
2684 int outlen, state;
2685 struct screen_write_ctx ctx;
2686 struct paste_buffer *pb;
2687
2688 if (wp == NULL((void *)0))
2689 return;
2690 state = options_get_number(global_options, "set-clipboard");
2691 if (state != 2)
2692 return;
2693
2694 if ((end = strchr(p, ';')) == NULL((void *)0))
2695 return;
2696 end++;
2697 if (*end == '\0')
2698 return;
2699 log_debug("%s: %s", __func__, end);
2700
2701 if (strcmp(end, "?") == 0) {
2702 if ((pb = paste_get_top(NULL((void *)0))) != NULL((void *)0)) {
2703 buf = paste_buffer_data(pb, &len);
2704 outlen = 4 * ((len + 2) / 3) + 1;
2705 out = xmalloc(outlen);
2706 if ((outlen = b64_ntop__b64_ntop(buf, len, out, outlen)) == -1) {
2707 free(out);
2708 return;
2709 }
2710 } else {
2711 outlen = 0;
2712 out = NULL((void *)0);
2713 }
2714 bufferevent_write(ictx->event, "\033]52;;", 6);
2715 if (outlen != 0)
2716 bufferevent_write(ictx->event, out, outlen);
2717 if (ictx->input_end == INPUT_END_BEL)
2718 bufferevent_write(ictx->event, "\007", 1);
2719 else
2720 bufferevent_write(ictx->event, "\033\\", 2);
2721 free(out);
2722 return;
2723 }
2724
2725 len = (strlen(end) / 4) * 3;
2726 if (len == 0)
2727 return;
2728
2729 out = xmalloc(len);
2730 if ((outlen = b64_pton__b64_pton(end, out, len)) == -1) {
2731 free(out);
2732 return;
2733 }
2734
2735 screen_write_start_pane(&ctx, wp, NULL((void *)0));
2736 screen_write_setselection(&ctx, out, outlen);
2737 screen_write_stop(&ctx);
2738 notify_pane("pane-set-clipboard", wp);
2739
2740 paste_add(NULL((void *)0), out, outlen);
2741}
2742
2743/* Handle the OSC 104 sequence for unsetting (multiple) palette entries. */
2744static void
2745input_osc_104(struct input_ctx *ictx, const char *p)
2746{
2747 char *copy, *s;
2748 long idx;
2749 int bad = 0, redraw = 0;
2750
2751 if (*p == '\0') {
2752 colour_palette_clear(ictx->palette);
2753 screen_write_fullredraw(&ictx->ctx);
2754 return;
2755 }
2756
2757 copy = s = xstrdup(p);
2758 while (*s != '\0') {
2759 idx = strtol(s, &s, 10);
2760 if (*s != '\0' && *s != ';') {
2761 bad = 1;
2762 break;
2763 }
2764 if (idx < 0 || idx >= 256) {
2765 bad = 1;
2766 break;
2767 }
2768 if (colour_palette_set(ictx->palette, idx, -1))
2769 redraw = 1;
2770 if (*s == ';')
2771 s++;
2772 }
2773 if (bad)
2774 log_debug("bad OSC 104: %s", p);
2775 if (redraw)
2776 screen_write_fullredraw(&ictx->ctx);
2777 free(copy);
2778}