1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
36 #include "dirent-util.h"
39 #include "locale-util.h"
40 #include "path-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
47 static int add_locales_from_archive(Set *locales) {
48 /* Stolen from glibc... */
54 /* Name hash table. */
55 uint32_t namehash_offset;
56 uint32_t namehash_used;
57 uint32_t namehash_size;
59 uint32_t string_offset;
62 /* Table with locale records. */
63 uint32_t locrectab_offset;
64 uint32_t locrectab_used;
65 uint32_t locrectab_size;
66 /* MD5 sum hash table. */
67 uint32_t sumhash_offset;
68 uint32_t sumhash_used;
69 uint32_t sumhash_size;
73 /* Hash value of the name. */
75 /* Offset of the name in the string table. */
77 /* Offset of the locale record. */
78 uint32_t locrec_offset;
81 const struct locarhead *h;
82 const struct namehashent *e;
83 const void *p = MAP_FAILED;
84 _cleanup_close_ int fd = -1;
90 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
92 return errno == ENOENT ? 0 : -errno;
94 if (fstat(fd, &st) < 0)
97 if (!S_ISREG(st.st_mode))
100 if (st.st_size < (off_t) sizeof(struct locarhead))
103 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
107 h = (const struct locarhead *) p;
108 if (h->magic != 0xde020109 ||
109 h->namehash_offset + h->namehash_size > st.st_size ||
110 h->string_offset + h->string_size > st.st_size ||
111 h->locrectab_offset + h->locrectab_size > st.st_size ||
112 h->sumhash_offset + h->sumhash_size > st.st_size) {
117 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
118 for (i = 0; i < h->namehash_size; i++) {
121 if (e[i].locrec_offset == 0)
124 if (!utf8_is_valid((char*) p + e[i].name_offset))
127 z = strdup((char*) p + e[i].name_offset);
133 r = set_consume(locales, z);
142 munmap((void*) p, sz);
147 static int add_locales_from_libdir (Set *locales) {
148 _cleanup_closedir_ DIR *dir = NULL;
149 struct dirent *entry;
152 dir = opendir("/usr/lib/locale");
154 return errno == ENOENT ? 0 : -errno;
156 FOREACH_DIRENT(entry, dir, return -errno) {
159 dirent_ensure_type(dir, entry);
161 if (entry->d_type != DT_DIR)
164 z = strdup(entry->d_name);
168 r = set_consume(locales, z);
169 if (r < 0 && r != -EEXIST)
176 int get_locales(char ***ret) {
177 _cleanup_set_free_ Set *locales = NULL;
178 _cleanup_strv_free_ char **l = NULL;
181 locales = set_new(&string_hash_ops);
185 r = add_locales_from_archive(locales);
186 if (r < 0 && r != -ENOENT)
189 r = add_locales_from_libdir(locales);
193 l = set_get_strv(locales);
205 bool locale_is_valid(const char *name) {
210 if (strlen(name) >= 128)
213 if (!utf8_is_valid(name))
216 if (!filename_is_valid(name))
219 if (!string_is_safe(name))
225 void init_gettext(void) {
226 setlocale(LC_ALL, "");
227 textdomain(GETTEXT_PACKAGE);
230 bool is_locale_utf8(void) {
232 static int cached_answer = -1;
234 /* Note that we default to 'true' here, since today UTF8 is
235 * pretty much supported everywhere. */
237 if (cached_answer >= 0)
240 if (!setlocale(LC_ALL, "")) {
241 cached_answer = true;
245 set = nl_langinfo(CODESET);
247 cached_answer = true;
251 if (streq(set, "UTF-8")) {
252 cached_answer = true;
256 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
257 * unset and everything can do to UTF-8 nowadays. */
258 set = setlocale(LC_CTYPE, NULL);
260 cached_answer = true;
264 /* Check result, but ignore the result if C was set
267 STR_IN_SET(set, "C", "POSIX") &&
269 !getenv("LC_CTYPE") &&
273 return (bool) cached_answer;
276 static thread_local Set *keymaps = NULL;
280 const struct stat *sb,
282 struct FTW *ftwbuf) {
290 if (!endswith(fpath, ".map") &&
291 !endswith(fpath, ".map.gz"))
294 p = strdup(basename(fpath));
298 e = endswith(p, ".map");
302 e = endswith(p, ".map.gz");
306 r = set_consume(keymaps, p);
307 if (r < 0 && r != -EEXIST)
313 int get_keymaps(char ***ret) {
314 _cleanup_strv_free_ char **l = NULL;
318 keymaps = set_new(&string_hash_ops);
322 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
323 r = nftw(dir, nftw_cb, 20, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL);
326 log_debug("Directory not found %s", dir);
328 log_debug_errno(r, "Can't add keymap: %m");
331 l = set_get_strv(keymaps);
333 set_free_free(keymaps);
349 bool keymap_is_valid(const char *name) {
354 if (strlen(name) >= 128)
357 if (!utf8_is_valid(name))
360 if (!filename_is_valid(name))
363 if (!string_is_safe(name))
369 const char *special_glyph(SpecialGlyph code) {
371 static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
374 [TREE_VERTICAL] = "| ",
375 [TREE_BRANCH] = "|-",
378 [TRIANGULAR_BULLET] = ">",
379 [BLACK_CIRCLE] = "*",
386 [TREE_VERTICAL] = "\342\224\202 ", /* │ */
387 [TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
388 [TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
389 [TREE_SPACE] = " ", /* */
390 [TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
391 [BLACK_CIRCLE] = "\342\227\217", /* ● */
392 [ARROW] = "\342\206\222", /* → */
393 [MDASH] = "\342\200\223", /* – */
397 return draw_table[is_locale_utf8()][code];
400 static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
401 [VARIABLE_LANG] = "LANG",
402 [VARIABLE_LANGUAGE] = "LANGUAGE",
403 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
404 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
405 [VARIABLE_LC_TIME] = "LC_TIME",
406 [VARIABLE_LC_COLLATE] = "LC_COLLATE",
407 [VARIABLE_LC_MONETARY] = "LC_MONETARY",
408 [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
409 [VARIABLE_LC_PAPER] = "LC_PAPER",
410 [VARIABLE_LC_NAME] = "LC_NAME",
411 [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
412 [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
413 [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
414 [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
417 DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);