about summary refs log tree commit diff
path: root/nix-repl.cc
diff options
context:
space:
mode:
authorScott Olson <scott@solson.me>2016-02-18T12·50-0600
committerScott Olson <scott@solson.me>2016-02-18T12·50-0600
commit64080d26fe9364bc0ea0893f357386ed3121878f (patch)
treee9fd9dcef1e2ba4a6fe779d06f71eda608560810 /nix-repl.cc
parent2d729e4f6f45c079ddf149610357e648e805f42c (diff)
Cancel multiline input on Ctrl-C.
Diffstat (limited to 'nix-repl.cc')
-rw-r--r--nix-repl.cc20
1 files changed, 9 insertions, 11 deletions
diff --git a/nix-repl.cc b/nix-repl.cc
index 9dddb26037ad..8d2fbd919c9d 100644
--- a/nix-repl.cc
+++ b/nix-repl.cc
@@ -41,7 +41,7 @@ struct NixRepl
     NixRepl(const Strings & searchPath);
     void mainLoop(const Strings & files);
     void completePrefix(string prefix);
-    bool getLine(string & line, const char * prompt);
+    bool getLine(string & input, const char * prompt);
     bool processLine(string line);
     void loadFile(const Path & path);
     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
         // number of chars as the prompt.
         const char * prompt = input.empty() ? "nix-repl> " : "          ";
-        string line;
-        if (!getLine(line, prompt)) {
+        if (!getLine(input, prompt)) {
             std::cout << std::endl;
             break;
         }
 
-        input.append(removeWhitespace(line));
-        input.push_back('\n');
-
         try {
             if (!processLine(input)) return;
         } 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;
     act.sa_handler = sigintHandler;
@@ -176,15 +172,17 @@ bool NixRepl::getLine(string & line, const char * prompt)
     if (sigaction(SIGINT, &act, &old))
         throw SysError("installing handler for SIGINT");
 
-    if (sigsetjmp(sigintJmpBuf, 1))
-        line = "";
-    else {
+    if (sigsetjmp(sigintJmpBuf, 1)) {
+        input.clear();
+    } else {
         curRepl = this;
         rl_completion_entry_function = completerThunk;
 
         char * s = readline(prompt);
         if (!s) return false;
-        line = chomp(string(s));
+        string line = chomp(string(s));
+        input.append(removeWhitespace(line));
+        input.push_back('\n');
         free(s);
         if (line != "") {
             add_history(line.c_str());