]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
75683450 | 2 | |
11c3a366 TA |
3 | #include <dirent.h> |
4 | #include <errno.h> | |
5 | #include <fcntl.h> | |
ed457f13 | 6 | #include <ftw.h> |
8752c575 | 7 | #include <langinfo.h> |
11c3a366 | 8 | #include <libintl.h> |
8752c575 | 9 | #include <locale.h> |
11c3a366 TA |
10 | #include <stddef.h> |
11 | #include <stdint.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
75683450 | 14 | #include <sys/mman.h> |
11c3a366 | 15 | #include <sys/stat.h> |
75683450 | 16 | |
ed457f13 | 17 | #include "def.h" |
a0956174 | 18 | #include "dirent-util.h" |
5f1b0cc6 | 19 | #include "env-util.h" |
3ffd4af2 | 20 | #include "fd-util.h" |
93cc7779 | 21 | #include "hashmap.h" |
3ffd4af2 | 22 | #include "locale-util.h" |
bb15fafe | 23 | #include "path-util.h" |
75683450 | 24 | #include "set.h" |
8b43440b | 25 | #include "string-table.h" |
07630cea | 26 | #include "string-util.h" |
75683450 | 27 | #include "strv.h" |
07630cea | 28 | #include "utf8.h" |
75683450 LP |
29 | |
30 | static int add_locales_from_archive(Set *locales) { | |
31 | /* Stolen from glibc... */ | |
32 | ||
33 | struct locarhead { | |
34 | uint32_t magic; | |
35 | /* Serial number. */ | |
36 | uint32_t serial; | |
37 | /* Name hash table. */ | |
38 | uint32_t namehash_offset; | |
39 | uint32_t namehash_used; | |
40 | uint32_t namehash_size; | |
41 | /* String table. */ | |
42 | uint32_t string_offset; | |
43 | uint32_t string_used; | |
44 | uint32_t string_size; | |
45 | /* Table with locale records. */ | |
46 | uint32_t locrectab_offset; | |
47 | uint32_t locrectab_used; | |
48 | uint32_t locrectab_size; | |
49 | /* MD5 sum hash table. */ | |
50 | uint32_t sumhash_offset; | |
51 | uint32_t sumhash_used; | |
52 | uint32_t sumhash_size; | |
53 | }; | |
54 | ||
55 | struct namehashent { | |
56 | /* Hash value of the name. */ | |
57 | uint32_t hashval; | |
58 | /* Offset of the name in the string table. */ | |
59 | uint32_t name_offset; | |
60 | /* Offset of the locale record. */ | |
61 | uint32_t locrec_offset; | |
62 | }; | |
63 | ||
64 | const struct locarhead *h; | |
65 | const struct namehashent *e; | |
66 | const void *p = MAP_FAILED; | |
67 | _cleanup_close_ int fd = -1; | |
68 | size_t sz = 0; | |
69 | struct stat st; | |
da6053d0 | 70 | size_t i; |
75683450 LP |
71 | int r; |
72 | ||
73 | fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC); | |
74 | if (fd < 0) | |
75 | return errno == ENOENT ? 0 : -errno; | |
76 | ||
77 | if (fstat(fd, &st) < 0) | |
78 | return -errno; | |
79 | ||
80 | if (!S_ISREG(st.st_mode)) | |
81 | return -EBADMSG; | |
82 | ||
83 | if (st.st_size < (off_t) sizeof(struct locarhead)) | |
84 | return -EBADMSG; | |
85 | ||
86 | p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); | |
87 | if (p == MAP_FAILED) | |
88 | return -errno; | |
89 | ||
90 | h = (const struct locarhead *) p; | |
91 | if (h->magic != 0xde020109 || | |
92 | h->namehash_offset + h->namehash_size > st.st_size || | |
93 | h->string_offset + h->string_size > st.st_size || | |
94 | h->locrectab_offset + h->locrectab_size > st.st_size || | |
95 | h->sumhash_offset + h->sumhash_size > st.st_size) { | |
96 | r = -EBADMSG; | |
97 | goto finish; | |
98 | } | |
99 | ||
100 | e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset); | |
101 | for (i = 0; i < h->namehash_size; i++) { | |
102 | char *z; | |
103 | ||
104 | if (e[i].locrec_offset == 0) | |
105 | continue; | |
106 | ||
107 | if (!utf8_is_valid((char*) p + e[i].name_offset)) | |
108 | continue; | |
109 | ||
110 | z = strdup((char*) p + e[i].name_offset); | |
111 | if (!z) { | |
112 | r = -ENOMEM; | |
113 | goto finish; | |
114 | } | |
115 | ||
116 | r = set_consume(locales, z); | |
117 | if (r < 0) | |
118 | goto finish; | |
119 | } | |
120 | ||
121 | r = 0; | |
122 | ||
123 | finish: | |
124 | if (p != MAP_FAILED) | |
125 | munmap((void*) p, sz); | |
126 | ||
127 | return r; | |
128 | } | |
129 | ||
130 | static int add_locales_from_libdir (Set *locales) { | |
131 | _cleanup_closedir_ DIR *dir = NULL; | |
132 | struct dirent *entry; | |
133 | int r; | |
134 | ||
135 | dir = opendir("/usr/lib/locale"); | |
136 | if (!dir) | |
137 | return errno == ENOENT ? 0 : -errno; | |
138 | ||
139 | FOREACH_DIRENT(entry, dir, return -errno) { | |
140 | char *z; | |
141 | ||
331fb4ca EV |
142 | dirent_ensure_type(dir, entry); |
143 | ||
75683450 LP |
144 | if (entry->d_type != DT_DIR) |
145 | continue; | |
146 | ||
147 | z = strdup(entry->d_name); | |
148 | if (!z) | |
149 | return -ENOMEM; | |
150 | ||
151 | r = set_consume(locales, z); | |
152 | if (r < 0 && r != -EEXIST) | |
153 | return r; | |
154 | } | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | int get_locales(char ***ret) { | |
160 | _cleanup_set_free_ Set *locales = NULL; | |
161 | _cleanup_strv_free_ char **l = NULL; | |
162 | int r; | |
163 | ||
d5099efc | 164 | locales = set_new(&string_hash_ops); |
75683450 LP |
165 | if (!locales) |
166 | return -ENOMEM; | |
167 | ||
168 | r = add_locales_from_archive(locales); | |
169 | if (r < 0 && r != -ENOENT) | |
170 | return r; | |
171 | ||
172 | r = add_locales_from_libdir(locales); | |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
176 | l = set_get_strv(locales); | |
177 | if (!l) | |
178 | return -ENOMEM; | |
179 | ||
180 | strv_sort(l); | |
181 | ||
1cc6c93a | 182 | *ret = TAKE_PTR(l); |
75683450 LP |
183 | |
184 | return 0; | |
185 | } | |
186 | ||
187 | bool locale_is_valid(const char *name) { | |
188 | ||
189 | if (isempty(name)) | |
190 | return false; | |
191 | ||
192 | if (strlen(name) >= 128) | |
193 | return false; | |
194 | ||
195 | if (!utf8_is_valid(name)) | |
196 | return false; | |
197 | ||
ae6c3cc0 | 198 | if (!filename_is_valid(name)) |
75683450 LP |
199 | return false; |
200 | ||
201 | if (!string_is_safe(name)) | |
202 | return false; | |
203 | ||
204 | return true; | |
205 | } | |
a3428668 | 206 | |
8752c575 LP |
207 | void init_gettext(void) { |
208 | setlocale(LC_ALL, ""); | |
209 | textdomain(GETTEXT_PACKAGE); | |
210 | } | |
211 | ||
212 | bool is_locale_utf8(void) { | |
213 | const char *set; | |
214 | static int cached_answer = -1; | |
215 | ||
216 | /* Note that we default to 'true' here, since today UTF8 is | |
217 | * pretty much supported everywhere. */ | |
218 | ||
219 | if (cached_answer >= 0) | |
220 | goto out; | |
221 | ||
222 | if (!setlocale(LC_ALL, "")) { | |
223 | cached_answer = true; | |
224 | goto out; | |
225 | } | |
226 | ||
227 | set = nl_langinfo(CODESET); | |
228 | if (!set) { | |
229 | cached_answer = true; | |
230 | goto out; | |
231 | } | |
232 | ||
233 | if (streq(set, "UTF-8")) { | |
234 | cached_answer = true; | |
235 | goto out; | |
236 | } | |
237 | ||
238 | /* For LC_CTYPE=="C" return true, because CTYPE is effectly | |
239 | * unset and everything can do to UTF-8 nowadays. */ | |
240 | set = setlocale(LC_CTYPE, NULL); | |
241 | if (!set) { | |
242 | cached_answer = true; | |
243 | goto out; | |
244 | } | |
245 | ||
246 | /* Check result, but ignore the result if C was set | |
247 | * explicitly. */ | |
248 | cached_answer = | |
249 | STR_IN_SET(set, "C", "POSIX") && | |
250 | !getenv("LC_ALL") && | |
251 | !getenv("LC_CTYPE") && | |
252 | !getenv("LANG"); | |
253 | ||
254 | out: | |
255 | return (bool) cached_answer; | |
256 | } | |
257 | ||
ed457f13 TB |
258 | static thread_local Set *keymaps = NULL; |
259 | ||
260 | static int nftw_cb( | |
261 | const char *fpath, | |
262 | const struct stat *sb, | |
263 | int tflag, | |
264 | struct FTW *ftwbuf) { | |
265 | ||
266 | char *p, *e; | |
267 | int r; | |
268 | ||
269 | if (tflag != FTW_F) | |
270 | return 0; | |
271 | ||
272 | if (!endswith(fpath, ".map") && | |
273 | !endswith(fpath, ".map.gz")) | |
274 | return 0; | |
275 | ||
276 | p = strdup(basename(fpath)); | |
277 | if (!p) | |
278 | return FTW_STOP; | |
279 | ||
280 | e = endswith(p, ".map"); | |
281 | if (e) | |
282 | *e = 0; | |
283 | ||
284 | e = endswith(p, ".map.gz"); | |
285 | if (e) | |
286 | *e = 0; | |
287 | ||
288 | r = set_consume(keymaps, p); | |
289 | if (r < 0 && r != -EEXIST) | |
290 | return r; | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
295 | int get_keymaps(char ***ret) { | |
296 | _cleanup_strv_free_ char **l = NULL; | |
297 | const char *dir; | |
298 | int r; | |
299 | ||
300 | keymaps = set_new(&string_hash_ops); | |
301 | if (!keymaps) | |
302 | return -ENOMEM; | |
303 | ||
304 | NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) { | |
16dc38dd | 305 | r = nftw(dir, nftw_cb, 20, FTW_PHYS|FTW_ACTIONRETVAL); |
ed457f13 TB |
306 | |
307 | if (r == FTW_STOP) | |
308 | log_debug("Directory not found %s", dir); | |
309 | else if (r < 0) | |
310 | log_debug_errno(r, "Can't add keymap: %m"); | |
311 | } | |
312 | ||
313 | l = set_get_strv(keymaps); | |
314 | if (!l) { | |
315 | set_free_free(keymaps); | |
316 | return -ENOMEM; | |
317 | } | |
318 | ||
319 | set_free(keymaps); | |
320 | ||
321 | if (strv_isempty(l)) | |
322 | return -ENOENT; | |
323 | ||
324 | strv_sort(l); | |
325 | ||
ae2a15bc | 326 | *ret = TAKE_PTR(l); |
ed457f13 TB |
327 | |
328 | return 0; | |
329 | } | |
330 | ||
331 | bool keymap_is_valid(const char *name) { | |
332 | ||
333 | if (isempty(name)) | |
334 | return false; | |
335 | ||
336 | if (strlen(name) >= 128) | |
337 | return false; | |
338 | ||
339 | if (!utf8_is_valid(name)) | |
340 | return false; | |
341 | ||
342 | if (!filename_is_valid(name)) | |
343 | return false; | |
344 | ||
345 | if (!string_is_safe(name)) | |
346 | return false; | |
347 | ||
348 | return true; | |
349 | } | |
8752c575 | 350 | |
5f1b0cc6 LP |
351 | static bool emoji_enabled(void) { |
352 | static int cached_emoji_enabled = -1; | |
353 | ||
354 | if (cached_emoji_enabled < 0) { | |
355 | int val; | |
356 | ||
357 | val = getenv_bool("SYSTEMD_EMOJI"); | |
358 | if (val < 0) | |
359 | cached_emoji_enabled = | |
360 | is_locale_utf8() && | |
361 | !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux"); | |
362 | else | |
363 | cached_emoji_enabled = val; | |
364 | } | |
365 | ||
366 | return cached_emoji_enabled; | |
367 | } | |
368 | ||
323b7dc9 ZJS |
369 | const char *special_glyph(SpecialGlyph code) { |
370 | ||
5d01f5dc LP |
371 | /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be |
372 | * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still | |
373 | * works reasonably well on the Linux console. For details see: | |
374 | * | |
375 | * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr | |
376 | */ | |
377 | ||
dff4bf93 | 378 | static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = { |
323b7dc9 ZJS |
379 | /* ASCII fallback */ |
380 | [false] = { | |
5f1b0cc6 LP |
381 | [TREE_VERTICAL] = "| ", |
382 | [TREE_BRANCH] = "|-", | |
383 | [TREE_RIGHT] = "`-", | |
384 | [TREE_SPACE] = " ", | |
385 | [TRIANGULAR_BULLET] = ">", | |
386 | [BLACK_CIRCLE] = "*", | |
387 | [BULLET] = "*", | |
388 | [ARROW] = "->", | |
389 | [MDASH] = "-", | |
390 | [ELLIPSIS] = "...", | |
391 | [MU] = "u", | |
392 | [CHECK_MARK] = "+", | |
393 | [CROSS_MARK] = "-", | |
394 | [ECSTATIC_SMILEY] = ":-]", | |
395 | [HAPPY_SMILEY] = ":-}", | |
396 | [SLIGHTLY_HAPPY_SMILEY] = ":-)", | |
397 | [NEUTRAL_SMILEY] = ":-|", | |
398 | [SLIGHTLY_UNHAPPY_SMILEY] = ":-(", | |
399 | [UNHAPPY_SMILEY] = ":-{️", | |
400 | [DEPRESSED_SMILEY] = ":-[", | |
8752c575 LP |
401 | }, |
402 | ||
323b7dc9 | 403 | /* UTF-8 */ |
b77f5e27 | 404 | [true] = { |
5f1b0cc6 LP |
405 | [TREE_VERTICAL] = "\342\224\202 ", /* │ */ |
406 | [TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */ | |
407 | [TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */ | |
408 | [TREE_SPACE] = " ", /* */ | |
409 | [TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */ | |
410 | [BLACK_CIRCLE] = "\342\227\217", /* ● */ | |
411 | [BULLET] = "\342\200\242", /* • */ | |
412 | [ARROW] = "\342\206\222", /* → */ | |
413 | [MDASH] = "\342\200\223", /* – */ | |
414 | [ELLIPSIS] = "\342\200\246", /* … */ | |
415 | [MU] = "\316\274", /* μ */ | |
416 | [CHECK_MARK] = "\342\234\223", /* ✓ */ | |
417 | [CROSS_MARK] = "\342\234\227", /* ✗ */ | |
418 | [ECSTATIC_SMILEY] = "\360\237\230\207", /* 😇 */ | |
419 | [HAPPY_SMILEY] = "\360\237\230\200", /* 😀 */ | |
420 | [SLIGHTLY_HAPPY_SMILEY] = "\360\237\231\202", /* 🙂 */ | |
421 | [NEUTRAL_SMILEY] = "\360\237\230\220", /* 😐 */ | |
422 | [SLIGHTLY_UNHAPPY_SMILEY] = "\360\237\231\201", /* 🙁 */ | |
423 | [UNHAPPY_SMILEY] = "\360\237\230\250", /* 😨️️ */ | |
424 | [DEPRESSED_SMILEY] = "\360\237\244\242", /* 🤢 */ | |
323b7dc9 | 425 | }, |
8752c575 LP |
426 | }; |
427 | ||
5f1b0cc6 LP |
428 | assert(code < _SPECIAL_GLYPH_MAX); |
429 | ||
430 | return draw_table[code >= _SPECIAL_GLYPH_FIRST_SMILEY ? emoji_enabled() : is_locale_utf8()][code]; | |
8752c575 LP |
431 | } |
432 | ||
e6755a33 LP |
433 | void locale_variables_free(char*l[_VARIABLE_LC_MAX]) { |
434 | LocaleVariable i; | |
435 | ||
436 | if (!l) | |
437 | return; | |
438 | ||
439 | for (i = 0; i < _VARIABLE_LC_MAX; i++) | |
440 | l[i] = mfree(l[i]); | |
441 | } | |
442 | ||
a3428668 MS |
443 | static const char * const locale_variable_table[_VARIABLE_LC_MAX] = { |
444 | [VARIABLE_LANG] = "LANG", | |
445 | [VARIABLE_LANGUAGE] = "LANGUAGE", | |
446 | [VARIABLE_LC_CTYPE] = "LC_CTYPE", | |
447 | [VARIABLE_LC_NUMERIC] = "LC_NUMERIC", | |
448 | [VARIABLE_LC_TIME] = "LC_TIME", | |
449 | [VARIABLE_LC_COLLATE] = "LC_COLLATE", | |
450 | [VARIABLE_LC_MONETARY] = "LC_MONETARY", | |
451 | [VARIABLE_LC_MESSAGES] = "LC_MESSAGES", | |
452 | [VARIABLE_LC_PAPER] = "LC_PAPER", | |
453 | [VARIABLE_LC_NAME] = "LC_NAME", | |
454 | [VARIABLE_LC_ADDRESS] = "LC_ADDRESS", | |
455 | [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE", | |
456 | [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT", | |
457 | [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION" | |
458 | }; | |
459 | ||
460 | DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable); |