summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-24 00:16:51 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-24 00:16:51 +0200
commit3afaf3d8f7617beed427c96e544ab177fabadaa4 (patch)
tree2e21acbbd074029da9a51e667ecc38fbcedeabf8
parent460b10553ac898232bfc5444e335dfa938690d1b (diff)
Simplify config_parse_header
-rw-r--r--minecctl/minecctl.c5
-rw-r--r--minecproxy/main.c4
-rw-r--r--minecproxy/server-config.c4
-rw-r--r--shared/config-parser.c22
-rw-r--r--shared/config-parser.h3
5 files changed, 16 insertions, 22 deletions
diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c
index d601980..1dc2a85 100644
--- a/minecctl/minecctl.c
+++ b/minecctl/minecctl.c
@@ -225,9 +225,8 @@ parse_config(char *buf, const char *filename,
*password = NULL;
list_init(addrs);
- /* FIXME: filename argument is superfluous */
- if (!config_parse_header(filename, SERVER_CFG_HEADER, &buf))
- die("Unable to parse %s: invalid header", filename);
+ if (!config_parse_header(SERVER_CFG_HEADER, &buf))
+ die("Unable to parse %s: invalid/missing header", filename);
/* FIXME: this will cause superflous DNS lookups of other cfg entries */
while (true) {
diff --git a/minecproxy/main.c b/minecproxy/main.c
index bbe3fad..3c996ad 100644
--- a/minecproxy/main.c
+++ b/minecproxy/main.c
@@ -298,8 +298,8 @@ cfg_read()
*pos = '\0';
pos = buf;
- if (!config_parse_header(path, "mcproxy", &pos))
- die("main config file (%s) invalid", path);
+ if (!config_parse_header("mcproxy", &pos))
+ die("main config file (%s) missing/invalid header", path);
while (true) {
int key;
diff --git a/minecproxy/server-config.c b/minecproxy/server-config.c
index 4e16155..e7cc422 100644
--- a/minecproxy/server-config.c
+++ b/minecproxy/server-config.c
@@ -154,8 +154,10 @@ scfg_parse(struct server *server)
pos = server->tbuf.buf;
- if (!config_parse_header(server->name, SERVER_CFG_HEADER, &pos))
+ if (!config_parse_header(SERVER_CFG_HEADER, &pos)) {
+ verbose("%s: missing/invalid header", server->name);
return;
+ }
while (true) {
int key;
diff --git a/shared/config-parser.c b/shared/config-parser.c
index 1c9979e..8dedbe5 100644
--- a/shared/config-parser.c
+++ b/shared/config-parser.c
@@ -35,6 +35,8 @@ get_line(char **pos)
assert_return(pos && *pos, NULL);
+ eat_whitespace_and_comments(pos);
+
begin = *pos;
while (isspace(*begin))
begin++;
@@ -325,7 +327,6 @@ config_parse_line(const char *filename, char **buf,
assert_return(buf && *buf && kvmap && rkey && rkeyname && rvalue, false);
- eat_whitespace_and_comments(buf);
line = get_line(buf);
if (!line)
return false;
@@ -467,27 +468,20 @@ error:
}
bool
-config_parse_header(const char *filename, const char *title, char **buf)
+config_parse_header(const char *title, char **buf)
{
char *line;
- assert_return(!empty_str(filename) && !empty_str(title) && buf && *buf, false);
-
- eat_whitespace_and_comments(buf);
+ assert_return(!empty_str(title) && buf && *buf, false);
line = get_line(buf);
- if (!line) {
- error("%s: missing header in configuration file", filename);
- return false;
- } else {
+ if (line) {
char titlehdr[strlen(title) + 3];
sprintf(titlehdr, "[%s]", title);
- if (!streq(line, titlehdr)) {
- error("%s: incorrect header in configuration file", filename);
- return false;
- }
+ if (streq(line, titlehdr))
+ return true;
}
- return true;
+ return false;
}
diff --git a/shared/config-parser.h b/shared/config-parser.h
index 3a117a3..4b2103f 100644
--- a/shared/config-parser.h
+++ b/shared/config-parser.h
@@ -53,7 +53,6 @@ bool config_parse_line(const char *filename, char **buf,
int *rkey, const char **rkeyname,
struct cfg_value *rvalue);
-bool config_parse_header(const char *filename,
- const char *title, char **buf);
+bool config_parse_header(const char *title, char **buf);
#endif