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
);
204 bool locale_is_valid(const char *name
) {
209 if (strlen(name
) >= 128)
212 if (!utf8_is_valid(name
))
215 if (!filename_is_valid(name
))
218 if (!string_is_safe(name
))
224 void init_gettext(void) {
225 setlocale(LC_ALL
, "");
226 textdomain(GETTEXT_PACKAGE
);
229 bool is_locale_utf8(void) {
231 static int cached_answer
= -1;
233 /* Note that we default to 'true' here, since today UTF8 is
234 * pretty much supported everywhere. */
236 if (cached_answer
>= 0)
239 if (!setlocale(LC_ALL
, "")) {
240 cached_answer
= true;
244 set
= nl_langinfo(CODESET
);
246 cached_answer
= true;
250 if (streq(set
, "UTF-8")) {
251 cached_answer
= true;
255 /* For LC_CTYPE=="C" return true, because CTYPE is effectly
256 * unset and everything can do to UTF-8 nowadays. */
257 set
= setlocale(LC_CTYPE
, NULL
);
259 cached_answer
= true;
263 /* Check result, but ignore the result if C was set
266 STR_IN_SET(set
, "C", "POSIX") &&
268 !getenv("LC_CTYPE") &&
272 return (bool) cached_answer
;
275 static thread_local Set
*keymaps
= NULL
;
279 const struct stat
*sb
,
281 struct FTW
*ftwbuf
) {
289 if (!endswith(fpath
, ".map") &&
290 !endswith(fpath
, ".map.gz"))
293 p
= strdup(basename(fpath
));
297 e
= endswith(p
, ".map");
301 e
= endswith(p
, ".map.gz");
305 r
= set_consume(keymaps
, p
);
306 if (r
< 0 && r
!= -EEXIST
)
312 int get_keymaps(char ***ret
) {
313 _cleanup_strv_free_
char **l
= NULL
;
317 keymaps
= set_new(&string_hash_ops
);
321 NULSTR_FOREACH(dir
, KBD_KEYMAP_DIRS
) {
322 r
= nftw(dir
, nftw_cb
, 20, FTW_MOUNT
|FTW_PHYS
|FTW_ACTIONRETVAL
);
325 log_debug("Directory not found %s", dir
);
327 log_debug_errno(r
, "Can't add keymap: %m");
330 l
= set_get_strv(keymaps
);
332 set_free_free(keymaps
);
348 bool keymap_is_valid(const char *name
) {
353 if (strlen(name
) >= 128)
356 if (!utf8_is_valid(name
))
359 if (!filename_is_valid(name
))
362 if (!string_is_safe(name
))
368 const char *special_glyph(SpecialGlyph code
) {
370 static const char* const draw_table
[2][_SPECIAL_GLYPH_MAX
] = {
373 [TREE_VERTICAL
] = "| ",
374 [TREE_BRANCH
] = "|-",
377 [TRIANGULAR_BULLET
] = ">",
378 [BLACK_CIRCLE
] = "*",
385 [TREE_VERTICAL
] = "\342\224\202 ", /* │ */
386 [TREE_BRANCH
] = "\342\224\234\342\224\200", /* ├─ */
387 [TREE_RIGHT
] = "\342\224\224\342\224\200", /* └─ */
388 [TREE_SPACE
] = " ", /* */
389 [TRIANGULAR_BULLET
] = "\342\200\243", /* ‣ */
390 [BLACK_CIRCLE
] = "\342\227\217", /* ● */
391 [ARROW
] = "\342\206\222", /* → */
392 [MDASH
] = "\342\200\223", /* – */
396 return draw_table
[is_locale_utf8()][code
];
399 static const char * const locale_variable_table
[_VARIABLE_LC_MAX
] = {
400 [VARIABLE_LANG
] = "LANG",
401 [VARIABLE_LANGUAGE
] = "LANGUAGE",
402 [VARIABLE_LC_CTYPE
] = "LC_CTYPE",
403 [VARIABLE_LC_NUMERIC
] = "LC_NUMERIC",
404 [VARIABLE_LC_TIME
] = "LC_TIME",
405 [VARIABLE_LC_COLLATE
] = "LC_COLLATE",
406 [VARIABLE_LC_MONETARY
] = "LC_MONETARY",
407 [VARIABLE_LC_MESSAGES
] = "LC_MESSAGES",
408 [VARIABLE_LC_PAPER
] = "LC_PAPER",
409 [VARIABLE_LC_NAME
] = "LC_NAME",
410 [VARIABLE_LC_ADDRESS
] = "LC_ADDRESS",
411 [VARIABLE_LC_TELEPHONE
] = "LC_TELEPHONE",
412 [VARIABLE_LC_MEASUREMENT
] = "LC_MEASUREMENT",
413 [VARIABLE_LC_IDENTIFICATION
] = "LC_IDENTIFICATION"
416 DEFINE_STRING_TABLE_LOOKUP(locale_variable
, LocaleVariable
);