Commit 1e34b12a authored by kyab's avatar kyab Committed by Yasuhiro Matsumoto

mirb: Don't exit on Ctrl-C

parent 76d44b1b
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <signal.h>
#include <setjmp.h>
#ifdef ENABLE_READLINE #ifdef ENABLE_READLINE
#include <readline/readline.h> #include <readline/readline.h>
#include <readline/history.h> #include <readline/history.h>
...@@ -29,6 +32,16 @@ ...@@ -29,6 +32,16 @@
#define MIRB_USING_HISTORY() #define MIRB_USING_HISTORY()
#endif #endif
#ifndef _WIN32
#define MIRB_SIGSETJMP(env) sigsetjmp(env, 1)
#define MIRB_SIGLONGJMP(env, val) siglongjmp(env, val)
#define SIGJMP_BUF sigjmp_buf
#else
#define MIRB_SIGSETJMP(env) setjmp(env)
#define MIRB_SIGLONGJMP(env, val) longjmp(env, val)
#define SIGJMP_BUF jmp_buf
#endif
#include <mruby.h> #include <mruby.h>
#include <mruby/array.h> #include <mruby/array.h>
#include <mruby/proc.h> #include <mruby/proc.h>
...@@ -326,6 +339,23 @@ check_keyword(const char *buf, const char *word) ...@@ -326,6 +339,23 @@ check_keyword(const char *buf, const char *word)
return 1; return 1;
} }
#ifndef ENABLE_READLINE
volatile sig_atomic_t input_canceled = 0;
void
ctrl_c_handler(int signo)
{
input_canceled = 1;
}
#else
SIGJMP_BUF ctrl_c_buf;
void
ctrl_c_handler(int signo)
{
MIRB_SIGLONGJMP(ctrl_c_buf, 1);
}
#endif
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
...@@ -336,6 +366,7 @@ main(int argc, char **argv) ...@@ -336,6 +366,7 @@ main(int argc, char **argv)
size_t char_index; size_t char_index;
#else #else
char *history_path; char *history_path;
char* line;
#endif #endif
mrbc_context *cxt; mrbc_context *cxt;
struct mrb_parser_state *parser; struct mrb_parser_state *parser;
...@@ -410,6 +441,7 @@ main(int argc, char **argv) ...@@ -410,6 +441,7 @@ main(int argc, char **argv)
#ifndef ENABLE_READLINE #ifndef ENABLE_READLINE
print_cmdline(code_block_open); print_cmdline(code_block_open);
signal(SIGINT, ctrl_c_handler);
char_index = 0; char_index = 0;
while ((last_char = getchar()) != '\n') { while ((last_char = getchar()) != '\n') {
if (last_char == EOF) break; if (last_char == EOF) break;
...@@ -419,6 +451,15 @@ main(int argc, char **argv) ...@@ -419,6 +451,15 @@ main(int argc, char **argv)
} }
last_code_line[char_index++] = last_char; last_code_line[char_index++] = last_char;
} }
signal(SIGINT, SIG_DFL);
if (input_canceled) {
ruby_code[0] = '\0';
last_code_line[0] = '\0';
code_block_open = FALSE;
puts("^C");
input_canceled = 0;
continue;
}
if (last_char == EOF) { if (last_char == EOF) {
fputs("\n", stdout); fputs("\n", stdout);
break; break;
...@@ -427,7 +468,19 @@ main(int argc, char **argv) ...@@ -427,7 +468,19 @@ main(int argc, char **argv)
last_code_line[char_index++] = '\n'; last_code_line[char_index++] = '\n';
last_code_line[char_index] = '\0'; last_code_line[char_index] = '\0';
#else #else
if (MIRB_SIGSETJMP(ctrl_c_buf) == 0) {
;
}
else {
ruby_code[0] = '\0';
last_code_line[0] = '\0';
code_block_open = FALSE;
puts("^C");
}
signal(SIGINT, ctrl_c_handler);
line = MIRB_READLINE(code_block_open ? "* " : "> "); line = MIRB_READLINE(code_block_open ? "* " : "> ");
signal(SIGINT, SIG_DFL);
if (line == NULL) { if (line == NULL) {
printf("\n"); printf("\n");
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment