From f87c9bb9f70f1a6c8c7081c762c15ba7eaaf971e Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 3 Aug 2024 08:12:13 +0100 Subject: xmalloc: remove delim param from xstrjoin() and add separate xstrjoin3() This avoids the need for an unused third argument for most xstrjoin() calls and replaces the cases where it's needed with a more flexible function. Code generation is the same in both cases, when there are 2 string params and a compile-time known delimiter. This commit also converts 4 uses of xasprintf() to use xstrjoin*(). See also: https://godbolt.org/z/xsjrhv9b6 --- main.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index a6ec1cb7..f46d712e 100644 --- a/main.c +++ b/main.c @@ -258,7 +258,7 @@ main(int argc, char *const *argv) break; case 't': - tll_push_back(overrides, xstrjoin("term=", optarg, 0)); + tll_push_back(overrides, xstrjoin("term=", optarg)); break; case 'L': @@ -266,11 +266,11 @@ main(int argc, char *const *argv) break; case 'T': - tll_push_back(overrides, xstrjoin("title=", optarg, 0)); + tll_push_back(overrides, xstrjoin("title=", optarg)); break; case 'a': - tll_push_back(overrides, xstrjoin("app-id=", optarg, 0)); + tll_push_back(overrides, xstrjoin("app-id=", optarg)); break; case 'D': { @@ -284,7 +284,7 @@ main(int argc, char *const *argv) } case 'f': { - char *font_override = xstrjoin("font=", optarg, 0); + char *font_override = xstrjoin("font=", optarg); tll_push_back(overrides, font_override); break; } @@ -658,15 +658,19 @@ out: UNITTEST { - char *s = xstrjoin("foo", "bar", 0); + char *s = xstrjoin("foo", "bar"); xassert(streq(s, "foobar")); free(s); - s = xstrjoin("foo", "bar", ' '); + s = xstrjoin3("foo", " ", "bar"); xassert(streq(s, "foo bar")); free(s); - s = xstrjoin("foo", "bar", ','); + s = xstrjoin3("foo", ",", "bar"); xassert(streq(s, "foo,bar")); free(s); + + s = xstrjoin3("foo", "bar", "baz"); + xassert(streq(s, "foobarbaz")); + free(s); } -- cgit v1.3 From 6d351ffc43175b59359258eca83e652d76cfc426 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 10 Aug 2024 16:38:35 +0200 Subject: main: invalid locale != non-UTF-8 locale When doing locale fallback, and printing user notifications and log warnings, better separate the case "locale is invalid" from "locale is valid but not UTF-8". Closes #1798 --- main.c | 57 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index f46d712e..b2266d88 100644 --- a/main.c +++ b/main.c @@ -425,19 +425,18 @@ main(int argc, char *const *argv) * that does not exist on this system, then the above call may return * NULL. We should just continue with the fallback method below. */ - LOG_WARN("setlocale() failed"); - locale = "C"; + LOG_ERR("setlocale() failed"); } - LOG_INFO("locale: %s", locale); + LOG_INFO("locale: %s", locale != NULL ? locale : ""); - bool bad_locale = !locale_is_utf8(); + bool bad_locale = locale == NULL || !locale_is_utf8(); if (bad_locale) { static const char fallback_locales[][12] = { "C.UTF-8", "en_US.UTF-8", }; - char *saved_locale = xstrdup(locale); + char *saved_locale = locale != NULL ? xstrdup(locale) : NULL; /* * Try to force an UTF-8 locale. If we succeed, launch the @@ -448,13 +447,23 @@ main(int argc, char *const *argv) const char *const fallback_locale = fallback_locales[i]; if (setlocale(LC_CTYPE, fallback_locale) != NULL) { - LOG_WARN("'%s' is not a UTF-8 locale, using '%s' instead", - saved_locale, fallback_locale); - - user_notification_add_fmt( - &user_notifications, USER_NOTIFICATION_WARNING, - "'%s' is not a UTF-8 locale, using '%s' instead", - saved_locale, fallback_locale); + if (saved_locale != NULL) { + LOG_WARN( + "'%s' is not a UTF-8 locale, falling back to '%s'", + saved_locale, fallback_locale); + + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_WARNING, + "'%s' is not a UTF-8 locale, falling back to '%s'", + saved_locale, fallback_locale); + + } else { + LOG_WARN( + "invalid locale, falling back to '%s'", fallback_locale); + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_WARNING, + "invalid locale, falling back to '%s'", fallback_locale); + } bad_locale = false; break; @@ -462,14 +471,22 @@ main(int argc, char *const *argv) } if (bad_locale) { - LOG_ERR( - "'%s' is not a UTF-8 locale, and failed to find a fallback", - saved_locale); - - user_notification_add_fmt( - &user_notifications, USER_NOTIFICATION_ERROR, - "'%s' is not a UTF-8 locale, and failed to find a fallback", - saved_locale); + if (saved_locale != NULL) { + LOG_ERR( + "'%s' is not a UTF-8 locale, and failed to find a fallback", + saved_locale); + + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_ERROR, + "'%s' is not a UTF-8 locale, and failed to find a fallback", + saved_locale); + } else { + LOG_ERR("invalid locale, and failed to find a fallback"); + + user_notification_add_fmt( + &user_notifications, USER_NOTIFICATION_ERROR, + "invalid locale, and failed to find a fallback"); + } } free(saved_locale); } -- cgit v1.3 From cee0c5423ae1b306fc85960058adfd9574e4c91b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 11 Aug 2024 11:11:38 +0200 Subject: main: spell out the most common reason for setlocale() to fail --- main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'main.c') diff --git a/main.c b/main.c index b2266d88..15741012 100644 --- a/main.c +++ b/main.c @@ -425,7 +425,8 @@ main(int argc, char *const *argv) * that does not exist on this system, then the above call may return * NULL. We should just continue with the fallback method below. */ - LOG_ERR("setlocale() failed"); + LOG_ERR("setlocale() failed. The most common cause is that the " + "configured locale is not available, or has been misspelled"); } LOG_INFO("locale: %s", locale != NULL ? locale : ""); -- cgit v1.3