File 0115-erts-Fix-oldshell-and-noshell-on-Windows.patch of Package erlang
From 142ebf3b5fd85f90f27a9bc784a39979b790a6fa Mon Sep 17 00:00:00 2001
From: Lukas Larsson <lukas@erlang.org>
Date: Mon, 5 Jun 2023 09:31:31 +0200
Subject: [PATCH 1/2] erts: Fix -oldshell and -noshell on Windows
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
---
erts/emulator/nifs/common/prim_tty_nif.c | 17 ++++++++++++++++-
lib/kernel/src/prim_tty.erl | 14 ++++++++------
2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/erts/emulator/nifs/common/prim_tty_nif.c b/erts/emulator/nifs/common/prim_tty_nif.c
index 06e336b650..c8edcc4cee 100644
--- a/erts/emulator/nifs/common/prim_tty_nif.c
+++ b/erts/emulator/nifs/common/prim_tty_nif.c
@@ -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;
@@ -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[]) {
@@ -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];
diff --git a/lib/kernel/src/prim_tty.erl b/lib/kernel/src/prim_tty.erl
index e58eb6682f..8b09744b6b 100644
--- a/lib/kernel/src/prim_tty.erl
+++ b/lib/kernel/src/prim_tty.erl
@@ -131,11 +131,11 @@
-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,
- reader,
- writer,
+-record(state, {tty :: tty() | undefined,
+ reader :: {pid(), reference()} | undefined,
+ writer :: {pid(), reference()} | undefined,
options,
- unicode,
+ unicode = true :: boolean(),
lines_before = [], %% All lines before the current line in reverse order
lines_after = [], %% All lines after the current line.
buffer_before = [], %% Current line before cursor in reverse
@@ -184,6 +184,7 @@
{move_combo, integer(), integer(), integer()} |
clear |
beep.
+-type tty() :: reference().
-opaque state() :: #state{}.
-export_type([state/0]).
@@ -433,7 +434,7 @@ reader([TTY, Parent]) ->
FromEnc = case os:type() of
{unix, _} -> utf8;
{win32, _} ->
- case isatty(stdin) of
+ case isatty(TTY) of
true ->
{utf16, little};
_ ->
@@ -1212,9 +1213,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) ->
--
2.35.3