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;
}
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

View file

@ -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