63 lines
1.2 KiB
C
63 lines
1.2 KiB
C
#include "input.h"
|
|
|
|
enum { LIBSCOMM_READ, LIBSCOMM_DISCARD };
|
|
|
|
void libscomm_reset(struct libscomm_input *in) {
|
|
in->state = LIBSCOMM_READ;
|
|
in->len = 0;
|
|
}
|
|
|
|
static enum libscomm_input_r libscomm_parse(
|
|
struct libscomm_input *in, struct libscomm_line *line) {
|
|
char *s = in->intbuf;
|
|
enum libscomm_input_r r = LIBSCOMM_ARG_OVERFLOW;
|
|
|
|
line->name = (*s == ':');
|
|
line->len = 0;
|
|
|
|
while (line->len < LIBSCOMM_MAXARG) {
|
|
line->buf[line->len++] = s;
|
|
for (; *s && *s != '\t'; s++);
|
|
|
|
if (!*s) {
|
|
r = LIBSCOMM_COMPLETE;
|
|
break;
|
|
} else {
|
|
*s = 0;
|
|
s++;
|
|
}
|
|
}
|
|
|
|
libscomm_reset(in);
|
|
return r;
|
|
}
|
|
|
|
enum libscomm_input_r libscomm_read(struct libscomm_input *in,
|
|
char **s,
|
|
struct libscomm_line *line) {
|
|
char c;
|
|
|
|
while ((c = **s)) {
|
|
(*s)++;
|
|
|
|
if (in->state == LIBSCOMM_DISCARD) {
|
|
if (c == '\n') {
|
|
libscomm_reset(in);
|
|
return LIBSCOMM_OVERFLOW;
|
|
}
|
|
} else {
|
|
switch (c) {
|
|
case '\n':
|
|
in->intbuf[in->len] = 0;
|
|
return libscomm_parse(in, line);
|
|
default:
|
|
in->intbuf[in->len++] = c;
|
|
if (in->len == LIBSCOMM_MAXBUF)
|
|
in->state = LIBSCOMM_DISCARD;
|
|
}
|
|
}
|
|
}
|
|
|
|
return LIBSCOMM_MORE;
|
|
}
|