degesch: shuffle some code

This commit is contained in:
Přemysl Eric Janouch 2015-05-09 06:00:40 +02:00
parent 1b2fabe4cc
commit 699636d9a2

234
degesch.c
View File

@ -1666,72 +1666,6 @@ init_colors (struct app_context *ctx)
g_log_message_real = log_message_attributed;
}
// --- Signals -----------------------------------------------------------------
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
/// Program termination has been requested by a signal
static volatile sig_atomic_t g_termination_requested;
/// The window has changed in size
static volatile sig_atomic_t g_winch_received;
static void
sigterm_handler (int signum)
{
(void) signum;
g_termination_requested = true;
int original_errno = errno;
if (write (g_signal_pipe[1], "t", 1) == -1)
soft_assert (errno == EAGAIN);
errno = original_errno;
}
static void
sigwinch_handler (int signum)
{
(void) signum;
g_winch_received = true;
int original_errno = errno;
if (write (g_signal_pipe[1], "w", 1) == -1)
soft_assert (errno == EAGAIN);
errno = original_errno;
}
static void
setup_signal_handlers (void)
{
if (pipe (g_signal_pipe) == -1)
exit_fatal ("%s: %s", "pipe", strerror (errno));
set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]);
// So that the pipe cannot overflow; it would make write() block within
// the signal handler, which is something we really don't want to happen.
// The same holds true for read().
set_blocking (g_signal_pipe[0], false);
set_blocking (g_signal_pipe[1], false);
signal (SIGPIPE, SIG_IGN);
struct sigaction sa;
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigwinch_handler;
sigemptyset (&sa.sa_mask);
if (sigaction (SIGWINCH, &sa, NULL) == -1)
exit_fatal ("sigaction: %s", strerror (errno));
sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1)
exit_fatal ("sigaction: %s", strerror (errno));
}
// --- Output formatter --------------------------------------------------------
// This complicated piece of code makes attributed text formatting simple.
@ -5796,49 +5730,6 @@ app_editline_init (struct input *self)
#endif // HAVE_EDITLINE
// --- I/O event handlers ------------------------------------------------------
static void
on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx)
{
char dummy;
(void) read (fd->fd, &dummy, 1);
if (g_termination_requested && !ctx->quitting)
{
// There may be a timer set to reconnect to the server
// TODO: multiserver
struct server *s = &ctx->server;
// TODO: a faster timer for quitting
irc_reset_connection_timeouts (s);
// FIXME: use a normal quit message
if (irc_is_connected (s))
irc_send (s, "QUIT :Terminated by signal");
initiate_quit (ctx);
}
if (g_winch_received)
{
input_on_terminal_resized (&ctx->input);
update_screen_size ();
}
}
static void
on_tty_readable (const struct pollfd *fd, struct app_context *ctx)
{
(void) ctx;
if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
// XXX: this may loop for a bit: stop the event or eat the input?
// (This prevents a segfault when the input has been stopped.)
if (ctx->input.active)
input_on_readable (&ctx->input);
}
// --- Configuration loading ---------------------------------------------------
static bool
@ -5963,7 +5854,114 @@ load_configuration (struct app_context *ctx)
get_config_integer (ctx, "server.reconnect_delay");
}
// --- Main program ------------------------------------------------------------
// --- Signals -----------------------------------------------------------------
static int g_signal_pipe[2]; ///< A pipe used to signal... signals
/// Program termination has been requested by a signal
static volatile sig_atomic_t g_termination_requested;
/// The window has changed in size
static volatile sig_atomic_t g_winch_received;
static void
sigterm_handler (int signum)
{
(void) signum;
g_termination_requested = true;
int original_errno = errno;
if (write (g_signal_pipe[1], "t", 1) == -1)
soft_assert (errno == EAGAIN);
errno = original_errno;
}
static void
sigwinch_handler (int signum)
{
(void) signum;
g_winch_received = true;
int original_errno = errno;
if (write (g_signal_pipe[1], "w", 1) == -1)
soft_assert (errno == EAGAIN);
errno = original_errno;
}
static void
setup_signal_handlers (void)
{
if (pipe (g_signal_pipe) == -1)
exit_fatal ("%s: %s", "pipe", strerror (errno));
set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]);
// So that the pipe cannot overflow; it would make write() block within
// the signal handler, which is something we really don't want to happen.
// The same holds true for read().
set_blocking (g_signal_pipe[0], false);
set_blocking (g_signal_pipe[1], false);
signal (SIGPIPE, SIG_IGN);
struct sigaction sa;
sa.sa_flags = SA_RESTART;
sa.sa_handler = sigwinch_handler;
sigemptyset (&sa.sa_mask);
if (sigaction (SIGWINCH, &sa, NULL) == -1)
exit_fatal ("sigaction: %s", strerror (errno));
sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1)
exit_fatal ("sigaction: %s", strerror (errno));
}
// --- I/O event handlers ------------------------------------------------------
static void
on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx)
{
char dummy;
(void) read (fd->fd, &dummy, 1);
if (g_termination_requested && !ctx->quitting)
{
// There may be a timer set to reconnect to the server
// TODO: multiserver
struct server *s = &ctx->server;
// TODO: a faster timer for quitting
irc_reset_connection_timeouts (s);
// FIXME: use a normal quit message
if (irc_is_connected (s))
irc_send (s, "QUIT :Terminated by signal");
initiate_quit (ctx);
}
if (g_winch_received)
{
input_on_terminal_resized (&ctx->input);
update_screen_size ();
}
}
static void
on_tty_readable (const struct pollfd *fd, struct app_context *ctx)
{
(void) ctx;
if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
// XXX: this may loop for a bit: stop the event or eat the input?
// (This prevents a segfault when the input has been stopped.)
if (ctx->input.active)
input_on_readable (&ctx->input);
}
static void
init_poller_events (struct app_context *ctx)
@ -5979,17 +5977,19 @@ init_poller_events (struct app_context *ctx)
poller_fd_set (&ctx->tty_event, POLLIN);
}
// --- Main program ------------------------------------------------------------
static void
display_logo (void)
{
const char *logo =
" __ __ \n"
" __/ / ____ ____ ____ ____ ____ / /_ \n"
" / / / , / / / / , / / __/ / __/ / __ \\ \n"
" / / / / __/ / / / / __/ /_ / / /_ / / / / \n"
" /___/ /___/ /_ / /___/ /___/ /___/ /_/ /_/ \n"
" /___/ \n"
" ";
" __ __ \n"
" __/ / ____ ____ ____ ____ ____ / /_ \n"
" / / / , / / / / , / / __/ / __/ / __ \\ \n"
" / / / / __/ / / / / __/ /_ / / /_ / / / / \n"
" /___/ /___/ /_ / /___/ /___/ /___/ /_/ /_/ \n"
" /___/ \n"
" ";
struct str_vector v;
str_vector_init (&v);