Skip to content

Commit

Permalink
quirks: allow for in-line comments
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
  • Loading branch information
whot committed Jun 8, 2018
1 parent fc6e6aa commit 3ce70cf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
1 change: 0 additions & 1 deletion data/README.md
Expand Up @@ -60,7 +60,6 @@ The following will cause parser errors and are considered invalid data
files:

* Whitespace at the beginning of the line
* Inline comments, e.g. `MatchBus=usb # oops, fail`
* Sections without at least one `Match*` entry
* Sections with the same `Match*` entry repeated
* Sections without at least one of `Model*` or `Attr` entries
Expand Down
20 changes: 18 additions & 2 deletions src/quirks.c
Expand Up @@ -767,10 +767,26 @@ parse_file(struct quirks_context *ctx, const char *path)
}

while (fgets(line, sizeof(line), fp)) {
char *comment;

lineno++;
if (strlen(line) >= 1 && line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0'; /* drop trailing \n */

comment = strstr(line, "#");
if (comment) {
/* comment points to # but we need to remove the
* preceding whitespaces too */
comment--;
while (comment >= line) {
if (*comment != ' ' && *comment != '\t')
break;
comment--;
}
*(comment + 1) = '\0';
} else { /* strip the trailing newline */
comment = strstr(line, "\n");
if (comment)
*comment = '\0';
}
if (strlen(line) == 0)
continue;

Expand Down
64 changes: 64 additions & 0 deletions test/test-quirks.c
Expand Up @@ -264,6 +264,25 @@ START_TEST(quirks_parse_error_section)
}
END_TEST

START_TEST(quirks_parse_error_trailing_whitespace)
{
struct quirks_context *ctx;
const char quirks_file[] =
"[Section name]\n"
"MatchUdevType=mouse \n"
"AttrSizeHint=10x10\n";
struct data_dir dd = make_data_dir(quirks_file);

ctx = quirks_init_subsystem(dd.dirname,
NULL,
log_handler,
NULL,
QLOG_CUSTOM_LOG_PRIORITIES);
ck_assert(ctx == NULL);
cleanup_data_dir(dd);
}
END_TEST

START_TEST(quirks_parse_error_unknown_match)
{
struct quirks_context *ctx;
Expand Down Expand Up @@ -340,6 +359,48 @@ START_TEST(quirks_parse_error_model_not_one)
}
END_TEST

START_TEST(quirks_parse_comment_inline)
{
struct quirks_context *ctx;
const char quirks_file[] =
"[Section name] # some inline comment\n"
"MatchUdevType=mouse\t # another inline comment\n"
"ModelAppleTouchpad=1#\n";
struct data_dir dd = make_data_dir(quirks_file);

ctx = quirks_init_subsystem(dd.dirname,
NULL,
log_handler,
NULL,
QLOG_CUSTOM_LOG_PRIORITIES);
ck_assert_notnull(ctx);
quirks_context_unref(ctx);
cleanup_data_dir(dd);
}
END_TEST

START_TEST(quirks_parse_comment_empty)
{
struct quirks_context *ctx;
const char quirks_file[] =
"[Section name]\n"
"#\n"
" #\n"
"MatchUdevType=mouse\n"
"ModelAppleTouchpad=1\n";
struct data_dir dd = make_data_dir(quirks_file);

ctx = quirks_init_subsystem(dd.dirname,
NULL,
log_handler,
NULL,
QLOG_CUSTOM_LOG_PRIORITIES);
ck_assert_notnull(ctx);
quirks_context_unref(ctx);
cleanup_data_dir(dd);
}
END_TEST

START_TEST(quirks_parse_bustype)
{
struct quirks_context *ctx;
Expand Down Expand Up @@ -786,10 +847,13 @@ TEST_COLLECTION(quirks)
litest_add_for_device("quirks:structure", quirks_section_duplicate_attr, LITEST_MOUSE);

litest_add_for_device("quirks:parsing", quirks_parse_error_section, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_error_trailing_whitespace, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_error_unknown_match, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_error_unknown_attr, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_error_unknown_model, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_error_model_not_one, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_comment_inline, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_comment_empty, LITEST_MOUSE);

litest_add_for_device("quirks:parsing", quirks_parse_bustype, LITEST_MOUSE);
litest_add_for_device("quirks:parsing", quirks_parse_bustype_invalid, LITEST_MOUSE);
Expand Down

0 comments on commit 3ce70cf

Please sign in to comment.