Fix all -Wsign-compare warnings
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbpath.c
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
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.
15
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.
24
25  ********************************************************/
26
27 #include "utils.h"
28 #include <errno.h>
29 #include <stdlib.h>
30 #include "xkbpath.h"
31 #include "xkbcommon/xkbcommon.h"
32 #include "XKBcommonint.h"
33
34 #ifndef DFLT_XKB_CONFIG_ROOT
35 #define DFLT_XKB_CONFIG_ROOT    "/usr/lib/X11/xkb"
36 #endif
37
38 #ifndef PATH_MAX
39 #define PATH_MAX 1024
40 #endif
41
42 /* initial szPath */
43 #define PATH_CHUNK      8
44
45 static Bool noDefaultPath = False;
46 /* number of entries allocated for includePath */
47 static size_t szPath;
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;
52
53 /**
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.
61  *
62  * @return True if parsing was succcessful, False for an illegal string.
63  *
64  * Example: "evdev+aliases(qwerty)"
65  *      str_inout = aliases(qwerty)
66  *      nextop_retrn = +
67  *      extra_data = NULL
68  *      file_rtrn = evdev
69  *      map_rtrn = NULL
70  *
71  * 2nd run with "aliases(qwerty)"
72  *      str_inout = NULL
73  *      file_rtrn = aliases
74  *      map_rtrn = qwerty
75  *      extra_data = NULL
76  *      nextop_retrn = ""
77  *
78  */
79 Bool
80 XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
81                    char *nextop_rtrn, char **extra_data)
82 {
83     char *tmp, *str, *next;
84
85     str = *str_inout;
86     if ((*str == '+') || (*str == '|'))
87     {
88         *file_rtrn = *map_rtrn = NULL;
89         *nextop_rtrn = *str;
90         next = str + 1;
91     }
92     else if (*str == '%')
93     {
94         *file_rtrn = *map_rtrn = NULL;
95         *nextop_rtrn = str[1];
96         next = str + 2;
97     }
98     else
99     {
100         /* search for tokens inside the string */
101         next = strpbrk(str, "|+");
102         if (next)
103         {
104             /* set nextop_rtrn to \0, next to next character */
105             *nextop_rtrn = *next;
106             *next++ = '\0';
107         }
108         else
109         {
110             *nextop_rtrn = '\0';
111             next = NULL;
112         }
113         /* search for :, store result in extra_data */
114         tmp = strchr(str, ':');
115         if (tmp != NULL)
116         {
117             *tmp++ = '\0';
118             *extra_data = uDupString(tmp);
119         }
120         else
121         {
122             *extra_data = NULL;
123         }
124         tmp = strchr(str, '(');
125         if (tmp == NULL)
126         {
127             *file_rtrn = uDupString(str);
128             *map_rtrn = NULL;
129         }
130         else if (str[0] == '(')
131         {
132             free(*extra_data);
133             return False;
134         }
135         else
136         {
137             *tmp++ = '\0';
138             *file_rtrn = uDupString(str);
139             str = tmp;
140             tmp = strchr(str, ')');
141             if ((tmp == NULL) || (tmp[1] != '\0'))
142             {
143                 free(*file_rtrn);
144                 free(*extra_data);
145                 return False;
146             }
147             *tmp++ = '\0';
148             *map_rtrn = uDupString(str);
149         }
150     }
151     if (*nextop_rtrn == '\0')
152         *str_inout = NULL;
153     else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
154         *str_inout = next;
155     else
156         return False;
157     return True;
158 }
159
160 static void
161 XkbAddDefaultDirectoriesToPath(void);
162
163 /**
164  * Init memory for include paths.
165  */
166 static Bool
167 XkbInitIncludePath(void)
168 {
169     if (includePath)
170         return True;
171
172     szPath = PATH_CHUNK;
173     includePath = calloc(szPath, sizeof(char *));
174     if (!includePath)
175         return False;
176
177     XkbAddDefaultDirectoriesToPath();
178     return True;
179 }
180
181 /**
182  * Remove all entries from the global includePath.
183  */
184 static void
185 XkbClearIncludePath(void)
186 {
187     size_t i;
188
189     if (szPath > 0)
190     {
191         for (i = 0; i < nPathEntries; i++)
192         {
193             if (includePath[i] != NULL)
194             {
195                 free(includePath[i]);
196                 includePath[i] = NULL;
197             }
198         }
199         nPathEntries = 0;
200     }
201     noDefaultPath = True;
202 }
203
204 void
205 XkbFreeIncludePath(void)
206 {
207     XkbClearIncludePath();
208     free(includePath);
209     includePath = NULL;
210 }
211
212 /**
213  * Add the given path to the global includePath variable.
214  * If dir is NULL, the includePath is emptied.
215  */
216 static Bool
217 XkbAddDirectoryToPath(const char *dir)
218 {
219     int len;
220
221     if (!XkbInitIncludePath())
222         return False;
223
224     if ((dir == NULL) || (dir[0] == '\0'))
225     {
226         XkbClearIncludePath();
227         return True;
228     }
229 #ifdef __UNIXOS2__
230     dir = (char *) __XOS2RedirRoot(dir);
231 #endif
232     len = strlen(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",
236                dir, PATH_MAX - 3);
237         return False;
238     }
239     if (nPathEntries >= szPath)
240     {
241         szPath += PATH_CHUNK;
242         includePath = realloc(includePath, szPath * sizeof(char *));
243         if (includePath == NULL)
244         {
245             WSGO("Allocation failed (includePath)\n");
246             return False;
247         }
248     }
249     includePath[nPathEntries] = strdup(dir);
250     if (includePath[nPathEntries] == NULL)
251     {
252         WSGO("Allocation failed (includePath[%zd])\n", nPathEntries);
253         return False;
254     }
255     nPathEntries++;
256     return True;
257 }
258
259 static void
260 XkbAddDefaultDirectoriesToPath(void)
261 {
262     if (!XkbInitIncludePath())
263         return;
264     if (noDefaultPath)
265         return;
266     XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
267 }
268
269 /***====================================================================***/
270
271 /**
272  * Return the xkb directory based on the type.
273  */
274 const char *
275 XkbDirectoryForInclude(unsigned type)
276 {
277     switch (type)
278     {
279     case XkmSemanticsFile:
280         return "semantics";
281     case XkmLayoutFile:
282         return "layout";
283     case XkmKeymapFile:
284         return "keymap";
285     case XkmKeyNamesIndex:
286         return "keycodes";
287     case XkmTypesIndex:
288         return "types";
289     case XkmSymbolsIndex:
290         return "symbols";
291     case XkmCompatMapIndex:
292         return "compat";
293     case XkmGeometryFile:
294     case XkmGeometryIndex:
295         return "geometry";
296     case XkmRulesFile:
297         return "rules";
298     default:
299         return "";
300     }
301 }
302
303 /***====================================================================***/
304
305 /**
306  * Search for the given file name in the include directories.
307  *
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.
311  *
312  * @return an FD to the file or NULL. If NULL is returned, the value of
313  * pathRtrn is undefined.
314  */
315 FILE *
316 XkbFindFileInPath(const char *name, unsigned type, char **pathRtrn)
317 {
318     size_t i;
319     int ret;
320     FILE *file = NULL;
321     char buf[PATH_MAX];
322     const char *typeDir;
323
324     if (!XkbInitIncludePath())
325         return NULL;
326
327     typeDir = XkbDirectoryForInclude(type);
328     for (i = 0; i < nPathEntries; i++)
329     {
330         if (includePath[i] == NULL || *includePath[i] == '\0')
331             continue;
332
333         ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
334                        includePath[i], typeDir, name);
335         if (ret >= (ssize_t)sizeof(buf))
336         {
337             ERROR("File name (%s/%s/%s) too long\n", includePath[i],
338                    typeDir, name);
339             ACTION("Ignored\n");
340             continue;
341         }
342         file = fopen(buf, "r");
343         if (file == NULL) {
344             ERROR("Couldn't open file (%s/%s/%s): %s\n", includePath[i],
345                    typeDir, name, strerror(-errno));
346             ACTION("Ignored\n");
347             continue;
348         }
349         break;
350     }
351
352     if ((file != NULL) && (pathRtrn != NULL))
353         *pathRtrn = strdup(buf);
354     return file;
355 }