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