1 /* SPDX-License-Identifier: LGPL-2.1+ */
17 #include "dirent-util.h"
21 #include "locale-util.h"
22 #include "path-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
29 static char *normalize_locale(const char *name
) {
32 /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
33 * lowercases everything, and removes most special chars. This means the official .UTF-8 suffix
34 * becomes .utf8 when looking things up on disk. When enumerating locales, let's do the reverse
35 * operation, and go back to ".UTF-8" which appears to be the more commonly accepted name. We only do
36 * that for UTF-8 however, since it's kinda the only charset that matters. */
38 e
= endswith(name
, ".utf8");
40 _cleanup_free_
char *prefix
= NULL
;
42 prefix
= strndup(name
, e
- name
);
46 return strjoin(prefix
, ".UTF-8");
49 e
= strstr(name
, ".utf8@");
51 _cleanup_free_
char *prefix
= NULL
;
53 prefix
= strndup(name
, e
- name
);
57 return strjoin(prefix
, ".UTF-8@", e
+ 6);
63 static int add_locales_from_archive(Set
*locales
) {
64 /* Stolen from glibc... */
70 /* Name hash table. */
71 uint32_t namehash_offset
;
72 uint32_t namehash_used
;
73 uint32_t namehash_size
;
75 uint32_t string_offset
;
78 /* Table with locale records. */
79 uint32_t locrectab_offset
;
80 uint32_t locrectab_used
;
81 uint32_t locrectab_size
;
82 /* MD5 sum hash table. */
83 uint32_t sumhash_offset
;
84 uint32_t sumhash_used
;
85 uint32_t sumhash_size
;
89 /* Hash value of the name. */
91 /* Offset of the name in the string table. */
93 /* Offset of the locale record. */
94 uint32_t locrec_offset
;
97 const struct locarhead
*h
;
98 const struct namehashent
*e
;
99 const void *p
= MAP_FAILED
;
100 _cleanup_close_
int fd
= -1;
106 fd
= open("/usr/lib/locale/locale-archive", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
108 return errno
== ENOENT
? 0 : -errno
;
110 if (fstat(fd
, &st
) < 0)
113 if (!S_ISREG(st
.st_mode
))
116 if (st
.st_size
< (off_t
) sizeof(struct locarhead
))
119 p
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
123 h
= (const struct locarhead
*) p
;
124 if (h
->magic
!= 0xde020109 ||
125 h
->namehash_offset
+ h
->namehash_size
> st
.st_size
||
126 h
->string_offset
+ h
->string_size
> st
.st_size
||
127 h
->locrectab_offset
+ h
->locrectab_size
> st
.st_size
||
128 h
->sumhash_offset
+ h
->sumhash_size
> st
.st_size
) {
133 e
= (const struct namehashent
*) ((const uint8_t*) p
+ h
->namehash_offset
);
134 for (i
= 0; i
< h
->namehash_size
; i
++) {
137 if (e
[i
].locrec_offset
== 0)
140 if (!utf8_is_valid((char*) p
+ e
[i
].name_offset
))
143 z
= normalize_locale((char*) p
+ e
[i
].name_offset
);
149 r
= set_consume(locales
, z
);
158 munmap((void*) p
, sz
);
163 static int add_locales_from_libdir (Set
*locales
) {
164 _cleanup_closedir_
DIR *dir
= NULL
;
165 struct dirent
*entry
;
168 dir
= opendir("/usr/lib/locale");
170 return errno
== ENOENT
? 0 : -errno
;
172 FOREACH_DIRENT(entry
, dir
, return -errno
) {
175 dirent_ensure_type(dir
, entry
);
177 if (entry
->d_type
!= DT_DIR
)
180 z
= normalize_locale(entry
->d_name
);
184 r
= set_consume(locales
, z
);
185 if (r
< 0 && r
!= -EEXIST
)
192 int get_locales(char ***ret
) {
193 _cleanup_set_free_ Set
*locales
= NULL
;
194 _cleanup_strv_free_
char **l
= NULL
;
197 locales
= set_new(&string_hash_ops
);
201 r
= add_locales_from_archive(locales
);
202 if (r
< 0 && r
!= -ENOENT
)
205 r
= add_locales_from_libdir(locales
);
209 l
= set_get_strv(locales
);
213 r
= getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
214 if (r
== -ENXIO
|| r
== 0) {
217 /* Filter out non-UTF-8 locales, because it's 2019, by default */
218 for (a
= b
= l
; *a
; a
++) {
220 if (endswith(*a
, "UTF-8") ||
221 strstr(*a
, ".UTF-8@"))
230 log_debug_errno(r
, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean");
239 bool locale_is_valid(const char *name
) {
244 if (strlen(name
) >= 128)
247 if (!utf8_is_valid(name
))
250 if (!filename_is_valid(name
))
253 if (!string_is_safe(name
))
259 void init_gettext(void) {
260 setlocale(LC_ALL
, "");
261 textdomain(GETTEXT_PACKAGE
);
264 bool is_locale_utf8(void) {
266 static int cached_answer
= -1;
268 /* Note that we default to 'true' here, since today UTF8 is
269 * pretty much supported everywhere. */
271 if (cached_answer
>= 0)
274 if (!setlocale(LC_ALL
, "")) {
275 cached_answer
= true;
279 set
= nl_langinfo(CODESET
);
281 cached_answer
= true;
285 if (streq(set
, "UTF-8")) {
286 cached_answer
= true;
290 /* For LC_CTYPE=="C" return true, because CTYPE is effectively
291 * unset and everything can do to UTF-8 nowadays. */
292 set
= setlocale(LC_CTYPE
, NULL
);
294 cached_answer
= true;
298 /* Check result, but ignore the result if C was set
301 STR_IN_SET(set
, "C", "POSIX") &&
303 !getenv("LC_CTYPE") &&
307 return (bool) cached_answer
;
310 static bool emoji_enabled(void) {
311 static int cached_emoji_enabled
= -1;
313 if (cached_emoji_enabled
< 0) {
316 val
= getenv_bool("SYSTEMD_EMOJI");
318 cached_emoji_enabled
=
320 !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
322 cached_emoji_enabled
= val
;
325 return cached_emoji_enabled
;
328 const char *special_glyph(SpecialGlyph code
) {
330 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
331 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
332 * works reasonably well on the Linux console. For details see:
334 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
337 static const char* const draw_table
[2][_SPECIAL_GLYPH_MAX
] = {
340 [SPECIAL_GLYPH_TREE_VERTICAL
] = "| ",
341 [SPECIAL_GLYPH_TREE_BRANCH
] = "|-",
342 [SPECIAL_GLYPH_TREE_RIGHT
] = "`-",
343 [SPECIAL_GLYPH_TREE_SPACE
] = " ",
344 [SPECIAL_GLYPH_TRIANGULAR_BULLET
] = ">",
345 [SPECIAL_GLYPH_BLACK_CIRCLE
] = "*",
346 [SPECIAL_GLYPH_BULLET
] = "*",
347 [SPECIAL_GLYPH_ARROW
] = "->",
348 [SPECIAL_GLYPH_MDASH
] = "-",
349 [SPECIAL_GLYPH_ELLIPSIS
] = "...",
350 [SPECIAL_GLYPH_MU
] = "u",
351 [SPECIAL_GLYPH_CHECK_MARK
] = "+",
352 [SPECIAL_GLYPH_CROSS_MARK
] = "-",
353 [SPECIAL_GLYPH_ECSTATIC_SMILEY
] = ":-]",
354 [SPECIAL_GLYPH_HAPPY_SMILEY
] = ":-}",
355 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
] = ":-)",
356 [SPECIAL_GLYPH_NEUTRAL_SMILEY
] = ":-|",
357 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
] = ":-(",
358 [SPECIAL_GLYPH_UNHAPPY_SMILEY
] = ":-{",
359 [SPECIAL_GLYPH_DEPRESSED_SMILEY
] = ":-[",
364 [SPECIAL_GLYPH_TREE_VERTICAL
] = "\342\224\202 ", /* │ */
365 [SPECIAL_GLYPH_TREE_BRANCH
] = "\342\224\234\342\224\200", /* ├─ */
366 [SPECIAL_GLYPH_TREE_RIGHT
] = "\342\224\224\342\224\200", /* └─ */
367 [SPECIAL_GLYPH_TREE_SPACE
] = " ", /* */
368 [SPECIAL_GLYPH_TRIANGULAR_BULLET
] = "\342\200\243", /* ‣ */
369 [SPECIAL_GLYPH_BLACK_CIRCLE
] = "\342\227\217", /* ● */
370 [SPECIAL_GLYPH_BULLET
] = "\342\200\242", /* • */
371 [SPECIAL_GLYPH_ARROW
] = "\342\206\222", /* → */
372 [SPECIAL_GLYPH_MDASH
] = "\342\200\223", /* – */
373 [SPECIAL_GLYPH_ELLIPSIS
] = "\342\200\246", /* … */
374 [SPECIAL_GLYPH_MU
] = "\316\274", /* μ */
375 [SPECIAL_GLYPH_CHECK_MARK
] = "\342\234\223", /* ✓ */
376 [SPECIAL_GLYPH_CROSS_MARK
] = "\342\234\227", /* ✗ */
377 [SPECIAL_GLYPH_ECSTATIC_SMILEY
] = "\360\237\230\207", /* 😇 */
378 [SPECIAL_GLYPH_HAPPY_SMILEY
] = "\360\237\230\200", /* 😀 */
379 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY
] = "\360\237\231\202", /* 🙂 */
380 [SPECIAL_GLYPH_NEUTRAL_SMILEY
] = "\360\237\230\220", /* 😐 */
381 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY
] = "\360\237\231\201", /* 🙁 */
382 [SPECIAL_GLYPH_UNHAPPY_SMILEY
] = "\360\237\230\250", /* 😨️️ */
383 [SPECIAL_GLYPH_DEPRESSED_SMILEY
] = "\360\237\244\242", /* 🤢 */
387 assert(code
< _SPECIAL_GLYPH_MAX
);
389 return draw_table
[code
>= _SPECIAL_GLYPH_FIRST_SMILEY
? emoji_enabled() : is_locale_utf8()][code
];
392 void locale_variables_free(char *l
[_VARIABLE_LC_MAX
]) {
398 for (i
= 0; i
< _VARIABLE_LC_MAX
; i
++)
402 static const char * const locale_variable_table
[_VARIABLE_LC_MAX
] = {
403 [VARIABLE_LANG
] = "LANG",
404 [VARIABLE_LANGUAGE
] = "LANGUAGE",
405 [VARIABLE_LC_CTYPE
] = "LC_CTYPE",
406 [VARIABLE_LC_NUMERIC
] = "LC_NUMERIC",
407 [VARIABLE_LC_TIME
] = "LC_TIME",
408 [VARIABLE_LC_COLLATE
] = "LC_COLLATE",
409 [VARIABLE_LC_MONETARY
] = "LC_MONETARY",
410 [VARIABLE_LC_MESSAGES
] = "LC_MESSAGES",
411 [VARIABLE_LC_PAPER
] = "LC_PAPER",
412 [VARIABLE_LC_NAME
] = "LC_NAME",
413 [VARIABLE_LC_ADDRESS
] = "LC_ADDRESS",
414 [VARIABLE_LC_TELEPHONE
] = "LC_TELEPHONE",
415 [VARIABLE_LC_MEASUREMENT
] = "LC_MEASUREMENT",
416 [VARIABLE_LC_IDENTIFICATION
] = "LC_IDENTIFICATION"
419 DEFINE_STRING_TABLE_LOOKUP(locale_variable
, LocaleVariable
);