Don't printf NULL strings
[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 "xkbcomp-priv.h"
28 #include "rules.h"
29
30 static struct xkb_keymap *
31 compile_keymap_file(struct xkb_context *ctx, XkbFile *file)
32 {
33     struct xkb_keymap *keymap;
34
35     keymap = xkb_map_new(ctx);
36     if (!keymap)
37         goto err;
38
39     if (file->file_type != FILE_TYPE_KEYMAP) {
40         log_err(ctx, "Cannot compile a %s file alone into a keymap\n",
41                 xkb_file_type_to_string(file->file_type));
42         goto err;
43     }
44
45     if (!CompileKeymap(file, keymap, MERGE_OVERRIDE)) {
46         log_err(ctx, "Failed to compile keymap\n");
47         goto err;
48     }
49
50     return keymap;
51
52 err:
53     xkb_map_unref(keymap);
54     return NULL;
55 }
56
57 XKB_EXPORT struct xkb_keymap *
58 xkb_map_new_from_names(struct xkb_context *ctx,
59                        const struct xkb_rule_names *rmlvo_in,
60                        enum xkb_map_compile_flags flags)
61 {
62     bool ok;
63     struct xkb_component_names kccgst;
64     struct xkb_rule_names rmlvo = *rmlvo_in;
65     XkbFile *file;
66     struct xkb_keymap *keymap;
67
68     if (isempty(rmlvo.rules))
69         rmlvo.rules = DEFAULT_XKB_RULES;
70     if (isempty(rmlvo.model))
71         rmlvo.model = DEFAULT_XKB_MODEL;
72     if (isempty(rmlvo.layout))
73         rmlvo.layout = DEFAULT_XKB_LAYOUT;
74
75     log_dbg(ctx,
76             "Compiling from RMLVO: rules '%s', model '%s', layout '%s', "
77             "variant '%s', options '%s'\n",
78             strnull(rmlvo.rules), strnull(rmlvo.model),
79             strnull(rmlvo.layout), strnull(rmlvo.variant),
80             strnull(rmlvo.options));
81
82     ok = xkb_components_from_rules(ctx, &rmlvo, &kccgst);
83     if (!ok) {
84         log_err(ctx,
85                 "Couldn't look up rules '%s', model '%s', layout '%s', "
86                 "variant '%s', options '%s'\n",
87                 strnull(rmlvo.rules), strnull(rmlvo.model),
88                 strnull(rmlvo.layout), strnull(rmlvo.variant),
89                 strnull(rmlvo.options));
90         return NULL;
91     }
92
93     log_dbg(ctx,
94             "Compiling from KcCGST: keycodes '%s', types '%s', "
95             "compat '%s', symbols '%s'\n",
96             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
97
98     file = XkbFileFromComponents(ctx, &kccgst);
99
100     free(kccgst.keycodes);
101     free(kccgst.types);
102     free(kccgst.compat);
103     free(kccgst.symbols);
104
105     if (!file) {
106         log_err(ctx,
107                 "Failed to generate parsed XKB file from components\n");
108         return NULL;
109     }
110
111     keymap = compile_keymap_file(ctx, file);
112     FreeXkbFile(file);
113     return keymap;
114 }
115
116 XKB_EXPORT struct xkb_keymap *
117 xkb_map_new_from_string(struct xkb_context *ctx,
118                         const char *string,
119                         enum xkb_keymap_format format,
120                         enum xkb_map_compile_flags flags)
121 {
122     bool ok;
123     XkbFile *file;
124     struct xkb_keymap *keymap;
125
126     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
127         log_err(ctx, "Unsupported keymap format %d\n", format);
128         return NULL;
129     }
130
131     if (!string) {
132         log_err(ctx, "No string specified to generate XKB keymap\n");
133         return NULL;
134     }
135
136     ok = XkbParseString(ctx, string, "input", &file);
137     if (!ok) {
138         log_err(ctx, "Failed to parse input xkb file\n");
139         return NULL;
140     }
141
142     keymap = compile_keymap_file(ctx, file);
143     FreeXkbFile(file);
144     return keymap;
145 }
146
147 XKB_EXPORT struct xkb_keymap *
148 xkb_map_new_from_file(struct xkb_context *ctx,
149                       FILE *file,
150                       enum xkb_keymap_format format,
151                       enum xkb_map_compile_flags flags)
152 {
153     bool ok;
154     XkbFile *xkb_file;
155     struct xkb_keymap *keymap;
156
157     if (format != XKB_KEYMAP_FORMAT_TEXT_V1) {
158         log_err(ctx, "Unsupported keymap format %d\n", format);
159         return NULL;
160     }
161
162     if (!file) {
163         log_err(ctx, "No file specified to generate XKB keymap\n");
164         return NULL;
165     }
166
167     ok = XkbParseFile(ctx, file, "(unknown file)", &xkb_file);
168     if (!ok) {
169         log_err(ctx, "Failed to parse input xkb file\n");
170         return NULL;
171     }
172
173     keymap = compile_keymap_file(ctx, xkb_file);
174     FreeXkbFile(xkb_file);
175     return keymap;
176 }