]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/locale-util.c
5fd18028bba5a78892c298e8b1e3d6be498153ea
[thirdparty/systemd.git] / src / basic / locale-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <dirent.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <ftw.h>
7 #include <langinfo.h>
8 #include <libintl.h>
9 #include <locale.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15
16 #include "def.h"
17 #include "dirent-util.h"
18 #include "env-util.h"
19 #include "fd-util.h"
20 #include "hashmap.h"
21 #include "locale-util.h"
22 #include "path-util.h"
23 #include "set.h"
24 #include "string-table.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "utf8.h"
28
29 static char *normalize_locale(const char *name) {
30 const char *e;
31
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. */
37
38 e = endswith(name, ".utf8");
39 if (e) {
40 _cleanup_free_ char *prefix = NULL;
41
42 prefix = strndup(name, e - name);
43 if (!prefix)
44 return NULL;
45
46 return strjoin(prefix, ".UTF-8");
47 }
48
49 e = strstr(name, ".utf8@");
50 if (e) {
51 _cleanup_free_ char *prefix = NULL;
52
53 prefix = strndup(name, e - name);
54 if (!prefix)
55 return NULL;
56
57 return strjoin(prefix, ".UTF-8@", e + 6);
58 }
59
60 return strdup(name);
61 }
62
63 static int add_locales_from_archive(Set *locales) {
64 /* Stolen from glibc... */
65
66 struct locarhead {
67 uint32_t magic;
68 /* Serial number. */
69 uint32_t serial;
70 /* Name hash table. */
71 uint32_t namehash_offset;
72 uint32_t namehash_used;
73 uint32_t namehash_size;
74 /* String table. */
75 uint32_t string_offset;
76 uint32_t string_used;
77 uint32_t string_size;
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;
86 };
87
88 struct namehashent {
89 /* Hash value of the name. */
90 uint32_t hashval;
91 /* Offset of the name in the string table. */
92 uint32_t name_offset;
93 /* Offset of the locale record. */
94 uint32_t locrec_offset;
95 };
96
97 const struct locarhead *h;
98 const struct namehashent *e;
99 const void *p = MAP_FAILED;
100 _cleanup_close_ int fd = -1;
101 size_t sz = 0;
102 struct stat st;
103 size_t i;
104 int r;
105
106 fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
107 if (fd < 0)
108 return errno == ENOENT ? 0 : -errno;
109
110 if (fstat(fd, &st) < 0)
111 return -errno;
112
113 if (!S_ISREG(st.st_mode))
114 return -EBADMSG;
115
116 if (st.st_size < (off_t) sizeof(struct locarhead))
117 return -EBADMSG;
118
119 p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
120 if (p == MAP_FAILED)
121 return -errno;
122
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) {
129 r = -EBADMSG;
130 goto finish;
131 }
132
133 e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
134 for (i = 0; i < h->namehash_size; i++) {
135 char *z;
136
137 if (e[i].locrec_offset == 0)
138 continue;
139
140 if (!utf8_is_valid((char*) p + e[i].name_offset))
141 continue;
142
143 z = normalize_locale((char*) p + e[i].name_offset);
144 if (!z) {
145 r = -ENOMEM;
146 goto finish;
147 }
148
149 r = set_consume(locales, z);
150 if (r < 0)
151 goto finish;
152 }
153
154 r = 0;
155
156 finish:
157 if (p != MAP_FAILED)
158 munmap((void*) p, sz);
159
160 return r;
161 }
162
163 static int add_locales_from_libdir (Set *locales) {
164 _cleanup_closedir_ DIR *dir = NULL;
165 struct dirent *entry;
166 int r;
167
168 dir = opendir("/usr/lib/locale");
169 if (!dir)
170 return errno == ENOENT ? 0 : -errno;
171
172 FOREACH_DIRENT(entry, dir, return -errno) {
173 char *z;
174
175 dirent_ensure_type(dir, entry);
176
177 if (entry->d_type != DT_DIR)
178 continue;
179
180 z = normalize_locale(entry->d_name);
181 if (!z)
182 return -ENOMEM;
183
184 r = set_consume(locales, z);
185 if (r < 0 && r != -EEXIST)
186 return r;
187 }
188
189 return 0;
190 }
191
192 int get_locales(char ***ret) {
193 _cleanup_set_free_ Set *locales = NULL;
194 _cleanup_strv_free_ char **l = NULL;
195 int r;
196
197 locales = set_new(&string_hash_ops);
198 if (!locales)
199 return -ENOMEM;
200
201 r = add_locales_from_archive(locales);
202 if (r < 0 && r != -ENOENT)
203 return r;
204
205 r = add_locales_from_libdir(locales);
206 if (r < 0)
207 return r;
208
209 l = set_get_strv(locales);
210 if (!l)
211 return -ENOMEM;
212
213 r = getenv_bool("SYSTEMD_LIST_NON_UTF8_LOCALES");
214 if (r == -ENXIO || r == 0) {
215 char **a, **b;
216
217 /* Filter out non-UTF-8 locales, because it's 2019, by default */
218 for (a = b = l; *a; a++) {
219
220 if (endswith(*a, "UTF-8") ||
221 strstr(*a, ".UTF-8@"))
222 *(b++) = *a;
223 else
224 free(*a);
225 }
226
227 *b = NULL;
228
229 } else if (r < 0)
230 log_debug_errno(r, "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean");
231
232 strv_sort(l);
233
234 *ret = TAKE_PTR(l);
235
236 return 0;
237 }
238
239 bool locale_is_valid(const char *name) {
240
241 if (isempty(name))
242 return false;
243
244 if (strlen(name) >= 128)
245 return false;
246
247 if (!utf8_is_valid(name))
248 return false;
249
250 if (!filename_is_valid(name))
251 return false;
252
253 if (!string_is_safe(name))
254 return false;
255
256 return true;
257 }
258
259 void init_gettext(void) {
260 setlocale(LC_ALL, "");
261 textdomain(GETTEXT_PACKAGE);
262 }
263
264 bool is_locale_utf8(void) {
265 const char *set;
266 static int cached_answer = -1;
267
268 /* Note that we default to 'true' here, since today UTF8 is
269 * pretty much supported everywhere. */
270
271 if (cached_answer >= 0)
272 goto out;
273
274 if (!setlocale(LC_ALL, "")) {
275 cached_answer = true;
276 goto out;
277 }
278
279 set = nl_langinfo(CODESET);
280 if (!set) {
281 cached_answer = true;
282 goto out;
283 }
284
285 if (streq(set, "UTF-8")) {
286 cached_answer = true;
287 goto out;
288 }
289
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);
293 if (!set) {
294 cached_answer = true;
295 goto out;
296 }
297
298 /* Check result, but ignore the result if C was set
299 * explicitly. */
300 cached_answer =
301 STR_IN_SET(set, "C", "POSIX") &&
302 !getenv("LC_ALL") &&
303 !getenv("LC_CTYPE") &&
304 !getenv("LANG");
305
306 out:
307 return (bool) cached_answer;
308 }
309
310 static bool emoji_enabled(void) {
311 static int cached_emoji_enabled = -1;
312
313 if (cached_emoji_enabled < 0) {
314 int val;
315
316 val = getenv_bool("SYSTEMD_EMOJI");
317 if (val < 0)
318 cached_emoji_enabled =
319 is_locale_utf8() &&
320 !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
321 else
322 cached_emoji_enabled = val;
323 }
324
325 return cached_emoji_enabled;
326 }
327
328 const char *special_glyph(SpecialGlyph code) {
329
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:
333 *
334 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
335 */
336
337 static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
338 /* ASCII fallback */
339 [false] = {
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] = ":-[",
360 },
361
362 /* UTF-8 */
363 [true] = {
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", /* 🤢 */
384 },
385 };
386
387 assert(code < _SPECIAL_GLYPH_MAX);
388
389 return draw_table[code >= _SPECIAL_GLYPH_FIRST_SMILEY ? emoji_enabled() : is_locale_utf8()][code];
390 }
391
392 void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
393 LocaleVariable i;
394
395 if (!l)
396 return;
397
398 for (i = 0; i < _VARIABLE_LC_MAX; i++)
399 l[i] = mfree(l[i]);
400 }
401
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"
417 };
418
419 DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);