libscomp/input.c

63 lines
1.1 KiB
C
Raw Normal View History

2021-07-10 15:10:00 -04:00
#include "input.h"
enum { READ, DISCARD };
void libscomm_reset(struct libscomm_input *in) {
in->state = READ;
in->len = 0;
}
static enum libscomm_input_r parse(struct libscomm_input *in,
struct libscomm_line *line) {
char *s = in->intbuf;
2021-07-10 15:45:51 -04:00
enum libscomm_input_r r = LIBSCOMM_ARG_OVERFLOW;
2021-07-10 15:10:00 -04:00
line->name = (*s == ':');
line->len = 0;
while (line->len < LIBSCOMM_MAXARG) {
line->buf[line->len++] = s;
for (; *s && *s != '\t'; s++);
if (!*s) {
2021-07-10 15:45:51 -04:00
r = LIBSCOMM_COMPLETE;
break;
2021-07-10 15:10:00 -04:00
} else {
*s = 0;
s++;
}
}
2021-07-10 15:45:51 -04:00
libscomm_reset(in);
return r;
2021-07-10 15:10:00 -04:00
}
enum libscomm_input_r libscomm_read(struct libscomm_input *in,
char **s,
2021-07-10 15:10:00 -04:00
struct libscomm_line *line) {
char c;
while ((c = **s)) {
(*s)++;
2021-07-10 15:10:00 -04:00
if (in->state == DISCARD) {
if (c == '\n') {
libscomm_reset(in);
return LIBSCOMM_OVERFLOW;
}
} else {
switch (c) {
case '\n':
in->intbuf[in->len] = 0;
return parse(in, line);
default:
in->intbuf[in->len++] = c;
if (in->len == LIBSCOMM_MAXBUF)
in->state = DISCARD;
}
}
}
return LIBSCOMM_MORE;
}