Skip to content

Commit

Permalink
erts: Fix -oldshell and -noshell on Windows
Browse files Browse the repository at this point in the history
When not using a terminal we need to use ReadFile to
read from stdin. Before this fix we only used ReadFile
if stdin was not a terminal, but the same behaviour
should be used if we disable the terminal using
flags to erl.exe.

Fixes #7324
  • Loading branch information
garazdawi committed Jun 7, 2023
1 parent d653acf commit 22287c2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 16 additions & 1 deletion erts/emulator/nifs/common/prim_tty_nif.c
Expand Up @@ -212,6 +212,10 @@ static int tty_get_fd(ErlNifEnv *env, ERL_NIF_TERM atom, int *fd) {

static ERL_NIF_TERM isatty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
int fd;
#ifdef __WIN32__
TTYResource *tty;
#endif

if (tty_get_fd(env, argv[0], &fd)) {
if (isatty(fd)) {
return atom_true;
Expand All @@ -221,7 +225,18 @@ static ERL_NIF_TERM isatty_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv
return atom_ebadf;
}
}

#ifdef __WIN32__
if (!enif_get_resource(env, argv[0], tty_rt, (void **)&tty))
return enif_make_badarg(env);
if (tty->tty && tty->dwInMode)
return atom_true;
else
return atom_false;
#else
return enif_make_badarg(env);
#endif

}

static ERL_NIF_TERM isprint_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
Expand Down Expand Up @@ -353,7 +368,7 @@ static ERL_NIF_TERM tty_read_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM ar
return enif_make_badarg(env);

#ifdef __WIN32__
if (tty->dwInMode) {
if (tty->tty && tty->dwInMode) {
ssize_t inputs_read, num_characters = 0;
wchar_t *characters = NULL;
INPUT_RECORD inputs[128];
Expand Down
8 changes: 5 additions & 3 deletions lib/kernel/src/prim_tty.erl
Expand Up @@ -131,7 +131,7 @@
-endif.
%% Copied from https://github.com/chalk/ansi-regex/blob/main/index.js
-define(ANSI_REGEXP, <<"^[\e",194,155,"][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?",7,")|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))">>).
-record(state, {tty,
-record(state, {tty :: tty(),
reader,
writer,
options,
Expand Down Expand Up @@ -184,6 +184,7 @@
{move_combo, integer(), integer(), integer()} |
clear |
beep.
-type tty() :: reference().
-opaque state() :: #state{}.
-export_type([state/0]).

Expand Down Expand Up @@ -430,7 +431,7 @@ reader([TTY, Parent]) ->
FromEnc = case os:type() of
{unix, _} -> utf8;
{win32, _} ->
case isatty(stdin) of
case isatty(TTY) of
true ->
{utf16, little};
_ ->
Expand Down Expand Up @@ -1209,9 +1210,10 @@ dbg(_) ->
-endif.

%% Nif functions
-spec isatty(stdin | stdout | stderr) -> boolean() | ebadf.
-spec isatty(stdin | stdout | stderr | tty()) -> boolean() | ebadf.
isatty(_Fd) ->
erlang:nif_error(undef).
-spec tty_create() -> {ok, tty()}.
tty_create() ->
erlang:nif_error(undef).
tty_init(_TTY, _Fd, _Options) ->
Expand Down

0 comments on commit 22287c2

Please sign in to comment.