ref: 7a1a90f46e6c7de5ce85bac73ecc8c4931900d7c
dir: /parse.c/
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
static Prefix
parseprefix(char *line, char **next)
{
/* nick!user@host or servername */
Prefix r;
char *c;
r.name = nil;
r.user = nil;
r.host = nil;
/* first, find end of prefix */
c = strchr(line, ' ');
if (c) {
while (*c == ' ') {
*c = 0;
c++;
}
*next = c;
} else {
*next = line;
return r;
}
/* nick/servername follows ':' */
*line = 0;
r.name = &line[1]; /* fixed character position */
c = r.name;
/* next field: user after '!' */
c = strchr(c, '!');
if (c) {
*c = 0;
r.user = &c[1];
c = r.user;
} else {
c = r.name;
}
/* next field: host after '@' */
c = strchr(c, '@');
if (c) {
*c = 0;
r.host = &c[1];
}
return r;
}
static void
parseargs(Request *r, char *line)
{
char *c;
char *trailing;
int i, maxargs;
maxargs = 15;
c = strchr(line, ':');
trailing = c ? &c[1] : nil;
if (c) {
*c = 0;
maxargs--;
}
i = getfields(line, r->args, maxargs, 1, " ");
r->args[i] = trailing;
}
Request
parseline(char *line)
{
Request r;
char *l;
char *c;
r.cmd = nil;
r.args[0] = nil;
r.prefix.name = nil;
r.prefix.user = nil;
r.prefix.host = nil;
for (int i = 0; i < 15; i++) {
r.args[i] = nil;
}
if (!line || line[0] == 0)
return r;
l = line;
/* prefix */
if (l[0] == ':') {
r.prefix = parseprefix(l, &l);
}
/* command */
c = strchr(l, ' ');
if (!c) {
r.cmd = findcommand(l);
return r;
}
*c = 0;
r.cmd = findcommand(l);
l = &c[1];
while (*l == ' ') {
*l = 0;
l++;
}
if (l[0])
parseargs(&r, l);
return r;
}