Added code to quit more gracefully. Ctrl-C still needs work.

This commit is contained in:
John Wiegley 2009-02-04 20:11:27 -04:00
parent ea1e898eec
commit 796feb2634
2 changed files with 24 additions and 5 deletions

View file

@ -57,6 +57,11 @@ namespace {
return s; 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 split_arguments(char * line)
{ {
strings_list args; strings_list args;
@ -220,6 +225,8 @@ int main(int argc, char * argv[], char * envp[])
if (status == -1) { // no command was given; enter the REPL if (status == -1) { // no command was given; enter the REPL
session->option_version(*session); session->option_version(*session);
std::signal(SIGINT, sigint_handler);
#ifdef HAVE_LIBEDIT #ifdef HAVE_LIBEDIT
rl_readline_name = const_cast<char *>("Ledger"); rl_readline_name = const_cast<char *>("Ledger");
@ -228,22 +235,29 @@ int main(int argc, char * argv[], char * envp[])
#endif #endif
while (char * line = stripwhite(readline("==> "))) { while (char * line = stripwhite(readline("==> "))) {
char * expansion; char * expansion = NULL;
int result; int result;
if (std::strcmp(line, "quit") == 0) {
std::free(line);
break;
}
result = history_expand(line, &expansion); result = history_expand(line, &expansion);
if (result < 0 || result == 2) { if (result < 0 || result == 2) {
std::free(line);
throw_(std::logic_error, throw_(std::logic_error,
"Failed to expand history reference '" << line << "'"); "Failed to expand history reference '" << line << "'");
} else { }
else if (expansion) {
add_history(expansion); add_history(expansion);
strings_list line_argv = split_arguments(line); strings_list line_argv = split_arguments(line);
execute_command_wrapper(*session, line_argv); execute_command_wrapper(*session, line_argv);
}
std::free(expansion);
std::free(expansion);
}
std::free(line); std::free(line);
} }
@ -255,8 +269,12 @@ int main(int argc, char * argv[], char * envp[])
std::cin.getline(line, 1023); std::cin.getline(line, 1023);
char * p = stripwhite(line); char * p = stripwhite(line);
if (*p) if (*p) {
if (std::strcmp(p, "quit") == 0)
break;
execute_command_wrapper(*session, split_arguments(line)); execute_command_wrapper(*session, split_arguments(line));
}
} }
#endif // HAVE_LIBEDIT #endif // HAVE_LIBEDIT

View file

@ -112,6 +112,7 @@ typedef std::ostream::pos_type ostream_pos_type;
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <csignal>
#if defined __FreeBSD__ && __FreeBSD__ <= 4 #if defined __FreeBSD__ && __FreeBSD__ <= 4
// FreeBSD has a broken isspace macro, so don't use it // FreeBSD has a broken isspace macro, so don't use it