context: Maintain list of failed include paths
[platform/upstream/libxkbcommon.git] / src / context.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <syslog.h>
34
35 #include "xkb-priv.h"
36 #include "atom.h"
37
38 struct xkb_context {
39     int refcnt;
40
41     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx, int priority,
42                                      const char *fmt, va_list args);
43     int log_priority;
44     int log_verbosity;
45     void *user_data;
46
47     darray(char *) includes;
48     darray(char *) failed_includes;
49
50     /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
51     unsigned file_id;
52
53     struct atom_table *atom_table;
54 };
55
56 /**
57  * Append one directory to the context's include path.
58  */
59 XKB_EXPORT int
60 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
61 {
62     struct stat stat_buf;
63     int err;
64     char *tmp;
65
66     tmp = strdup(path);
67     if (!tmp)
68         goto err;
69
70     err = stat(path, &stat_buf);
71     if (err != 0)
72         goto err;
73     if (!S_ISDIR(stat_buf.st_mode))
74         goto err;
75
76 #if defined(HAVE_EACCESS)
77     if (eaccess(path, R_OK | X_OK) != 0)
78         goto err;
79 #elif defined(HAVE_EUIDACCESS)
80     if (euidaccess(path, R_OK | X_OK) != 0)
81         goto err;
82 #endif
83
84     darray_append(ctx->includes, tmp);
85     return 1;
86
87 err:
88     darray_append(ctx->failed_includes, tmp);
89     return 0;
90 }
91
92 /**
93  * Append the default include directories to the context.
94  */
95 XKB_EXPORT int
96 xkb_context_include_path_append_default(struct xkb_context *ctx)
97 {
98     const char *home;
99     char *user_path;
100     int err;
101     int ret = 0;
102
103     ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
104
105     home = getenv("HOME");
106     if (!home)
107         return ret;
108     err = asprintf(&user_path, "%s/.xkb", home);
109     if (err <= 0)
110         return ret;
111     ret |= xkb_context_include_path_append(ctx, user_path);
112     free(user_path);
113
114     return ret;
115 }
116
117 /**
118  * Remove all entries in the context's include path.
119  */
120 XKB_EXPORT void
121 xkb_context_include_path_clear(struct xkb_context *ctx)
122 {
123     char **path;
124
125     darray_foreach(path, ctx->includes)
126         free(*path);
127     darray_free(ctx->includes);
128
129     darray_foreach(path, ctx->failed_includes)
130         free(*path);
131     darray_free(ctx->failed_includes);
132 }
133
134 /**
135  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
136  */
137 XKB_EXPORT int
138 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
139 {
140     xkb_context_include_path_clear(ctx);
141     return xkb_context_include_path_append_default(ctx);
142 }
143
144 /**
145  * Returns the number of entries in the context's include path.
146  */
147 XKB_EXPORT unsigned int
148 xkb_context_num_include_paths(struct xkb_context *ctx)
149 {
150     return darray_size(ctx->includes);
151 }
152
153 /**
154  * Returns the given entry in the context's include path, or NULL if an
155  * invalid index is passed.
156  */
157 XKB_EXPORT const char *
158 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
159 {
160     if (idx >= xkb_context_num_include_paths(ctx))
161         return NULL;
162
163     return darray_item(ctx->includes, idx);
164 }
165
166 unsigned
167 xkb_context_take_file_id(struct xkb_context *ctx)
168 {
169     return ctx->file_id++;
170 }
171
172 /**
173  * Take a new reference on the context.
174  */
175 XKB_EXPORT struct xkb_context *
176 xkb_context_ref(struct xkb_context *ctx)
177 {
178     ctx->refcnt++;
179     return ctx;
180 }
181
182 /**
183  * Drop an existing reference on the context, and free it if the refcnt is
184  * now 0.
185  */
186 XKB_EXPORT void
187 xkb_context_unref(struct xkb_context *ctx)
188 {
189     if (--ctx->refcnt > 0)
190         return;
191
192     xkb_context_include_path_clear(ctx);
193     atom_table_free(ctx->atom_table);
194     free(ctx);
195 }
196
197 static const char *
198 priority_to_prefix(int priority)
199 {
200     switch (priority) {
201     case LOG_DEBUG:
202         return "Debug:";
203     case LOG_INFO:
204         return "Info:";
205     case LOG_WARNING:
206         return "Warning:";
207     case LOG_ERR:
208         return "Error:";
209     case LOG_CRIT:
210     case LOG_ALERT:
211     case LOG_EMERG:
212         return "Internal error:";
213     default:
214         return NULL;
215     }
216 }
217
218 ATTR_PRINTF(3, 0) static void
219 default_log_fn(struct xkb_context *ctx, int priority,
220                const char *fmt, va_list args)
221 {
222     const char *prefix = priority_to_prefix(priority);
223
224     if (prefix)
225         fprintf(stderr, "%-15s", prefix);
226     vfprintf(stderr, fmt, args);
227 }
228
229 static int
230 log_priority(const char *priority) {
231     char *endptr;
232     int prio;
233
234     errno = 0;
235     prio = strtol(priority, &endptr, 10);
236     if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
237         return prio;
238     if (strncasecmp(priority, "err", 3) == 0)
239         return LOG_ERR;
240     if (strncasecmp(priority, "warn", 4) == 0)
241         return LOG_WARNING;
242     if (strncasecmp(priority, "info", 4) == 0)
243         return LOG_INFO;
244     if (strncasecmp(priority, "debug", 5) == 0)
245         return LOG_DEBUG;
246
247     return LOG_ERR;
248 }
249
250 static int
251 log_verbosity(const char *verbosity) {
252     char *endptr;
253     int v;
254
255     errno = 0;
256     v = strtol(verbosity, &endptr, 10);
257     if (errno == 0)
258         return v;
259
260     return 0;
261 }
262
263 /**
264  * Create a new context.
265  */
266 XKB_EXPORT struct xkb_context *
267 xkb_context_new(enum xkb_context_flags flags)
268 {
269     const char *env;
270     struct xkb_context *ctx = calloc(1, sizeof(*ctx));
271
272     if (!ctx)
273         return NULL;
274
275     ctx->refcnt = 1;
276     ctx->log_fn = default_log_fn;
277     ctx->log_priority = LOG_ERR;
278     ctx->log_verbosity = 0;
279
280     /* Environment overwrites defaults. */
281     env = getenv("XKB_LOG");
282     if (env)
283         xkb_set_log_priority(ctx, log_priority(env));
284
285     env = getenv("XKB_VERBOSITY");
286     if (env)
287         xkb_set_log_verbosity(ctx, log_verbosity(env));
288
289     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
290         !xkb_context_include_path_append_default(ctx)) {
291         log_err(ctx, "failed to add default include path %s\n",
292                 DFLT_XKB_CONFIG_ROOT);
293         xkb_context_unref(ctx);
294         return NULL;
295     }
296
297     ctx->atom_table = atom_table_new();
298     if (!ctx->atom_table) {
299         xkb_context_unref(ctx);
300         return NULL;
301     }
302
303     return ctx;
304 }
305
306 xkb_atom_t
307 xkb_atom_intern(struct xkb_context *ctx, const char *string)
308 {
309     return atom_intern(ctx->atom_table, string, false);
310 }
311
312 xkb_atom_t
313 xkb_atom_steal(struct xkb_context *ctx, char *string)
314 {
315     return atom_intern(ctx->atom_table, string, true);
316 }
317
318 char *
319 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
320 {
321     return atom_strdup(ctx->atom_table, atom);
322 }
323
324 const char *
325 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
326 {
327     return atom_text(ctx->atom_table, atom);
328 }
329
330 void
331 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...)
332 {
333     va_list args;
334
335     va_start(args, fmt);
336     ctx->log_fn(ctx, priority, fmt, args);
337     va_end(args);
338 }
339
340 XKB_EXPORT void
341 xkb_set_log_fn(struct xkb_context *ctx,
342                void (*log_fn)(struct xkb_context *ctx, int priority,
343                               const char *fmt, va_list args))
344 {
345     ctx->log_fn = (log_fn ? log_fn : default_log_fn);
346 }
347
348 XKB_EXPORT int
349 xkb_get_log_priority(struct xkb_context *ctx)
350 {
351     return ctx->log_priority;
352 }
353
354 XKB_EXPORT void
355 xkb_set_log_priority(struct xkb_context *ctx, int priority)
356 {
357     ctx->log_priority = priority;
358 }
359
360 XKB_EXPORT int
361 xkb_get_log_verbosity(struct xkb_context *ctx)
362 {
363     return ctx->log_verbosity;
364 }
365
366 XKB_EXPORT void
367 xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity)
368 {
369     ctx->log_verbosity = verbosity;
370 }
371
372 XKB_EXPORT void *
373 xkb_get_user_data(struct xkb_context *ctx)
374 {
375     if (ctx)
376         return ctx->user_data;
377     return NULL;
378 }
379
380 XKB_EXPORT void
381 xkb_set_user_data(struct xkb_context *ctx, void *user_data)
382 {
383     ctx->user_data = user_data;
384 }