compose: add xdg base directory support
[platform/upstream/libxkbcommon.git] / src / compose / table.c
1 /*
2  * Copyright © 2013 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "config.h"
25
26 #include "utils.h"
27 #include "table.h"
28 #include "parser.h"
29 #include "paths.h"
30
31 static struct xkb_compose_table *
32 xkb_compose_table_new(struct xkb_context *ctx,
33                       const char *locale,
34                       enum xkb_compose_format format,
35                       enum xkb_compose_compile_flags flags)
36 {
37     char *resolved_locale;
38     struct xkb_compose_table *table;
39     struct compose_node root;
40
41     resolved_locale = resolve_locale(locale);
42     if (!resolved_locale)
43         return NULL;
44
45     table = calloc(1, sizeof(*table));
46     if (!table) {
47         free(resolved_locale);
48         return NULL;
49     }
50
51     table->refcnt = 1;
52     table->ctx = xkb_context_ref(ctx);
53
54     table->locale = resolved_locale;
55     table->format = format;
56     table->flags = flags;
57
58     darray_init(table->nodes);
59     darray_init(table->utf8);
60
61     root.keysym = XKB_KEY_NoSymbol;
62     root.next = 0;
63     root.is_leaf = true;
64     root.u.leaf.utf8 = 0;
65     root.u.leaf.keysym = XKB_KEY_NoSymbol;
66     darray_append(table->nodes, root);
67
68     darray_append(table->utf8, '\0');
69
70     return table;
71 }
72
73 XKB_EXPORT struct xkb_compose_table *
74 xkb_compose_table_ref(struct xkb_compose_table *table)
75 {
76     table->refcnt++;
77     return table;
78 }
79
80 XKB_EXPORT void
81 xkb_compose_table_unref(struct xkb_compose_table *table)
82 {
83     if (!table || --table->refcnt > 0)
84         return;
85     free(table->locale);
86     darray_free(table->nodes);
87     darray_free(table->utf8);
88     xkb_context_unref(table->ctx);
89     free(table);
90 }
91
92 XKB_EXPORT struct xkb_compose_table *
93 xkb_compose_table_new_from_file(struct xkb_context *ctx,
94                                 FILE *file,
95                                 const char *locale,
96                                 enum xkb_compose_format format,
97                                 enum xkb_compose_compile_flags flags)
98 {
99     struct xkb_compose_table *table;
100     bool ok;
101
102     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
103         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
104         return NULL;
105     }
106
107     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
108         log_err_func(ctx, "unsupported compose format: %d\n", format);
109         return NULL;
110     }
111
112     table = xkb_compose_table_new(ctx, locale, format, flags);
113     if (!table)
114         return NULL;
115
116     ok = parse_file(table, file, "(unknown file)");
117     if (!ok) {
118         xkb_compose_table_unref(table);
119         return NULL;
120     }
121
122     return table;
123 }
124
125 XKB_EXPORT struct xkb_compose_table *
126 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
127                                   const char *buffer, size_t length,
128                                   const char *locale,
129                                   enum xkb_compose_format format,
130                                   enum xkb_compose_compile_flags flags)
131 {
132     struct xkb_compose_table *table;
133     bool ok;
134
135     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
136         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
137         return NULL;
138     }
139
140     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
141         log_err_func(ctx, "unsupported compose format: %d\n", format);
142         return NULL;
143     }
144
145     table = xkb_compose_table_new(ctx, locale, format, flags);
146     if (!table)
147         return NULL;
148
149     ok = parse_string(table, buffer, length, "(input string)");
150     if (!ok) {
151         xkb_compose_table_unref(table);
152         return NULL;
153     }
154
155     return table;
156 }
157
158 XKB_EXPORT struct xkb_compose_table *
159 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
160                                   const char *locale,
161                                   enum xkb_compose_compile_flags flags)
162 {
163     struct xkb_compose_table *table;
164     char *path = NULL;
165     const char *cpath;
166     FILE *file;
167     bool ok;
168
169     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
170         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
171         return NULL;
172     }
173
174     table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
175                                   flags);
176     if (!table)
177         return NULL;
178
179     cpath = get_xcomposefile_path();
180     if (cpath) {
181         file = fopen(cpath, "rb");
182         if (file)
183             goto found_path;
184     }
185
186     cpath = path = get_xdg_xcompose_file_path();
187     if (path) {
188         file = fopen(path, "rb");
189         if (file)
190             goto found_path;
191     }
192     free(path);
193     path = NULL;
194
195     cpath = path = get_home_xcompose_file_path();
196     if (path) {
197         file = fopen(path, "rb");
198         if (file)
199             goto found_path;
200     }
201     free(path);
202     path = NULL;
203
204     cpath = path = get_locale_compose_file_path(table->locale);
205     if (path) {
206         file = fopen(path, "rb");
207         if (file)
208             goto found_path;
209     }
210     free(path);
211     path = NULL;
212
213     log_err(ctx, "couldn't find a Compose file for locale \"%s\"\n", locale);
214     xkb_compose_table_unref(table);
215     return NULL;
216
217 found_path:
218     ok = parse_file(table, file, cpath);
219     fclose(file);
220     if (!ok) {
221         xkb_compose_table_unref(table);
222         return NULL;
223     }
224
225     log_dbg(ctx, "created compose table from locale %s with path %s\n",
226             table->locale, path);
227
228     free(path);
229     return table;
230 }