1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
23 #include "dirent-util.h"
26 #include "locale-util.h"
27 #include "path-util.h"
29 #include "string-table.h"
30 #include "string-util.h"
34 static int add_locales_from_archive(Set
*locales
) {
35 /* Stolen from glibc... */
41 /* Name hash table. */
42 uint32_t namehash_offset
;
43 uint32_t namehash_used
;
44 uint32_t namehash_size
;
46 uint32_t string_offset
;
49 /* Table with locale records. */
50 uint32_t locrectab_offset
;
51 uint32_t locrectab_used
;
52 uint32_t locrectab_size
;
53 /* MD5 sum hash table. */
54 uint32_t sumhash_offset
;
55 uint32_t sumhash_used
;
56 uint32_t sumhash_size
;
60 /* Hash value of the name. */
62 /* Offset of the name in the string table. */
64 /* Offset of the locale record. */
65 uint32_t locrec_offset
;
68 const struct locarhead
*h
;
69 const struct namehashent
*e
;
70 const void *p
= MAP_FAILED
;
71 _cleanup_close_
int fd
= -1;
77 fd
= open("/usr/lib/locale/locale-archive", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
79 return errno
== ENOENT
? 0 : -errno
;
81 if (fstat(fd
, &st
) < 0)
84 if (!S_ISREG(st
.st_mode
))
87 if (st
.st_size
< (off_t
) sizeof(struct locarhead
))
90 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
94 h
= (const struct locarhead
*) p
;
95 if (h
->magic
!= 0xde020109 ||
96 h
->namehash_offset
+ h
->namehash_size
> st
.st_size
||
97 h
->string_offset
+ h
->string_size
> st
.st_size
||
98 h
->locrectab_offset
+ h
->locrectab_size
> st
.st_size
||
99 h
->sumhash_offset
+ h
->sumhash_size
> st
.st_size
) {
104 e
= (const struct namehashent
*) ((const uint8_t*) p
+ h
->namehash_offset
);
105 for (i
= 0; i
< h
->namehash_size
; i
++) {
108 if (e
[i
].locrec_offset
== 0)
111 if (!utf8_is_valid((char*) p
+ e
[i
].name_offset
))
114 z
= strdup((char*) p
+ e
[i
].name_offset
);
120 r
= set_consume(locales
, z
);
129 munmap((void*) p
, sz
);
134 static int add_locales_from_libdir (Set
*locales
) {
135 _cleanup_closedir_
DIR *dir
= NULL
;
136 struct dirent
*entry
;
139 dir
= opendir("/usr/lib/locale");
141 return errno
== ENOENT
? 0 : -errno
;
143 FOREACH_DIRENT(entry
, dir
, return -errno
) {
146 dirent_ensure_type(dir
, entry
);
148 if (entry
->d_type
!= DT_DIR
)
151 z
= strdup(entry
->d_name
);
155 r
= set_consume(locales
, z
);
156 if (r
< 0 && r
!= -EEXIST
)
163 int get_locales(char ***ret
) {
164 _cleanup_set_free_ Set
*locales
= NULL
;
165 _cleanup_strv_free_
char **l
= NULL
;
168 locales
= set_new(&string_hash_ops
);
172 r
= add_locales_from_archive(locales
);
173 if (r
< 0 && r
!= -ENOENT
)
176 r
= add_locales_from_libdir(locales
);
180 l
= set_get_strv(locales
);
191 bool locale_is_valid(const char *name
) {
196 if (strlen(name
) >= 128)
199 if (!utf8_is_valid(name
))
202 if (!filename_is_valid(name
))
205 if (!string_is_safe(name
))
211 void init_gettext(void) {
212 setlocale(LC_ALL
, "");
213 textdomain(GETTEXT_PACKAGE
);
216 bool is_locale_utf8(void) {
218 static int cached_answer
= -1;
220 /* Note that we default to 'true' here, since today UTF8 is
221 * pretty much supported everywhere. */
223 if (cached_answer
>= 0)
226 if (!setlocale(LC_ALL
, "")) {
227 cached_answer
= true;
231 set
= nl_langinfo(CODESET
);
233 cached_answer
= true;
237 if (streq(set
, "UTF-8")) {
238 cached_answer
= true;
242 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
243 * unset and everything can do to UTF-8 nowadays. */
244 set
= setlocale(LC_CTYPE
, NULL
);
246 cached_answer
= true;
250 /* Check result, but ignore the result if C was set
253 STR_IN_SET(set
, "C", "POSIX") &&
255 !getenv("LC_CTYPE") &&
259 return (bool) cached_answer
;
262 static thread_local Set
*keymaps
= NULL
;
266 const struct stat
*sb
,
268 struct FTW
*ftwbuf
) {
276 if (!endswith(fpath
, ".map") &&
277 !endswith(fpath
, ".map.gz"))
280 p
= strdup(basename(fpath
));
284 e
= endswith(p
, ".map");
288 e
= endswith(p
, ".map.gz");
292 r
= set_consume(keymaps
, p
);
293 if (r
< 0 && r
!= -EEXIST
)
299 int get_keymaps(char ***ret
) {
300 _cleanup_strv_free_
char **l
= NULL
;
304 keymaps
= set_new(&string_hash_ops
);
308 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
309 r
= nftw(dir
, nftw_cb
, 20, FTW_PHYS
|FTW_ACTIONRETVAL
);
312 log_debug("Directory not found %s", dir
);
314 log_debug_errno(r
, "Can't add keymap: %m");
317 l
= set_get_strv(keymaps
);
319 set_free_free(keymaps
);
335 bool keymap_is_valid(const char *name
) {
340 if (strlen(name
) >= 128)
343 if (!utf8_is_valid(name
))
346 if (!filename_is_valid(name
))
349 if (!string_is_safe(name
))
355 const char *special_glyph(SpecialGlyph code
) {
357 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
358 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
359 * works reasonably well on the Linux console. For details see:
361 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
364 static const char* const draw_table
[2][_SPECIAL_GLYPH_MAX
] = {
367 [TREE_VERTICAL
] = "| ",
368 [TREE_BRANCH
] = "|-",
371 [TRIANGULAR_BULLET
] = ">",
372 [BLACK_CIRCLE
] = "*",
380 [TREE_VERTICAL
] = "\342\224\202 ", /* │ */
381 [TREE_BRANCH
] = "\342\224\234\342\224\200", /* ├─ */
382 [TREE_RIGHT
] = "\342\224\224\342\224\200", /* └─ */
383 [TREE_SPACE
] = " ", /* */
384 [TRIANGULAR_BULLET
] = "\342\200\243", /* ‣ */
385 [BLACK_CIRCLE
] = "\342\227\217", /* ● */
386 [ARROW
] = "\342\206\222", /* → */
387 [MDASH
] = "\342\200\223", /* – */
388 [ELLIPSIS
] = "\342\200\246", /* … */
392 return draw_table
[is_locale_utf8()][code
];
395 static const char * const locale_variable_table
[_VARIABLE_LC_MAX
] = {
396 [VARIABLE_LANG
] = "LANG",
397 [VARIABLE_LANGUAGE
] = "LANGUAGE",
398 [VARIABLE_LC_CTYPE
] = "LC_CTYPE",
399 [VARIABLE_LC_NUMERIC
] = "LC_NUMERIC",
400 [VARIABLE_LC_TIME
] = "LC_TIME",
401 [VARIABLE_LC_COLLATE
] = "LC_COLLATE",
402 [VARIABLE_LC_MONETARY
] = "LC_MONETARY",
403 [VARIABLE_LC_MESSAGES
] = "LC_MESSAGES",
404 [VARIABLE_LC_PAPER
] = "LC_PAPER",
405 [VARIABLE_LC_NAME
] = "LC_NAME",
406 [VARIABLE_LC_ADDRESS
] = "LC_ADDRESS",
407 [VARIABLE_LC_TELEPHONE
] = "LC_TELEPHONE",
408 [VARIABLE_LC_MEASUREMENT
] = "LC_MEASUREMENT",
409 [VARIABLE_LC_IDENTIFICATION
] = "LC_IDENTIFICATION"
412 DEFINE_STRING_TABLE_LOOKUP(locale_variable
, LocaleVariable
);