summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cc28
-rw-r--r--src/system.hh1
2 files changed, 24 insertions, 5 deletions
diff --git a/src/main.cc b/src/main.cc
index 63115dd3..221374c0 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -57,6 +57,11 @@ namespace {
return s;
}
+ void sigint_handler(int sig)
+ {
+ throw std::logic_error("Interrupted by user (use Control-D to quit)");
+ }
+
strings_list split_arguments(char * line)
{
strings_list args;
@@ -219,6 +224,8 @@ int main(int argc, char * argv[], char * envp[])
int status = execute_command_wrapper(*session, cmd_args, envp);
if (status == -1) { // no command was given; enter the REPL
session->option_version(*session);
+
+ std::signal(SIGINT, sigint_handler);
#ifdef HAVE_LIBEDIT
@@ -228,22 +235,29 @@ int main(int argc, char * argv[], char * envp[])
#endif
while (char * line = stripwhite(readline("==> "))) {
- char * expansion;
+ char * expansion = NULL;
int result;
+ if (std::strcmp(line, "quit") == 0) {
+ std::free(line);
+ break;
+ }
+
result = history_expand(line, &expansion);
if (result < 0 || result == 2) {
+ std::free(line);
throw_(std::logic_error,
"Failed to expand history reference '" << line << "'");
- } else {
+ }
+ else if (expansion) {
add_history(expansion);
strings_list line_argv = split_arguments(line);
execute_command_wrapper(*session, line_argv);
- }
- std::free(expansion);
+ std::free(expansion);
+ }
std::free(line);
}
@@ -255,8 +269,12 @@ int main(int argc, char * argv[], char * envp[])
std::cin.getline(line, 1023);
char * p = stripwhite(line);
- if (*p)
+ if (*p) {
+ if (std::strcmp(p, "quit") == 0)
+ break;
+
execute_command_wrapper(*session, split_arguments(line));
+ }
}
#endif // HAVE_LIBEDIT
diff --git a/src/system.hh b/src/system.hh
index f70ee57b..69b8c4e1 100644
--- a/src/system.hh
+++ b/src/system.hh
@@ -112,6 +112,7 @@ typedef std::ostream::pos_type ostream_pos_type;
#include <cstdlib>
#include <cstring>
#include <ctime>
+#include <csignal>
#if defined __FreeBSD__ && __FreeBSD__ <= 4
// FreeBSD has a broken isspace macro, so don't use it