Skip to content

Commit

Permalink
config: ignore end-of-line whitespace
Browse files Browse the repository at this point in the history
End-of-line whitespace is now stripped before the option is applied.

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

Additionally, the entire line (end-of-line whitespace stripped) is now
printed to stderr when mako fails to parse an option.
  • Loading branch information
pranjalkole committed Jan 23, 2022
1 parent aafbc91 commit fdd84e2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,17 +734,18 @@ 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] == '#') {
continue;
}

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

if (line[0] == '[' && line[strlen(line) - 1] == ']') {
if (line[0] == '[' && 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 @@ -755,7 +756,7 @@ int load_config_file(struct mako_config *config, char *config_arg) {
}

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

// Strip end-of-line whitespace
char *whitespace = &line[len - 1];
while (whitespace[0] == ' ') {
--whitespace;
}
whitespace[1] = '\0';

bool valid_option = false;
eq[0] = '\0';

Expand All @@ -784,6 +792,7 @@ int load_config_file(struct mako_config *config, char *config_arg) {


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

0 comments on commit fdd84e2

Please sign in to comment.