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:52:52 -04:00
enum { LIBSCOMP_READ, LIBSCOMP_DISCARD };
2021-07-10 15:10:00 -04:00
2021-08-09 13:52:52 -04:00
void libscomp_reset(struct libscomp_input *in) {
in->state = LIBSCOMP_READ;
2021-07-10 15:10:00 -04:00
in->len = 0;
}
2021-08-09 13:52:52 -04:00
static enum libscomp_input_r libscomp_parse(
struct libscomp_input *in, struct libscomp_line *line) {
2021-07-10 15:10:00 -04:00
char *s = in->intbuf;
2021-08-09 13:52:52 -04:00
enum libscomp_input_r r = LIBSCOMP_ARG_OVERFLOW;
2021-07-10 15:10:00 -04:00
line->name = (*s == ':');
line->len = 0;
2021-08-09 13:52:52 -04:00
while (line->len < LIBSCOMP_MAXARG) {
2021-07-10 15:10:00 -04:00
line->buf[line->len++] = s;
for (; *s && *s != '\t'; s++);
if (!*s) {
2021-08-09 13:52:52 -04:00
r = LIBSCOMP_COMPLETE;
2021-07-10 15:45:51 -04:00
break;
2021-07-10 15:10:00 -04:00
} else {
*s = 0;
s++;
}
}
2021-08-09 13:52:52 -04:00
libscomp_reset(in);
2021-07-10 15:45:51 -04:00
return r;
2021-07-10 15:10:00 -04:00
}
2021-08-09 13:52:52 -04:00
enum libscomp_input_r libscomp_read(struct libscomp_input *in,
char **s,
2021-08-09 13:52:52 -04:00
struct libscomp_line *line) {
2021-07-10 15:10:00 -04:00
char c;
while ((c = **s)) {
(*s)++;
2021-07-10 15:10:00 -04:00
2021-08-09 13:52:52 -04:00
if (in->state == LIBSCOMP_DISCARD) {
2021-07-10 15:10:00 -04:00
if (c == '\n') {
2021-08-09 13:52:52 -04:00
libscomp_reset(in);
return LIBSCOMP_OVERFLOW;
2021-07-10 15:10:00 -04:00
}
} else {
switch (c) {
case '\n':
in->intbuf[in->len] = 0;
2021-08-09 13:52:52 -04:00
return libscomp_parse(in, line);
2021-07-10 15:10:00 -04:00
default:
in->intbuf[in->len++] = c;
2021-08-09 13:52:52 -04:00
if (in->len == LIBSCOMP_MAXBUF)
in->state = LIBSCOMP_DISCARD;
2021-07-10 15:10:00 -04:00
}
}
}
2021-08-09 13:52:52 -04:00
return LIBSCOMP_MORE;
2021-07-10 15:10:00 -04:00
}