From a44bd300f006b74c09a74aff419b43912e114e0c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 29 Sep 2024 11:53:48 +0000 Subject: Ignore pending input before confirmation prompts Pressing ENTER for too long lets it end up in the input buffer which the confirmation prompt happily picks up and accepts as confirmation for deleting potentially half your system or doing other silly things. Ironically, this confirmation will also be silent in terms of output as it is likely erased by future progress reporting. Closes: #1082956 --- apt-private/private-output.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apt-private/private-output.cc b/apt-private/private-output.cc index 4a946f2b8..0c56026a5 100644 --- a/apt-private/private-output.cc +++ b/apt-private/private-output.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -809,6 +810,12 @@ bool YnPrompt(char const * const Question, bool const Default, bool const ShowGl else _error->DumpErrors(c2o, GlobalError::NOTICE); } + // ignore pending input on terminal + if (not AssumeYes && not AssumeNo && isatty(STDIN_FILENO) == 1) + { + tcflush(STDIN_FILENO, TCIFLUSH); + std::cin.clear(); + } c2o << Question << std::flush; -- cgit v1.2.3-70-g09d2 From c4de80035d4164d86ec5f15a7568598993b504b6 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 29 Sep 2024 12:14:19 +0000 Subject: Show prompt reply if input is from non-terminal In the unlikely case input is not a terminal (but not in some way assumed with command line flags) we should print the string we act on after the prompt so that people can see what is happening rather than having the prompt "mysteriously" hidden on terminals or prepended to the first line of the download progress in logs. --- apt-private/private-output.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apt-private/private-output.cc b/apt-private/private-output.cc index 0c56026a5..3d234727f 100644 --- a/apt-private/private-output.cc +++ b/apt-private/private-output.cc @@ -861,9 +861,10 @@ bool YnPrompt(char const * const Question, bool const Default, bool const ShowGl char response[1024] = ""; std::cin.getline(response, sizeof(response)); - if (!std::cin) return false; + if (isatty(STDIN_FILENO) == 0) + c1o << response << '\n'; if (strlen(response) == 0) return Default; -- cgit v1.2.3-70-g09d2