]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/locale-util.c
codespell: fix spelling errors
[thirdparty/systemd.git] / src / basic / locale-util.c
CommitLineData
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
30static 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
130static 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
159int 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
187bool 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
207void init_gettext(void) {
208 setlocale(LC_ALL, "");
209 textdomain(GETTEXT_PACKAGE);
210}
211
212bool 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
5238e957 238 /* For LC_CTYPE=="C" return true, because CTYPE is effectively
8752c575
LP
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
254out:
255 return (bool) cached_answer;
256}
257
5f1b0cc6
LP
258static bool emoji_enabled(void) {
259 static int cached_emoji_enabled = -1;
260
261 if (cached_emoji_enabled < 0) {
262 int val;
263
264 val = getenv_bool("SYSTEMD_EMOJI");
265 if (val < 0)
266 cached_emoji_enabled =
267 is_locale_utf8() &&
268 !STRPTR_IN_SET(getenv("TERM"), "dumb", "linux");
269 else
270 cached_emoji_enabled = val;
271 }
272
273 return cached_emoji_enabled;
274}
275
323b7dc9
ZJS
276const char *special_glyph(SpecialGlyph code) {
277
5d01f5dc
LP
278 /* A list of a number of interesting unicode glyphs we can use to decorate our output. It's probably wise to be
279 * conservative here, and primarily stick to the glyphs defined in the eurlatgr font, so that display still
280 * works reasonably well on the Linux console. For details see:
281 *
282 * http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=blob;f=data/consolefonts/README.eurlatgr
283 */
284
dff4bf93 285 static const char* const draw_table[2][_SPECIAL_GLYPH_MAX] = {
323b7dc9
ZJS
286 /* ASCII fallback */
287 [false] = {
9a6f746f
LP
288 [SPECIAL_GLYPH_TREE_VERTICAL] = "| ",
289 [SPECIAL_GLYPH_TREE_BRANCH] = "|-",
290 [SPECIAL_GLYPH_TREE_RIGHT] = "`-",
291 [SPECIAL_GLYPH_TREE_SPACE] = " ",
292 [SPECIAL_GLYPH_TRIANGULAR_BULLET] = ">",
293 [SPECIAL_GLYPH_BLACK_CIRCLE] = "*",
294 [SPECIAL_GLYPH_BULLET] = "*",
295 [SPECIAL_GLYPH_ARROW] = "->",
296 [SPECIAL_GLYPH_MDASH] = "-",
297 [SPECIAL_GLYPH_ELLIPSIS] = "...",
298 [SPECIAL_GLYPH_MU] = "u",
299 [SPECIAL_GLYPH_CHECK_MARK] = "+",
300 [SPECIAL_GLYPH_CROSS_MARK] = "-",
301 [SPECIAL_GLYPH_ECSTATIC_SMILEY] = ":-]",
302 [SPECIAL_GLYPH_HAPPY_SMILEY] = ":-}",
303 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY] = ":-)",
304 [SPECIAL_GLYPH_NEUTRAL_SMILEY] = ":-|",
305 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = ":-(",
306 [SPECIAL_GLYPH_UNHAPPY_SMILEY] = ":-{️",
307 [SPECIAL_GLYPH_DEPRESSED_SMILEY] = ":-[",
8752c575
LP
308 },
309
323b7dc9 310 /* UTF-8 */
b77f5e27 311 [true] = {
9a6f746f
LP
312 [SPECIAL_GLYPH_TREE_VERTICAL] = "\342\224\202 ", /* │ */
313 [SPECIAL_GLYPH_TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
314 [SPECIAL_GLYPH_TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
315 [SPECIAL_GLYPH_TREE_SPACE] = " ", /* */
316 [SPECIAL_GLYPH_TRIANGULAR_BULLET] = "\342\200\243", /* ‣ */
317 [SPECIAL_GLYPH_BLACK_CIRCLE] = "\342\227\217", /* ● */
318 [SPECIAL_GLYPH_BULLET] = "\342\200\242", /* • */
319 [SPECIAL_GLYPH_ARROW] = "\342\206\222", /* → */
320 [SPECIAL_GLYPH_MDASH] = "\342\200\223", /* – */
321 [SPECIAL_GLYPH_ELLIPSIS] = "\342\200\246", /* … */
322 [SPECIAL_GLYPH_MU] = "\316\274", /* μ */
323 [SPECIAL_GLYPH_CHECK_MARK] = "\342\234\223", /* ✓ */
324 [SPECIAL_GLYPH_CROSS_MARK] = "\342\234\227", /* ✗ */
325 [SPECIAL_GLYPH_ECSTATIC_SMILEY] = "\360\237\230\207", /* 😇 */
326 [SPECIAL_GLYPH_HAPPY_SMILEY] = "\360\237\230\200", /* 😀 */
327 [SPECIAL_GLYPH_SLIGHTLY_HAPPY_SMILEY] = "\360\237\231\202", /* 🙂 */
328 [SPECIAL_GLYPH_NEUTRAL_SMILEY] = "\360\237\230\220", /* 😐 */
329 [SPECIAL_GLYPH_SLIGHTLY_UNHAPPY_SMILEY] = "\360\237\231\201", /* 🙁 */
330 [SPECIAL_GLYPH_UNHAPPY_SMILEY] = "\360\237\230\250", /* 😨️️ */
331 [SPECIAL_GLYPH_DEPRESSED_SMILEY] = "\360\237\244\242", /* 🤢 */
323b7dc9 332 },
8752c575
LP
333 };
334
5f1b0cc6
LP
335 assert(code < _SPECIAL_GLYPH_MAX);
336
337 return draw_table[code >= _SPECIAL_GLYPH_FIRST_SMILEY ? emoji_enabled() : is_locale_utf8()][code];
8752c575
LP
338}
339
f2a3de01 340void locale_variables_free(char *l[_VARIABLE_LC_MAX]) {
e6755a33
LP
341 LocaleVariable i;
342
343 if (!l)
344 return;
345
346 for (i = 0; i < _VARIABLE_LC_MAX; i++)
347 l[i] = mfree(l[i]);
348}
349
a3428668
MS
350static const char * const locale_variable_table[_VARIABLE_LC_MAX] = {
351 [VARIABLE_LANG] = "LANG",
352 [VARIABLE_LANGUAGE] = "LANGUAGE",
353 [VARIABLE_LC_CTYPE] = "LC_CTYPE",
354 [VARIABLE_LC_NUMERIC] = "LC_NUMERIC",
355 [VARIABLE_LC_TIME] = "LC_TIME",
356 [VARIABLE_LC_COLLATE] = "LC_COLLATE",
357 [VARIABLE_LC_MONETARY] = "LC_MONETARY",
358 [VARIABLE_LC_MESSAGES] = "LC_MESSAGES",
359 [VARIABLE_LC_PAPER] = "LC_PAPER",
360 [VARIABLE_LC_NAME] = "LC_NAME",
361 [VARIABLE_LC_ADDRESS] = "LC_ADDRESS",
362 [VARIABLE_LC_TELEPHONE] = "LC_TELEPHONE",
363 [VARIABLE_LC_MEASUREMENT] = "LC_MEASUREMENT",
364 [VARIABLE_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
365};
366
367DEFINE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable);