Move allocation of xkb_desc into CompileKeymap
[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 "xkbmisc.h"
33 #include "parseutils.h"
34 #include "utils.h"
35
36 /* Global debugging flags */
37 unsigned int debugFlags = 0;
38 unsigned int warningLevel = 0;
39
40 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
41
42 static XkbFile *
43 XkbKeymapFileFromComponents(const struct xkb_component_names * ktcsg)
44 {
45     XkbFile *keycodes, *types, *compat, *symbols;
46     IncludeStmt *inc;
47
48     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
49     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
50
51     inc = IncludeCreate(ktcsg->types, MergeDefault);
52     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
53     AppendStmt(&keycodes->common, &types->common);
54
55     inc = IncludeCreate(ktcsg->compat, MergeDefault);
56     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
57     AppendStmt(&keycodes->common, &compat->common);
58
59     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
60     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
61     AppendStmt(&keycodes->common, &symbols->common);
62
63     return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : strdup(""),
64                          &keycodes->common, 0);
65 }
66
67 static struct xkb_component_names *
68 XkbComponentsFromRules(const char *rules, const XkbRF_VarDefsPtr defs)
69 {
70     FILE *rulesFile = NULL;
71     char *rulesPath = NULL;
72     XkbRF_RulesPtr loaded = NULL;
73     struct xkb_component_names * names = NULL;
74
75     rulesFile = XkbFindFileInPath(rules, XkmRulesFile, &rulesPath);
76     if (!rulesFile) {
77         ERROR("could not find \"%s\" rules in XKB path\n", rules);
78         return NULL;
79     }
80
81     if (!(loaded = _XkbTypedCalloc(1, XkbRF_RulesRec))) {
82         ERROR("failed to allocate XKB rules\n");
83         goto unwind_file;
84     }
85
86     if (!XkbcRF_LoadRules(rulesFile, loaded)) {
87         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
88         goto unwind_file;
89     }
90
91     if (!(names = _XkbTypedCalloc(1, struct xkb_component_names))) {
92         ERROR("failed to allocate XKB components\n");
93         goto unwind_file;
94     }
95
96     if (!XkbcRF_GetComponents(loaded, defs, names)) {
97         free(names->keymap);
98         free(names->keycodes);
99         free(names->types);
100         free(names->compat);
101         free(names->symbols);
102         free(names);
103         names = NULL;
104         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
105     }
106
107 unwind_file:
108     XkbcRF_Free(loaded);
109     if (rulesFile)
110         fclose(rulesFile);
111     free(rulesPath);
112     return names;
113 }
114
115 struct xkb_desc *
116 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rmlvo)
117 {
118     XkbRF_VarDefsRec defs;
119     struct xkb_component_names * names;
120     struct xkb_desc * xkb;
121
122     if (!rmlvo || ISEMPTY(rmlvo->rules) || ISEMPTY(rmlvo->layout)) {
123         ERROR("rules and layout required to generate XKB keymap\n");
124         return NULL;
125     }
126
127     defs.model = rmlvo->model;
128     defs.layout = rmlvo->layout;
129     defs.variant = rmlvo->variant;
130     defs.options = rmlvo->options;
131
132     names = XkbComponentsFromRules(rmlvo->rules, &defs);
133     if (!names) {
134         ERROR("failed to generate XKB components from rules \"%s\"\n",
135               rmlvo->rules);
136         return NULL;
137     }
138
139     xkb = xkb_compile_keymap_from_components(names);
140
141     free(names->keymap);
142     free(names->keycodes);
143     free(names->types);
144     free(names->compat);
145     free(names->symbols);
146     free(names);
147
148     return xkb;
149 }
150
151 static XkbFile *
152 XkbChooseMap(XkbFile *file, const char *name)
153 {
154     XkbFile *map = file;
155
156     /* map specified? */
157     if (name) {
158         while (map) {
159             if (map->name && strcmp(map->name, name) == 0)
160                 break;
161             map = (XkbFile *) map->common.next;
162         }
163
164         if (!map)
165             ERROR("no map named \"%s\" in input file\n", name);
166     }
167     else if (file->common.next) {
168         /* look for map with XkbLC_Default flag. */
169         for (; map; map = (XkbFile *) map->common.next) {
170             if (map->flags & XkbLC_Default)
171                 break;
172         }
173
174         if (!map) {
175             map = file;
176             WARN("no map specified, but components have several\n");
177             WARN("using the first defined map, \"%s\"\n",
178                  map->name ? map->name : "");
179         }
180     }
181
182     return map;
183 }
184
185 static struct xkb_desc *
186 compile_keymap(XkbFile *file, const char *mapName)
187 {
188     XkbFile *mapToUse;
189     struct xkb_desc * xkb = NULL;
190
191     /* Find map to use */
192     mapToUse = XkbChooseMap(file, mapName);
193     if (!mapToUse)
194         goto err;
195
196     switch (mapToUse->type) {
197     case XkmSemanticsFile:
198     case XkmLayoutFile:
199     case XkmKeymapFile:
200         break;
201     default:
202         ERROR("file type %d not handled\n", mapToUse->type);
203         goto err;
204     }
205
206     xkb = CompileKeymap(mapToUse, MergeReplace);
207     if (!xkb)
208         goto err;
209
210     return xkb;
211
212 err:
213     FreeXKBFile(file);
214     free(scanFile);
215     XkbFreeIncludePath();
216     return NULL;
217 }
218
219 struct xkb_desc *
220 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg)
221 {
222     XkbFile *file;
223
224     if (!ktcsg) {
225         ERROR("no components specified\n");
226         return NULL;
227     }
228
229     if (ISEMPTY(ktcsg->keycodes)) {
230         ERROR("keycodes required to generate XKB keymap\n");
231         return NULL;
232     }
233
234     if (ISEMPTY(ktcsg->compat)) {
235         ERROR("compat map required to generate XKB keymap\n");
236         return NULL;
237     }
238
239     if (ISEMPTY(ktcsg->types)) {
240         ERROR("types required to generate XKB keymap\n");
241         return NULL;
242     }
243
244     if (ISEMPTY(ktcsg->symbols)) {
245         ERROR("symbols required to generate XKB keymap\n");
246         return NULL;
247     }
248
249     if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
250         ERROR("failed to generate parsed XKB file from components\n");
251         return NULL;
252     }
253
254     return compile_keymap(file, NULL);
255 }
256
257 struct xkb_desc *
258 xkb_compile_keymap_from_string(const char *string, const char *mapName)
259 {
260     XkbFile *file;
261
262     if (!string) {
263         ERROR("no string specified to generate XKB keymap\n");
264         return NULL;
265     }
266
267     setScanState("input", 1);
268     if (!XKBParseString(string, &file) || !file) {
269         ERROR("failed to parse input xkb file\n");
270         return NULL;
271     }
272
273     return compile_keymap(file, mapName);
274 }
275
276 struct xkb_desc *
277 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName)
278 {
279     XkbFile *file;
280
281     if (!inputFile) {
282         ERROR("no file specified to generate XKB keymap\n");
283         return NULL;
284     }
285
286     setScanState("input", 1);
287     if (!XKBParseFile(inputFile, &file) || !file) {
288         ERROR("failed to parse input xkb file\n");
289         return NULL;
290     }
291
292     return compile_keymap(file, mapName);
293 }
294
295 void
296 xkb_free_keymap(struct xkb_desc *xkb)
297 {
298     XkbcFreeKeyboard(xkb);
299     XkbcFreeAllAtoms();
300 }