From daa93b240a0b2cf6e9497884971fabb5aacf6e83 Mon Sep 17 00:00:00 2001 From: un1c0rn Date: Tue, 3 Apr 2012 00:12:40 +0200 Subject: [PATCH] fixup small leak and split off a part of mainloop into handle_line --- main.c | 132 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/main.c b/main.c index 0b87f42..52be7f8 100644 --- a/main.c +++ b/main.c @@ -345,6 +345,70 @@ static char *token(char **foo) return ret; } +static inline void handle_line(struct bio *b, char *line) +{ + char *prefix = NULL, *command, *ident = NULL, *host = NULL; + const char *args[128]; + int i, nargs = 0; + if (line[0] == ':') { + line++; + prefix = token(&line); + ident = strchr(prefix, '!'); + if (ident) { + *(ident++) = 0; + host = strchr(ident, '@'); + if (host) + *(host++) = 0; + } + if (!line) + return; + } + command = token(&line); + + while (line && nargs < sizeof(args)/sizeof(*args)) { + if (line[0] == ':') { + args[nargs++] = ++line; + break; + } + args[nargs++] = token(&line); + } + for (i = 0; i < sizeof(cmds)/sizeof(*cmds)-1; ++i) { + typeof(*cmds) *c = cmds+i; + if (strcasecmp(c->cmd, command)) + continue; + if (nargs < c->min_args) { + fprintf(stderr, "Dropped %s because of %u < %u\n", command, nargs, c->min_args); + continue; + } + if (!prefix && c->prefixed) { + fprintf(stderr, "Dropped %s because not prefixed\n", command); + continue; + } + if (!c->builtin) { + int sig = sigsetjmp(crash_buf, 1); + if (!sig) { + set_signals(1); + c->ptr(b, prefix, ident, host, args, nargs); + } + set_signals(0); + if (sig) + crash_mod(b, channels[0]); + } else + c->ptr(b, prefix, ident, host, args, nargs); + break; + } + if (!cmds[i].cmd && dl_mod && command_ptr) { + int sig = setjmp(crash_buf); + if (!sig) { + set_signals(1); + command_ptr(b, prefix, ident, host, command, args, nargs); + } + set_signals(0); + if (sig) + crash_mod(b, channels[0]); + } +} + static void mainloop(struct bio *b, const char *server) { int ret = 0, pinged = 0; @@ -368,68 +432,8 @@ static void mainloop(struct bio *b, const char *server) ret = b->fill(b); if (ret <= 0) continue; - while ((ret = b->readline(b, &line) > 0)) { - char *prefix = NULL, *command, *ident = NULL, *host = NULL; - const char *args[128]; - int i, nargs = 0; - if (line[0] == ':') { - line++; - prefix = token(&line); - ident = strchr(prefix, '!'); - if (ident) { - *(ident++) = 0; - host = strchr(ident, '@'); - if (host) - *(host++) = 0; - } - if (!line) - continue; - } - command = token(&line); - - while (line && nargs < sizeof(args)/sizeof(*args)) { - if (line[0] == ':') { - args[nargs++] = ++line; - break; - } - args[nargs++] = token(&line); - } - for (i = 0; i < sizeof(cmds)/sizeof(*cmds)-1; ++i) { - typeof(*cmds) *c = cmds+i; - if (strcasecmp(c->cmd, command)) - continue; - if (nargs < c->min_args) { - fprintf(stderr, "Dropped %s because of %u < %u\n", command, nargs, c->min_args); - continue; - } - if (!prefix && c->prefixed) { - fprintf(stderr, "Dropped %s because not prefixed\n", command); - continue; - } - if (!c->builtin) { - int sig = sigsetjmp(crash_buf, 1); - if (!sig) { - set_signals(1); - c->ptr(b, prefix, ident, host, args, nargs); - } - set_signals(0); - if (sig) - crash_mod(b, channels[0]); - } else - c->ptr(b, prefix, ident, host, args, nargs); - break; - } - if (!cmds[i].cmd && dl_mod && command_ptr) { - int sig = setjmp(crash_buf); - if (!sig) { - set_signals(1); - command_ptr(b, prefix, ident, host, command, args, nargs); - } - set_signals(0); - if (sig) - crash_mod(b, channels[0]); - } - } + while ((ret = b->readline(b, &line) > 0)) + handle_line(b, line); } } @@ -518,8 +522,9 @@ int main(int argc, char *argv[]) }; mainloop(&b, server); SSL_shutdown(ssl); - SSL_free(ssl); free(b.priv); +retry: + SSL_free(ssl); } else { struct bio b = { .readline = bio_readline, @@ -531,7 +536,6 @@ int main(int argc, char *argv[]) mainloop(&b, server); free(b.priv); } -retry: close(fd); sleep(3); } -- 2.11.4.GIT