Skip to content

Commit

Permalink
kernel: unicode character cannot be part of escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
frazze-jobb committed Jan 9, 2025
1 parent 53ffb6a commit 6a928fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/kernel/src/prim_tty.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,10 @@ insert_buf(State, Bin, LineAcc, Acc) ->
State#state.buffer_expand =:= undefined ->
[PrevChar | BB] = State#state.buffer_before,
case string:next_grapheme([PrevChar | Bin]) of
[$\e | _] ->
%% Ansi escape sequences can never have unicode characters in them
%% so Char cannot be part of this cluster
insert_buf(State, Rest, [Char | LineAcc], Acc);
[PrevChar | _] ->
%% It was not part of the previous cluster, so just insert
%% it as a normal character
Expand Down
26 changes: 24 additions & 2 deletions lib/kernel/test/interactive_shell_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@
remsh_basic/1, remsh_error/1, remsh_longnames/1, remsh_no_epmd/1,
remsh_expand_compatibility_25/1, remsh_expand_compatibility_later_version/1,
external_editor/1, external_editor_visual/1,
external_editor_unicode/1, shell_ignore_pager_commands/1]).
external_editor_unicode/1, shell_ignore_pager_commands/1,
shell_escape_sequence_end_of_prompt_followed_by_unicode/1]).

-export([get_until/2]).

-export([test_invalid_keymap/1, test_valid_keymap/1]).
%% Exports for custom shell history module
-export([load/0, add/1]).
%% For custom prompt testing
-export([prompt/1]).
-export([prompt/1, prompt_2/1]).
-export([output_to_stdout_slowly/1]).
-record(tmux, {peer, node, name, ssh_server_name, orig_location }).
suite() ->
Expand Down Expand Up @@ -153,6 +154,7 @@ groups() ->
{tty_unicode,[parallel],
[{group,tty_tests},
shell_invalid_unicode,
shell_escape_sequence_end_of_prompt_followed_by_unicode,
external_editor_unicode
%% unicode wrapping does not work right yet
%% shell_unicode_wrap,
Expand Down Expand Up @@ -1407,6 +1409,26 @@ shell_help(Config) ->
stop_tty(Term),
ok
end.

prompt_2(_) ->
["\e[94m",54620,44397,50612,47,51312,49440,47568,"\e[0m"].
shell_escape_sequence_end_of_prompt_followed_by_unicode(_Config) ->
%% If the prompt ended with an escape sequence, and the user input a unicode character
%% prim_tty tried to put that character in the escape sequence. This test checks that
%% the unicode character will not be part of the escape sequence.
Prompt = prompt_2(none),
rtnode:run(
[{putline,"÷."},
{expect, "÷[.]"},
{expect, ["\\Q",Prompt,"\\E"]}],
"", "",
["-pz",filename:dirname(code:which(?MODULE)),
"-connect_all","false",
"-kernel","logger_level","all",
"-kernel","shell_history","disabled",
"-kernel","prevent_overlapping_partitions","false",
"-eval","shell:prompt_func({interactive_shell_SUITE,prompt_2})."
]).
%% Test the we can handle invalid ansi escape chars.
%% tmux cannot handle this... so we test this using to_erl
shell_invalid_ansi(_Config) ->
Expand Down

0 comments on commit 6a928fe

Please sign in to comment.