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) ...@@ -816,26 +816,17 @@ parse_option(int c, gnc_t gnc, void *closure, char *token)
} }
static int static int
parse_config(gnc_t gnc, void *closure) parse_config_line(int c, gnc_t gnc, void *closure)
{ {
int c;
char *token; char *token;
c = gnc(closure);
if(c < -1)
return -1;
while(1) {
c = skip_whitespace(c, gnc, closure); c = skip_whitespace(c, gnc, closure);
if(c == '\n' || c == '#') { if(c == '\n' || c == '#')
c = skip_to_eol(c, gnc, closure); return skip_to_eol(c, gnc, closure);
continue;
}
if(c < 0)
break;
c = getword(c, &token, gnc, closure); c = getword(c, &token, gnc, closure);
if(c < -1) if(c < -1)
return -1; return c;
if(strcmp(token, "in") == 0) { if(strcmp(token, "in") == 0) {
struct filter *filter; struct filter *filter;
...@@ -860,7 +851,6 @@ parse_config(gnc_t gnc, void *closure) ...@@ -860,7 +851,6 @@ parse_config(gnc_t gnc, void *closure)
c = parse_filter(c, gnc, closure, &filter); c = parse_filter(c, gnc, closure, &filter);
if(c < -1) if(c < -1)
return -1; return -1;
add_filter(filter, &install_filters);
} else if(strcmp(token, "interface") == 0) { } else if(strcmp(token, "interface") == 0) {
struct interface_conf *if_conf; struct interface_conf *if_conf;
c = parse_ifconf(c, gnc, closure, &if_conf); c = parse_ifconf(c, gnc, closure, &if_conf);
...@@ -885,8 +875,7 @@ parse_config(gnc_t gnc, void *closure) ...@@ -885,8 +875,7 @@ parse_config(gnc_t gnc, void *closure)
return -1; return -1;
} }
free(token); free(token);
} return c;
return 1;
} }
struct file_state { struct file_state {
...@@ -908,7 +897,7 @@ int ...@@ -908,7 +897,7 @@ int
parse_config_from_file(const char *filename, int *line_return) parse_config_from_file(const char *filename, int *line_return)
{ {
struct file_state s = { NULL, 1 }; struct file_state s = { NULL, 1 };
int rc; int c;
s.f = fopen(filename, "r"); s.f = fopen(filename, "r");
if(s.f == NULL) { if(s.f == NULL) {
...@@ -916,11 +905,22 @@ parse_config_from_file(const char *filename, int *line_return) ...@@ -916,11 +905,22 @@ parse_config_from_file(const char *filename, int *line_return)
return -1; return -1;
} }
rc = parse_config((gnc_t)gnc_file, &s); c = gnc_file(&s);
fclose(s.f); if(c < 0)
return 0;
while(1) {
c = parse_config_line(c, (gnc_t)gnc_file, &s);
if(c < -1) {
*line_return = s.line; *line_return = s.line;
return rc; return -1;
}
if(c == -1)
break;
}
fclose(s.f);
return 1;
} }
struct buf_state { struct buf_state {
...@@ -940,8 +940,18 @@ gnc_buf(struct buf_state *s) ...@@ -940,8 +940,18 @@ gnc_buf(struct buf_state *s)
int int
parse_config_from_string(char *string, int n) parse_config_from_string(char *string, int n)
{ {
int c;
struct buf_state s = { string, 0, n }; 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 static void
......
...@@ -53,6 +53,7 @@ int ...@@ -53,6 +53,7 @@ int
local_read(struct local_socket *s) local_read(struct local_socket *s)
{ {
int rc; int rc;
char *eol;
if(s->buf == NULL) if(s->buf == NULL)
s->buf = malloc(LOCAL_BUFSIZE); s->buf = malloc(LOCAL_BUFSIZE);
...@@ -69,6 +70,14 @@ local_read(struct local_socket *s) ...@@ -69,6 +70,14 @@ local_read(struct local_socket *s)
if(rc <= 0) if(rc <= 0)
return rc; 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; 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