Skip to content
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

repl: Restore CTRL-C behaviour #2767

Merged
merged 1 commit into from
May 8, 2019
Merged
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
40 changes: 40 additions & 0 deletions src/nix/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ static int listPossibleCallback(char *s, char ***avp) {
return ac;
}

namespace {
// Used to communicate to NixRepl::getLine whether a signal occurred in ::readline.
volatile sig_atomic_t g_signal_received = 0;

void sigintHandler(int signo) {
g_signal_received = signo;
}
}

void NixRepl::mainLoop(const std::vector<std::string> & files)
{
Expand Down Expand Up @@ -251,8 +259,40 @@ void NixRepl::mainLoop(const std::vector<std::string> & files)

bool NixRepl::getLine(string & input, const std::string &prompt)
{
struct sigaction act, old;
sigset_t savedSignalMask, set;

auto setupSignals = [&]() {
act.sa_handler = sigintHandler;
sigfillset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGINT, &act, &old))
throw SysError("installing handler for SIGINT");

sigemptyset(&set);
sigaddset(&set, SIGINT);
if (sigprocmask(SIG_UNBLOCK, &set, &savedSignalMask))
throw SysError("unblocking SIGINT");
};
auto restoreSignals = [&]() {
if (sigprocmask(SIG_SETMASK, &savedSignalMask, nullptr))
throw SysError("restoring signals");

if (sigaction(SIGINT, &old, 0))
throw SysError("restoring handler for SIGINT");
};

setupSignals();
char * s = readline(prompt.c_str());
Finally doFree([&]() { free(s); });
restoreSignals();

if (g_signal_received) {
g_signal_received = 0;
input.clear();
return true;
}

if (!s)
return false;
input += s;
Expand Down