Commit ce457a79 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Refactor parse_config into parse_config_line.

parent ceaaff06
......@@ -816,26 +816,17 @@ parse_option(int c, gnc_t gnc, void *closure, char *token)
}
static int
parse_config(gnc_t gnc, void *closure)
parse_config_line(int c, gnc_t gnc, void *closure)
{
int c;
char *token;
c = gnc(closure);
if(c < -1)
return -1;
while(1) {
c = skip_whitespace(c, gnc, closure);
if(c == '\n' || c == '#') {
c = skip_to_eol(c, gnc, closure);
continue;
}
if(c < 0)
break;
if(c == '\n' || c == '#')
return skip_to_eol(c, gnc, closure);
c = getword(c, &token, gnc, closure);
if(c < -1)
return -1;
return c;
if(strcmp(token, "in") == 0) {
struct filter *filter;
......@@ -860,7 +851,6 @@ parse_config(gnc_t gnc, void *closure)
c = parse_filter(c, gnc, closure, &filter);
if(c < -1)
return -1;
add_filter(filter, &install_filters);
} else if(strcmp(token, "interface") == 0) {
struct interface_conf *if_conf;
c = parse_ifconf(c, gnc, closure, &if_conf);
......@@ -885,8 +875,7 @@ parse_config(gnc_t gnc, void *closure)
return -1;
}
free(token);
}
return 1;
return c;
}
struct file_state {
......@@ -908,7 +897,7 @@ int
parse_config_from_file(const char *filename, int *line_return)
{
struct file_state s = { NULL, 1 };
int rc;
int c;
s.f = fopen(filename, "r");
if(s.f == NULL) {
......@@ -916,11 +905,22 @@ parse_config_from_file(const char *filename, int *line_return)
return -1;
}
rc = parse_config((gnc_t)gnc_file, &s);
fclose(s.f);
c = gnc_file(&s);
if(c < 0)
return 0;
while(1) {
c = parse_config_line(c, (gnc_t)gnc_file, &s);
if(c < -1) {
*line_return = s.line;
return rc;
return -1;
}
if(c == -1)
break;
}
fclose(s.f);
return 1;
}
struct buf_state {
......@@ -940,8 +940,18 @@ gnc_buf(struct buf_state *s)
int
parse_config_from_string(char *string, int n)
{
int c;
struct buf_state s = { string, 0, n };
return parse_config((gnc_t)gnc_buf, &s);
c = gnc_buf(&s);
if(c < 0)
return -1;
c = parse_config_line(c, (gnc_t)gnc_buf, &s);
if(c == -1)
return 1;
else
return -1;
}
static void
......
......@@ -53,6 +53,7 @@ int
local_read(struct local_socket *s)
{
int rc;
char *eol;
if(s->buf == NULL)
s->buf = malloc(LOCAL_BUFSIZE);
......@@ -69,6 +70,14 @@ local_read(struct local_socket *s)
if(rc <= 0)
return rc;
eol = memchr(s->buf, '\n', s->n);
if(eol == NULL)
return 1;
memmove(s->buf, eol + 1, s->n - (eol + 1 - s->buf));
s->n -= (eol + 1 - s->buf);
return 1;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment