From d517427dff319eb0735d24197e945dd99e4e44b1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 18 Dec 2024 11:10:47 +0900 Subject: [PATCH] ptyfwd: save the last character before the escape sequence If we write e.g. a line break and CSI sequence, then it is not necessary to write another line break on exit. --- src/shared/ptyfwd.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index f41b525b471..9fc8e30b0c0 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -85,6 +85,7 @@ struct PTYForward { bool last_char_set:1; char last_char; + char last_char_safe; char in_buffer[LINE_MAX], *out_buffer; size_t out_buffer_size; @@ -439,8 +440,11 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) { if (r < 0) return r; i += r; + f->last_char_safe = c; } else if (c == 0x1B) /* ESC */ f->ansi_color_state = ANSI_COLOR_STATE_ESC; + else if (!char_is_cc(c)) + f->last_char_safe = c; break; case ANSI_COLOR_STATE_ESC: @@ -705,8 +709,15 @@ static int do_shovel(PTYForward *f) { } else { - if (k > 0) { - f->last_char = f->out_buffer[k-1]; + if (k > 0 && f->last_char_safe != '\0') { + if ((size_t) k == f->out_buffer_write_len) + /* If we wrote all, then save the last safe character. */ + f->last_char = f->last_char_safe; + else + /* If we wrote partially, then tentatively save the last written character. + * Hopefully, we will write more in the next loop. */ + f->last_char = f->out_buffer[k-1]; + f->last_char_set = true; }