libscomp/input.c

63 lines
1.2 KiB
C
Raw Normal View History

2021-07-10 15:10:00 -04:00
#include "input.h"
2021-08-09 13:44:06 -04:00
enum { LIBSCOMM_READ, LIBSCOMM_DISCARD };
2021-07-10 15:10:00 -04:00
void libscomm_reset(struct libscomm_input *in) {
2021-08-09 13:44:06 -04:00
in->state = LIBSCOMM_READ;
2021-07-10 15:10:00 -04:00
in->len = 0;
}
2021-08-09 13:38:43 -04:00
static enum libscomm_input_r libscomm_parse(
struct libscomm_input *in, struct libscomm_line *line) {
2021-07-10 15:10:00 -04:00
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
2021-08-09 13:44:06 -04:00
if (in->state == LIBSCOMM_DISCARD) {
2021-07-10 15:10:00 -04:00
if (c == '\n') {
libscomm_reset(in);
return LIBSCOMM_OVERFLOW;
}
} else {
switch (c) {
case '\n':
in->intbuf[in->len] = 0;
2021-08-09 13:41:50 -04:00
return libscomm_parse(in, line);
2021-07-10 15:10:00 -04:00
default:
in->intbuf[in->len++] = c;
if (in->len == LIBSCOMM_MAXBUF)
2021-08-09 13:44:06 -04:00
in->state = LIBSCOMM_DISCARD;
2021-07-10 15:10:00 -04:00
}
}
}
return LIBSCOMM_MORE;
}