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>
33 #ifndef DFLT_XKB_CONFIG_ROOT
34 #define DFLT_XKB_CONFIG_ROOT "/usr/lib/X11/xkb"
41 #define PATH_CHUNK 8 /* initial szPath */
43 static Bool noDefaultPath = False;
44 static int szPath; /* number of entries allocated for includePath */
45 static int nPathEntries; /* number of actual entries in includePath */
46 static char **includePath; /* Holds all directories we might be including data from */
49 * Extract the first token from an include statement.
50 * @param str_inout Input statement, modified in-place. Can be passed in
51 * repeatedly. If str_inout is NULL, the parsing has completed.
52 * @param file_rtrn Set to the include file to be used.
53 * @param map_rtrn Set to whatever comes after ), if any.
54 * @param nextop_rtrn Set to the next operation in the complete statement.
55 * @param extra_data Set to the string between ( and ), if any.
57 * @return True if parsing was succcessful, False for an illegal string.
59 * Example: "evdev+aliases(qwerty)"
60 * str_inout = aliases(qwerty)
66 * 2nd run with "aliases(qwerty)"
75 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
76 char *nextop_rtrn, char **extra_data)
78 char *tmp, *str, *next;
81 if ((*str == '+') || (*str == '|'))
83 *file_rtrn = *map_rtrn = NULL;
89 *file_rtrn = *map_rtrn = NULL;
90 *nextop_rtrn = str[1];
95 /* search for tokens inside the string */
96 next = strpbrk(str, "|+");
99 /* set nextop_rtrn to \0, next to next character */
100 *nextop_rtrn = *next;
108 /* search for :, store result in extra_data */
109 tmp = strchr(str, ':');
113 *extra_data = uStringDup(tmp);
119 tmp = strchr(str, '(');
122 *file_rtrn = uStringDup(str);
125 else if (str[0] == '(')
133 *file_rtrn = uStringDup(str);
135 tmp = strchr(str, ')');
136 if ((tmp == NULL) || (tmp[1] != '\0'))
143 *map_rtrn = uStringDup(str);
146 if (*nextop_rtrn == '\0')
148 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
156 * Init memory for include paths.
159 XkbInitIncludePath(void)
162 includePath = (char **) calloc(szPath, sizeof(char *));
163 if (includePath == NULL)
169 XkbAddDefaultDirectoriesToPath(void)
173 XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
177 * Remove all entries from the global includePath.
180 XkbClearIncludePath(void)
186 for (i = 0; i < nPathEntries; i++)
188 if (includePath[i] != NULL)
190 uFree(includePath[i]);
191 includePath[i] = NULL;
196 noDefaultPath = True;
201 * Add the given path to the global includePath variable.
202 * If dir is NULL, the includePath is emptied.
205 XkbAddDirectoryToPath(const char *dir)
208 if ((dir == NULL) || (dir[0] == '\0'))
210 XkbClearIncludePath();
214 dir = (char *) __XOS2RedirRoot(dir);
217 if (len + 2 >= PATH_MAX)
218 { /* allow for '/' and at least one character */
219 ERROR2("Path entry (%s) too long (maxiumum length is %d)\n",
223 if (nPathEntries >= szPath)
225 szPath += PATH_CHUNK;
226 includePath = (char **) realloc(includePath, szPath * sizeof(char *));
227 if (includePath == NULL)
229 WSGO("Allocation failed (includePath)\n");
233 includePath[nPathEntries] =
234 (char *) calloc(strlen(dir) + 1, sizeof(char));
235 if (includePath[nPathEntries] == NULL)
237 WSGO1("Allocation failed (includePath[%d])\n", nPathEntries);
240 strcpy(includePath[nPathEntries++], dir);
244 /***====================================================================***/
247 * Return the xkb directory based on the type.
248 * Do not free the memory returned by this function.
251 XkbDirectoryForInclude(unsigned type)
257 case XkmSemanticsFile:
258 strcpy(buf, "semantics");
261 strcpy(buf, "layout");
264 strcpy(buf, "keymap");
266 case XkmKeyNamesIndex:
267 strcpy(buf, "keycodes");
270 strcpy(buf, "types");
272 case XkmSymbolsIndex:
273 strcpy(buf, "symbols");
275 case XkmCompatMapIndex:
276 strcpy(buf, "compat");
278 case XkmGeometryFile:
279 case XkmGeometryIndex:
280 strcpy(buf, "geometry");
289 /***====================================================================***/
291 typedef struct _FileCacheEntry
297 struct _FileCacheEntry *next;
299 static FileCacheEntry *fileCache;
302 * Add the file with the given name to the internal cache to avoid opening and
303 * parsing the file multiple times. If a cache entry for the same name + type
304 * is already present, the entry is overwritten and the data belonging to the
305 * previous entry is returned.
307 * @parameter name The name of the file (e.g. evdev).
308 * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
309 * @parameter path The full path to the file.
310 * @parameter data Already parsed data.
312 * @return The data from the overwritten file or NULL.
315 XkbAddFileToCache(char *name, unsigned type, char *path, void *data)
317 FileCacheEntry *entry;
319 for (entry = fileCache; entry != NULL; entry = entry->next)
321 if ((type == entry->type) && (uStringEqual(name, entry->name)))
323 void *old = entry->data;
324 WSGO2("Replacing file cache entry (%s/%d)\n", name, type);
330 entry = uTypedAlloc(FileCacheEntry);
337 entry->next = fileCache;
344 * Search for the given name + type in the cache.
346 * @parameter name The name of the file (e.g. evdev).
347 * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
348 * @parameter pathRtrn Set to the full path of the given entry.
350 * @return the data from the cache entry or NULL if no matching entry was found.
353 XkbFindFileInCache(char *name, unsigned type, char **pathRtrn)
355 FileCacheEntry *entry;
357 for (entry = fileCache; entry != NULL; entry = entry->next)
359 if ((type == entry->type) && (uStringEqual(name, entry->name)))
361 *pathRtrn = entry->path;
368 /***====================================================================***/
371 * Search for the given file name in the include directories.
373 * @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
374 * XkbSemanticsFile, XkmKeymapFile, ...
375 * @param pathReturn is set to the full path of the file if found.
377 * @return an FD to the file or NULL. If NULL is returned, the value of
378 * pathRtrn is undefined.
381 XkbFindFileInPath(char *name, unsigned type, char **pathRtrn)
385 int nameLen, typeLen, pathLen;
386 char buf[PATH_MAX], *typeDir;
388 typeDir = XkbDirectoryForInclude(type);
389 nameLen = strlen(name);
390 typeLen = strlen(typeDir);
391 for (i = 0; i < nPathEntries; i++)
393 pathLen = strlen(includePath[i]);
397 if ((nameLen + typeLen + pathLen + 2) >= PATH_MAX)
399 ERROR3("File name (%s/%s/%s) too long\n", includePath[i],
404 snprintf(buf, sizeof(buf), "%s/%s/%s", includePath[i], typeDir, name);
405 file = fopen(buf, "r");
410 if ((file != NULL) && (pathRtrn != NULL))
412 *pathRtrn = (char *) calloc(strlen(buf) + 1, sizeof(char));
413 if (*pathRtrn != NULL)
414 strcpy(*pathRtrn, buf);