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 "xkbcommon/xkbcommon.h"
32 #include "XKBcommonint.h"
34 #ifndef DFLT_XKB_CONFIG_ROOT
35 #define DFLT_XKB_CONFIG_ROOT "/usr/lib/X11/xkb"
45 static Bool noDefaultPath = False;
46 /* number of entries allocated for includePath */
48 /* number of actual entries in includePath */
49 static size_t nPathEntries;
50 /* Holds all directories we might be including data from */
51 static char **includePath = NULL;
54 * Extract the first token from an include statement.
55 * @param str_inout Input statement, modified in-place. Can be passed in
56 * repeatedly. If str_inout is NULL, the parsing has completed.
57 * @param file_rtrn Set to the include file to be used.
58 * @param map_rtrn Set to whatever comes after ), if any.
59 * @param nextop_rtrn Set to the next operation in the complete statement.
60 * @param extra_data Set to the string between ( and ), if any.
62 * @return True if parsing was succcessful, False for an illegal string.
64 * Example: "evdev+aliases(qwerty)"
65 * str_inout = aliases(qwerty)
71 * 2nd run with "aliases(qwerty)"
80 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
81 char *nextop_rtrn, char **extra_data)
83 char *tmp, *str, *next;
86 if ((*str == '+') || (*str == '|'))
88 *file_rtrn = *map_rtrn = NULL;
94 *file_rtrn = *map_rtrn = NULL;
95 *nextop_rtrn = str[1];
100 /* search for tokens inside the string */
101 next = strpbrk(str, "|+");
104 /* set nextop_rtrn to \0, next to next character */
105 *nextop_rtrn = *next;
113 /* search for :, store result in extra_data */
114 tmp = strchr(str, ':');
118 *extra_data = uDupString(tmp);
124 tmp = strchr(str, '(');
127 *file_rtrn = uDupString(str);
130 else if (str[0] == '(')
138 *file_rtrn = uDupString(str);
140 tmp = strchr(str, ')');
141 if ((tmp == NULL) || (tmp[1] != '\0'))
148 *map_rtrn = uDupString(str);
151 if (*nextop_rtrn == '\0')
153 else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
161 XkbAddDefaultDirectoriesToPath(void);
164 * Init memory for include paths.
167 XkbInitIncludePath(void)
173 includePath = calloc(szPath, sizeof(char *));
177 XkbAddDefaultDirectoriesToPath();
182 * Remove all entries from the global includePath.
185 XkbClearIncludePath(void)
191 for (i = 0; i < nPathEntries; i++)
193 if (includePath[i] != NULL)
195 free(includePath[i]);
196 includePath[i] = NULL;
201 noDefaultPath = True;
205 XkbFreeIncludePath(void)
207 XkbClearIncludePath();
213 * Add the given path to the global includePath variable.
214 * If dir is NULL, the includePath is emptied.
217 XkbAddDirectoryToPath(const char *dir)
221 if (!XkbInitIncludePath())
224 if ((dir == NULL) || (dir[0] == '\0'))
226 XkbClearIncludePath();
230 dir = (char *) __XOS2RedirRoot(dir);
233 if (len + 2 >= PATH_MAX)
234 { /* allow for '/' and at least one character */
235 ERROR("Path entry (%s) too long (maxiumum length is %d)\n",
239 if (nPathEntries >= szPath)
241 szPath += PATH_CHUNK;
242 includePath = realloc(includePath, szPath * sizeof(char *));
243 if (includePath == NULL)
245 WSGO("Allocation failed (includePath)\n");
249 includePath[nPathEntries] = strdup(dir);
250 if (includePath[nPathEntries] == NULL)
252 WSGO("Allocation failed (includePath[%zd])\n", nPathEntries);
260 XkbAddDefaultDirectoriesToPath(void)
262 if (!XkbInitIncludePath())
266 XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
269 /***====================================================================***/
272 * Return the xkb directory based on the type.
275 XkbDirectoryForInclude(unsigned type)
279 case XkmSemanticsFile:
285 case XkmKeyNamesIndex:
289 case XkmSymbolsIndex:
291 case XkmCompatMapIndex:
293 case XkmGeometryFile:
294 case XkmGeometryIndex:
303 /***====================================================================***/
306 * Search for the given file name in the include directories.
308 * @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
309 * XkbSemanticsFile, XkmKeymapFile, ...
310 * @param pathReturn is set to the full path of the file if found.
312 * @return an FD to the file or NULL. If NULL is returned, the value of
313 * pathRtrn is undefined.
316 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 >= (ssize_t)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);