Fix a couple of mistakes from previous commits
[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 <errno.h>
51 #include <limits.h>
52 #include <stdio.h>
53
54 #include "xkbcomp-priv.h"
55 #include "include.h"
56
57 /**
58  * Parse an include statement. Each call returns a file name, along with
59  * (possibly) a specific map in the file, an explicit group designator, and
60  * the separator from the next file, used to determine the merge mode.
61  *
62  * @param str_inout Input statement, modified in-place. Should be passed in
63  * repeatedly. If str_inout is NULL, the parsing has completed.
64  *
65  * @param file_rtrn Set to the name of the include file to be used. Combined
66  * with an enum xkb_file_type, this determines which file to look for in the
67  * include path.
68  *
69  * @param map_rtrn Set to the string between '(' and ')', if any. This will
70  * result in the compilation of a specific named map within the file (e.g.
71  * xkb_symbols "basic" { ... }) , as opposed to the default map of the file.
72  *
73  * @param nextop_rtrn Set to the next operation in the complete statement,
74  * which is '\0' if it's the last file or '+' or '|' if there are more.
75  * Separating the files with '+' sets the merge mode to MERGE_MODE_OVERRIDE,
76  * while '|' sets the merge mode to MERGE_MODE_AUGMENT.
77  *
78  * @param extra_data Set to the string after ':', if any. Currently the
79  * extra data is only used for setting an explicit group index for a symbols
80  * file.
81  *
82  * @return true if parsing was successful, false for an illegal string.
83  *
84  * Example: "evdev+aliases(qwerty):2"
85  *      str_inout = "aliases(qwerty):2"
86  *      file_rtrn = "evdev"
87  *      map_rtrn = NULL
88  *      nextop_retrn = "+"
89  *      extra_data = NULL
90  *
91  * 2nd run with "aliases(qwerty):2"
92  *      str_inout = NULL
93  *      file_rtrn = "aliases"
94  *      map_rtrn = "qwerty"
95  *      nextop_retrn = ""
96  *      extra_data = "2"
97  *
98  */
99 bool
100 ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
101                 char *nextop_rtrn, char **extra_data)
102 {
103     char *tmp, *str, *next;
104
105     str = *str_inout;
106
107     /*
108      * Find the position in the string where the next file is included,
109      * if there is more than one left in the statement.
110      */
111     next = strpbrk(str, "|+");
112     if (next) {
113         /* Got more files, this function will be called again. */
114         *nextop_rtrn = *next;
115         /* Separate the string, for strchr etc. to work on this file only. */
116         *next++ = '\0';
117     }
118     else {
119         /* This is the last file in this statement, won't be called again. */
120         *nextop_rtrn = '\0';
121         next = NULL;
122     }
123
124     /*
125      * Search for the explicit group designator, if any. If it's there,
126      * it goes after the file name and map.
127      */
128     tmp = strchr(str, ':');
129     if (tmp != NULL) {
130         *tmp++ = '\0';
131         *extra_data = strdup(tmp);
132     }
133     else {
134         *extra_data = NULL;
135     }
136
137     /* Look for a map, if any. */
138     tmp = strchr(str, '(');
139     if (tmp == NULL) {
140         /* No map. */
141         *file_rtrn = strdup(str);
142         *map_rtrn = NULL;
143     }
144     else if (str[0] == '(') {
145         /* Map without file - invalid. */
146         free(*extra_data);
147         return false;
148     }
149     else {
150         /* Got a map; separate the file and the map for the strdup's. */
151         *tmp++ = '\0';
152         *file_rtrn = strdup(str);
153         str = tmp;
154         tmp = strchr(str, ')');
155         if (tmp == NULL || tmp[1] != '\0') {
156             free(*file_rtrn);
157             free(*extra_data);
158             return false;
159         }
160         *tmp++ = '\0';
161         *map_rtrn = strdup(str);
162     }
163
164     /* Set up the next file for the next call, if any. */
165     if (*nextop_rtrn == '\0')
166         *str_inout = NULL;
167     else if (*nextop_rtrn == '|' || *nextop_rtrn == '+')
168         *str_inout = next;
169     else
170         return false;
171
172     return true;
173 }
174
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 /***====================================================================***/
199
200 /**
201  * Search for the given file name in the include directories.
202  *
203  * @param ctx the XKB ctx containing the include paths
204  * @param type one of FILE_TYPE_TYPES, FILE_TYPE_COMPAT, ..., or
205  *             FILE_TYPE_KEYMAP or FILE_TYPE_RULES
206  * @param pathRtrn is set to the full path of the file if found.
207  *
208  * @return an FD to the file or NULL. If NULL is returned, the value of
209  * pathRtrn is undefined.
210  */
211 FILE *
212 FindFileInXkbPath(struct xkb_context *ctx, const char *name,
213                   enum xkb_file_type type, char **pathRtrn)
214 {
215     unsigned int i;
216     FILE *file = NULL;
217     char buf[PATH_MAX];
218     const char *typeDir;
219
220     typeDir = DirectoryForInclude(type);
221
222     for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
223         int ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
224                            xkb_context_include_path_get(ctx, i),
225                            typeDir, name);
226         if (ret >= (ssize_t) sizeof(buf)) {
227             log_err(ctx, "File name (%s/%s/%s) too long\n",
228                     xkb_context_include_path_get(ctx, i), typeDir, name);
229             continue;
230         }
231
232         file = fopen(buf, "r");
233         if (file)
234             break;
235     }
236
237     if (!file) {
238         log_err(ctx, "Couldn't find file \"%s/%s\" in include paths\n",
239                 typeDir, name);
240
241         if (xkb_context_num_include_paths(ctx) > 0) {
242             log_err(ctx, "%d include paths searched:\n",
243                     xkb_context_num_include_paths(ctx));
244             for (i = 0; i < xkb_context_num_include_paths(ctx); i++)
245                 log_err(ctx, "\t%s\n",
246                         xkb_context_include_path_get(ctx, i));
247         }
248         else {
249             log_err(ctx, "There are no include paths to search\n");
250         }
251
252         if (xkb_context_num_failed_include_paths(ctx) > 0) {
253             log_err(ctx, "%d include paths could not be added:\n",
254                     xkb_context_num_failed_include_paths(ctx));
255             for (i = 0; i < xkb_context_num_failed_include_paths(ctx); i++)
256                 log_err(ctx, "\t%s\n",
257                         xkb_context_failed_include_path_get(ctx, i));
258         }
259
260         return NULL;
261     }
262
263     if (pathRtrn)
264         *pathRtrn = strdup(buf);
265     return file;
266 }
267
268 /**
269  * Open the file given in the include statement and parse it's content.
270  * If the statement defines a specific map to use, this map is returned in
271  * file_rtrn. Otherwise, the default map is returned.
272  *
273  * @param ctx The ctx containing include paths
274  * @param stmt The include statement, specifying the file name to look for.
275  * @param file_type Type of file (FILE_TYPE_KEYCODES, etc.)
276  * @param file_rtrn Returns the key map to be used.
277  * @param merge_rtrn Always returns stmt->merge.
278  *
279  * @return true on success or false otherwise.
280  */
281 bool
282 ProcessIncludeFile(struct xkb_context *ctx,
283                    IncludeStmt * stmt,
284                    enum xkb_file_type file_type,
285                    XkbFile ** file_rtrn, enum merge_mode *merge_rtrn)
286 {
287     FILE *file;
288     XkbFile *rtrn, *mapToUse, *next;
289
290     file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL);
291     if (!file)
292         return false;
293
294     if (!XkbParseFile(ctx, file, stmt->file, &rtrn)) {
295         log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file);
296         fclose(file);
297         return false;
298     }
299     fclose(file);
300
301     mapToUse = rtrn;
302     if (stmt->map != NULL) {
303         while (mapToUse)
304         {
305             next = (XkbFile *) mapToUse->common.next;
306             mapToUse->common.next = NULL;
307             if (streq(mapToUse->name, stmt->map) &&
308                 mapToUse->file_type == file_type) {
309                 FreeXkbFile(next);
310                 break;
311             }
312             else {
313                 FreeXkbFile(mapToUse);
314             }
315             mapToUse = next;
316         }
317         if (!mapToUse) {
318             log_err(ctx, "No %s named \"%s\" in the include file \"%s\"\n",
319                     xkb_file_type_to_string(file_type), stmt->map,
320                     stmt->file);
321             return false;
322         }
323     }
324     else if (rtrn->common.next) {
325         for (; mapToUse; mapToUse = (XkbFile *) mapToUse->common.next)
326             if (mapToUse->flags & MAP_IS_DEFAULT)
327                 break;
328
329         if (!mapToUse) {
330             mapToUse = rtrn;
331             log_vrb(ctx, 5,
332                     "No map in include statement, but \"%s\" contains several; "
333                     "Using first defined map, \"%s\"\n",
334                     stmt->file, rtrn->name);
335         }
336     }
337
338     if (mapToUse->file_type != file_type) {
339         log_err(ctx,
340                 "Include file wrong type (expected %s, got %s); "
341                 "Include file \"%s\" ignored\n",
342                 xkb_file_type_to_string(file_type),
343                 xkb_file_type_to_string(mapToUse->file_type), stmt->file);
344         return false;
345     }
346
347     /* FIXME: we have to check recursive includes here (or somewhere) */
348
349     *file_rtrn = mapToUse;
350     *merge_rtrn = stmt->merge;
351     return true;
352 }