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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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