sic

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

commit 75d42255f22a40935ced8b2e4c134996f8efebe2
parent dad75aa6ba9f9b50b5f6c656929092aad0963462
Author: Anselm R Garbe <anselm@garbe.us>
Date:   Sat, 17 Apr 2010 11:12:54 +0100

applied anonymous cleanup patch, thanks anonymous!
Diffstat:
sic.c | 87++++++++++++++++++++++++++++++++++++++++---------------------------------------
util.c | 43++++++++++++++++++-------------------------
2 files changed, 62 insertions(+), 68 deletions(-)

diff --git a/sic.c b/sic.c @@ -55,76 +55,77 @@ privmsg(char *channel, char *msg) { } static void -parsein(char *msg) { - char *p; - char c; +parsein(char *s) { + char c, *p; - if(msg[0] == '\0') + if(s[0] == '\0') return; - msg = ctok(&msg, '\n'); - if(msg[0] != ':') { - privmsg(channel, msg); + skip(s, '\n'); + if(s[0] != ':') { + privmsg(channel, s); return; } - c = *++msg; - if(!c || !isspace(msg[1])) - sout("%s", msg); - else { - if(msg[1]) - msg += 2; + c = *++s; + if(c != '\0' && isspace(s[1])) { + p = s + 2; switch(c) { case 'j': - sout("JOIN %s", msg); + sout("JOIN %s", p); if(channel[0] == '\0') - strlcpy(channel, msg, sizeof channel); - break; + strlcpy(channel, p, sizeof channel); + return; case 'l': - p = tok(&msg); + s = eat(p, isspace, 1); + p = eat(s, isspace, 0); + if(!*s) + s = channel; + if(*p) + *p++ = '\0'; if(!*p) - p = channel; - if(!*msg) - msg = "sic - 250 LOC are too much!"; - sout("PART %s :%s", p, msg); - break; + p = "sic - 250 LOC are too much!"; + sout("PART %s :%s", s, p); + return; case 'm': - p = tok(&msg); - privmsg(p, msg); - break; + s = eat(p, isspace, 1); + p = eat(s, isspace, 0); + if(*p) + *p++ = '\0'; + privmsg(s, p); + return; case 's': - strlcpy(channel, msg, sizeof channel); - break; - default: - sout("%c %s", c, msg); - break; + strlcpy(channel, p, sizeof channel); + return; } } + sout("%s", s); } static void -parsesrv(char *msg) { - char *cmd, *p, *usr, *txt; +parsesrv(char *cmd) { + char *usr, *par, *txt; usr = host; - if(!msg || !*msg) + if(!cmd || !*cmd) return; - if(msg[0] == ':') { - msg++; - p = tok(&msg); - if(!*msg) + if(cmd[0] == ':') { + usr = cmd + 1; + cmd = skip(usr, ' '); + if(cmd[0] == '\0') return; - usr = ctok(&p, '!'); + skip(usr, '!'); } - txt = ctok(&msg, '\r'); - msg = ctok(&txt, ':'); - cmd = tok(&msg); + skip(cmd, '\r'); + par = skip(cmd, ' '); + txt = skip(par, ':'); + trim(par); if(!strcmp("PONG", cmd)) return; if(!strcmp("PRIVMSG", cmd)) - pout(msg, "<%s> %s", usr, txt); + pout(par, "<%s> %s", usr, txt); else if(!strcmp("PING", cmd)) sout("PONG %s", txt); else { - pout(usr, ">< %s: %s", cmd, txt); + pout(usr, ">< %s (%s): %s", cmd, par, txt); if(!strcmp("NICK", cmd) && !strcmp(usr, nick)) strlcpy(nick, txt, sizeof nick); } diff --git a/util.c b/util.c @@ -47,35 +47,28 @@ strlcpy(char *to, const char *from, int l) { to[l-1] = '\0'; } -static void -eat(char **s, int (*p)(int), int r) { - char *q; - - for(q = *s; *q && p(*q) == r; q++) - ; - *s = q; +static char * +eat(char *s, int (*p)(int), int r) { + while(s != '\0' && p(*s) == r) + s++; + return s; } static char* -tok(char **s) { - char *p; - - eat(s, isspace, 1); - p = *s; - eat(s, isspace, 0); - if(**s) - *(*s)++ = '\0'; - return p; +skip(char *s, char c) { + while(*s != c && *s != '\0') + s++; + if (*s != '\0') + *s++ = '\0'; + return s; } -static char* -ctok(char **s, int c) { - char *p, *q; +static void +trim(char *s) { + char *e; - q = *s; - for(p = q; *p && *p != c; p++) - ; - if(*p) *p++ = '\0'; - *s = p; - return q; + e = s + strlen(s) - 1; + while (isspace(*e) && e > s) + e--; + *(e + 1) = '\0'; }