Skip to content

kernel: Update standard_error to use prim_tty nif #9116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bootstrap/lib/kernel/ebin/prim_tty.beam
Binary file not shown.
Binary file modified bootstrap/lib/kernel/ebin/standard_error.beam
Binary file not shown.
11 changes: 8 additions & 3 deletions erts/emulator/nifs/common/prim_tty_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static ERL_NIF_TERM tty_tgoto_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM a

static ErlNifFunc nif_funcs[] = {
{"isatty", 1, isatty_nif},
{"tty_create", 0, tty_create_nif},
{"tty_create", 1, tty_create_nif},
{"tty_init", 2, tty_init_nif},
{"setlocale", 1, setlocale_nif},
{"tty_is_open", 2, tty_is_open},
Expand Down Expand Up @@ -884,7 +884,9 @@ static ERL_NIF_TERM tty_create_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM

#ifndef __WIN32__
tty->ifd = 0;
tty->ofd = 1;
tty->ofd = 0;
if (!tty_get_fd(env, argv[0], &tty->ofd) || tty->ofd == 0)
return enif_make_badarg(env);

#ifdef HAVE_TERMCAP
if (tcgetattr(tty->ofd, &tty->tty_rmode) >= 0) {
Expand All @@ -899,7 +901,10 @@ static ERL_NIF_TERM tty_create_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM
tty->ifd = CreateFile("nul", GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
tty->ofd = GetStdHandle(STD_OUTPUT_HANDLE);

if (enif_is_identical(argv[0], atom_stdin))
return enif_make_badarg(env);
tty->ofd = tty_get_handle(env, argv[0]);
if (tty->ofd == INVALID_HANDLE_VALUE || tty->ofd == NULL) {
tty->ofd = CreateFile("nul", GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Expand Down
29 changes: 17 additions & 12 deletions lib/kernel/src/prim_tty.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
-export([reader_stop/1, disable_reader/1, enable_reader/1, read/1, read/2,
is_reader/2, is_writer/2, output_mode/1]).

-nifs([isatty/1, tty_create/0, tty_init/2, setlocale/1,
-nifs([isatty/1, tty_create/1, tty_init/2, setlocale/1,
tty_select/2, tty_window_size/1,
tty_encoding/1, tty_is_open/2, write_nif/2, read_nif/3, isprint/1,
wcwidth/1, wcswidth/1,
Expand All @@ -128,8 +128,6 @@
%% proc_lib exports
-export([reader/1, writer/1]).

-on_load(on_load/0).

%%-define(debug, true).
-ifdef(debug).
-define(dbg(Term), dbg(Term)).
Expand Down Expand Up @@ -170,7 +168,8 @@
}).

-type options() :: #{ input := cooked | raw | disabled,
output := raw | cooked }.
output := raw | cooked,
ofd => stdout | stderr }.
-type request() ::
{putc_raw, binary()} |
{putc, unicode:unicode_binary()} |
Expand Down Expand Up @@ -251,8 +250,10 @@ window_size(State = #state{ tty = TTY }) ->
-spec init(options()) -> state().
init(UserOptions) when is_map(UserOptions) ->

on_load(),

Options = options(UserOptions),
{ok, TTY} = tty_create(),
{ok, TTY} = tty_create(maps:get(ofd, Options)),

%% Initialize the locale to see if we support utf-8 or not
UnicodeSupported =
Expand Down Expand Up @@ -295,7 +296,7 @@ init_term(State = #state{ tty = TTY, options = Options }) ->

WriterState =
if TTYState#state.writer =:= undefined ->
{ok, Writer} = proc_lib:start_link(?MODULE, writer, [State#state.tty]),
{ok, Writer} = proc_lib:start_link(?MODULE, writer, [[State#state.tty, self()]]),
TTYState#state{ writer = Writer };
true ->
TTYState
Expand Down Expand Up @@ -356,7 +357,7 @@ reinit(State = #state{ options = OldOptions }, UserOptions) ->
end.

options(UserOptions) ->
maps:merge(#{ input => raw, output => cooked }, UserOptions).
maps:merge(#{ input => raw, output => cooked, ofd => stdout }, UserOptions).

init(State, ssh) ->
State#state{ xn = true };
Expand Down Expand Up @@ -541,7 +542,7 @@ call(Pid, Msg) ->
end.

reader([TTY, Encoding, Parent]) ->
register(user_drv_reader, self()),
set_name(Parent, "reader"),
ReaderRef = make_ref(),

ok = tty_select(TTY, ReaderRef),
Expand Down Expand Up @@ -638,8 +639,8 @@ flush_unicode_state(FromEnc) ->
FromEnc
end.

writer(TTY) ->
register(user_drv_writer, self()),
writer([TTY, Parent]) ->
set_name(Parent, "writer"),
WriterRef = make_ref(),
proc_lib:init_ack({ok, {self(), WriterRef}}),
writer_loop(TTY, WriterRef).
Expand Down Expand Up @@ -673,6 +674,10 @@ writer_loop(TTY, WriterRef) ->
end
end.

set_name(Parent, Postfix) ->
{registered_name, Name} = erlang:process_info(Parent, registered_name),
register(list_to_atom(atom_to_list(Name) ++ "_" ++ Postfix), self()).

-spec handle_request(state(), request()) -> {erlang:iovec(), state()}.
handle_request(State = #state{ options = #{ output := raw } }, Request) ->
case Request of
Expand Down Expand Up @@ -1467,8 +1472,8 @@ dbg(_) ->
-spec isatty(stdin | stdout | stderr | tty()) -> boolean() | ebadf.
isatty(_Fd) ->
erlang:nif_error(undef).
-spec tty_create() -> {ok, tty()}.
tty_create() ->
-spec tty_create(stdout | stderr) -> {ok, tty()}.
tty_create(_Fd) ->
erlang:nif_error(undef).
tty_init(_TTY, _Options) ->
erlang:nif_error(undef).
Expand Down
Loading
Loading