1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
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.
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.
25 ********************************************************/
31 #include "parseutils.h"
34 * Extract the first token from an include statement.
35 * @param str_inout Input statement, modified in-place. Can be passed in
36 * repeatedly. If str_inout is NULL, the parsing has completed.
37 * @param file_rtrn Set to the include file to be used.
38 * @param map_rtrn Set to whatever comes after ), if any.
39 * @param nextop_rtrn Set to the next operation in the complete statement.
40 * @param extra_data Set to the string between ( and ), if any.
42 * @return true if parsing was succcessful, false for an illegal string.
44 * Example: "evdev+aliases(qwerty)"
45 * str_inout = aliases(qwerty)
51 * 2nd run with "aliases(qwerty)"
60 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
61 char *nextop_rtrn, char **extra_data)
63 char *tmp, *str, *next;
67 /* search for tokens inside the string */
68 next = strpbrk(str, "|+");
70 /* set nextop_rtrn to \0, next to next character */
79 /* search for :, store result in extra_data */
80 tmp = strchr(str, ':');
83 *extra_data = strdup(tmp);
89 tmp = strchr(str, '(');
91 *file_rtrn = strdup(str);
94 else if (str[0] == '(') {
100 *file_rtrn = strdup(str);
102 tmp = strchr(str, ')');
103 if ((tmp == NULL) || (tmp[1] != '\0')) {
109 *map_rtrn = strdup(str);
112 if (*nextop_rtrn == '\0')
114 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
122 /***====================================================================***/
125 * Return the xkb directory based on the type.
128 XkbDirectoryForInclude(enum xkb_file_type type)
131 case FILE_TYPE_KEYMAP:
134 case FILE_TYPE_KEYCODES:
137 case FILE_TYPE_TYPES:
140 case FILE_TYPE_SYMBOLS:
143 case FILE_TYPE_COMPAT:
146 case FILE_TYPE_GEOMETRY:
149 case FILE_TYPE_RULES:
157 /***====================================================================***/
160 * Search for the given file name in the include directories.
162 * @param ctx the XKB ctx containing the include paths
163 * @param type one of FILE_TYPE_TYPES, FILE_TYPE_COMPAT, ..., or
164 * FILE_TYPE_KEYMAP or FILE_TYPE_RULES
165 * @param pathRtrn is set to the full path of the file if found.
167 * @return an FD to the file or NULL. If NULL is returned, the value of
168 * pathRtrn is undefined.
171 XkbFindFileInPath(struct xkb_context *ctx, const char *name,
172 enum xkb_file_type type, char **pathRtrn)
180 typeDir = XkbDirectoryForInclude(type);
181 for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
182 ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
183 xkb_context_include_path_get(ctx, i), typeDir, name);
184 if (ret >= (ssize_t) sizeof(buf)) {
185 log_err(ctx, "File name (%s/%s/%s) too long\n",
186 xkb_context_include_path_get(ctx, i), typeDir, name);
189 file = fopen(buf, "r");
191 log_err(ctx, "Couldn't open file (%s/%s/%s): %s\n",
192 xkb_context_include_path_get(ctx, i), typeDir, name,
199 if ((file != NULL) && (pathRtrn != NULL))
200 *pathRtrn = strdup(buf);
205 * Open the file given in the include statement and parse it's content.
206 * If the statement defines a specific map to use, this map is returned in
207 * file_rtrn. Otherwise, the default map is returned.
209 * @param ctx The ctx containing include paths
210 * @param stmt The include statement, specifying the file name to look for.
211 * @param file_type Type of file (FILE_TYPE_KEYCODES, etc.)
212 * @param file_rtrn Returns the key map to be used.
213 * @param merge_rtrn Always returns stmt->merge.
215 * @return true on success or false otherwise.
218 ProcessIncludeFile(struct xkb_context *ctx,
220 enum xkb_file_type file_type,
221 XkbFile ** file_rtrn, enum merge_mode *merge_rtrn)
224 XkbFile *rtrn, *mapToUse, *next;
226 file = XkbFindFileInPath(ctx, stmt->file, file_type, NULL);
228 log_err(ctx, "Can't find file \"%s\" for %s include\n", stmt->file,
229 XkbDirectoryForInclude(file_type));
233 if (!XKBParseFile(ctx, file, stmt->file, &rtrn)) {
234 log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file);
241 if (stmt->map != NULL) {
244 next = (XkbFile *) mapToUse->common.next;
245 mapToUse->common.next = NULL;
246 if (streq(mapToUse->name, stmt->map) &&
247 mapToUse->file_type == file_type) {
252 FreeXKBFile(mapToUse);
257 log_err(ctx, "No %s named \"%s\" in the include file \"%s\"\n",
258 FileTypeText(file_type), stmt->map, stmt->file);
262 else if (rtrn->common.next) {
264 "No map in include statement, but \"%s\" contains several; "
265 "Using first defined map, \"%s\"\n",
266 stmt->file, rtrn->name);
268 if (mapToUse->file_type != file_type) {
270 "Include file wrong type (expected %s, got %s); "
271 "Include file \"%s\" ignored\n",
272 FileTypeText(file_type), FileTypeText(mapToUse->file_type),
276 /* FIXME: we have to check recursive includes here (or somewhere) */
278 *file_rtrn = mapToUse;
279 *merge_rtrn = stmt->merge;