}
static bool
-parse_line(struct conf *conf, const char *line, char **errmsg)
+parse_line(const char *line, char **key, char **value, char **errmsg)
{
const char *p, *q;
- char *key, *value;
- bool result;
#define SKIP_WS(x) while (isspace(*x)) { ++x; }
p = line;
SKIP_WS(p);
if (*p == '\0' || *p == '#') {
+ *key = NULL;
+ *value = NULL;
return true;
}
q = p;
while (isalpha(*q) || *q == '_') {
++q;
}
- key = x_strndup(p, q - p);
+ *key = x_strndup(p, q - p);
p = q;
SKIP_WS(p);
if (*p != '=') {
*errmsg = x_strdup("missing equal sign");
- free(key);
+ free(*key);
return false;
}
++p;
while (isspace(q[-1])) {
--q;
}
- value = x_strndup(p, q - p);
-
-#undef SKIP_WS
+ *value = x_strndup(p, q - p);
- result = handle_conf_setting(conf, key, value, errmsg, false, false);
+ return true;
- free(key);
- free(value);
- return result;
+#undef SKIP_WS
}
/* For test purposes. */
line_number = 0;
while (fgets(buf, sizeof(buf), f)) {
- char *errmsg2;
+ char *errmsg2, *key, *value;
+ bool ok;
++line_number;
- if (!parse_line(conf, buf, &errmsg2)) {
+ ok = parse_line(buf, &key, &value, &errmsg2);
+ if (ok && key) { /* key == NULL if comment or blank line */
+ ok = handle_conf_setting(conf, key, value, &errmsg2, false, false);
+ }
+ if (!ok) {
*errmsg = format("%s:%u: %s", path, line_number, errmsg2);
free(errmsg2);
result = false;