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 ********************************************************/
27 #define DEBUG_VAR debugFlags
30 #include <X11/extensions/XKM.h>
32 #include "XKBcommonint.h"
34 #ifndef DFLT_XKB_CONFIG_ROOT
35 #define DFLT_XKB_CONFIG_ROOT "/usr/lib/X11/xkb"
42 #define PATH_CHUNK 8 /* initial szPath */
44 static Bool noDefaultPath = False;
45 static int szPath; /* number of entries allocated for includePath */
46 static int nPathEntries; /* number of actual entries in includePath */
47 static char **includePath; /* Holds all directories we might be including data from */
50 * Extract the first token from an include statement.
51 * @param str_inout Input statement, modified in-place. Can be passed in
52 * repeatedly. If str_inout is NULL, the parsing has completed.
53 * @param file_rtrn Set to the include file to be used.
54 * @param map_rtrn Set to whatever comes after ), if any.
55 * @param nextop_rtrn Set to the next operation in the complete statement.
56 * @param extra_data Set to the string between ( and ), if any.
58 * @return True if parsing was succcessful, False for an illegal string.
60 * Example: "evdev+aliases(qwerty)"
61 * str_inout = aliases(qwerty)
67 * 2nd run with "aliases(qwerty)"
76 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
77 char *nextop_rtrn, char **extra_data)
79 char *tmp, *str, *next;
82 if ((*str == '+') || (*str == '|'))
84 *file_rtrn = *map_rtrn = NULL;
90 *file_rtrn = *map_rtrn = NULL;
91 *nextop_rtrn = str[1];
96 /* search for tokens inside the string */
97 next = strpbrk(str, "|+");
100 /* set nextop_rtrn to \0, next to next character */
101 *nextop_rtrn = *next;
109 /* search for :, store result in extra_data */
110 tmp = strchr(str, ':');
114 *extra_data = uStringDup(tmp);
120 tmp = strchr(str, '(');
123 *file_rtrn = uStringDup(str);
126 else if (str[0] == '(')
134 *file_rtrn = uStringDup(str);
136 tmp = strchr(str, ')');
137 if ((tmp == NULL) || (tmp[1] != '\0'))
144 *map_rtrn = uStringDup(str);
147 if (*nextop_rtrn == '\0')
149 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
157 * Init memory for include paths.
160 XkbInitIncludePath(void)
163 includePath = (char **) calloc(szPath, sizeof(char *));
164 if (includePath == NULL)
170 XkbAddDefaultDirectoriesToPath(void)
174 XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
178 * Remove all entries from the global includePath.
181 XkbClearIncludePath(void)
187 for (i = 0; i < nPathEntries; i++)
189 if (includePath[i] != NULL)
191 uFree(includePath[i]);
192 includePath[i] = NULL;
197 noDefaultPath = True;
202 * Add the given path to the global includePath variable.
203 * If dir is NULL, the includePath is emptied.
206 XkbAddDirectoryToPath(const char *dir)
209 if ((dir == NULL) || (dir[0] == '\0'))
211 XkbClearIncludePath();
215 dir = (char *) __XOS2RedirRoot(dir);
218 if (len + 2 >= PATH_MAX)
219 { /* allow for '/' and at least one character */
220 ERROR("Path entry (%s) too long (maxiumum length is %d)\n",
224 if (nPathEntries >= szPath)
226 szPath += PATH_CHUNK;
227 includePath = (char **) realloc(includePath, szPath * sizeof(char *));
228 if (includePath == NULL)
230 WSGO("Allocation failed (includePath)\n");
234 includePath[nPathEntries] =
235 (char *) calloc(strlen(dir) + 1, sizeof(char));
236 if (includePath[nPathEntries] == NULL)
238 WSGO("Allocation failed (includePath[%d])\n", nPathEntries);
241 strcpy(includePath[nPathEntries++], dir);
245 /***====================================================================***/
248 * Return the xkb directory based on the type.
249 * Do not free the memory returned by this function.
252 XkbDirectoryForInclude(unsigned type)
258 case XkmSemanticsFile:
259 strcpy(buf, "semantics");
262 strcpy(buf, "layout");
265 strcpy(buf, "keymap");
267 case XkmKeyNamesIndex:
268 strcpy(buf, "keycodes");
271 strcpy(buf, "types");
273 case XkmSymbolsIndex:
274 strcpy(buf, "symbols");
276 case XkmCompatMapIndex:
277 strcpy(buf, "compat");
279 case XkmGeometryFile:
280 case XkmGeometryIndex:
281 strcpy(buf, "geometry");
290 /***====================================================================***/
292 typedef struct _FileCacheEntry
298 struct _FileCacheEntry *next;
300 static FileCacheEntry *fileCache;
303 * Add the file with the given name to the internal cache to avoid opening and
304 * parsing the file multiple times. If a cache entry for the same name + type
305 * is already present, the entry is overwritten and the data belonging to the
306 * previous entry is returned.
308 * @parameter name The name of the file (e.g. evdev).
309 * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
310 * @parameter path The full path to the file.
311 * @parameter data Already parsed data.
313 * @return The data from the overwritten file or NULL.
316 XkbAddFileToCache(char *name, unsigned type, char *path, void *data)
318 FileCacheEntry *entry;
320 for (entry = fileCache; entry != NULL; entry = entry->next)
322 if ((type == entry->type) && (uStringEqual(name, entry->name)))
324 void *old = entry->data;
325 WSGO("Replacing file cache entry (%s/%d)\n", name, type);
331 entry = uTypedAlloc(FileCacheEntry);
338 entry->next = fileCache;
345 * Search for the given name + type in the cache.
347 * @parameter name The name of the file (e.g. evdev).
348 * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
349 * @parameter pathRtrn Set to the full path of the given entry.
351 * @return the data from the cache entry or NULL if no matching entry was found.
354 XkbFindFileInCache(char *name, unsigned type, char **pathRtrn)
356 FileCacheEntry *entry;
358 for (entry = fileCache; entry != NULL; entry = entry->next)
360 if ((type == entry->type) && (uStringEqual(name, entry->name)))
362 *pathRtrn = entry->path;
369 /***====================================================================***/
372 * Search for the given file name in the include directories.
374 * @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
375 * XkbSemanticsFile, XkmKeymapFile, ...
376 * @param pathReturn is set to the full path of the file if found.
378 * @return an FD to the file or NULL. If NULL is returned, the value of
379 * pathRtrn is undefined.
382 XkbFindFileInPath(char *name, unsigned type, char **pathRtrn)
386 int nameLen, typeLen, pathLen;
387 char buf[PATH_MAX], *typeDir;
389 typeDir = XkbDirectoryForInclude(type);
390 nameLen = strlen(name);
391 typeLen = strlen(typeDir);
392 for (i = 0; i < nPathEntries; i++)
394 pathLen = strlen(includePath[i]);
398 if ((nameLen + typeLen + pathLen + 2) >= PATH_MAX)
400 ERROR("File name (%s/%s/%s) too long\n", includePath[i],
405 snprintf(buf, sizeof(buf), "%s/%s/%s", includePath[i], typeDir, name);
406 file = fopen(buf, "r");
411 if ((file != NULL) && (pathRtrn != NULL))
413 *pathRtrn = (char *) calloc(strlen(buf) + 1, sizeof(char));
414 if (*pathRtrn != NULL)
415 strcpy(*pathRtrn, buf);