1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
21 #include "dirent-util.h"
24 #include "locale-util.h"
25 #include "path-util.h"
27 #include "string-table.h"
28 #include "string-util.h"
32 static int add_locales_from_archive(Set *locales) {
33 /* Stolen from glibc... */
39 /* Name hash table. */
40 uint32_t namehash_offset;
41 uint32_t namehash_used;
42 uint32_t namehash_size;
44 uint32_t string_offset;
47 /* Table with locale records. */
48 uint32_t locrectab_offset;
49 uint32_t locrectab_used;
50 uint32_t locrectab_size;
51 /* MD5 sum hash table. */
52 uint32_t sumhash_offset;
53 uint32_t sumhash_used;
54 uint32_t sumhash_size;
58 /* Hash value of the name. */
60 /* Offset of the name in the string table. */
62 /* Offset of the locale record. */
63 uint32_t locrec_offset;
66 const struct locarhead *h;
67 const struct namehashent *e;
68 const void *p = MAP_FAILED;
69 _cleanup_close_ int fd = -1;
75 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
77 return errno == ENOENT ? 0 : -errno;
79 if (fstat(fd, &st) < 0)
82 if (!S_ISREG(st.st_mode))
85 if (st.st_size < (off_t) sizeof(struct locarhead))
88 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
92 h = (const struct locarhead *) p;
93 if (h->magic != 0xde020109 ||
94 h->namehash_offset + h->namehash_size > st.st_size ||
95 h->string_offset + h->string_size > st.st_size ||
96 h->locrectab_offset + h->locrectab_size > st.st_size ||
97 h->sumhash_offset + h->sumhash_size > st.st_size) {
102 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
103 for (i = 0; i < h->namehash_size; i++) {
106 if (e[i].locrec_offset == 0)
109 if (!utf8_is_valid((char*) p + e[i].name_offset))
112 z = strdup((char*) p + e[i].name_offset);
118 r = set_consume(locales, z);
127 munmap((void*) p, sz);
132 static int add_locales_from_libdir (Set *locales) {
133 _cleanup_closedir_ DIR *dir = NULL;
134 struct dirent *entry;
137 dir = opendir("/usr/lib/locale");
139 return errno == ENOENT ? 0 : -errno;
141 FOREACH_DIRENT(entry, dir, return -errno) {
144 dirent_ensure_type(dir, entry);
146 if (entry->d_type != DT_DIR)
149 z = strdup(entry->d_name);
153 r = set_consume(locales, z);
154 if (r < 0 && r != -EEXIST)
161 int get_locales(char ***ret) {
162 _cleanup_set_free_ Set *locales = NULL;
163 _cleanup_strv_free_ char **l = NULL;
166 locales = set_new(&string_hash_ops);
170 r = add_locales_from_archive(locales);
171 if (r < 0 && r != -ENOENT)
174 r = add_locales_from_libdir(locales);
178 l = set_get_strv(locales);
189 bool locale_is_valid(const char *name) {
194 if (strlen(name) >= 128)
197 if (!utf8_is_valid(name))
200 if (!filename_is_valid(name))
203 if (!string_is_safe(name))
209 void init_gettext(void) {
210 setlocale(LC_ALL, "");
211 textdomain(GETTEXT_PACKAGE);
214 bool is_locale_utf8(void) {
216 static int cached_answer = -1;
218 /* Note that we default to 'true' here, since today UTF8 is
219 * pretty much supported everywhere. */
221 if (cached_answer >= 0)
224 if (!setlocale(LC_ALL, "")) {
225 cached_answer = true;
229 set = nl_langinfo(CODESET);
231 cached_answer = true;
235 if (streq(set, "UTF-8")) {
236 cached_answer = true;
240 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
241 * unset and everything can do to UTF-8 nowadays. */
242 set = setlocale(LC_CTYPE, NULL);
244 cached_answer = true;
248 /* Check result, but ignore the result if C was set
251 STR_IN_SET(set, "C", "POSIX") &&
253 !getenv("LC_CTYPE") &&
257 return (bool) cached_answer;
260 static thread_local Set *keymaps = NULL;
264 const struct stat *sb,
266 struct FTW *ftwbuf) {
274 if (!endswith(fpath, ".map") &&
275 !endswith(fpath, ".map.gz"))
278 p = strdup(basename(fpath));
282 e = endswith(p, ".map");
286 e = endswith(p, ".map.gz");
290 r = set_consume(keymaps, p);
291 if (r < 0 && r != -EEXIST)
297 int get_keymaps(char ***ret) {
298 _cleanup_strv_free_ char **l = NULL;
302 keymaps = set_new(&string_hash_ops);
306 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
307 r = nftw(dir, nftw_cb, 20, FTW_PHYS|FTW_ACTIONRETVAL);
310 log_debug("Directory not found %s", dir);
312 log_debug_errno(r, "Can't add keymap: %m");
315 l = set_get_strv(keymaps);
317 set_free_free(keymaps);
333 bool keymap_is_valid(const char *name) {
338 if (strlen(name) >= 128)
341 if (!utf8_is_valid(name))
344 if (!filename_is_valid(name))
347 if (!string_is_safe(name))
353 const char *special_glyph(SpecialGlyph code) {
355 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
356 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
357 * works reasonably well on the Linux console. For details see:
359 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
362 static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
365 [TREE_VERTICAL] = "| ",
366 [TREE_BRANCH] = "|-",
369 [TRIANGULAR_BULLET] = ">",
370 [BLACK_CIRCLE] = "*",
378 [TREE_VERTICAL] = "\342\224\202 ", /* │ */
379 [TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
380 [TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
381 [TREE_SPACE] = " ", /* */
382 [TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
383 [BLACK_CIRCLE] = "\342\227\217", /* ● */
384 [ARROW] = "\342\206\222", /* → */
385 [MDASH] = "\342\200\223", /* – */
386 [ELLIPSIS] = "\342\200\246", /* … */
390 return draw_table[is_locale_utf8()][code];
393 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
394 [VARIABLE_LANG] = "LANG",
395 [VARIABLE_LANGUAGE] = "LANGUAGE",
396 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
397 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
398 [VARIABLE_LC_TIME] = "LC_TIME",
399 [VARIABLE_LC_COLLATE] = "LC_COLLATE",
400 [VARIABLE_LC_MONETARY] = "LC_MONETARY",
401 [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
402 [VARIABLE_LC_PAPER] = "LC_PAPER",
403 [VARIABLE_LC_NAME] = "LC_NAME",
404 [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
405 [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
406 [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
407 [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
410 DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);