Skip to content

Commit 9e00455

Browse files
authored
Merge pull request #3 from mcpcpc/ringbuffer
simplified parser(), simplified IRC stream buffer
2 parents 76a5ea1 + 38ec3d4 commit 9e00455

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

kirc.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#define IRC_MSG_MAX 512 /* guaranteed max message length */
1616
#define IRC_CHAN_MAX 200 /* gauranteed max channel length */
17-
#define STREAM_BUFF 2048 /* maximum stream buffer length */
1817

1918
static int conn; /* connection socket */
2019
static int verb = 0; /* verbose output (e.g. raw stream) */
@@ -146,38 +145,33 @@ printw(const char *format, ...) {
146145

147146
/* parse irc stream */
148147
static void
149-
parser(char *string) {
148+
parser(char *in) {
150149

151150
int len;
152-
char *in = string, *tok, ltr[200], cha[IRC_CHAN_MAX], nic[200], hos[200], \
151+
char ltr[200], cha[IRC_CHAN_MAX], nic[200], hos[200], \
153152
usr[200], cmd[200], msg[200], pre[200];
154153

155-
do {
156-
cmd[0] = msg[0] = pre[0] = '\0';
157-
tok = strstr(in, "\r\n");
158-
if (tok) *tok = '\0';
159-
if (tok && verb) printf(">> %s\n", in);
160-
if (!strncmp(in, "PING", 4)) {
161-
in[string - in + 1] = 'O';
162-
raw("%s\r\n", in);
163-
}
164-
else if (tok && in[0] == ':') {
165-
sscanf(in, ":%[^ ] %[^:]:%[^\r]", pre, cmd, msg);
166-
sscanf(pre, "%[^!]!%[^@]@%s", nic, usr, hos);
167-
sscanf(cmd, "%[^#& ]%s", ltr, cha);
168-
if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan);
169-
if (!strncmp(ltr, "QUIT", 4)) {
170-
printw("%*.*s \x1b[34;1m%s\x1b[0m\n", gutl, gutl, "<--", nic);
171-
} else if (!strncmp(ltr, "JOIN", 4)) {
172-
printw("%*.*s \x1b[32;1m%s\x1b[0m\n", gutl, gutl, "-->", nic);
173-
} else {
174-
len = strlen(nic);
175-
printw("%*s\x1b[33;1m%-.*s\x1b[0m %s\n", \
176-
gutl-(len <= gutl ? len : gutl), "", gutl, nic, msg);
177-
}
154+
cmd[0] = msg[0] = pre[0] = '\0';
155+
if (verb) printf(">> %s\n", in);
156+
if (!strncmp(in, "PING", 4)) {
157+
in[1] = 'O';
158+
raw("%s\r\n", in);
159+
}
160+
else if (in[0] == ':') {
161+
sscanf(in, ":%[^ ] %[^:]:%[^\r]", pre, cmd, msg);
162+
sscanf(pre, "%[^!]!%[^@]@%s", nic, usr, hos);
163+
sscanf(cmd, "%[^#& ]%s", ltr, cha);
164+
if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan);
165+
if (!strncmp(ltr, "QUIT", 4)) {
166+
printw("%*.*s \x1b[34;1m%s\x1b[0m\n", gutl, gutl, "<--", nic);
167+
} else if (!strncmp(ltr, "JOIN", 4)) {
168+
printw("%*.*s \x1b[32;1m%s\x1b[0m\n", gutl, gutl, "-->", nic);
169+
} else {
170+
len = strlen(nic);
171+
printw("%*s\x1b[33;1m%-.*s\x1b[0m %s\n", \
172+
gutl-(len <= gutl ? len : gutl), "", gutl, nic, msg);
178173
}
179-
in = tok + strlen("\r\n");
180-
} while (tok != NULL);
174+
}
181175
}
182176

183177
int
@@ -216,13 +210,19 @@ main(int argc, char **argv) {
216210
pid_t pid = fork();
217211

218212
if (pid == 0) {
219-
int sl, i;
220-
char u[IRC_MSG_MAX], s[IRC_MSG_MAX];
213+
int sl, i, o = 0;
214+
char u[IRC_MSG_MAX], s, b[IRC_MSG_MAX];
221215

222216
irc_init();
223217

224-
while ((sl = read(conn, s, STREAM_BUFF))) {
225-
if (sl > 0) parser(s);
218+
while ((sl = read(conn, &s, 1))) {
219+
if (sl > 0) b[o] = s;
220+
if ((o > 0 && b[o - 1] == '\r' && b[o] == '\n') || o == IRC_MSG_MAX) {
221+
b[o + 1] = '\0';
222+
parser(b);
223+
o = 0;
224+
}
225+
else if (sl > 0) o++;
226226
if (read(fd[0], u, IRC_MSG_MAX) > 0) {
227227
for (i = 0; u[i] != '\n'; i++) continue;
228228
if (u[0] != ':') raw("%-*.*s\r\n", i, i, u);

0 commit comments

Comments
 (0)