Require strdup and remove utils wrapper
[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 "X11/extensions/XKBcommon.h"
29 #include <X11/extensions/XKM.h>
30 #include "xkbcomp.h"
31 #include "parseutils.h"
32 #include "utils.h"
33
34 #ifndef DFLT_XKB_CONFIG_ROOT
35 #define DFLT_XKB_CONFIG_ROOT "/usr/share/X11/xkb"
36 #endif
37
38 /* Global debugging flags */
39 unsigned int debugFlags = 0;
40 unsigned int warningLevel = 0;
41
42 #define ISEMPTY(str) (!(str) || (strlen(str) == 0))
43
44 static XkbFile *
45 XkbKeymapFileFromComponents(const XkbComponentNamesPtr ktcsg)
46 {
47     XkbFile *keycodes, *types, *compat, *symbols, *geometry;
48     IncludeStmt *inc;
49
50     if (!ktcsg) {
51         ERROR("no components to generate keymap file from\n");
52         return NULL;
53     }
54
55     inc = IncludeCreate(ktcsg->keycodes, MergeDefault);
56     keycodes = CreateXKBFile(XkmKeyNamesIndex, NULL, (ParseCommon *)inc, 0);
57
58     inc = IncludeCreate(ktcsg->types, MergeDefault);
59     types = CreateXKBFile(XkmTypesIndex, NULL, (ParseCommon *)inc, 0);
60     AppendStmt(&keycodes->common, &types->common);
61
62     inc = IncludeCreate(ktcsg->compat, MergeDefault);
63     compat = CreateXKBFile(XkmCompatMapIndex, NULL, (ParseCommon *)inc, 0);
64     AppendStmt(&keycodes->common, &compat->common);
65
66     inc = IncludeCreate(ktcsg->symbols, MergeDefault);
67     symbols = CreateXKBFile(XkmSymbolsIndex, NULL, (ParseCommon *)inc, 0);
68     AppendStmt(&keycodes->common, &symbols->common);
69
70     inc = IncludeCreate(ktcsg->geometry, MergeDefault);
71     geometry = CreateXKBFile(XkmGeometryIndex, NULL, (ParseCommon *)inc, 0);
72     AppendStmt(&keycodes->common, &geometry->common);
73
74     return CreateXKBFile(XkmKeymapFile, ktcsg->keymap ? ktcsg->keymap : "",
75                          &keycodes->common, 0);
76 }
77
78 static XkbComponentNamesPtr
79 XkbComponentsFromRules(const char *rulesPath, const XkbRF_VarDefsPtr defs)
80 {
81     XkbRF_RulesPtr rules;
82     XkbComponentNamesPtr names = NULL;
83
84     if (!(rules = XkbcRF_Load((char *)rulesPath, NULL, False, True))) {
85         ERROR("failed to load XKB rules \"%s\"\n", rulesPath);
86         goto fail;
87     }
88
89     if (!(names = _XkbTypedCalloc(1, XkbComponentNamesRec))) {
90         ERROR("failed to allocate XKB components\n");
91         goto unwind_rules;
92     }
93
94     if (!XkbcRF_GetComponents(rules, defs, names)) {
95         _XkbFree(names->keymap);
96         _XkbFree(names->keycodes);
97         _XkbFree(names->types);
98         _XkbFree(names->compat);
99         _XkbFree(names->symbols);
100         _XkbFree(names->geometry);
101         _XkbFree(names);
102         names = NULL;
103         ERROR("no components returned from XKB rules \"%s\"\n", rulesPath);
104     }
105
106 unwind_rules:
107     XkbcRF_Free(rules, True);
108 fail:
109     return names;
110 }
111
112 XkbcDescPtr
113 XkbcCompileKeymapFromRules(const char *rules, XkbRF_VarDefsPtr defs)
114 {
115     char rulesPath[PATH_MAX];
116     int pathlen;
117     XkbComponentNamesPtr names;
118     XkbcDescPtr xkb;
119
120     if (ISEMPTY(rules) || !defs || ISEMPTY(defs->layout)) {
121         ERROR("rules and layout required to generate XKB keymap\n");
122         return NULL;
123     }
124
125     pathlen = snprintf(rulesPath, sizeof(rulesPath),
126                        DFLT_XKB_CONFIG_ROOT "/rules/%s", rules);
127     if (pathlen >= sizeof(rulesPath)) {
128         ERROR("XKB rules path truncated\n");
129         return NULL;
130     }
131
132     names = XkbComponentsFromRules(rulesPath, defs);
133     if (!names) {
134         ERROR("failed to generate XKB components from rules \"%s\"\n",
135               rules);
136         return NULL;
137     }
138
139     xkb = XkbcCompileKeymapFromComponents(names);
140
141     _XkbFree(names->keymap);
142     _XkbFree(names->keycodes);
143     _XkbFree(names->types);
144     _XkbFree(names->compat);
145     _XkbFree(names->symbols);
146     _XkbFree(names->geometry);
147     _XkbFree(names);
148
149     return xkb;
150 }
151
152 XkbcDescPtr
153 XkbcCompileKeymapFromComponents(XkbComponentNamesPtr ktcsg)
154 {
155     XkbFile *file, *mapToUse;
156     XkbcDescPtr xkb;
157
158     if (!ktcsg || ISEMPTY(ktcsg->keycodes)) {
159         ERROR("keycodes required to generate XKB keymap\n");
160         goto fail;
161     }
162
163     if (!(file = XkbKeymapFileFromComponents(ktcsg))) {
164         ERROR("failed to generate parsed XKB file from components\n");
165         goto fail;
166     }
167
168     /* Find map to use */
169     mapToUse = file;
170     if (file->common.next) {
171         for (; mapToUse; mapToUse = (XkbFile *)mapToUse->common.next) {
172             if (mapToUse->flags & XkbLC_Default)
173                 break;
174         }
175         if (!mapToUse) {
176             mapToUse = file;
177             WARN("no map specified, but components have several\n");
178         }
179     }
180
181     /* Compile the keyboard */
182     if (!(xkb = XkbcAllocKeyboard())) {
183         ERROR("could not allocate keyboard description\n");
184         goto unwind_file;
185     }
186
187     if (!CompileKeymap(mapToUse, xkb, MergeReplace)) {
188         ERROR("failed to compile keymap\n");
189         goto unwind_xkb;
190     }
191
192     return xkb;
193 unwind_xkb:
194     XkbcFreeKeyboard(xkb, XkbAllComponentsMask, True);
195 unwind_file:
196     /* XXX: here's where we would free the XkbFile */
197 fail:
198     return NULL;
199 }