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.
master
Pierre Le Marre 2023-11-07 12:58:46 +01:00 committed by Wismill
parent 0a577a0998
commit cfcc7922c2
2 changed files with 15 additions and 35 deletions

View File

@ -36,7 +36,7 @@ static void
usage(FILE *fp, char *progname) usage(FILE *fp, char *progname)
{ {
fprintf(fp, 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); progname);
fprintf(fp, fprintf(fp,
"\n" "\n"
@ -48,12 +48,8 @@ usage(FILE *fp, char *progname)
" --file FILE\n" " --file FILE\n"
" Specify a Compose file to load\n" " Specify a Compose file to load\n"
" --locale LOCALE\n" " --locale LOCALE\n"
" Specify the locale directly\n" " Specify the locale directly, instead of relying on the environment variables\n"
" --locale-from-env\n" " LC_ALL, LC_TYPE and LANG.\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"
);
} }
static bool static bool
@ -102,20 +98,21 @@ main(int argc, char *argv[])
enum options { enum options {
OPT_FILE, OPT_FILE,
OPT_LOCALE, OPT_LOCALE,
OPT_LOCALE_FROM_ENV,
OPT_LOCALE_FROM_SETLOCALE,
}; };
static struct option opts[] = { static struct option opts[] = {
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"file", required_argument, 0, OPT_FILE}, {"file", required_argument, 0, OPT_FILE},
{"locale", required_argument, 0, OPT_LOCALE}, {"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},
{0, 0, 0, 0}, {0, 0, 0, 0},
}; };
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
/* Initialize the locale to use */
locale = setlocale(LC_CTYPE, NULL);
if (!locale)
locale = "C";
while (1) { while (1) {
int opt; int opt;
int option_index = 0; int option_index = 0;
@ -131,18 +128,6 @@ main(int argc, char *argv[])
case OPT_LOCALE: case OPT_LOCALE:
locale = optarg; locale = optarg;
break; 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': case 'h':
usage(stdout, argv[0]); usage(stdout, argv[0]);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -151,7 +136,9 @@ main(int argc, char *argv[])
return EXIT_INVALID_USAGE; return EXIT_INVALID_USAGE;
} }
} }
if (locale == NULL) { if (locale == NULL) {
fprintf(stderr, "ERROR: Cannot determine the locale.\n");
usage(stderr, argv[0]); usage(stderr, argv[0]);
return EXIT_INVALID_USAGE; return EXIT_INVALID_USAGE;
} }

View File

@ -22,15 +22,8 @@ Print help and exit
Specify a Compose file to load Specify a Compose file to load
. .
.It Fl \-locale Ar LOCALE .It Fl \-locale Ar LOCALE
Specify a locale Specify the locale directly, instead of relying on the environment variables
. LC_ALL, LC_TYPE and LANG.
.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
.El .El
. .
.Sh SEE ALSO .Sh SEE ALSO