[PATCH v2 libinput 07/14] quirks: allow for in-line comments

Peter Hutterer peter.hutterer at who-t.net
Fri Jun 8 06:00:14 UTC 2018


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 data/README.md     |  1 -
 src/quirks.c       | 20 +++++++++++++++--
 test/test-quirks.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/data/README.md b/data/README.md
index 66897938..55b9056f 100644
--- a/data/README.md
+++ b/data/README.md
@@ -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
diff --git a/src/quirks.c b/src/quirks.c
index b88d8bc9..1a44574a 100644
--- a/src/quirks.c
+++ b/src/quirks.c
@@ -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;
 
diff --git a/test/test-quirks.c b/test/test-quirks.c
index 646559ed..6c427dad 100644
--- a/test/test-quirks.c
+++ b/test/test-quirks.c
@@ -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);
-- 
2.14.4



More information about the wayland-devel mailing list