Commit ce457a79 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Refactor parse_config into parse_config_line.

parent ceaaff06
...@@ -816,77 +816,66 @@ parse_option(int c, gnc_t gnc, void *closure, char *token) ...@@ -816,77 +816,66 @@ 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); c = skip_whitespace(c, gnc, closure);
if(c == '\n' || c == '#')
return skip_to_eol(c, gnc, closure);
c = getword(c, &token, gnc, closure);
if(c < -1) if(c < -1)
return -1; return c;
while(1) { if(strcmp(token, "in") == 0) {
c = skip_whitespace(c, gnc, closure); struct filter *filter;
if(c == '\n' || c == '#') { c = parse_filter(c, gnc, closure, &filter);
c = skip_to_eol(c, gnc, closure);
continue;
}
if(c < 0)
break;
c = getword(c, &token, gnc, closure);
if(c < -1) if(c < -1)
return -1; return -1;
add_filter(filter, &input_filters);
if(strcmp(token, "in") == 0) { } else if(strcmp(token, "out") == 0) {
struct filter *filter; struct filter *filter;
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, &input_filters); add_filter(filter, &output_filters);
} else if(strcmp(token, "out") == 0) { } else if(strcmp(token, "redistribute") == 0) {
struct filter *filter; struct filter *filter;
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, &output_filters); add_filter(filter, &redistribute_filters);
} else if(strcmp(token, "redistribute") == 0) { } else if(strcmp(token, "install") == 0) {
struct filter *filter; struct filter *filter;
c = parse_filter(c, gnc, closure, &filter); c = parse_filter(c, gnc, closure, &filter);
if(c < -1) if(c < -1)
return -1; return -1;
Please register or sign in to reply
add_filter(filter, &redistribute_filters); } else if(strcmp(token, "interface") == 0) {
} else if(strcmp(token, "install") == 0) { struct interface_conf *if_conf;
struct filter *filter; c = parse_ifconf(c, gnc, closure, &if_conf);
c = parse_filter(c, gnc, closure, &filter); if(c < -1)
if(c < -1) return -1;
return -1; add_ifconf(if_conf, &interface_confs);
add_filter(filter, &install_filters); } else if(strcmp(token, "default") == 0) {
} else if(strcmp(token, "interface") == 0) { struct interface_conf *if_conf;
struct interface_conf *if_conf; c = parse_anonymous_ifconf(c, gnc, closure, NULL, &if_conf);
c = parse_ifconf(c, gnc, closure, &if_conf); if(c < -1)
if(c < -1) return -1;
return -1; if(default_interface_conf == NULL)
add_ifconf(if_conf, &interface_confs); default_interface_conf = if_conf;
} else if(strcmp(token, "default") == 0) { else {
struct interface_conf *if_conf; merge_ifconf(default_interface_conf,
c = parse_anonymous_ifconf(c, gnc, closure, NULL, &if_conf); if_conf, default_interface_conf);
if(c < -1) free(if_conf);
return -1;
if(default_interface_conf == NULL)
default_interface_conf = if_conf;
else {
merge_ifconf(default_interface_conf,
if_conf, default_interface_conf);
free(if_conf);
}
} else {
c = parse_option(c, gnc, closure, token);
if(c < -1)
return -1;
} }
free(token); } else {
c = parse_option(c, gnc, closure, token);
if(c < -1)
return -1;
} }
return 1; free(token);
return c;
} }
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);
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 -1;
}
if(c == -1)
break;
}
fclose(s.f); fclose(s.f);
*line_return = s.line; return 1;
return rc;
} }
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