From 5e9863fa82f674ad8eb05148eade5c859a32c7ba Mon Sep 17 00:00:00 2001 From: C. McEnroe Date: Tue, 4 Feb 2020 19:02:54 -0500 Subject: Handle signals in poll loop --- chat.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/chat.c b/chat.c index 3402621..3aa4ad2 100644 --- a/chat.c +++ b/chat.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -34,7 +35,7 @@ char *idNames[IDCap] = { enum Color idColors[IDCap] = { [None] = Black, - [Debug] = Red, + [Debug] = Green, [Network] = Gray, }; @@ -42,6 +43,11 @@ size_t idNext = Network + 1; struct Self self; +static volatile sig_atomic_t signals[NSIG]; +static void signalHandler(int signal) { + signals[signal] = 1; +} + int main(int argc, char *argv[]) { setlocale(LC_CTYPE, ""); @@ -100,6 +106,11 @@ int main(int argc, char *argv[]) { ircFormat("NICK :%s\r\n", nick); ircFormat("USER %s 0 * :%s\r\n", user, real); + signal(SIGHUP, signalHandler); + signal(SIGINT, signalHandler); + signal(SIGTERM, signalHandler); + sig_t cursesWinch = signal(SIGWINCH, signalHandler); + struct pollfd fds[2] = { { .events = POLLIN, .fd = STDIN_FILENO }, { .events = POLLIN, .fd = irc }, @@ -108,8 +119,20 @@ int main(int argc, char *argv[]) { int nfds = poll(fds, 2, -1); if (nfds < 0 && errno != EINTR) err(EX_IOERR, "poll"); + if (signals[SIGHUP] || signals[SIGINT] || signals[SIGTERM]) { + break; + } + if (signals[SIGWINCH]) { + signals[SIGWINCH] = 0; + cursesWinch(SIGWINCH); + fds[0].revents = POLLIN; + } + if (fds[0].revents) uiRead(); if (fds[1].revents) ircRecv(); uiDraw(); } + + ircFormat("QUIT\r\n"); + uiHide(); } -- cgit 1.4.1-2-gfad0