1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 11:11:03 -04:00

Cancel multiline input on Ctrl-C.

This commit is contained in:
Scott Olson 2016-02-18 06:50:52 -06:00
parent 2d729e4f6f
commit 64080d26fe

View file

@ -41,7 +41,7 @@ struct NixRepl
NixRepl(const Strings & searchPath); NixRepl(const Strings & searchPath);
void mainLoop(const Strings & files); void mainLoop(const Strings & files);
void completePrefix(string prefix); void completePrefix(string prefix);
bool getLine(string & line, const char * prompt); bool getLine(string & input, const char * prompt);
bool processLine(string line); bool processLine(string line);
void loadFile(const Path & path); void loadFile(const Path & path);
void initEnv(); void initEnv();
@ -102,15 +102,11 @@ void NixRepl::mainLoop(const Strings & files)
// When continuing input from a previous, don't print a prompt, just align to the same // When continuing input from a previous, don't print a prompt, just align to the same
// number of chars as the prompt. // number of chars as the prompt.
const char * prompt = input.empty() ? "nix-repl> " : " "; const char * prompt = input.empty() ? "nix-repl> " : " ";
string line; if (!getLine(input, prompt)) {
if (!getLine(line, prompt)) {
std::cout << std::endl; std::cout << std::endl;
break; break;
} }
input.append(removeWhitespace(line));
input.push_back('\n');
try { try {
if (!processLine(input)) return; if (!processLine(input)) return;
} catch (ParseError & e) { } catch (ParseError & e) {
@ -167,7 +163,7 @@ char * completerThunk(const char * s, int state)
} }
bool NixRepl::getLine(string & line, const char * prompt) bool NixRepl::getLine(string & input, const char * prompt)
{ {
struct sigaction act, old; struct sigaction act, old;
act.sa_handler = sigintHandler; act.sa_handler = sigintHandler;
@ -176,15 +172,17 @@ bool NixRepl::getLine(string & line, const char * prompt)
if (sigaction(SIGINT, &act, &old)) if (sigaction(SIGINT, &act, &old))
throw SysError("installing handler for SIGINT"); throw SysError("installing handler for SIGINT");
if (sigsetjmp(sigintJmpBuf, 1)) if (sigsetjmp(sigintJmpBuf, 1)) {
line = ""; input.clear();
else { } else {
curRepl = this; curRepl = this;
rl_completion_entry_function = completerThunk; rl_completion_entry_function = completerThunk;
char * s = readline(prompt); char * s = readline(prompt);
if (!s) return false; if (!s) return false;
line = chomp(string(s)); string line = chomp(string(s));
input.append(removeWhitespace(line));
input.push_back('\n');
free(s); free(s);
if (line != "") { if (line != "") {
add_history(line.c_str()); add_history(line.c_str());