1 /* SPDX-License-Identifier: LGPL-2.1+ */
15 #include "dirent-util.h"
19 #include "locale-util.h"
20 #include "path-util.h"
22 #include "string-table.h"
23 #include "string-util.h"
27 static char *normalize_locale(const char *name
) {
30 /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
31 * lowercases everything, and removes most special chars. This means the official .UTF-8 suffix
32 * becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse
33 * operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do
34 * that for UTF-8 however, since it's kinda the only charset that matters. */
36 e
= endswith(name
, ".utf8");
38 _cleanup_free_
char *prefix
= NULL
;
40 prefix
= strndup(name
, e
- name
);
44 return strjoin(prefix
, ".UTF-8");
47 e
= strstr(name
, ".utf8@");
49 _cleanup_free_
char *prefix
= NULL
;
51 prefix
= strndup(name
, e
- name
);
55 return strjoin(prefix
, ".UTF-8@", e
+ 6);
61 static int add_locales_from_archive(Set
*locales
) {
62 /* Stolen from glibc... */
68 /* Name hash table. */
69 uint32_t namehash_offset
;
70 uint32_t namehash_used
;
71 uint32_t namehash_size
;
73 uint32_t string_offset
;
76 /* Table with locale records. */
77 uint32_t locrectab_offset
;
78 uint32_t locrectab_used
;
79 uint32_t locrectab_size
;
80 /* MD5 sum hash table. */
81 uint32_t sumhash_offset
;
82 uint32_t sumhash_used
;
83 uint32_t sumhash_size
;
87 /* Hash value of the name. */
89 /* Offset of the name in the string table. */
91 /* Offset of the locale record. */
92 uint32_t locrec_offset
;
95 const struct locarhead
*h
;
96 const struct namehashent
*e
;
97 const void *p
= MAP_FAILED
;
98 _cleanup_close_
int fd
= -1;
104 fd
= open("/usr/lib/locale/locale-archive", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
106 return errno
== ENOENT
? 0 : -errno
;
108 if (fstat(fd
, &st
) < 0)
111 if (!S_ISREG(st
.st_mode
))
114 if (st
.st_size
< (off_t
) sizeof(struct locarhead
))
117 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
121 h
= (const struct locarhead
*) p
;
122 if (h
->magic
!= 0xde020109 ||
123 h
->namehash_offset
+ h
->namehash_size
> st
.st_size
||
124 h
->string_offset
+ h
->string_size
> st
.st_size
||
125 h
->locrectab_offset
+ h
->locrectab_size
> st
.st_size
||
126 h
->sumhash_offset
+ h
->sumhash_size
> st
.st_size
) {
131 e
= (const struct namehashent
*) ((const uint8_t*) p
+ h
->namehash_offset
);
132 for (i
= 0; i
< h
->namehash_size
; i
++) {
135 if (e
[i
].locrec_offset
== 0)
138 if (!utf8_is_valid((char*) p
+ e
[i
].name_offset
))
141 z
= normalize_locale((char*) p
+ e
[i
].name_offset
);
147 r
= set_consume(locales
, z
);
156 munmap((void*) p
, sz
);
161 static int add_locales_from_libdir (Set
*locales
) {
162 _cleanup_closedir_
DIR *dir
= NULL
;
163 struct dirent
*entry
;
166 dir
= opendir("/usr/lib/locale");
168 return errno
== ENOENT
? 0 : -errno
;
170 FOREACH_DIRENT(entry
, dir
, return -errno
) {
173 dirent_ensure_type(dir
, entry
);
175 if (entry
->d_type
!= DT_DIR
)
178 z
= normalize_locale(entry
->d_name
);
182 r
= set_consume(locales
, z
);
183 if (r
< 0 && r
!= -EEXIST
)
190 int get_locales(char ***ret
) {
191 _cleanup_set_free_ Set
*locales
= NULL
;
192 _cleanup_strv_free_
char **l
= NULL
;
195 locales
= set_new(&string_hash_ops
);
199 r
= add_locales_from_archive(locales
);
200 if (r
< 0 && r
!= -ENOENT
)
203 r
= add_locales_from_libdir(locales
);
207 l
= set_get_strv(locales
);
211 r
= getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
212 if (r
== -ENXIO
|| r
== 0) {
215 /* Filter out non-UTF-8 locales, because it's 2019, by default */
216 for (a
= b
= l
; *a
; a
++) {
218 if (endswith(*a
, "UTF-8") ||
219 strstr(*a
, ".UTF-8@"))
228 log_debug_errno(r
, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean");
237 bool locale_is_valid(const char *name
) {
242 if (strlen(name
) >= 128)
245 if (!utf8_is_valid(name
))
248 if (!filename_is_valid(name
))
251 if (!string_is_safe(name
))
257 void init_gettext(void) {
258 setlocale(LC_ALL
, "");
259 textdomain(GETTEXT_PACKAGE
);
262 bool is_locale_utf8(void) {
264 static int cached_answer
= -1;
266 /* Note that we default to 'true' here, since today UTF8 is
267 * pretty much supported everywhere. */
269 if (cached_answer
>= 0)
272 if (!setlocale(LC_ALL
, "")) {
273 cached_answer
= true;
277 set
= nl_langinfo(CODESET
);
279 cached_answer
= true;
283 if (streq(set
, "UTF-8")) {
284 cached_answer
= true;
288 /* For LC_CTYPE=="C" return true, because CTYPE is effectively
289 * unset and everything can do to UTF-8 nowadays. */
290 set
= setlocale(LC_CTYPE
, NULL
);
292 cached_answer
= true;
296 /* Check result, but ignore the result if C was set
299 STR_IN_SET(set
, "C", "POSIX") &&
301 !getenv("LC_CTYPE") &&
305 return (bool) cached_answer
;
308 static bool emoji_enabled(void) {
309 static int cached_emoji_enabled
= -1;
311 if (cached_emoji_enabled
< 0) {
314 val
= getenv_bool("SYSTEMD_EMOJI");
316 cached_emoji_enabled
=
318 !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
320 cached_emoji_enabled
= val
;
323 return cached_emoji_enabled
;
326 const char *special_glyph(SpecialGlyph code
) {
328 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
329 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
330 * works reasonably well on the Linux console. For details see:
332 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
335 static const char* const draw_table
[2][_SPECIAL_GLYPH_MAX
] = {
338 [SPECIAL_GLYPH_TREE_VERTICAL
] = "| ",
339 [SPECIAL_GLYPH_TREE_BRANCH
] = "|-",
340 [SPECIAL_GLYPH_TREE_RIGHT
] = "`-",
341 [SPECIAL_GLYPH_TREE_SPACE
] = " ",
342 [SPECIAL_GLYPH_TRIANGULAR_BULLET
] = ">",
343 [SPECIAL_GLYPH_BLACK_CIRCLE
] = "*",
344 [SPECIAL_GLYPH_BULLET
] = "*",
345 [SPECIAL_GLYPH_ARROW
] = "->",
346 [SPECIAL_GLYPH_MDASH
] = "-",
347 [SPECIAL_GLYPH_ELLIPSIS
] = "...",
348 [SPECIAL_GLYPH_MU
] = "u",
349 [SPECIAL_GLYPH_CHECK_MARK
] = "+",
350 [SPECIAL_GLYPH_CROSS_MARK
] = "-",
351 [SPECIAL_GLYPH_ECSTATIC_SMILEY
] = ":-]",
352 [SPECIAL_GLYPH_HAPPY_SMILEY
] = ":-}",
353 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
] = ":-)",
354 [SPECIAL_GLYPH_NEUTRAL_SMILEY
] = ":-|",
355 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
] = ":-(",
356 [SPECIAL_GLYPH_UNHAPPY_SMILEY
] = ":-{",
357 [SPECIAL_GLYPH_DEPRESSED_SMILEY
] = ":-[",
362 [SPECIAL_GLYPH_TREE_VERTICAL
] = "\342\224\202 ", /* │ */
363 [SPECIAL_GLYPH_TREE_BRANCH
] = "\342\224\234\342\224\200", /* ├─ */
364 [SPECIAL_GLYPH_TREE_RIGHT
] = "\342\224\224\342\224\200", /* └─ */
365 [SPECIAL_GLYPH_TREE_SPACE
] = " ", /* */
366 [SPECIAL_GLYPH_TRIANGULAR_BULLET
] = "\342\200\243", /* ‣ */
367 [SPECIAL_GLYPH_BLACK_CIRCLE
] = "\342\227\217", /* ● */
368 [SPECIAL_GLYPH_BULLET
] = "\342\200\242", /* • */
369 [SPECIAL_GLYPH_ARROW
] = "\342\206\222", /* → */
370 [SPECIAL_GLYPH_MDASH
] = "\342\200\223", /* – */
371 [SPECIAL_GLYPH_ELLIPSIS
] = "\342\200\246", /* … */
372 [SPECIAL_GLYPH_MU
] = "\316\274", /* μ */
373 [SPECIAL_GLYPH_CHECK_MARK
] = "\342\234\223", /* ✓ */
374 [SPECIAL_GLYPH_CROSS_MARK
] = "\342\234\227", /* ✗ */
375 [SPECIAL_GLYPH_ECSTATIC_SMILEY
] = "\360\237\230\207", /* 😇 */
376 [SPECIAL_GLYPH_HAPPY_SMILEY
] = "\360\237\230\200", /* 😀 */
377 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
] = "\360\237\231\202", /* 🙂 */
378 [SPECIAL_GLYPH_NEUTRAL_SMILEY
] = "\360\237\230\220", /* 😐 */
379 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
] = "\360\237\231\201", /* 🙁 */
380 [SPECIAL_GLYPH_UNHAPPY_SMILEY
] = "\360\237\230\250", /* 😨️️ */
381 [SPECIAL_GLYPH_DEPRESSED_SMILEY
] = "\360\237\244\242", /* 🤢 */
385 assert(code
< _SPECIAL_GLYPH_MAX
);
387 return draw_table
[code
>= _SPECIAL_GLYPH_FIRST_SMILEY
? emoji_enabled() : is_locale_utf8()][code
];
390 void locale_variables_free(char *l
[_VARIABLE_LC_MAX
]) {
396 for (i
= 0; i
< _VARIABLE_LC_MAX
; i
++)
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
);