[hackers] [sic] applied anonymous cleanup patch, thanks anonymous! || Anselm R Garbe

From: <hg_AT_suckless.org>
Date: Sat, 17 Apr 2010 10:12:56 +0000 (UTC)

changeset: 107:034d17785d30
tag: tip
user: Anselm R Garbe <anselm_AT_garbe.us>
date: Sat Apr 17 11:12:54 2010 +0100
files: sic.c util.c
description:
applied anonymous cleanup patch, thanks anonymous!

diff -r 10f41d80803e -r 034d17785d30 sic.c
--- a/sic.c Tue Mar 23 18:00:37 2010 +0000
+++ b/sic.c Sat Apr 17 11:12:54 2010 +0100
@@ -55,76 +55,77 @@
 }
 
 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 -r 10f41d80803e -r 034d17785d30 util.c
--- a/util.c Tue Mar 23 18:00:37 2010 +0000
+++ b/util.c Sat Apr 17 11:12:54 2010 +0100
@@ -47,35 +47,28 @@
         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';
 }
Received on Sat Apr 17 2010 - 10:12:56 UTC

This archive was generated by hypermail 2.2.0 : Sat Apr 17 2010 - 10:24:04 UTC