Debian Codegraph @

src/compose/paths.c

raw
1 /*
2 * Copyright © 2014 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <locale.h>
9
10 #include "xkbcommon/xkbcommon.h"
11 #include "messages-codes.h"
12 #include "utils.h"
13 #include "utils-paths.h"
14 #include "context.h"
15 #include "paths.h"
16
17 enum resolve_name_direction {
18 LEFT_TO_RIGHT,
19 RIGHT_TO_LEFT,
20 };
21
22 const char *
23 get_xlocaledir_path(struct xkb_context *ctx)
24 {
25 const char *dir = xkb_context_getenv(ctx, "XLOCALEDIR");
26 if (!dir)
27 dir = XLOCALEDIR;
28 return dir;
29 }
30
31 /*
32 * Files like compose.dir have the format LEFT: RIGHT. Lookup @name in
33 * such a file and return its matching value, according to @direction.
34 * @filename is relative to the xlocaledir.
35 */
36 static char *
37 resolve_name(struct xkb_context *ctx, const char *filename,
38 enum resolve_name_direction direction, const char *name)
39 {
40 int ret;
41 bool ok;
42 const char *xlocaledir;
43 char path[512];
44 FILE *file;
45 char *string;
46 size_t string_size;
47 const char *end;
48 const char *s, *left, *right;
49 char *match;
50 size_t left_len, right_len, name_len;
51
52 xlocaledir = get_xlocaledir_path(ctx);
53
54 ret = snprintf(path, sizeof(path), "%s/%s", xlocaledir, filename);
55 if (ret < 0 || (size_t) ret >= sizeof(path))
56 return NULL;
57
58 file = fopen(path, "rb");
59 if (!file)
60 return NULL;
61
62 ok = map_file(file, &string, &string_size);
63 fclose(file);
64 if (!ok)
65 return NULL;
66
67 s = string;
68 end = string + string_size;
69 name_len = strlen(name);
70 match = NULL;
71
72 while (s < end) {
73 /* Skip spaces. */
74 while (s < end && is_space(*s))
75 s++;
76
77 /* Skip comments. */
78 if (s < end && *s == '#') {
79 while (s < end && *s != '\n')
80 s++;
81 continue;
82 }
83
84 /* Get the left value. */
85 left = s;
86 while (s < end && !is_space(*s) && *s != ':')
87 s++;
88 left_len = s - left;
89
90 /* There's an optional colon between left and right. */
91 if (s < end && *s == ':')
92 s++;
93
94 /* Skip spaces. */
95 while (s < end && is_space(*s))
96 s++;
97
98 /* Get the right value. */
99 right = s;
100 while (s < end && !is_space(*s))
101 s++;
102 right_len = s - right;
103
104 /* Discard rest of line. */
105 while (s < end && *s != '\n')
106 s++;
107
108 if (direction == LEFT_TO_RIGHT) {
109 if (left_len == name_len && memcmp(left, name, left_len) == 0) {
110 match = strndup(right, right_len);
111 break;
112 }
113 }
114 else if (direction == RIGHT_TO_LEFT) {
115 if (right_len == name_len && memcmp(right, name, right_len) == 0) {
116 match = strndup(left, left_len);
117 break;
118 }
119 }
120 }
121
122 unmap_file(string, string_size);
123 return match;
124 }
125
126 char *
127 resolve_locale(struct xkb_context *ctx, const char *locale)
128 {
129 char *alias = resolve_name(ctx, "locale.alias", LEFT_TO_RIGHT, locale);
130 return alias ? alias : strdup(locale);
131 }
132
133 char *
134 get_xcomposefile_path(struct xkb_context *ctx)
135 {
136 return strdup_safe(xkb_context_getenv(ctx, "XCOMPOSEFILE"));
137 }
138
139 char *
140 get_xdg_xcompose_file_path(struct xkb_context *ctx)
141 {
142 const char *xdg_config_home;
143 const char *home;
144
145 xdg_config_home = xkb_context_getenv(ctx, "XDG_CONFIG_HOME");
146 if (!xdg_config_home || !is_absolute_path(xdg_config_home)) {
147 home = xkb_context_getenv(ctx, "HOME");
148 if (!home)
149 return NULL;
150 return asprintf_safe("%s/.config/XCompose", home);
151 }
152
153 return asprintf_safe("%s/XCompose", xdg_config_home);
154 }
155
156 char *
157 get_home_xcompose_file_path(struct xkb_context *ctx)
158 {
159 const char *home;
160
161 home = xkb_context_getenv(ctx, "HOME");
162 if (!home)
163 return NULL;
164
165 return asprintf_safe("%s/.XCompose", home);
166 }
167
168 char *
169 get_locale_compose_file_path(struct xkb_context *ctx, const char *locale)
170 {
171 char *resolved;
172 char *path;
173
174 /*
175 * WARNING: Random workaround ahead.
176 *
177 * We currently do not support non-UTF-8 Compose files. The C/POSIX
178 * locale is specified to be the default fallback locale with an
179 * ASCII charset. But for some reason the compose.dir points the C
180 * locale to the iso8859-1/Compose file, which is not ASCII but
181 * ISO8859-1. Since this is bound to happen a lot, and since our API
182 * is UTF-8 based, and since 99% of the time a C locale is really just
183 * a misconfiguration for UTF-8, let's do the most helpful thing.
184 */
185 static const char * const fallback = "en_US.UTF-8";
186 static const char * const registry = "compose.dir";
187 if (streq(locale, "C"))
188 locale = fallback;
189
190 resolved = resolve_name(ctx, registry, RIGHT_TO_LEFT, locale);
191 if (!resolved) {
192 #ifdef HAVE_NEWLOCALE
193 /*
194 * There is no extension mechanism for X11 Compose locales.
195 * Instead of failing, we just use `en_US.UTF-8` as a fallback because
196 * it is what most locales use anyway.
197 * But we still want to fail if the locale is invalid in the system,
198 * so that we can catch missing system locales and typos.
199 */
200 locale_t loc = newlocale(LC_ALL_MASK, locale, (locale_t) 0);
201 if (loc == (locale_t) 0) {
202 log_err(ctx, XKB_ERROR_INVALID_COMPOSE_LOCALE,
203 "No Compose file for locale \"%s\": "
204 "locale is either invalid or not installed\n", locale);
205 return NULL;
206 } else {
207 /*
208 * The locale is legit but have no entry in the X11 Compose registry,
209 * so use the `en_US.UTF-8` fallback.
210 */
211 freelocale(loc);
212 resolved = resolve_name(ctx, registry, RIGHT_TO_LEFT, fallback);
213 if (!resolved) {
214 log_err(ctx, XKB_ERROR_INVALID_COMPOSE_LOCALE,
215 "No Compose file for locale \"%s\": "
216 "failed to use fallback \"%s\"\n", locale, fallback);
217 return NULL;
218 } else {
219 log_warn(ctx, XKB_ERROR_INVALID_COMPOSE_LOCALE,
220 "No Compose file for locale \"%s\": "
221 "using locale fallback \"%s\"\n", locale, fallback);
222 }
223 }
224 #else
225 return NULL;
226 #endif
227 }
228
229 if (is_absolute_path(resolved)) {
230 path = resolved;
231 }
232 else {
233 const char *xlocaledir = get_xlocaledir_path(ctx);
234 path = asprintf_safe("%s/%s", xlocaledir, resolved);
235 free(resolved);
236 }
237
238 return path;
239 }