Watch for SIGINT and SIGTERM

This commit is contained in:
Přemysl Eric Janouch 2015-04-08 02:33:19 +02:00
parent f0d60bb689
commit 3339f43ec9
1 changed files with 29 additions and 5 deletions

View File

@ -1874,18 +1874,35 @@ on_winch (EV_P_ ev_signal *handle, int revents)
rl_resize_terminal (); rl_resize_terminal ();
} }
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 static void
on_readline_input (char *line) on_readline_input (char *line)
{ {
// Otherwise the prompt is shown at all times // Otherwise the prompt is shown at all times
// Stupid readline forces us to use a global variable
g_ctx.readline_prompt_shown = false; g_ctx.readline_prompt_shown = false;
if (!line) if (!line)
{ {
if (g_ctx.backend->on_quit) quit (&g_ctx);
g_ctx.backend->on_quit (&g_ctx);
ev_break (EV_DEFAULT_ EVBREAK_ALL);
// We must do this here, or the prompt gets printed twice. *shrug* // We must do this here, or the prompt gets printed twice. *shrug*
rl_callback_handler_remove (); rl_callback_handler_remove ();
@ -1899,7 +1916,6 @@ on_readline_input (char *line)
if (*line) if (*line)
add_history (line); add_history (line);
// Stupid readline forces us to use a global variable
process_input (&g_ctx, line); process_input (&g_ctx, line);
free (line); free (line);
@ -2099,11 +2115,19 @@ main (int argc, char *argv[])
exit_fatal ("libev initialization failed"); exit_fatal ("libev initialization failed");
ev_signal winch_watcher; ev_signal winch_watcher;
ev_signal term_watcher;
ev_signal int_watcher;
ev_io tty_watcher; ev_io tty_watcher;
ev_signal_init (&winch_watcher, on_winch, SIGWINCH); ev_signal_init (&winch_watcher, on_winch, SIGWINCH);
ev_signal_start (EV_DEFAULT_ &winch_watcher); 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_init (&tty_watcher, on_tty_readable, STDIN_FILENO, EV_READ);
ev_io_start (EV_DEFAULT_ &tty_watcher); ev_io_start (EV_DEFAULT_ &tty_watcher);