Re: [hackers] [SIC] alias patch

From: Kris Maglione <jg_AT_suckless.org>
Date: Fri, 10 Aug 2007 17:15:44 -0400

Something like this (still doesn't apply to channel name as
yours does; still untested; could be shorter);

#define nelem(x) (sizeof(x)/sizeof *(x))
static char *alias[256][2];
static int nalias;

static char**
getalias(char *a) {
        int i;

        for(i=0; i < nalias; i++)
                if(!strcmp(a, alias[i][0]))
                        return alias[i];
        return nil;
}

static void
parsein(long n, char *msg) {
        char *p, **q;

        if(msg[0] == '\0')
                return;
        msg = ctok(&msg, '\n');
        if(msg[0] != ':') {
                privmsg(channel, msg);
                return;
        }
        msg++;
        p = tok(&msg);
        if(!p[0] || p[1])
                goto casdef;
        else {
                if(msg[1])
                        msg += 2;
                switch(p[1]) {
                case 'a':
                        p = tok(&msg);
                        if(!p)
                                return;
                        if((q = getalias(p)))
                                free(q[1]);
                        else
                                q = alias[nalias++];
                        assert(nalias <= nelem(alias));
                        q[1] = strdup(msg);
                        break;
                /* ... */
                default:
                casdef:
                        if((q = getalias(p))) {
                                assert(n < 50);
                                p = smprint("%s %s", q[1], msg);
                                parsein(n+1, p);
                                free(p);
                        }else
                                sout("%s %s", p, msg);
                        break;
                }
        }
}

/* ... */

static char*
smprint(const char *fmt, ...) {
        va_list ap;
        char *buf = "";
        int n;

        va_start(ap, fmt);
        n = snprintf(buf, 0, fmt, ap);
        va_end(ap);

        buf = malloc(++n);
        if(buf) {
                va_start(ap, fmt);
                snprintf(buf, n, fmt, ap);
                va_end(ap);
        }
        return buf;
}

-- 
Kris Maglione
If you are given an open-book exam, you will forget
your book.
If you are given a take-home  exam, you will forget
where you live.
Received on Fri Aug 10 2007 - 23:16:13 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:57:49 UTC