diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-08 02:33:19 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-08 02:35:32 +0200 |
commit | 3339f43ec9d4aa64448cad9185258fdd9efd337b (patch) | |
tree | cb95907125b567a8739ece7e973ac1edf4db5e59 | |
parent | f0d60bb689324dd36e2266bbde6ff537f6d2233f (diff) | |
download | json-rpc-shell-3339f43ec9d4aa64448cad9185258fdd9efd337b.tar.gz json-rpc-shell-3339f43ec9d4aa64448cad9185258fdd9efd337b.tar.xz json-rpc-shell-3339f43ec9d4aa64448cad9185258fdd9efd337b.zip |
Watch for SIGINT and SIGTERM
-rw-r--r-- | json-rpc-shell.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/json-rpc-shell.c b/json-rpc-shell.c index bdbf27b..52b4e70 100644 --- a/json-rpc-shell.c +++ b/json-rpc-shell.c @@ -1875,17 +1875,34 @@ on_winch (EV_P_ ev_signal *handle, int revents) } static void +quit (struct app_context *ctx) +{ + if (ctx->backend->on_quit) + ctx->backend->on_quit (ctx); + + ev_break (EV_DEFAULT_ EVBREAK_ALL); +} + +static void +on_terminated (EV_P_ ev_signal *handle, int revents) +{ + (void) loop; + (void) handle; + (void) revents; + + quit (&g_ctx); +} + +static void on_readline_input (char *line) { // Otherwise the prompt is shown at all times + // Stupid readline forces us to use a global variable g_ctx.readline_prompt_shown = false; if (!line) { - if (g_ctx.backend->on_quit) - g_ctx.backend->on_quit (&g_ctx); - - ev_break (EV_DEFAULT_ EVBREAK_ALL); + quit (&g_ctx); // We must do this here, or the prompt gets printed twice. *shrug* rl_callback_handler_remove (); @@ -1899,7 +1916,6 @@ on_readline_input (char *line) if (*line) add_history (line); - // Stupid readline forces us to use a global variable process_input (&g_ctx, line); free (line); @@ -2099,11 +2115,19 @@ main (int argc, char *argv[]) exit_fatal ("libev initialization failed"); ev_signal winch_watcher; + ev_signal term_watcher; + ev_signal int_watcher; ev_io tty_watcher; ev_signal_init (&winch_watcher, on_winch, SIGWINCH); ev_signal_start (EV_DEFAULT_ &winch_watcher); + ev_signal_init (&term_watcher, on_terminated, SIGTERM); + ev_signal_start (EV_DEFAULT_ &term_watcher); + + ev_signal_init (&int_watcher, on_terminated, SIGINT); + ev_signal_start (EV_DEFAULT_ &int_watcher); + ev_io_init (&tty_watcher, on_tty_readable, STDIN_FILENO, EV_READ); ev_io_start (EV_DEFAULT_ &tty_watcher); |