Skip to content

Commit

Permalink
config: ignore leading and trailing whitespace
Browse files Browse the repository at this point in the history
Leading and trailing whitespace is now stripped before parsing the line.

getline returns line length, so strlen calls have been replaced with
that. Also, getline now runs until -1 is returned.

Additionally, the entire line (whitespace stripped) is now
printed to stderr when mako fails to parse an option.
  • Loading branch information
pranjalkole committed Oct 14, 2023
1 parent 1ee0327 commit 09a0e69
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,38 @@ int load_config_file(struct mako_config *config, char *config_arg) {
wl_container_of(config->criteria.next, criteria, link);

size_t n = 0;
while (getline(&line, &n, f) > 0) {
ssize_t len = 0;
while ((len = getline(&line, &n, f)) != -1) {
++lineno;
if (line[0] == '\0' || line[0] == '\n' || line[0] == '#') {

// We can't operate on `line` because we need to free it, so
// make a copy
char *stripped_line = line;

// Strip leading whitespace
while (stripped_line[0] == ' ' || stripped_line[0] == '\t') {
++stripped_line;
--len;
}

if (stripped_line[0] == '\0' || stripped_line[0] == '\n' ||
stripped_line[0] == '#') {
continue;
}

if (line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
if (stripped_line[len - 1] == '\n') {
stripped_line[--len] = '\0';
}

if (line[0] == '[' && line[strlen(line) - 1] == ']') {
// Strip trailing whitespace
while (stripped_line[len - 1] == ' ' ||
stripped_line[len - 1] == '\t') {
--len;
}
stripped_line[len] = '\0';


if (stripped_line[0] == '[' && stripped_line[len - 1] == ']') {
// Since we hit the end of the previous criteria section, validate
// that it doesn't break any rules before moving on.
if (criteria != NULL && !validate_criteria(criteria)) {
Expand All @@ -782,7 +803,7 @@ int load_config_file(struct mako_config *config, char *config_arg) {
}

free(section);
section = strndup(line + 1, strlen(line) - 2);
section = strndup(stripped_line + 1, len - 2);
criteria = create_criteria(config);
if (!parse_criteria(section, criteria)) {
fprintf(stderr, "[%s:%d] Invalid criteria definition\n", base,
Expand All @@ -793,7 +814,7 @@ int load_config_file(struct mako_config *config, char *config_arg) {
continue;
}

char *eq = strchr(line, '=');
char *eq = strchr(stripped_line, '=');
if (!eq) {
fprintf(stderr, "[%s:%d] Expected key=value\n", base, lineno);
ret = -1;
Expand All @@ -803,16 +824,19 @@ int load_config_file(struct mako_config *config, char *config_arg) {
bool valid_option = false;
eq[0] = '\0';

valid_option = apply_style_option(&criteria->style, line, eq + 1);
valid_option = apply_style_option(&criteria->style,
stripped_line, eq + 1);

if (!valid_option && section == NULL) {
valid_option = apply_config_option(config, line, eq + 1);
valid_option = apply_config_option(config,
stripped_line, eq + 1);
}


if (!valid_option) {
eq[0] = '=';
fprintf(stderr, "[%s:%d] Failed to parse option '%s'\n",
base, lineno, line);
base, lineno, stripped_line);
ret = -1;
break;
}
Expand Down

0 comments on commit 09a0e69

Please sign in to comment.