• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013,2021 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 dummy;
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     dummy.keysym = XKB_KEY_NoSymbol;
62     dummy.leaf.is_leaf = true;
63     dummy.leaf.utf8 = 0;
64     dummy.leaf.keysym = XKB_KEY_NoSymbol;
65     darray_append(table->nodes, dummy);
66 
67     darray_append(table->utf8, '\0');
68 
69     return table;
70 }
71 
72 XKB_EXPORT struct xkb_compose_table *
xkb_compose_table_ref(struct xkb_compose_table * table)73 xkb_compose_table_ref(struct xkb_compose_table *table)
74 {
75     table->refcnt++;
76     return table;
77 }
78 
79 XKB_EXPORT void
xkb_compose_table_unref(struct xkb_compose_table * table)80 xkb_compose_table_unref(struct xkb_compose_table *table)
81 {
82     if (!table || --table->refcnt > 0)
83         return;
84     free(table->locale);
85     darray_free(table->nodes);
86     darray_free(table->utf8);
87     xkb_context_unref(table->ctx);
88     free(table);
89 }
90 
91 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)92 xkb_compose_table_new_from_file(struct xkb_context *ctx,
93                                 FILE *file,
94                                 const char *locale,
95                                 enum xkb_compose_format format,
96                                 enum xkb_compose_compile_flags flags)
97 {
98     struct xkb_compose_table *table;
99     bool ok;
100 
101     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
102         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
103         return NULL;
104     }
105 
106     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
107         log_err_func(ctx, "unsupported compose format: %d\n", format);
108         return NULL;
109     }
110 
111     table = xkb_compose_table_new(ctx, locale, format, flags);
112     if (!table)
113         return NULL;
114 
115     ok = parse_file(table, file, "(unknown file)");
116     if (!ok) {
117         xkb_compose_table_unref(table);
118         return NULL;
119     }
120 
121     return table;
122 }
123 
124 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)125 xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
126                                   const char *buffer, size_t length,
127                                   const char *locale,
128                                   enum xkb_compose_format format,
129                                   enum xkb_compose_compile_flags flags)
130 {
131     struct xkb_compose_table *table;
132     bool ok;
133 
134     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
135         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
136         return NULL;
137     }
138 
139     if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
140         log_err_func(ctx, "unsupported compose format: %d\n", format);
141         return NULL;
142     }
143 
144     table = xkb_compose_table_new(ctx, locale, format, flags);
145     if (!table)
146         return NULL;
147 
148     ok = parse_string(table, buffer, length, "(input string)");
149     if (!ok) {
150         xkb_compose_table_unref(table);
151         return NULL;
152     }
153 
154     return table;
155 }
156 
157 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)158 xkb_compose_table_new_from_locale(struct xkb_context *ctx,
159                                   const char *locale,
160                                   enum xkb_compose_compile_flags flags)
161 {
162     struct xkb_compose_table *table;
163     char *path;
164     FILE *file;
165     bool ok;
166 
167     if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
168         log_err_func(ctx, "unrecognized flags: %#x\n", flags);
169         return NULL;
170     }
171 
172     table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
173                                   flags);
174     if (!table)
175         return NULL;
176 
177     path = get_xcomposefile_path();
178     if (path) {
179         file = fopen(path, "rb");
180         if (file)
181             goto found_path;
182     }
183     free(path);
184 
185     path = get_xdg_xcompose_file_path();
186     if (path) {
187         file = fopen(path, "rb");
188         if (file)
189             goto found_path;
190     }
191     free(path);
192 
193     path = get_home_xcompose_file_path();
194     if (path) {
195         file = fopen(path, "rb");
196         if (file)
197             goto found_path;
198     }
199     free(path);
200 
201     path = get_locale_compose_file_path(table->locale);
202     if (path) {
203         file = fopen(path, "rb");
204         if (file)
205             goto found_path;
206     }
207     free(path);
208 
209     log_err(ctx, "couldn't find a Compose file for locale \"%s\" (mapped to \"%s\")\n",
210             locale, table->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