sic

simple irc client
git clone git://git.suckless.org/sic
Log | Files | Refs | README | LICENSE

commit 6703fe45923e62fdad4ebdcc25c7df7dddbe1db8
parent 5eaa0fff0d2d5c4b3f875add16894d53a7da9c45
Author: Anselm R Garbe <garbeam@gmail.com>
Date:   Fri, 14 Nov 2014 16:06:28 +0100

applied 3 additional patches by Dimitris

Diffstat:
MMakefile | 2+-
Aarg.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msic.c | 64+++++++++++++++++++++++++++++++++++-----------------------------
3 files changed, 99 insertions(+), 30 deletions(-)

diff --git 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 @@ -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 @@ -8,8 +8,10 @@ #include <time.h> #include <unistd.h> +#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);