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 *
xkb_compose_table_new(struct xkb_context * ctx,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)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 *
xkb_compose_table_ref(struct xkb_compose_table * table)74 xkb_compose_table_ref(struct xkb_compose_table *table)
75 {
76     table->refcnt++;
77     return table;
78 }
79 
80 XKB_EXPORT void
xkb_compose_table_unref(struct xkb_compose_table * table)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 *
xkb_compose_table_new_from_file(struct xkb_context * ctx,FILE * file,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)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 *
xkb_compose_table_new_from_buffer(struct xkb_context * ctx,const char * buffer,size_t length,const char * locale,enum xkb_compose_format format,enum xkb_compose_compile_flags flags)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 *
xkb_compose_table_new_from_locale(struct xkb_context * ctx,const char * locale,enum xkb_compose_compile_flags flags)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;
165     FILE *file;
166     bool ok;
167 
168     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
169         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
170         return NULL;
171     }
172 
173     table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
174                                   flags);
175     if (!table)
176         return NULL;
177 
178     path = get_xcomposefile_path();
179     if (path) {
180         file = fopen(path, "rb");
181         if (file)
182             goto found_path;
183     }
184     free(path);
185 
186     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 
194     path = get_home_xcompose_file_path();
195     if (path) {
196         file = fopen(path, "rb");
197         if (file)
198             goto found_path;
199     }
200     free(path);
201 
202     path = get_locale_compose_file_path(table->locale);
203     if (path) {
204         file = fopen(path, "rb");
205         if (file)
206             goto found_path;
207     }
208     free(path);
209 
210     log_err(ctx, "couldn't find a Compose file for locale \"%s\"\n", locale);
211     xkb_compose_table_unref(table);
212     return NULL;
213 
214 found_path:
215     ok = parse_file(table, file, path);
216     fclose(file);
217     if (!ok) {
218         free(path);
219         xkb_compose_table_unref(table);
220         return NULL;
221     }
222 
223     log_dbg(ctx, "created compose table from locale %s with path %s\n",
224             table->locale, path);
225 
226     free(path);
227     return table;
228 }
229