diff --git a/Makefile b/Makefile index 6fdcfbb..0f4d6ce 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ clean: dist: clean @echo creating dist tarball @mkdir -p sic-${VERSION} - @cp -R LICENSE Makefile README config.mk sic.1 sic.c util.c sic-${VERSION} + @cp -R LICENSE Makefile README config.def.h config.mk sic.1 sic.c util.c sic-${VERSION} @tar -cf sic-${VERSION}.tar sic-${VERSION} @gzip sic-${VERSION}.tar @rm -rf sic-${VERSION} diff --git a/arg.h b/arg.h new file mode 100644 index 0000000..4df77a7 --- /dev/null +++ b/arg.h @@ -0,0 +1,63 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/sic.c b/sic.c index e91a426..6b5ae96 100644 --- a/sic.c +++ b/sic.c @@ -8,8 +8,10 @@ #include #include +#include "arg.h" #include "config.h" +char *argv0; static char *host = DEFAULT_HOST; static char *port = DEFAULT_PORT; static char *password; @@ -133,40 +135,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); @@ -181,13 +187,13 @@ main(int argc, char *argv[]) { 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);