X-Git-Url: https://git.armaanb.net/?p=sic.git;a=blobdiff_plain;f=sic.c;h=c1e428effa57a8675c123f100d122a1971a210a2;hp=e91a426fb0a2f85611dcbcd786652e87e2ec50d0;hb=HEAD;hpb=5eaa0fff0d2d5c4b3f875add16894d53a7da9c45 diff --git a/sic.c b/sic.c index e91a426..c1e428e 100644 --- a/sic.c +++ b/sic.c @@ -1,4 +1,6 @@ /* See LICENSE file for license details. */ +#include + #include #include #include @@ -8,8 +10,10 @@ #include #include +#include "arg.h" #include "config.h" +char *argv0; static char *host = DEFAULT_HOST; static char *port = DEFAULT_PORT; static char *password; @@ -20,6 +24,8 @@ static char channel[256]; static time_t trespond; static FILE *srv; +#undef strlcpy +#include "strlcpy.c" #include "util.c" static void @@ -68,7 +74,7 @@ parsein(char *s) { return; } c = *++s; - if(c != '\0' && isspace(s[1])) { + if(c != '\0' && isspace((unsigned char)s[1])) { p = s + 2; switch(c) { case 'j': @@ -133,40 +139,44 @@ parsesrv(char *cmd) { } } + +static void +usage(void) { + eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0); +} + int main(int argc, char *argv[]) { - int i, c; struct timeval tv; const char *user = getenv("USER"); + int n; fd_set rd; strlcpy(nick, user ? user : "unknown", sizeof nick); - for(i = 1; i < argc; i++) { - c = argv[i][1]; - if(argv[i][0] != '-' || argv[i][2]) - c = -1; - switch(c) { - case 'h': - if(++i < argc) host = argv[i]; - break; - case 'p': - if(++i < argc) port = argv[i]; - break; - case 'n': - if(++i < argc) strlcpy(nick, argv[i], sizeof nick); - break; - case 'k': - if(++i < argc) password = argv[i]; - break; - case 'v': - eprint("sic-"VERSION", © 2005-2012 Kris Maglione, Anselm R. Garbe, Nico Golde\n"); - default: - eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n"); - } - } + ARGBEGIN { + case 'h': + host = EARGF(usage()); + break; + case 'p': + port = EARGF(usage()); + break; + case 'n': + strlcpy(nick, EARGF(usage()), sizeof nick); + break; + case 'k': + password = EARGF(usage()); + break; + case 'v': + eprint("sic-"VERSION", © 2005-2014 Kris Maglione, Anselm R. Garbe, Nico Golde\n"); + break; + default: + usage(); + } ARGEND; + /* init */ - i = dial(host, port); - srv = fdopen(i, "r+"); + srv = fdopen(dial(host, port), "r+"); + if (!srv) + eprint("fdopen:"); /* login */ if(password) sout("PASS %s", password); @@ -175,19 +185,24 @@ main(int argc, char *argv[]) { fflush(srv); setbuf(stdout, NULL); setbuf(srv, NULL); + setbuf(stdin, NULL); +#ifdef __OpenBSD__ + if (pledge("stdio", NULL) == -1) + eprint("error: pledge:"); +#endif for(;;) { /* main loop */ FD_ZERO(&rd); FD_SET(0, &rd); FD_SET(fileno(srv), &rd); tv.tv_sec = 120; tv.tv_usec = 0; - i = select(fileno(srv) + 1, &rd, 0, 0, &tv); - if(i < 0) { + n = select(fileno(srv) + 1, &rd, 0, 0, &tv); + if(n < 0) { if(errno == EINTR) continue; eprint("sic: error on select():"); } - else if(i == 0) { + else if(n == 0) { if(time(NULL) - trespond >= 300) eprint("sic shutting down: parse timeout\n"); sout("PING %s", host);