From 25e124deb3c69256b35641cd1055b5328b309bf6 Mon Sep 17 00:00:00 2001 From: Gereon Kremer Date: Wed, 28 Jul 2021 14:41:22 -0700 Subject: [PATCH] Only use libedit on tty inputs (#6946) This PR improves the check when we use libedit: only when the input is an interactive terminal. This is motivated by a change to the unit test for the interactive mode that now properly redirects standard input (and output). --- src/main/interactive_shell.cpp | 11 ++++++++--- test/unit/main/interactive_shell_black.cpp | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/interactive_shell.cpp b/src/main/interactive_shell.cpp index 964b88ea0..763692d07 100644 --- a/src/main/interactive_shell.cpp +++ b/src/main/interactive_shell.cpp @@ -17,11 +17,13 @@ */ #include "main/interactive_shell.h" +#include +#include + #include #include #include #include -#include #include #include #include @@ -104,7 +106,8 @@ InteractiveShell::InteractiveShell(api::Solver* solver, SymbolManager* sm) } #if HAVE_LIBEDITLINE - if(&d_in == &cin) { + if (&d_in == &std::cin && isatty(fileno(stdin))) + { ::rl_readline_name = const_cast("cvc5"); #if EDITLINE_COMPENTRY_FUNC_RETURNS_CHARP ::rl_completion_entry_function = commandGenerator; @@ -155,7 +158,9 @@ InteractiveShell::InteractiveShell(api::Solver* solver, SymbolManager* sm) << ": " << strerror(err) << std::endl; } } - } else { + } + else + { d_usingEditline = false; } #else /* HAVE_LIBEDITLINE */ diff --git a/test/unit/main/interactive_shell_black.cpp b/test/unit/main/interactive_shell_black.cpp index 9a1a46da4..9c842adac 100644 --- a/test/unit/main/interactive_shell_black.cpp +++ b/test/unit/main/interactive_shell_black.cpp @@ -35,10 +35,15 @@ class TestMainBlackInteractiveShell : public TestInternal void SetUp() override { TestInternal::SetUp(); - d_sin.reset(new std::stringstream); - d_sout.reset(new std::stringstream); - d_options.base.in = d_sin.get(); - d_options.base.out = d_sout.get(); + + d_sin = std::make_unique(); + d_stdin = std::cin.rdbuf(); + std::cin.rdbuf(d_sin->rdbuf()); + + d_sout = std::make_unique(); + d_stdout = std::cout.rdbuf(); + std::cout.rdbuf(d_sout->rdbuf()); + d_options.base.inputLanguage = language::input::LANG_CVC; d_solver.reset(new cvc5::api::Solver(&d_options)); d_symman.reset(new SymbolManager(d_solver.get())); @@ -46,7 +51,9 @@ class TestMainBlackInteractiveShell : public TestInternal void TearDown() override { + std::cin.rdbuf(d_stdin); d_sin.reset(nullptr); + std::cout.rdbuf(d_stdout); d_sout.reset(nullptr); // ensure that symbol manager is destroyed before solver d_symman.reset(nullptr); @@ -81,6 +88,9 @@ class TestMainBlackInteractiveShell : public TestInternal std::unique_ptr d_symman; std::unique_ptr d_solver; Options d_options; + + std::streambuf* d_stdin; + std::streambuf* d_stdout; }; TEST_F(TestMainBlackInteractiveShell, assert_true) -- 2.30.2