Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: ignore leading and trailing whitespace #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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