Don't cache loaded rules files
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbcomp.c
1 /*
2 Copyright 2009  Dan Nicholson
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 shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the names of the authors or their
22 institutions shall not be used in advertising or otherwise to promote the
23 sale, use or other dealings in this Software without prior written
24 authorization from the authors.
25 */
26
27 #include <limits.h>
28 #include "xkbcomp.h"
29 #include "xkballoc.h"
30 #include "xkbrules.h"
31 #include "xkbpath.h"
32 #include "parseutils.h"
33 #include "utils.h"
34
35 /* Global debugging flags */
36 unsigned int debugFlags = 0;
37 unsigned int warningLevel = 0;
38
39 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
40
41 static XkbFile *
42 XkbKeymapFileFromComponents(const struct xkb_component_names * ktcsg)
43 {
44     XkbFile *keycodes, *types, *compat, *symbols, *geometry;
45     IncludeStmt *inc;
46
47     if (!ktcsg) {
48         ERROR("no components to generate keymap file from\n");
49         return NULL;
50     }
51
52     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
53     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
54
55     inc = IncludeCreate(ktcsg->types, MergeDefault);
56     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
57     AppendStmt(&keycodes->common, &types->common);
58
59     inc = IncludeCreate(ktcsg->compat, MergeDefault);
60     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
61     AppendStmt(&keycodes->common, &compat->common);
62
63     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
64     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
65     AppendStmt(&keycodes->common, &symbols->common);
66
67     inc = IncludeCreate(ktcsg->geometry, MergeDefault);
68     geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
69     AppendStmt(&keycodes->common, &geometry->common);
70
71     return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : strdup(""),
72                          &keycodes->common, 0);
73 }
74
75 static struct xkb_component_names *
76 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
77 {
78     FILE *rulesFile = NULL;
79     char *rulesPath = NULL;
80     XkbRF_RulesPtr loaded = NULL;
81     struct xkb_component_names * names = NULL;
82
83     rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
84     if (!rulesFile) {
85         ERROR("could not find \"%s\" rules in XKB path\n", rules);
86         return NULL;
87     }
88
89     if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
90         ERROR("failed to allocate XKB rules\n");
91         goto unwind_file;
92     }
93
94     if (!XkbcRF_LoadRules(rulesFile, loaded)) {
95         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
96         goto unwind_file;
97     }
98
99     if (!(names = _XkbTypedCalloc(1, struct xkb_component_names))) {
100         ERROR("failed to allocate XKB components\n");
101         goto unwind_file;
102     }
103
104     if (!XkbcRF_GetComponents(loaded, defs, names)) {
105         free(names->keymap);
106         free(names->keycodes);
107         free(names->types);
108         free(names->compat);
109         free(names->symbols);
110         free(names->geometry);
111         free(names);
112         names = NULL;
113         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
114     }
115
116 unwind_file:
117     XkbcRF_Free(loaded);
118     if (rulesFile)
119         fclose(rulesFile);
120     free(rulesPath);
121     return names;
122 }
123
124 struct xkb_desc *
125 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rmlvo)
126 {
127     XkbRF_VarDefsRec defs;
128     struct xkb_component_names * names;
129     struct xkb_desc * xkb;
130
131     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
132         ERROR("rules and layout required to generate XKB keymap\n");
133         return NULL;
134     }
135
136     defs.model = rmlvo->model;
137     defs.layout = rmlvo->layout;
138     defs.variant = rmlvo->variant;
139     defs.options = rmlvo->options;
140
141     names = XkbComponentsFromRules(rmlvo->rules, &defs);
142     if (!names) {
143         ERROR("failed to generate XKB components from rules \"%s\"\n",
144               rmlvo->rules);
145         return NULL;
146     }
147
148     xkb = xkb_compile_keymap_from_components(names);
149
150     free(names->keymap);
151     free(names->keycodes);
152     free(names->types);
153     free(names->compat);
154     free(names->symbols);
155     free(names->geometry);
156     free(names);
157
158     return xkb;
159 }
160
161 static XkbFile *
162 XkbChooseMap(XkbFile *file, const char *name)
163 {
164     XkbFile *map = file;
165
166     /* map specified? */
167     if (name) {
168         while (map) {
169             if (map->name && strcmp(map->name, name) == 0)
170                 break;
171             map = (XkbFile *) map->common.next;
172         }
173
174         if (!map)
175             ERROR("no map named \"%s\" in input file\n", name);
176     }
177     else if (file->common.next) {
178         /* look for map with XkbLC_Default flag. */
179         for (; map; map = (XkbFile *) map->common.next) {
180             if (map->flags & XkbLC_Default)
181                 break;
182         }
183
184         if (!map) {
185             map = file;
186             WARN("no map specified, but components have several\n");
187             WARN("using the first defined map, \"%s\"\n",
188                  map->name ? map->name : "");
189         }
190     }
191
192     return map;
193 }
194
195 struct xkb_desc *
196 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg)
197 {
198     XkbFile *file, *mapToUse;
199     struct xkb_desc * xkb;
200
201     uSetErrorFile(NULL);
202
203     if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
204         ERROR("keycodes required to generate XKB keymap\n");
205         goto fail;
206     }
207
208     if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
209         ERROR("failed to generate parsed XKB file from components\n");
210         goto fail;
211     }
212
213     /* Find map to use */
214     if (!(mapToUse = XkbChooseMap(file, NULL)))
215         goto unwind_file;
216
217     /* Compile the keyboard */
218     if (!(xkb = XkbcAllocKeyboard())) {
219         ERROR("could not allocate keyboard description\n");
220         goto unwind_file;
221     }
222
223     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
224         ERROR("failed to compile keymap\n");
225         goto unwind_xkb;
226     }
227
228     return xkb;
229 unwind_xkb:
230     XkbcFreeKeyboard(xkb);
231 unwind_file:
232     /* XXX: here's where we would free the XkbFile */
233 fail:
234     return NULL;
235 }
236
237 static struct xkb_desc *
238 compile_keymap(XkbFile *file, const char *mapName)
239 {
240     XkbFile *mapToUse;
241     struct xkb_desc * xkb;
242
243     /* Find map to use */
244     if (!(mapToUse = XkbChooseMap(file, mapName)))
245         goto unwind_file;
246
247     switch (mapToUse->type) {
248     case XkmSemanticsFile:
249     case XkmLayoutFile:
250     case XkmKeymapFile:
251         break;
252     default:
253         ERROR("file type %d not handled\n", mapToUse->type);
254         goto unwind_file;
255     }
256
257     /* Compile the keyboard */
258     if (!(xkb = XkbcAllocKeyboard())) {
259         ERROR("could not allocate keyboard description\n");
260         goto unwind_file;
261     }
262
263     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
264         ERROR("failed to compile keymap\n");
265         goto unwind_xkb;
266     }
267
268     return xkb;
269 unwind_xkb:
270     XkbcFreeKeyboard(xkb);
271 unwind_file:
272     /* XXX: here's where we would free the XkbFile */
273
274     return NULL;
275 }
276
277 struct xkb_desc *
278 xkb_compile_keymap_from_string(const char *string, const char *mapName)
279 {
280     XkbFile *file;
281
282     if (!string) {
283         ERROR("no string specified to generate XKB keymap\n");
284         return NULL;
285     }
286
287     setScanState("input", 1);
288     if (!XKBParseString(string, &file) || !file) {
289         ERROR("failed to parse input xkb file\n");
290         return NULL;
291     }
292
293     return compile_keymap(file, mapName);
294 }
295
296 struct xkb_desc *
297 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
298 {
299     XkbFile *file;
300
301     if (!inputFile) {
302         ERROR("no file specified to generate XKB keymap\n");
303         return NULL;
304     }
305
306     setScanState("input", 1);
307     if (!XKBParseFile(inputFile, &file) || !file) {
308         ERROR("failed to parse input xkb file\n");
309         return NULL;
310     }
311
312     return compile_keymap(file, mapName);
313 }
314
315 void
316 xkb_free_keymap(struct xkb_desc *xkb)
317 {
318     XkbcFreeKeyboard(xkb);
319 }