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