2 * Copyright © 2014 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
27 enum resolve_name_direction {
33 get_xlocaledir_path(void)
35 const char *dir = secure_getenv("XLOCALEDIR");
42 * Files like compose.dir have the format LEFT: RIGHT. Lookup @name in
43 * such a file and return its matching value, according to @direction.
44 * @filename is relative to the xlocaledir.
47 resolve_name(const char *filename, enum resolve_name_direction direction,
52 const char *xlocaledir;
58 const char *s, *left, *right;
60 size_t left_len, right_len, name_len;
62 xlocaledir = get_xlocaledir_path();
64 ret = snprintf(path, sizeof(path), "%s/%s", xlocaledir, filename);
65 if (ret < 0 || (size_t) ret >= sizeof(path))
68 file = fopen(path, "r");
72 ok = map_file(file, &string, &string_size);
78 end = string + string_size;
79 name_len = strlen(name);
84 while (s < end && is_space(*s))
88 if (s < end && *s == '#') {
89 while (s < end && *s != '\n')
94 /* Get the left value. */
96 while (s < end && !is_space(*s) && *s != ':')
100 /* There's an optional colon between left and right. */
101 if (s < end && *s == ':')
105 while (s < end && is_space(*s))
108 /* Get the right value. */
110 while (s < end && !is_space(*s))
112 right_len = s - right;
114 /* Discard rest of line. */
115 while (s < end && *s != '\n')
118 if (direction == LEFT_TO_RIGHT) {
119 if (left_len == name_len && memcmp(left, name, left_len) == 0) {
120 match = strndup(right, right_len);
124 else if (direction == RIGHT_TO_LEFT) {
125 if (right_len == name_len && memcmp(right, name, right_len) == 0) {
126 match = strndup(left, left_len);
132 unmap_file(string, string_size);
137 resolve_locale(const char *locale)
139 char *alias = resolve_name("locale.alias", LEFT_TO_RIGHT, locale);
140 return alias ? alias : strdup(locale);
144 get_xcomposefile_path(void)
146 return secure_getenv("XCOMPOSEFILE");
150 get_home_xcompose_file_path(void)
156 home = secure_getenv("HOME");
160 ret = asprintf(&path, "%s/.XCompose", home);
168 get_locale_compose_file_path(const char *locale)
174 * WARNING: Random workaround ahead.
176 * We currently do not support non-UTF-8 Compose files. The C/POSIX
177 * locale is specified to be the default fallback locale with an
178 * ASCII charset. But for some reason the compose.dir points the C
179 * locale to the iso8859-1/Compose file, which is not ASCII but
180 * ISO8859-1. Since this is bound to happen a lot, and since our API
181 * is UTF-8 based, and since 99% of the time a C locale is really just
182 * a misconfiguration for UTF-8, let's do the most helpful thing.
184 if (streq(locale, "C"))
185 locale = "en_US.UTF-8";
187 resolved = resolve_name("compose.dir", RIGHT_TO_LEFT, locale);
191 if (resolved[0] == '/') {
195 const char *xlocaledir = get_xlocaledir_path();
196 int ret = asprintf(&path, "%s/%s", xlocaledir, resolved);