From cfcc7922c2f3ae9659ab41c62c9ad36846013717 Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Tue, 7 Nov 2023 12:58:46 +0100 Subject: [PATCH] xkbcli-compose: Simplify locale options Current options to set the locale are convoluted: - An explicit locale *must* be given, while a sane default would be to use the user environment. - Then there are two options that were useful while testing locale handling: read environment variables or use `setlocale`. But the program has already called: ``` setlocale(LC_ALL, ""); ``` so it turns out the two options lead to the same results. Remove options `--locale-from-env` and `--locale-from-setlocale` and make the locale default to the user environment. --- tools/compile-compose.c | 39 ++++++++++++---------------------- tools/xkbcli-compile-compose.1 | 11 ++-------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/tools/compile-compose.c b/tools/compile-compose.c index a0b83ed..2c5bac3 100644 --- a/tools/compile-compose.c +++ b/tools/compile-compose.c @@ -36,7 +36,7 @@ static void usage(FILE *fp, char *progname) { fprintf(fp, - "Usage: %s [--help] [--file FILE] [--locale LOCALE | --locale-from-env | --locale-from-setlocale]\n", + "Usage: %s [--help] [--file FILE] [--locale LOCALE]\n", progname); fprintf(fp, "\n" @@ -48,12 +48,8 @@ usage(FILE *fp, char *progname) " --file FILE\n" " Specify a Compose file to load\n" " --locale LOCALE\n" - " Specify the locale directly\n" - " --locale-from-env\n" - " Get the locale from the LC_ALL/LC_CTYPE/LANG environment variables (falling back to C)\n" - " --locale-from-setlocale\n" - " Get the locale using setlocale(3)\n" - ); + " Specify the locale directly, instead of relying on the environment variables\n" + " LC_ALL, LC_TYPE and LANG.\n"); } static bool @@ -102,20 +98,21 @@ main(int argc, char *argv[]) enum options { OPT_FILE, OPT_LOCALE, - OPT_LOCALE_FROM_ENV, - OPT_LOCALE_FROM_SETLOCALE, }; static struct option opts[] = { - {"help", no_argument, 0, 'h'}, - {"file", required_argument, 0, OPT_FILE}, - {"locale", required_argument, 0, OPT_LOCALE}, - {"locale-from-env", no_argument, 0, OPT_LOCALE_FROM_ENV}, - {"locale-from-setlocale", no_argument, 0, OPT_LOCALE_FROM_SETLOCALE}, + {"help", no_argument, 0, 'h'}, + {"file", required_argument, 0, OPT_FILE}, + {"locale", required_argument, 0, OPT_LOCALE}, {0, 0, 0, 0}, }; setlocale(LC_ALL, ""); + /* Initialize the locale to use */ + locale = setlocale(LC_CTYPE, NULL); + if (!locale) + locale = "C"; + while (1) { int opt; int option_index = 0; @@ -131,18 +128,6 @@ main(int argc, char *argv[]) case OPT_LOCALE: locale = optarg; break; - case OPT_LOCALE_FROM_ENV: - locale = getenv("LC_ALL"); - if (!locale) - locale = getenv("LC_CTYPE"); - if (!locale) - locale = getenv("LANG"); - if (!locale) - locale = "C"; - break; - case OPT_LOCALE_FROM_SETLOCALE: - locale = setlocale(LC_CTYPE, NULL); - break; case 'h': usage(stdout, argv[0]); return EXIT_SUCCESS; @@ -151,7 +136,9 @@ main(int argc, char *argv[]) return EXIT_INVALID_USAGE; } } + if (locale == NULL) { + fprintf(stderr, "ERROR: Cannot determine the locale.\n"); usage(stderr, argv[0]); return EXIT_INVALID_USAGE; } diff --git a/tools/xkbcli-compile-compose.1 b/tools/xkbcli-compile-compose.1 index 6dc65ae..1d86a3c 100644 --- a/tools/xkbcli-compile-compose.1 +++ b/tools/xkbcli-compile-compose.1 @@ -22,15 +22,8 @@ Print help and exit Specify a Compose file to load . .It Fl \-locale Ar LOCALE -Specify a locale -. -.It Fl \-locale-from-env -Get the locale from the LC_ALL/LC_CTYPE/LANG environment variables -(falling back to C) -. -.It Fl \-locale\-from\-setlocale -Get the locale using -.Xr setlocale 3 +Specify the locale directly, instead of relying on the environment variables +LC_ALL, LC_TYPE and LANG. .El . .Sh SEE ALSO