diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2020-03-20 18:38:55 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2020-04-11 09:06:57 +1000 |
commit | 560ba890b3e125bd8fed9441a74d27818a69c8bf (patch) | |
tree | 50508daffcb67b076fc1082853fe549b4d3d7b24 | |
parent | f7a6f751dd5305911ab081a6595942c7782fed6b (diff) | |
download | libinput-560ba890b3e125bd8fed9441a74d27818a69c8bf.tar.gz |
tools: record: drop quotes from os-release information
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
(cherry picked from commit d80bbcb028910cc6c10acab04a2ee3e53fae7b16)
-rw-r--r-- | src/util-strings.h | 26 | ||||
-rw-r--r-- | test/test-utils.c | 39 | ||||
-rw-r--r-- | tools/libinput-record.c | 4 |
3 files changed, 67 insertions, 2 deletions
diff --git a/src/util-strings.h b/src/util-strings.h index 15e03cef..73c23d90 100644 --- a/src/util-strings.h +++ b/src/util-strings.h @@ -312,3 +312,29 @@ error: free(result); return -1; } + +/** + * Strip any of the characters in what from the beginning and end of the + * input string. + * + * @return a newly allocated string with none of "what" at the beginning or + * end of string + */ +static inline char * +strstrip(const char *input, const char *what) +{ + char *str, *last; + + str = safe_strdup(&input[strspn(input, what)]); + + last = str; + + for (char *c = str; *c != '\0'; c++) { + if (!strchr(what, *c)) + last = c + 1; + } + + *last = '\0'; + + return str; +} diff --git a/test/test-utils.c b/test/test-utils.c index 789d6e70..3304483e 100644 --- a/test/test-utils.c +++ b/test/test-utils.c @@ -1033,6 +1033,44 @@ START_TEST(strjoin_test) } END_TEST +START_TEST(strstrip_test) +{ + struct strstrip_test { + const char *string; + const char *expected; + const char *what; + } tests[] = { + { "foo", "foo", "1234" }, + { "\"bar\"", "bar", "\"" }, + { "'bar'", "bar", "'" }, + { "\"bar\"", "\"bar\"", "'" }, + { "'bar'", "'bar'", "\"" }, + { "\"bar\"", "bar", "\"" }, + { "\"\"", "", "\"" }, + { "\"foo\"bar\"", "foo\"bar", "\"" }, + { "\"'foo\"bar\"", "foo\"bar", "\"'" }, + { "abcfooabcbarbca", "fooabcbar", "abc" }, + { "xxxxfoo", "foo", "x" }, + { "fooyyyy", "foo", "y" }, + { "xxxxfooyyyy", "foo", "xy" }, + { "x xfooy y", " xfooy ", "xy" }, + { " foo\n", "foo", " \n" }, + { "", "", "abc" }, + { "", "", "" }, + { NULL , NULL, NULL } + }; + struct strstrip_test *t = tests; + + while (t->string) { + char *str; + str = strstrip(t->string, t->what); + ck_assert_str_eq(str, t->expected); + free(str); + t++; + } +} +END_TEST + START_TEST(list_test_insert) { struct list_test { @@ -1138,6 +1176,7 @@ litest_utils_suite(void) tcase_add_test(tc, strsplit_test); tcase_add_test(tc, kvsplit_double_test); tcase_add_test(tc, strjoin_test); + tcase_add_test(tc, strstrip_test); tcase_add_test(tc, time_conversion); tcase_add_test(tc, list_test_insert); diff --git a/tools/libinput-record.c b/tools/libinput-record.c index 6d45efc5..4c41009b 100644 --- a/tools/libinput-record.c +++ b/tools/libinput-record.c @@ -1447,9 +1447,9 @@ print_system_header(struct record_context *ctx) osrstr[strlen(osrstr) - 1] = '\0'; /* linebreak */ if (!distro && strneq(osrstr, "ID=", 3)) - distro = safe_strdup(&osrstr[3]); + distro = strstrip(&osrstr[3], "\"'"); else if (!version && strneq(osrstr, "VERSION_ID=", 11)) - version = safe_strdup(&osrstr[11]); + version = strstrip(&osrstr[11], "\"'"); if (distro && version) { iprintf(ctx, "os: \"%s:%s\"\n", distro, version); |