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
32 #include "xkbcommon/xkbcommon.h"
33 #include "XKBcommonint.h"
35 #ifndef DFLT_XKB_CONFIG_ROOT
36 #define DFLT_XKB_CONFIG_ROOT "/usr/lib/X11/xkb"
46 static Bool noDefaultPath = False;
47 /* number of entries allocated for includePath */
49 /* number of actual entries in includePath */
50 static int nPathEntries;
51 /* Holds all directories we might be including data from */
52 static char **includePath = NULL;
55 * Extract the first token from an include statement.
56 * @param str_inout Input statement, modified in-place. Can be passed in
57 * repeatedly. If str_inout is NULL, the parsing has completed.
58 * @param file_rtrn Set to the include file to be used.
59 * @param map_rtrn Set to whatever comes after ), if any.
60 * @param nextop_rtrn Set to the next operation in the complete statement.
61 * @param extra_data Set to the string between ( and ), if any.
63 * @return True if parsing was succcessful, False for an illegal string.
65 * Example: "evdev+aliases(qwerty)"
66 * str_inout = aliases(qwerty)
72 * 2nd run with "aliases(qwerty)"
81 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
82 char *nextop_rtrn, char **extra_data)
84 char *tmp, *str, *next;
87 if ((*str == '+') || (*str == '|'))
89 *file_rtrn = *map_rtrn = NULL;
95 *file_rtrn = *map_rtrn = NULL;
96 *nextop_rtrn = str[1];
101 /* search for tokens inside the string */
102 next = strpbrk(str, "|+");
105 /* set nextop_rtrn to \0, next to next character */
106 *nextop_rtrn = *next;
114 /* search for :, store result in extra_data */
115 tmp = strchr(str, ':');
119 *extra_data = _XkbDupString(tmp);
125 tmp = strchr(str, '(');
128 *file_rtrn = _XkbDupString(str);
131 else if (str[0] == '(')
139 *file_rtrn = _XkbDupString(str);
141 tmp = strchr(str, ')');
142 if ((tmp == NULL) || (tmp[1] != '\0'))
149 *map_rtrn = _XkbDupString(str);
152 if (*nextop_rtrn == '\0')
154 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
162 XkbAddDefaultDirectoriesToPath(void);
165 * Init memory for include paths.
168 XkbInitIncludePath(void)
174 includePath = calloc(szPath, sizeof(char *));
178 XkbAddDefaultDirectoriesToPath();
183 * Remove all entries from the global includePath.
186 XkbClearIncludePath(void)
192 for (i = 0; i < nPathEntries; i++)
194 if (includePath[i] != NULL)
196 free(includePath[i]);
197 includePath[i] = NULL;
202 noDefaultPath = True;
206 XkbFreeIncludePath(void)
208 XkbClearIncludePath();
214 * Add the given path to the global includePath variable.
215 * If dir is NULL, the includePath is emptied.
218 XkbAddDirectoryToPath(const char *dir)
222 if (!XkbInitIncludePath())
225 if ((dir == NULL) || (dir[0] == '\0'))
227 XkbClearIncludePath();
231 dir = (char *) __XOS2RedirRoot(dir);
234 if (len + 2 >= PATH_MAX)
235 { /* allow for '/' and at least one character */
236 ERROR("Path entry (%s) too long (maxiumum length is %d)\n",
240 if (nPathEntries >= szPath)
242 szPath += PATH_CHUNK;
243 includePath = realloc(includePath, szPath * sizeof(char *));
244 if (includePath == NULL)
246 WSGO("Allocation failed (includePath)\n");
250 includePath[nPathEntries] = strdup(dir);
251 if (includePath[nPathEntries] == NULL)
253 WSGO("Allocation failed (includePath[%d])\n", nPathEntries);
261 XkbAddDefaultDirectoriesToPath(void)
263 if (!XkbInitIncludePath())
267 XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
270 /***====================================================================***/
273 * Return the xkb directory based on the type.
276 XkbDirectoryForInclude(unsigned type)
280 case XkmSemanticsFile:
286 case XkmKeyNamesIndex:
290 case XkmSymbolsIndex:
292 case XkmCompatMapIndex:
294 case XkmGeometryFile:
295 case XkmGeometryIndex:
304 /***====================================================================***/
307 * Search for the given file name in the include directories.
309 * @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
310 * XkbSemanticsFile, XkmKeymapFile, ...
311 * @param pathReturn is set to the full path of the file if found.
313 * @return an FD to the file or NULL. If NULL is returned, the value of
314 * pathRtrn is undefined.
317 XkbFindFileInPath(const char *name, unsigned type, char **pathRtrn)
324 if (!XkbInitIncludePath())
327 typeDir = XkbDirectoryForInclude(type);
328 for (i = 0; i < nPathEntries; i++)
330 if (includePath[i] == NULL || *includePath[i] == '\0')
333 ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
334 includePath[i], typeDir, name);
335 if (ret >= sizeof(buf))
337 ERROR("File name (%s/%s/%s) too long\n", includePath[i],
342 file = fopen(buf, "r");
344 ERROR("Couldn't open file (%s/%s/%s): %s\n", includePath[i],
345 typeDir, name, strerror(-errno));
352 if ((file != NULL) && (pathRtrn != NULL))
353 *pathRtrn = strdup(buf);