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 "xkbcomp-priv.h"
36 * Extract the first token from an include statement.
37 * @param str_inout Input statement, modified in-place. Can be passed in
38 * repeatedly. If str_inout is NULL, the parsing has completed.
39 * @param file_rtrn Set to the include file to be used.
40 * @param map_rtrn Set to whatever comes after ), if any.
41 * @param nextop_rtrn Set to the next operation in the complete statement.
42 * @param extra_data Set to the string between ( and ), if any.
44 * @return true if parsing was succcessful, false for an illegal string.
46 * Example: "evdev+aliases(qwerty)"
47 * str_inout = aliases(qwerty)
53 * 2nd run with "aliases(qwerty)"
62 ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
63 char *nextop_rtrn, char **extra_data)
65 char *tmp, *str, *next;
69 /* search for tokens inside the string */
70 next = strpbrk(str, "|+");
72 /* set nextop_rtrn to \0, next to next character */
81 /* search for :, store result in extra_data */
82 tmp = strchr(str, ':');
85 *extra_data = strdup(tmp);
91 tmp = strchr(str, '(');
93 *file_rtrn = strdup(str);
96 else if (str[0] == '(') {
102 *file_rtrn = strdup(str);
104 tmp = strchr(str, ')');
105 if ((tmp == NULL) || (tmp[1] != '\0')) {
111 *map_rtrn = strdup(str);
114 if (*nextop_rtrn == '\0')
116 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
124 /***====================================================================***/
126 static const char *xkb_file_type_include_dirs[_FILE_TYPE_NUM_ENTRIES] = {
127 [FILE_TYPE_KEYCODES] = "keycodes",
128 [FILE_TYPE_TYPES] = "types",
129 [FILE_TYPE_COMPAT] = "compat",
130 [FILE_TYPE_SYMBOLS] = "symbols",
131 [FILE_TYPE_GEOMETRY] = "geometry",
132 [FILE_TYPE_KEYMAP] = "keymap",
133 [FILE_TYPE_RULES] = "rules",
137 * Return the xkb directory based on the type.
140 DirectoryForInclude(enum xkb_file_type type)
142 if (type >= _FILE_TYPE_NUM_ENTRIES)
144 return xkb_file_type_include_dirs[type];
147 /***====================================================================***/
150 * Search for the given file name in the include directories.
152 * @param ctx the XKB ctx containing the include paths
153 * @param type one of FILE_TYPE_TYPES, FILE_TYPE_COMPAT, ..., or
154 * FILE_TYPE_KEYMAP or FILE_TYPE_RULES
155 * @param pathRtrn is set to the full path of the file if found.
157 * @return an FD to the file or NULL. If NULL is returned, the value of
158 * pathRtrn is undefined.
161 FindFileInXkbPath(struct xkb_context *ctx, const char *name,
162 enum xkb_file_type type, char **pathRtrn)
170 typeDir = DirectoryForInclude(type);
171 for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
172 ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
173 xkb_context_include_path_get(ctx, i), typeDir, name);
174 if (ret >= (ssize_t) sizeof(buf)) {
175 log_err(ctx, "File name (%s/%s/%s) too long\n",
176 xkb_context_include_path_get(ctx, i), typeDir, name);
179 file = fopen(buf, "r");
181 log_err(ctx, "Couldn't open file (%s/%s/%s): %s\n",
182 xkb_context_include_path_get(ctx, i), typeDir, name,
189 if ((file != NULL) && (pathRtrn != NULL))
190 *pathRtrn = strdup(buf);
195 * Open the file given in the include statement and parse it's content.
196 * If the statement defines a specific map to use, this map is returned in
197 * file_rtrn. Otherwise, the default map is returned.
199 * @param ctx The ctx containing include paths
200 * @param stmt The include statement, specifying the file name to look for.
201 * @param file_type Type of file (FILE_TYPE_KEYCODES, etc.)
202 * @param file_rtrn Returns the key map to be used.
203 * @param merge_rtrn Always returns stmt->merge.
205 * @return true on success or false otherwise.
208 ProcessIncludeFile(struct xkb_context *ctx,
210 enum xkb_file_type file_type,
211 XkbFile ** file_rtrn, enum merge_mode *merge_rtrn)
214 XkbFile *rtrn, *mapToUse, *next;
216 file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL);
218 log_err(ctx, "Can't find file \"%s\" for %s include\n", stmt->file,
219 DirectoryForInclude(file_type));
223 if (!XkbParseFile(ctx, file, stmt->file, &rtrn)) {
224 log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file);
231 if (stmt->map != NULL) {
234 next = (XkbFile *) mapToUse->common.next;
235 mapToUse->common.next = NULL;
236 if (streq(mapToUse->name, stmt->map) &&
237 mapToUse->file_type == file_type) {
242 FreeXkbFile(mapToUse);
247 log_err(ctx, "No %s named \"%s\" in the include file \"%s\"\n",
248 FileTypeText(file_type), stmt->map, stmt->file);
252 else if (rtrn->common.next) {
254 "No map in include statement, but \"%s\" contains several; "
255 "Using first defined map, \"%s\"\n",
256 stmt->file, rtrn->name);
258 if (mapToUse->file_type != file_type) {
260 "Include file wrong type (expected %s, got %s); "
261 "Include file \"%s\" ignored\n",
262 FileTypeText(file_type), FileTypeText(mapToUse->file_type),
266 /* FIXME: we have to check recursive includes here (or somewhere) */
268 *file_rtrn = mapToUse;
269 *merge_rtrn = stmt->merge;