1 /* SPDX-License-Identifier: LGPL-2.1+ */
18 #include "dirent-util.h"
21 #include "locale-util.h"
22 #include "path-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
29 static int add_locales_from_archive(Set
*locales
) {
30 /* Stolen from glibc... */
36 /* Name hash table. */
37 uint32_t namehash_offset
;
38 uint32_t namehash_used
;
39 uint32_t namehash_size
;
41 uint32_t string_offset
;
44 /* Table with locale records. */
45 uint32_t locrectab_offset
;
46 uint32_t locrectab_used
;
47 uint32_t locrectab_size
;
48 /* MD5 sum hash table. */
49 uint32_t sumhash_offset
;
50 uint32_t sumhash_used
;
51 uint32_t sumhash_size
;
55 /* Hash value of the name. */
57 /* Offset of the name in the string table. */
59 /* Offset of the locale record. */
60 uint32_t locrec_offset
;
63 const struct locarhead
*h
;
64 const struct namehashent
*e
;
65 const void *p
= MAP_FAILED
;
66 _cleanup_close_
int fd
= -1;
72 fd
= open("/usr/lib/locale/locale-archive", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
74 return errno
== ENOENT
? 0 : -errno
;
76 if (fstat(fd
, &st
) < 0)
79 if (!S_ISREG(st
.st_mode
))
82 if (st
.st_size
< (off_t
) sizeof(struct locarhead
))
85 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
89 h
= (const struct locarhead
*) p
;
90 if (h
->magic
!= 0xde020109 ||
91 h
->namehash_offset
+ h
->namehash_size
> st
.st_size
||
92 h
->string_offset
+ h
->string_size
> st
.st_size
||
93 h
->locrectab_offset
+ h
->locrectab_size
> st
.st_size
||
94 h
->sumhash_offset
+ h
->sumhash_size
> st
.st_size
) {
99 e
= (const struct namehashent
*) ((const uint8_t*) p
+ h
->namehash_offset
);
100 for (i
= 0; i
< h
->namehash_size
; i
++) {
103 if (e
[i
].locrec_offset
== 0)
106 if (!utf8_is_valid((char*) p
+ e
[i
].name_offset
))
109 z
= strdup((char*) p
+ e
[i
].name_offset
);
115 r
= set_consume(locales
, z
);
124 munmap((void*) p
, sz
);
129 static int add_locales_from_libdir (Set
*locales
) {
130 _cleanup_closedir_
DIR *dir
= NULL
;
131 struct dirent
*entry
;
134 dir
= opendir("/usr/lib/locale");
136 return errno
== ENOENT
? 0 : -errno
;
138 FOREACH_DIRENT(entry
, dir
, return -errno
) {
141 dirent_ensure_type(dir
, entry
);
143 if (entry
->d_type
!= DT_DIR
)
146 z
= strdup(entry
->d_name
);
150 r
= set_consume(locales
, z
);
151 if (r
< 0 && r
!= -EEXIST
)
158 int get_locales(char ***ret
) {
159 _cleanup_set_free_ Set
*locales
= NULL
;
160 _cleanup_strv_free_
char **l
= NULL
;
163 locales
= set_new(&string_hash_ops
);
167 r
= add_locales_from_archive(locales
);
168 if (r
< 0 && r
!= -ENOENT
)
171 r
= add_locales_from_libdir(locales
);
175 l
= set_get_strv(locales
);
186 bool locale_is_valid(const char *name
) {
191 if (strlen(name
) >= 128)
194 if (!utf8_is_valid(name
))
197 if (!filename_is_valid(name
))
200 if (!string_is_safe(name
))
206 void init_gettext(void) {
207 setlocale(LC_ALL
, "");
208 textdomain(GETTEXT_PACKAGE
);
211 bool is_locale_utf8(void) {
213 static int cached_answer
= -1;
215 /* Note that we default to 'true' here, since today UTF8 is
216 * pretty much supported everywhere. */
218 if (cached_answer
>= 0)
221 if (!setlocale(LC_ALL
, "")) {
222 cached_answer
= true;
226 set
= nl_langinfo(CODESET
);
228 cached_answer
= true;
232 if (streq(set
, "UTF-8")) {
233 cached_answer
= true;
237 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
238 * unset and everything can do to UTF-8 nowadays. */
239 set
= setlocale(LC_CTYPE
, NULL
);
241 cached_answer
= true;
245 /* Check result, but ignore the result if C was set
248 STR_IN_SET(set
, "C", "POSIX") &&
250 !getenv("LC_CTYPE") &&
254 return (bool) cached_answer
;
257 static thread_local Set
*keymaps
= NULL
;
261 const struct stat
*sb
,
263 struct FTW
*ftwbuf
) {
271 if (!endswith(fpath
, ".map") &&
272 !endswith(fpath
, ".map.gz"))
275 p
= strdup(basename(fpath
));
279 e
= endswith(p
, ".map");
283 e
= endswith(p
, ".map.gz");
287 r
= set_consume(keymaps
, p
);
288 if (r
< 0 && r
!= -EEXIST
)
294 int get_keymaps(char ***ret
) {
295 _cleanup_strv_free_
char **l
= NULL
;
299 keymaps
= set_new(&string_hash_ops
);
303 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
304 r
= nftw(dir
, nftw_cb
, 20, FTW_PHYS
|FTW_ACTIONRETVAL
);
307 log_debug("Directory not found %s", dir
);
309 log_debug_errno(r
, "Can't add keymap: %m");
312 l
= set_get_strv(keymaps
);
314 set_free_free(keymaps
);
330 bool keymap_is_valid(const char *name
) {
335 if (strlen(name
) >= 128)
338 if (!utf8_is_valid(name
))
341 if (!filename_is_valid(name
))
344 if (!string_is_safe(name
))
350 const char *special_glyph(SpecialGlyph code
) {
352 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
353 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
354 * works reasonably well on the Linux console. For details see:
356 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
359 static const char* const draw_table
[2][_SPECIAL_GLYPH_MAX
] = {
362 [TREE_VERTICAL
] = "| ",
363 [TREE_BRANCH
] = "|-",
366 [TRIANGULAR_BULLET
] = ">",
367 [BLACK_CIRCLE
] = "*",
375 [TREE_VERTICAL
] = "\342\224\202 ", /* │ */
376 [TREE_BRANCH
] = "\342\224\234\342\224\200", /* ├─ */
377 [TREE_RIGHT
] = "\342\224\224\342\224\200", /* └─ */
378 [TREE_SPACE
] = " ", /* */
379 [TRIANGULAR_BULLET
] = "\342\200\243", /* ‣ */
380 [BLACK_CIRCLE
] = "\342\227\217", /* ● */
381 [ARROW
] = "\342\206\222", /* → */
382 [MDASH
] = "\342\200\223", /* – */
383 [ELLIPSIS
] = "\342\200\246", /* … */
387 return draw_table
[is_locale_utf8()][code
];
390 static const char * const locale_variable_table
[_VARIABLE_LC_MAX
] = {
391 [VARIABLE_LANG
] = "LANG",
392 [VARIABLE_LANGUAGE
] = "LANGUAGE",
393 [VARIABLE_LC_CTYPE
] = "LC_CTYPE",
394 [VARIABLE_LC_NUMERIC
] = "LC_NUMERIC",
395 [VARIABLE_LC_TIME
] = "LC_TIME",
396 [VARIABLE_LC_COLLATE
] = "LC_COLLATE",
397 [VARIABLE_LC_MONETARY
] = "LC_MONETARY",
398 [VARIABLE_LC_MESSAGES
] = "LC_MESSAGES",
399 [VARIABLE_LC_PAPER
] = "LC_PAPER",
400 [VARIABLE_LC_NAME
] = "LC_NAME",
401 [VARIABLE_LC_ADDRESS
] = "LC_ADDRESS",
402 [VARIABLE_LC_TELEPHONE
] = "LC_TELEPHONE",
403 [VARIABLE_LC_MEASUREMENT
] = "LC_MEASUREMENT",
404 [VARIABLE_LC_IDENTIFICATION
] = "LC_IDENTIFICATION"
407 DEFINE_STRING_TABLE_LOOKUP(locale_variable
, LocaleVariable
);