Remove file_id entirely
[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 <sys/types.h>
28 #include <sys/stat.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <unistd.h>
32
33 #include "xkbcommon/xkbcommon.h"
34 #include "utils.h"
35 #include "context.h"
36
37 struct xkb_context {
38     int refcnt;
39
40     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
41                                      enum xkb_log_level level,
42                                      const char *fmt, va_list args);
43     enum xkb_log_level log_level;
44     int log_verbosity;
45     void *user_data;
46
47     darray(char *) includes;
48     darray(char *) failed_includes;
49
50     struct atom_table *atom_table;
51
52     /* Buffer for the *Text() functions. */
53     char text_buffer[2048];
54     size_t text_next;
55 };
56
57 /**
58  * Append one directory to the context's include path.
59  */
60 XKB_EXPORT int
61 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
62 {
63     struct stat stat_buf;
64     int err;
65     char *tmp;
66
67     tmp = strdup(path);
68     if (!tmp)
69         goto err;
70
71     err = stat(path, &stat_buf);
72     if (err != 0)
73         goto err;
74     if (!S_ISDIR(stat_buf.st_mode))
75         goto err;
76
77 #if defined(HAVE_EACCESS)
78     if (eaccess(path, R_OK | X_OK) != 0)
79         goto err;
80 #elif defined(HAVE_EUIDACCESS)
81     if (euidaccess(path, R_OK | X_OK) != 0)
82         goto err;
83 #endif
84
85     darray_append(ctx->includes, tmp);
86     return 1;
87
88 err:
89     darray_append(ctx->failed_includes, tmp);
90     return 0;
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;
100     char *user_path;
101     int err;
102     int ret = 0;
103
104     ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
105
106     home = getenv("HOME");
107     if (!home)
108         return ret;
109     err = asprintf(&user_path, "%s/.xkb", home);
110     if (err <= 0)
111         return ret;
112     ret |= xkb_context_include_path_append(ctx, user_path);
113     free(user_path);
114
115     return ret;
116 }
117
118 /**
119  * Remove all entries in the context's include path.
120  */
121 XKB_EXPORT void
122 xkb_context_include_path_clear(struct xkb_context *ctx)
123 {
124     char **path;
125
126     darray_foreach(path, ctx->includes)
127         free(*path);
128     darray_free(ctx->includes);
129
130     darray_foreach(path, ctx->failed_includes)
131         free(*path);
132     darray_free(ctx->failed_includes);
133 }
134
135 /**
136  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
137  */
138 XKB_EXPORT int
139 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
140 {
141     xkb_context_include_path_clear(ctx);
142     return xkb_context_include_path_append_default(ctx);
143 }
144
145 /**
146  * Returns the number of entries in the context's include path.
147  */
148 XKB_EXPORT unsigned int
149 xkb_context_num_include_paths(struct xkb_context *ctx)
150 {
151     return darray_size(ctx->includes);
152 }
153
154 unsigned int
155 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
156 {
157     return darray_size(ctx->failed_includes);
158 }
159
160 /**
161  * Returns the given entry in the context's include path, or NULL if an
162  * invalid index is passed.
163  */
164 XKB_EXPORT const char *
165 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
166 {
167     if (idx >= xkb_context_num_include_paths(ctx))
168         return NULL;
169
170     return darray_item(ctx->includes, idx);
171 }
172
173 const char *
174 xkb_context_failed_include_path_get(struct xkb_context *ctx,
175                                     unsigned int idx)
176 {
177     if (idx >= xkb_context_num_failed_include_paths(ctx))
178         return NULL;
179
180     return darray_item(ctx->failed_includes, idx);
181 }
182
183 /**
184  * Take a new reference on the context.
185  */
186 XKB_EXPORT struct xkb_context *
187 xkb_context_ref(struct xkb_context *ctx)
188 {
189     ctx->refcnt++;
190     return ctx;
191 }
192
193 /**
194  * Drop an existing reference on the context, and free it if the refcnt is
195  * now 0.
196  */
197 XKB_EXPORT void
198 xkb_context_unref(struct xkb_context *ctx)
199 {
200     if (!ctx || --ctx->refcnt > 0)
201         return;
202
203     xkb_context_include_path_clear(ctx);
204     atom_table_free(ctx->atom_table);
205     free(ctx);
206 }
207
208 static const char *
209 log_level_to_prefix(enum xkb_log_level level)
210 {
211     switch (level) {
212     case XKB_LOG_LEVEL_DEBUG:
213         return "Debug:";
214     case XKB_LOG_LEVEL_INFO:
215         return "Info:";
216     case XKB_LOG_LEVEL_WARNING:
217         return "Warning:";
218     case XKB_LOG_LEVEL_ERROR:
219         return "Error:";
220     case XKB_LOG_LEVEL_CRITICAL:
221         return "Critical:";
222     default:
223         return NULL;
224     }
225 }
226
227 ATTR_PRINTF(3, 0) static void
228 default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
229                const char *fmt, va_list args)
230 {
231     const char *prefix = log_level_to_prefix(level);
232
233     if (prefix)
234         fprintf(stderr, "%-10s", prefix);
235     vfprintf(stderr, fmt, args);
236 }
237
238 static enum xkb_log_level
239 log_level(const char *level) {
240     char *endptr;
241     enum xkb_log_level lvl;
242
243     errno = 0;
244     lvl = strtol(level, &endptr, 10);
245     if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
246         return lvl;
247     if (istreq_prefix("crit", level))
248         return XKB_LOG_LEVEL_CRITICAL;
249     if (istreq_prefix("err", level))
250         return XKB_LOG_LEVEL_ERROR;
251     if (istreq_prefix("warn", level))
252         return XKB_LOG_LEVEL_WARNING;
253     if (istreq_prefix("info", level))
254         return XKB_LOG_LEVEL_INFO;
255     if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
256         return XKB_LOG_LEVEL_DEBUG;
257
258     return XKB_LOG_LEVEL_ERROR;
259 }
260
261 static int
262 log_verbosity(const char *verbosity) {
263     char *endptr;
264     int v;
265
266     errno = 0;
267     v = strtol(verbosity, &endptr, 10);
268     if (errno == 0)
269         return v;
270
271     return 0;
272 }
273
274 /**
275  * Create a new context.
276  */
277 XKB_EXPORT struct xkb_context *
278 xkb_context_new(enum xkb_context_flags flags)
279 {
280     const char *env;
281     struct xkb_context *ctx = calloc(1, sizeof(*ctx));
282
283     if (!ctx)
284         return NULL;
285
286     ctx->refcnt = 1;
287     ctx->log_fn = default_log_fn;
288     ctx->log_level = XKB_LOG_LEVEL_ERROR;
289     ctx->log_verbosity = 0;
290
291     /* Environment overwrites defaults. */
292     env = getenv("XKB_LOG_LEVEL");
293     if (env)
294         xkb_context_set_log_level(ctx, log_level(env));
295
296     env = getenv("XKB_LOG_VERBOSITY");
297     if (env)
298         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
299
300     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
301         !xkb_context_include_path_append_default(ctx)) {
302         log_err(ctx, "failed to add default include path %s\n",
303                 DFLT_XKB_CONFIG_ROOT);
304         xkb_context_unref(ctx);
305         return NULL;
306     }
307
308     ctx->atom_table = atom_table_new();
309     if (!ctx->atom_table) {
310         xkb_context_unref(ctx);
311         return NULL;
312     }
313
314     return ctx;
315 }
316
317 xkb_atom_t
318 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
319 {
320     return atom_lookup(ctx->atom_table, string);
321 }
322
323 xkb_atom_t
324 xkb_atom_intern(struct xkb_context *ctx, const char *string)
325 {
326     return atom_intern(ctx->atom_table, string, false);
327 }
328
329 xkb_atom_t
330 xkb_atom_steal(struct xkb_context *ctx, char *string)
331 {
332     return atom_intern(ctx->atom_table, string, true);
333 }
334
335 char *
336 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
337 {
338     return atom_strdup(ctx->atom_table, atom);
339 }
340
341 const char *
342 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
343 {
344     return atom_text(ctx->atom_table, atom);
345 }
346
347 void
348 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
349         const char *fmt, ...)
350 {
351     va_list args;
352
353     va_start(args, fmt);
354     ctx->log_fn(ctx, level, fmt, args);
355     va_end(args);
356 }
357
358 XKB_EXPORT void
359 xkb_context_set_log_fn(struct xkb_context *ctx,
360                        void (*log_fn)(struct xkb_context *ctx,
361                                       enum xkb_log_level level,
362                                       const char *fmt, va_list args))
363 {
364     ctx->log_fn = (log_fn ? log_fn : default_log_fn);
365 }
366
367 XKB_EXPORT enum xkb_log_level
368 xkb_context_get_log_level(struct xkb_context *ctx)
369 {
370     return ctx->log_level;
371 }
372
373 XKB_EXPORT void
374 xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
375 {
376     ctx->log_level = level;
377 }
378
379 XKB_EXPORT int
380 xkb_context_get_log_verbosity(struct xkb_context *ctx)
381 {
382     return ctx->log_verbosity;
383 }
384
385 XKB_EXPORT void
386 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
387 {
388     ctx->log_verbosity = verbosity;
389 }
390
391 XKB_EXPORT void *
392 xkb_context_get_user_data(struct xkb_context *ctx)
393 {
394     if (ctx)
395         return ctx->user_data;
396     return NULL;
397 }
398
399 XKB_EXPORT void
400 xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
401 {
402     ctx->user_data = user_data;
403 }
404
405 char *
406 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
407 {
408     char *rtrn;
409
410     if (size >= sizeof(ctx->text_buffer))
411         return NULL;
412
413     if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
414         ctx->text_next = 0;
415
416     rtrn = &ctx->text_buffer[ctx->text_next];
417     ctx->text_next += size;
418
419     return rtrn;
420 }