Skip to content

Commit

Permalink
xkbcli-compose: Simplify locale options
Browse files Browse the repository at this point in the history
Current options to set the locale are covoluted:
- An explicit locale *must* be given, while a sane default would be
  to use the user environment.
- Then there are two options: read environment variables or use
  `setlocale`. But the program has already called:
  ```
  setlocale(LC_ALL, "");
  ```
  so 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.
  • Loading branch information
wismill committed Nov 12, 2023
1 parent a7ac908 commit 881d2ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
39 changes: 13 additions & 26 deletions tools/compile-compose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 void
Expand Down Expand Up @@ -101,20 +97,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;
Expand All @@ -130,18 +127,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;
Expand All @@ -150,7 +135,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;
}
Expand Down
11 changes: 2 additions & 9 deletions tools/xkbcli-compile-compose.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 881d2ef

Please sign in to comment.