quirks: allow for in-line comments

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2018-05-31 09:42:15 +10:00
parent fc6e6aad36
commit 3ce70cfa91
3 changed files with 83 additions and 4 deletions

View file

@ -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

View file

@ -767,10 +767,26 @@ parse_file(struct quirks_context *ctx, const char *path)
}
while (fgets(line, sizeof(line), fp)) {
lineno++;
if (strlen(line) >= 1 && line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0'; /* drop trailing \n */
char *comment;
lineno++;
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;

View file

@ -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;
@ -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;
@ -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);