Skip to content

Commit

Permalink
kernel: group:edit_line with echo needs to handle binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Jun 16, 2023
1 parent c4a0341 commit 538bac4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
9 changes: 6 additions & 3 deletions lib/kernel/src/group.erl
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,12 @@ get_chars_loop(Pbs, M, F, Xa, Drv, Shell, Buf0, State, LineCont0, Encoding) ->
true ->
get_line(Buf0, Pbs, LineCont0, Drv, Shell, Encoding);
false ->
%% get_line_echo_off only deals with lists
%% and does not need encoding...
get_line_echo_off(Buf0, Pbs, Drv, Shell)
%% get_line_echo_off only deals with lists,
%% so convert to list before calling it.
get_line_echo_off(
if Buf0 =:= eof -> eof;
true -> unicode:characters_to_list(Buf0, Encoding)
end, Pbs, Drv, Shell)
end,
case Result of
{done,LineCont1,Buf} ->
Expand Down
31 changes: 25 additions & 6 deletions lib/stdlib/test/io_proto_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
read_modes_ogl/1, broken_unicode/1,eof_on_pipe/1,
unicode_prompt/1, shell_slogan/1, raw_stdout/1, raw_stdout_isatty/1,
file_read_stdin_binary_mode/1, file_read_stdin_list_mode/1,
io_get_chars_stdin_binary_mode/1, io_get_chars_stdin_list_mode/1
io_get_chars_stdin_binary_mode/1, io_get_chars_stdin_list_mode/1,
io_get_chars_file_read_stdin_binary_mode/1
]).


Expand Down Expand Up @@ -60,8 +61,8 @@ all() ->
file_read_stdin_binary_mode,
file_read_stdin_list_mode,
io_get_chars_stdin_binary_mode,
io_get_chars_stdin_list_mode

io_get_chars_stdin_list_mode,
io_get_chars_file_read_stdin_binary_mode
].

groups() ->
Expand Down Expand Up @@ -339,6 +340,27 @@ io_get_chars_stdin_list_mode(_Config) ->

ok.

%% Test that mixing io:get_chars and file:read works when stdin is in binary mode.
io_get_chars_file_read_stdin_binary_mode(_Config) ->
{ok, P, ErlPort} = start_stdin_node(
fun() -> case file:read(standard_io, 1) of
eof -> eof;
{ok, Chars} ->
case io:get_line(standard_io, "") of
eof -> Chars;
Line ->
{ok, [Chars, Line]}
end
end
end, [binary]),

erlang:port_command(ErlPort, "1\n"),
{ok, "got: [<<\"1\">>,<<\"\\n\">>]\n"} = gen_tcp:recv(P, 0),
ErlPort ! {self(), close},
{ok, "got: eof"} = gen_tcp:recv(P, 0),

ok.

start_stdin_node(ReadFun, IoOptions) ->
{ok, L} = gen_tcp:listen(0,[{active, false},{packet,4}]),
{ok, Port} = inet:port(L),
Expand All @@ -356,9 +378,6 @@ start_stdin_node(ReadFun, IoOptions) ->

read_raw_from_stdin([Port]) ->
try
dbg:tracer(file, "/home/eluklar/git/otp/trace.txt"),
dbg:p(whereis(user),[c,m]),
dbg:tpl(io_lib, x),
{ok, P} = gen_tcp:connect(localhost, list_to_integer(atom_to_list(Port)),
[binary, {packet, 4}, {active, false}]),
{ok, OptionsBin} = gen_tcp:recv(P, 0),
Expand Down

0 comments on commit 538bac4

Please sign in to comment.