1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-18 10:35:55 +00:00

Rewrite locale handling using nl_langinfo(CODESET), so aliases not needed here

now
This commit is contained in:
Andrey A. Chernov 2001-06-25 21:11:43 +00:00
parent 0cbc94d0e8
commit b15d61cf16
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=78779

View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <locale.h> #include <locale.h>
#include <langinfo.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -92,8 +93,8 @@ static char *alt_system_name;
#endif #endif
#ifdef __FreeBSD__ #ifdef __FreeBSD__
/* short_locale without country suffix */ static char *locale, *locale_opts, *locale_nroff, *locale_codeset;
static char *locale, *short_locale, *locale_opts, *locale_nroff; static char locale_terr[3], locale_lang[3];
static int use_original; static int use_original;
struct ltable { struct ltable {
char *lcode; char *lcode;
@ -102,9 +103,7 @@ struct ltable {
static struct ltable ltable[] = { static struct ltable ltable[] = {
{"KOI8-R", "koi8-r"}, {"KOI8-R", "koi8-r"},
{"ISO8859-1", "latin1"}, {"ISO8859-1", "latin1"},
{"ISO_8859-1", "latin1"},
{"ISO8859-15", "latin1"}, {"ISO8859-15", "latin1"},
{"ISO_8859-15", "latin1"},
{NULL} {NULL}
}; };
#endif #endif
@ -458,44 +457,64 @@ man_getopt (argc, argv)
} }
#ifdef __FreeBSD__ #ifdef __FreeBSD__
if (!use_original && (locale = setlocale(LC_CTYPE, NULL)) != NULL) { /* "" intentionally used to catch error */
char *tmp; if ((locale = setlocale(LC_CTYPE, "")) != NULL)
locale_codeset = nl_langinfo(CODESET);
if (!use_original && locale != NULL && *locale_codeset != '\0' &&
strcmp(locale_codeset, "US-ASCII") != 0
) {
char *tmp, *short_locale;
struct ltable *pltable; struct ltable *pltable;
if ((tmp = strchr(locale, '_')) == NULL *locale_lang = '\0';
|| tmp != locale + 2 *locale_terr = '\0';
|| strlen(tmp + 1) < 4
|| tmp[3] != '.') { if ((short_locale = strdup(locale)) == NULL) {
if (debug) { perror ("ctype locale strdup");
if (strcmp(locale, "C") != 0 && exit (1);
strcmp(locale, "POSIX") != 0 && }
strcmp(locale, "ASCII") != 0 && if ((tmp = strchr(short_locale, '.')) != NULL)
strcmp(locale, "US-ASCII") != 0) { *tmp = '\0';
errno = EINVAL;
perror ("ctype locale env"); if (strlen(short_locale) == 2)
} strcpy(locale_lang, short_locale);
} else if ((tmp = strchr(short_locale, '_')) == NULL ||
tmp != short_locale + 2 ||
strlen(tmp + 1) != 2
) {
errno = EINVAL;
perror ("ctype locale format");
locale = NULL; locale = NULL;
} else { } else {
if ((short_locale = strdup(locale)) == NULL) { strncpy(locale_terr, short_locale + 3, 2);
perror ("ctype locale strdup"); locale_terr[2] = '\0';
exit (1); strncpy(locale_lang, short_locale, 2);
} locale_lang[2] = '\0';
tmp = short_locale + 3; }
tmp[0] = short_locale[0];
tmp[1] = short_locale[1];
short_locale = tmp;
tmp = short_locale + 3; free(short_locale);
if (locale != NULL) {
for (pltable = ltable; pltable->lcode != NULL; pltable++) { for (pltable = ltable; pltable->lcode != NULL; pltable++) {
if (strcmp(pltable->lcode, tmp) == 0) { if (strcmp(pltable->lcode, locale_codeset) == 0) {
locale_nroff = pltable->nroff; locale_nroff = pltable->nroff;
break; break;
} }
} }
} }
} else {
if (locale == NULL) {
errno = EINVAL;
perror ("ctype locale");
} else {
locale = NULL;
if (*locale_codeset == '\0') {
errno = EINVAL;
perror ("ctype codeset");
}
}
} }
#endif #endif /* __FreeBSD__ */
if (pager == NULL || *pager == '\0') if (pager == NULL || *pager == '\0')
if ((pager = getenv ("PAGER")) == NULL) if ((pager = getenv ("PAGER")) == NULL)
@ -1630,15 +1649,22 @@ man (name)
l_found = 0; l_found = 0;
if (locale != NULL) { if (locale != NULL) {
locale_opts = locale_nroff; locale_opts = locale_nroff;
snprintf(buf, sizeof(buf), "%s/%s", *mp, locale); if (*locale_lang != '\0' && *locale_terr != '\0') {
if (is_directory (buf) == 1) snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
l_found = try_section (buf, section, name, glob); locale_lang, locale_terr, locale_codeset);
if (!l_found) {
snprintf(buf, sizeof(buf), "%s/%s", *mp, short_locale);
if (is_directory (buf) == 1) if (is_directory (buf) == 1)
l_found = try_section (buf, section, name, glob); l_found = try_section (buf, section, name, glob);
if (!l_found && (*short_locale != 'e' || *(short_locale + 1) != 'n')) { }
snprintf(buf, sizeof(buf), "%s/en.%s", *mp, short_locale + 3); if (!l_found) {
if (*locale_lang != '\0') {
snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
locale_lang, locale_codeset);
if (is_directory (buf) == 1)
l_found = try_section (buf, section, name, glob);
}
if (!l_found && strcmp(locale_lang, "en") != 0) {
snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
locale_codeset);
if (is_directory (buf) == 1) if (is_directory (buf) == 1)
l_found = try_section (buf, section, name, glob); l_found = try_section (buf, section, name, glob);
} }
@ -1672,15 +1698,22 @@ man (name)
l_found = 0; l_found = 0;
if (locale != NULL) { if (locale != NULL) {
locale_opts = locale_nroff; locale_opts = locale_nroff;
snprintf(buf, sizeof(buf), "%s/%s", *mp, locale); if (*locale_lang != '\0' && *locale_terr != '\0') {
if (is_directory (buf) == 1) snprintf(buf, sizeof(buf), "%s/%s_%s.%s", *mp,
l_found = try_section (buf, *sp, name, glob); locale_lang, locale_terr, locale_codeset);
if (!l_found) {
snprintf(buf, sizeof(buf), "%s/%s", *mp, short_locale);
if (is_directory (buf) == 1) if (is_directory (buf) == 1)
l_found = try_section (buf, *sp, name, glob); l_found = try_section (buf, *sp, name, glob);
if (!l_found && (*short_locale != 'e' || *(short_locale + 1) != 'n')) { }
snprintf(buf, sizeof(buf), "%s/en.%s", *mp, short_locale + 3); if (!l_found) {
if (*locale_lang != '\0') {
snprintf(buf, sizeof(buf), "%s/%s.%s", *mp,
locale_lang, locale_codeset);
if (is_directory (buf) == 1)
l_found = try_section (buf, *sp, name, glob);
}
if (!l_found && strcmp(locale_lang, "en") != 0) {
snprintf(buf, sizeof(buf), "%s/en.%s", *mp,
locale_codeset);
if (is_directory (buf) == 1) if (is_directory (buf) == 1)
l_found = try_section (buf, *sp, name, glob); l_found = try_section (buf, *sp, name, glob);
} }