Messages: add new messages to registry
[platform/upstream/libxkbcommon.git] / src / xkbcomp / include.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Ran Benita <ran234@gmail.com>
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the "Software"),
32  * to deal in the Software without restriction, including without limitation
33  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34  * and/or sell copies of the Software, and to permit persons to whom the
35  * Software is furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice (including the next
38  * paragraph) shall be included in all copies or substantial portions of the
39  * Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47  * DEALINGS IN THE SOFTWARE.
48  */
49
50 #include "config.h"
51
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdio.h>
55
56 #include "xkbcomp-priv.h"
57 #include "include.h"
58
59 /**
60  * Parse an include statement. Each call returns a file name, along with
61  * (possibly) a specific map in the file, an explicit group designator, and
62  * the separator from the next file, used to determine the merge mode.
63  *
64  * @param str_inout Input statement, modified in-place. Should be passed in
65  * repeatedly. If str_inout is NULL, the parsing has completed.
66  *
67  * @param file_rtrn Set to the name of the include file to be used. Combined
68  * with an enum xkb_file_type, this determines which file to look for in the
69  * include path.
70  *
71  * @param map_rtrn Set to the string between '(' and ')', if any. This will
72  * result in the compilation of a specific named map within the file (e.g.
73  * xkb_symbols "basic" { ... }) , as opposed to the default map of the file.
74  *
75  * @param nextop_rtrn Set to the next operation in the complete statement,
76  * which is '\0' if it's the last file or '+' or '|' if there are more.
77  * Separating the files with '+' sets the merge mode to MERGE_MODE_OVERRIDE,
78  * while '|' sets the merge mode to MERGE_MODE_AUGMENT.
79  *
80  * @param extra_data Set to the string after ':', if any. Currently the
81  * extra data is only used for setting an explicit group index for a symbols
82  * file.
83  *
84  * @return true if parsing was successful, false for an illegal string.
85  *
86  * Example: "evdev+aliases(qwerty):2"
87  *      str_inout = "aliases(qwerty):2"
88  *      file_rtrn = "evdev"
89  *      map_rtrn = NULL
90  *      nextop_retrn = "+"
91  *      extra_data = NULL
92  *
93  * 2nd run with "aliases(qwerty):2"
94  *      str_inout = NULL
95  *      file_rtrn = "aliases"
96  *      map_rtrn = "qwerty"
97  *      nextop_retrn = ""
98  *      extra_data = "2"
99  *
100  */
101 bool
102 ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
103                 char *nextop_rtrn, char **extra_data)
104 {
105     char *tmp, *str, *next;
106
107     str = *str_inout;
108
109     /*
110      * Find the position in the string where the next file is included,
111      * if there is more than one left in the statement.
112      */
113     next = strpbrk(str, "|+");
114     if (next) {
115         /* Got more files, this function will be called again. */
116         *nextop_rtrn = *next;
117         /* Separate the string, for strchr etc. to work on this file only. */
118         *next++ = '\0';
119     }
120     else {
121         /* This is the last file in this statement, won't be called again. */
122         *nextop_rtrn = '\0';
123         next = NULL;
124     }
125
126     /*
127      * Search for the explicit group designator, if any. If it's there,
128      * it goes after the file name and map.
129      */
130     tmp = strchr(str, ':');
131     if (tmp != NULL) {
132         *tmp++ = '\0';
133         *extra_data = strdup(tmp);
134     }
135     else {
136         *extra_data = NULL;
137     }
138
139     /* Look for a map, if any. */
140     tmp = strchr(str, '(');
141     if (tmp == NULL) {
142         /* No map. */
143         *file_rtrn = strdup(str);
144         *map_rtrn = NULL;
145     }
146     else if (str[0] == '(') {
147         /* Map without file - invalid. */
148         free(*extra_data);
149         return false;
150     }
151     else {
152         /* Got a map; separate the file and the map for the strdup's. */
153         *tmp++ = '\0';
154         *file_rtrn = strdup(str);
155         str = tmp;
156         tmp = strchr(str, ')');
157         if (tmp == NULL || tmp[1] != '\0') {
158             free(*file_rtrn);
159             free(*extra_data);
160             return false;
161         }
162         *tmp++ = '\0';
163         *map_rtrn = strdup(str);
164     }
165
166     /* Set up the next file for the next call, if any. */
167     if (*nextop_rtrn == '\0')
168         *str_inout = NULL;
169     else if (*nextop_rtrn == '|' || *nextop_rtrn == '+')
170         *str_inout = next;
171     else
172         return false;
173
174     return true;
175 }
176
177 static const char *xkb_file_type_include_dirs[_FILE_TYPE_NUM_ENTRIES] = {
178     [FILE_TYPE_KEYCODES] = "keycodes",
179     [FILE_TYPE_TYPES] = "types",
180     [FILE_TYPE_COMPAT] = "compat",
181     [FILE_TYPE_SYMBOLS] = "symbols",
182     [FILE_TYPE_GEOMETRY] = "geometry",
183     [FILE_TYPE_KEYMAP] = "keymap",
184     [FILE_TYPE_RULES] = "rules",
185 };
186
187 /**
188  * Return the xkb directory based on the type.
189  */
190 static const char *
191 DirectoryForInclude(enum xkb_file_type type)
192 {
193     if (type >= _FILE_TYPE_NUM_ENTRIES)
194         return "";
195     return xkb_file_type_include_dirs[type];
196 }
197
198 static void
199 LogIncludePaths(struct xkb_context *ctx)
200 {
201     unsigned int i;
202
203     if (xkb_context_num_include_paths(ctx) > 0) {
204         log_err_with_code(ctx,
205                 XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
206                 "%d include paths searched:\n",
207                 xkb_context_num_include_paths(ctx));
208         for (i = 0; i < xkb_context_num_include_paths(ctx); i++)
209             log_err_with_code(ctx,
210                     XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
211                     "\t%s\n",
212                     xkb_context_include_path_get(ctx, i));
213     }
214     else {
215         log_err_with_code(ctx,
216                 XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
217                 "There are no include paths to search\n");
218     }
219
220     if (xkb_context_num_failed_include_paths(ctx) > 0) {
221         log_err_with_code(ctx,
222                 XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
223                 "%d include paths could not be added:\n",
224                 xkb_context_num_failed_include_paths(ctx));
225         for (i = 0; i < xkb_context_num_failed_include_paths(ctx); i++)
226             log_err_with_code(ctx,
227                     XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
228                     "\t%s\n",
229                     xkb_context_failed_include_path_get(ctx, i));
230     }
231 }
232
233 /**
234  * Return an open file handle to the first file (counting from offset) with the
235  * given name in the include paths, starting at the offset.
236  *
237  * offset must be zero the first time this is called and is set to the index the
238  * file was found. Call again with offset+1 to keep searching through the
239  * include paths.
240  *
241  * If this function returns NULL, no more files are available.
242  */
243 FILE *
244 FindFileInXkbPath(struct xkb_context *ctx, const char *name,
245                   enum xkb_file_type type, char **pathRtrn,
246                   unsigned int *offset)
247 {
248     unsigned int i;
249     FILE *file = NULL;
250     char *buf = NULL;
251     const char *typeDir;
252
253     typeDir = DirectoryForInclude(type);
254
255     for (i = *offset; i < xkb_context_num_include_paths(ctx); i++) {
256         buf = asprintf_safe("%s/%s/%s", xkb_context_include_path_get(ctx, i),
257                             typeDir, name);
258         if (!buf) {
259             log_err_with_code(ctx,
260                     XKB_ERROR_ALLOCATION_ERROR,
261                     "Failed to alloc buffer for (%s/%s/%s)\n",
262                     xkb_context_include_path_get(ctx, i), typeDir, name);
263             continue;
264         }
265
266         file = fopen(buf, "rb");
267         if (file) {
268             if (pathRtrn) {
269                 *pathRtrn = buf;
270                 buf = NULL;
271             }
272             *offset = i;
273             goto out;
274         }
275     }
276
277     /* We only print warnings if we can't find the file on the first lookup */
278     if (*offset == 0) {
279         log_err_with_code(ctx,
280                 XKB_ERROR_INCLUDED_FILE_NOT_FOUND,
281                 "Couldn't find file \"%s/%s\" in include paths\n",
282                 typeDir, name);
283         LogIncludePaths(ctx);
284     }
285
286 out:
287     free(buf);
288     return file;
289 }
290
291 XkbFile *
292 ProcessIncludeFile(struct xkb_context *ctx, IncludeStmt *stmt,
293                    enum xkb_file_type file_type)
294 {
295     FILE *file;
296     XkbFile *xkb_file = NULL;
297     unsigned int offset = 0;
298
299     file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL, &offset);
300     if (!file)
301         return NULL;
302
303     while (file) {
304         xkb_file = XkbParseFile(ctx, file, stmt->file, stmt->map);
305         fclose(file);
306
307         if (xkb_file) {
308             if (xkb_file->file_type != file_type) {
309                 log_err_with_code(ctx,
310                         XKB_ERROR_INVALID_INCLUDED_FILE,
311                         "Include file of wrong type (expected %s, got %s); "
312                         "Include file \"%s\" ignored\n",
313                         xkb_file_type_to_string(file_type),
314                         xkb_file_type_to_string(xkb_file->file_type), stmt->file);
315                 FreeXkbFile(xkb_file);
316                 xkb_file = NULL;
317             } else {
318                 break;
319             }
320         }
321
322         offset++;
323         file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL, &offset);
324     }
325
326     if (!xkb_file) {
327         if (stmt->map)
328             log_err_with_code(ctx,
329                     XKB_ERROR_INVALID_INCLUDED_FILE,
330                     "Couldn't process include statement for '%s(%s)'\n",
331                     stmt->file, stmt->map);
332         else
333             log_err_with_code(ctx,
334                     XKB_ERROR_INVALID_INCLUDED_FILE,
335                     "Couldn't process include statement for '%s'\n",
336                     stmt->file);
337     }
338
339     /* FIXME: we have to check recursive includes here (or somewhere) */
340
341     return xkb_file;
342 }