Organize xkbcomp/ header files
[platform/upstream/libxkbcommon.git] / src / xkbcomp / include.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 <errno.h>
28 #include <limits.h>
29 #include <stdio.h>
30
31 #include "xkbcomp-priv.h"
32 #include "text.h"
33 #include "include.h"
34
35 /**
36  * Extract the first token from an include statement.
37  * @param str_inout Input statement, modified in-place. Can be passed in
38  * repeatedly. If str_inout is NULL, the parsing has completed.
39  * @param file_rtrn Set to the include file to be used.
40  * @param map_rtrn Set to whatever comes after ), if any.
41  * @param nextop_rtrn Set to the next operation in the complete statement.
42  * @param extra_data Set to the string between ( and ), if any.
43  *
44  * @return true if parsing was succcessful, false for an illegal string.
45  *
46  * Example: "evdev+aliases(qwerty)"
47  *      str_inout = aliases(qwerty)
48  *      nextop_retrn = +
49  *      extra_data = NULL
50  *      file_rtrn = evdev
51  *      map_rtrn = NULL
52  *
53  * 2nd run with "aliases(qwerty)"
54  *      str_inout = NULL
55  *      file_rtrn = aliases
56  *      map_rtrn = qwerty
57  *      extra_data = NULL
58  *      nextop_retrn = ""
59  *
60  */
61 bool
62 ParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
63                 char *nextop_rtrn, char **extra_data)
64 {
65     char *tmp, *str, *next;
66
67     str = *str_inout;
68
69     /* search for tokens inside the string */
70     next = strpbrk(str, "|+");
71     if (next) {
72         /* set nextop_rtrn to \0, next to next character */
73         *nextop_rtrn = *next;
74         *next++ = '\0';
75     }
76     else {
77         *nextop_rtrn = '\0';
78         next = NULL;
79     }
80
81     /* search for :, store result in extra_data */
82     tmp = strchr(str, ':');
83     if (tmp != NULL) {
84         *tmp++ = '\0';
85         *extra_data = strdup(tmp);
86     }
87     else {
88         *extra_data = NULL;
89     }
90
91     tmp = strchr(str, '(');
92     if (tmp == NULL) {
93         *file_rtrn = strdup(str);
94         *map_rtrn = NULL;
95     }
96     else if (str[0] == '(') {
97         free(*extra_data);
98         return false;
99     }
100     else {
101         *tmp++ = '\0';
102         *file_rtrn = strdup(str);
103         str = tmp;
104         tmp = strchr(str, ')');
105         if ((tmp == NULL) || (tmp[1] != '\0')) {
106             free(*file_rtrn);
107             free(*extra_data);
108             return false;
109         }
110         *tmp++ = '\0';
111         *map_rtrn = strdup(str);
112     }
113
114     if (*nextop_rtrn == '\0')
115         *str_inout = NULL;
116     else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
117         *str_inout = next;
118     else
119         return false;
120
121     return true;
122 }
123
124 /***====================================================================***/
125
126 /**
127  * Return the xkb directory based on the type.
128  */
129 static const char *
130 DirectoryForInclude(enum xkb_file_type type)
131 {
132     switch (type) {
133     case FILE_TYPE_KEYMAP:
134         return "keymap";
135
136     case FILE_TYPE_KEYCODES:
137         return "keycodes";
138
139     case FILE_TYPE_TYPES:
140         return "types";
141
142     case FILE_TYPE_SYMBOLS:
143         return "symbols";
144
145     case FILE_TYPE_COMPAT:
146         return "compat";
147
148     case FILE_TYPE_GEOMETRY:
149         return "geometry";
150
151     case FILE_TYPE_RULES:
152         return "rules";
153
154     default:
155         return "";
156     }
157 }
158
159 /***====================================================================***/
160
161 /**
162  * Search for the given file name in the include directories.
163  *
164  * @param ctx the XKB ctx containing the include paths
165  * @param type one of FILE_TYPE_TYPES, FILE_TYPE_COMPAT, ..., or
166  *             FILE_TYPE_KEYMAP or FILE_TYPE_RULES
167  * @param pathRtrn is set to the full path of the file if found.
168  *
169  * @return an FD to the file or NULL. If NULL is returned, the value of
170  * pathRtrn is undefined.
171  */
172 FILE *
173 FindFileInXkbPath(struct xkb_context *ctx, const char *name,
174                   enum xkb_file_type type, char **pathRtrn)
175 {
176     size_t i;
177     int ret;
178     FILE *file = NULL;
179     char buf[PATH_MAX];
180     const char *typeDir;
181
182     typeDir = DirectoryForInclude(type);
183     for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
184         ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
185                        xkb_context_include_path_get(ctx, i), typeDir, name);
186         if (ret >= (ssize_t) sizeof(buf)) {
187             log_err(ctx, "File name (%s/%s/%s) too long\n",
188                     xkb_context_include_path_get(ctx, i), typeDir, name);
189             continue;
190         }
191         file = fopen(buf, "r");
192         if (file == NULL) {
193             log_err(ctx, "Couldn't open file (%s/%s/%s): %s\n",
194                     xkb_context_include_path_get(ctx, i), typeDir, name,
195                     strerror(errno));
196             continue;
197         }
198         break;
199     }
200
201     if ((file != NULL) && (pathRtrn != NULL))
202         *pathRtrn = strdup(buf);
203     return file;
204 }
205
206 /**
207  * Open the file given in the include statement and parse it's content.
208  * If the statement defines a specific map to use, this map is returned in
209  * file_rtrn. Otherwise, the default map is returned.
210  *
211  * @param ctx The ctx containing include paths
212  * @param stmt The include statement, specifying the file name to look for.
213  * @param file_type Type of file (FILE_TYPE_KEYCODES, etc.)
214  * @param file_rtrn Returns the key map to be used.
215  * @param merge_rtrn Always returns stmt->merge.
216  *
217  * @return true on success or false otherwise.
218  */
219 bool
220 ProcessIncludeFile(struct xkb_context *ctx,
221                    IncludeStmt * stmt,
222                    enum xkb_file_type file_type,
223                    XkbFile ** file_rtrn, enum merge_mode *merge_rtrn)
224 {
225     FILE *file;
226     XkbFile *rtrn, *mapToUse, *next;
227
228     file = FindFileInXkbPath(ctx, stmt->file, file_type, NULL);
229     if (file == NULL) {
230         log_err(ctx, "Can't find file \"%s\" for %s include\n", stmt->file,
231                 DirectoryForInclude(file_type));
232         return false;
233     }
234
235     if (!XkbParseFile(ctx, file, stmt->file, &rtrn)) {
236         log_err(ctx, "Error interpreting include file \"%s\"\n", stmt->file);
237         fclose(file);
238         return false;
239     }
240     fclose(file);
241
242     mapToUse = rtrn;
243     if (stmt->map != NULL) {
244         while (mapToUse)
245         {
246             next = (XkbFile *) mapToUse->common.next;
247             mapToUse->common.next = NULL;
248             if (streq(mapToUse->name, stmt->map) &&
249                 mapToUse->file_type == file_type) {
250                 FreeXkbFile(next);
251                 break;
252             }
253             else {
254                 FreeXkbFile(mapToUse);
255             }
256             mapToUse = next;
257         }
258         if (!mapToUse) {
259             log_err(ctx, "No %s named \"%s\" in the include file \"%s\"\n",
260                     FileTypeText(file_type), stmt->map, stmt->file);
261             return false;
262         }
263     }
264     else if (rtrn->common.next) {
265         log_lvl(ctx, 5,
266                 "No map in include statement, but \"%s\" contains several; "
267                 "Using first defined map, \"%s\"\n",
268                 stmt->file, rtrn->name);
269     }
270     if (mapToUse->file_type != file_type) {
271         log_err(ctx,
272                 "Include file wrong type (expected %s, got %s); "
273                 "Include file \"%s\" ignored\n",
274                 FileTypeText(file_type), FileTypeText(mapToUse->file_type),
275                 stmt->file);
276         return false;
277     }
278     /* FIXME: we have to check recursive includes here (or somewhere) */
279
280     *file_rtrn = mapToUse;
281     *merge_rtrn = stmt->merge;
282     return true;
283 }