log: change the log prefixes to be more library-like
[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     struct xkb_rule_names names_dflt;
48
49     darray(char *) includes;
50     darray(char *) failed_includes;
51
52     struct atom_table *atom_table;
53
54     /* Buffer for the *Text() functions. */
55     char text_buffer[2048];
56     size_t text_next;
57
58     unsigned int use_environment_names : 1;
59 };
60
61 /**
62  * Append one directory to the context's include path.
63  */
64 XKB_EXPORT int
65 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
66 {
67     struct stat stat_buf;
68     int err;
69     char *tmp;
70
71     tmp = strdup(path);
72     if (!tmp)
73         goto err;
74
75     err = stat(path, &stat_buf);
76     if (err != 0)
77         goto err;
78     if (!S_ISDIR(stat_buf.st_mode))
79         goto err;
80
81 #if defined(HAVE_EACCESS)
82     if (eaccess(path, R_OK | X_OK) != 0)
83         goto err;
84 #elif defined(HAVE_EUIDACCESS)
85     if (euidaccess(path, R_OK | X_OK) != 0)
86         goto err;
87 #endif
88
89     darray_append(ctx->includes, tmp);
90     return 1;
91
92 err:
93     darray_append(ctx->failed_includes, tmp);
94     return 0;
95 }
96
97 /**
98  * Append the default include directories to the context.
99  */
100 XKB_EXPORT int
101 xkb_context_include_path_append_default(struct xkb_context *ctx)
102 {
103     const char *home;
104     char *user_path;
105     int err;
106     int ret = 0;
107
108     ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
109
110     home = getenv("HOME");
111     if (!home)
112         return ret;
113     err = asprintf(&user_path, "%s/.xkb", home);
114     if (err <= 0)
115         return ret;
116     ret |= xkb_context_include_path_append(ctx, user_path);
117     free(user_path);
118
119     return ret;
120 }
121
122 /**
123  * Remove all entries in the context's include path.
124  */
125 XKB_EXPORT void
126 xkb_context_include_path_clear(struct xkb_context *ctx)
127 {
128     char **path;
129
130     darray_foreach(path, ctx->includes)
131         free(*path);
132     darray_free(ctx->includes);
133
134     darray_foreach(path, ctx->failed_includes)
135         free(*path);
136     darray_free(ctx->failed_includes);
137 }
138
139 /**
140  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
141  */
142 XKB_EXPORT int
143 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
144 {
145     xkb_context_include_path_clear(ctx);
146     return xkb_context_include_path_append_default(ctx);
147 }
148
149 /**
150  * Returns the number of entries in the context's include path.
151  */
152 XKB_EXPORT unsigned int
153 xkb_context_num_include_paths(struct xkb_context *ctx)
154 {
155     return darray_size(ctx->includes);
156 }
157
158 unsigned int
159 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
160 {
161     return darray_size(ctx->failed_includes);
162 }
163
164 /**
165  * Returns the given entry in the context's include path, or NULL if an
166  * invalid index is passed.
167  */
168 XKB_EXPORT const char *
169 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
170 {
171     if (idx >= xkb_context_num_include_paths(ctx))
172         return NULL;
173
174     return darray_item(ctx->includes, idx);
175 }
176
177 const char *
178 xkb_context_failed_include_path_get(struct xkb_context *ctx,
179                                     unsigned int idx)
180 {
181     if (idx >= xkb_context_num_failed_include_paths(ctx))
182         return NULL;
183
184     return darray_item(ctx->failed_includes, idx);
185 }
186
187 /**
188  * Take a new reference on the context.
189  */
190 XKB_EXPORT struct xkb_context *
191 xkb_context_ref(struct xkb_context *ctx)
192 {
193     ctx->refcnt++;
194     return ctx;
195 }
196
197 /**
198  * Drop an existing reference on the context, and free it if the refcnt is
199  * now 0.
200  */
201 XKB_EXPORT void
202 xkb_context_unref(struct xkb_context *ctx)
203 {
204     if (!ctx || --ctx->refcnt > 0)
205         return;
206
207     xkb_context_include_path_clear(ctx);
208     atom_table_free(ctx->atom_table);
209     free(ctx);
210 }
211
212 static const char *
213 log_level_to_prefix(enum xkb_log_level level)
214 {
215     switch (level) {
216     case XKB_LOG_LEVEL_DEBUG:
217         return "xkbcommon: DEBUG: ";
218     case XKB_LOG_LEVEL_INFO:
219         return "xkbcommon: INFO: ";
220     case XKB_LOG_LEVEL_WARNING:
221         return "xkbcommon: WARNING: ";
222     case XKB_LOG_LEVEL_ERROR:
223         return "xkbcommon: ERROR: ";
224     case XKB_LOG_LEVEL_CRITICAL:
225         return "xkbcommon: CRITICAL: ";
226     default:
227         return NULL;
228     }
229 }
230
231 ATTR_PRINTF(3, 0) static void
232 default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
233                const char *fmt, va_list args)
234 {
235     const char *prefix = log_level_to_prefix(level);
236
237     if (prefix)
238         fprintf(stderr, "%s", prefix);
239     vfprintf(stderr, fmt, args);
240 }
241
242 static enum xkb_log_level
243 log_level(const char *level) {
244     char *endptr;
245     enum xkb_log_level lvl;
246
247     errno = 0;
248     lvl = strtol(level, &endptr, 10);
249     if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
250         return lvl;
251     if (istreq_prefix("crit", level))
252         return XKB_LOG_LEVEL_CRITICAL;
253     if (istreq_prefix("err", level))
254         return XKB_LOG_LEVEL_ERROR;
255     if (istreq_prefix("warn", level))
256         return XKB_LOG_LEVEL_WARNING;
257     if (istreq_prefix("info", level))
258         return XKB_LOG_LEVEL_INFO;
259     if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
260         return XKB_LOG_LEVEL_DEBUG;
261
262     return XKB_LOG_LEVEL_ERROR;
263 }
264
265 static int
266 log_verbosity(const char *verbosity) {
267     char *endptr;
268     int v;
269
270     errno = 0;
271     v = strtol(verbosity, &endptr, 10);
272     if (errno == 0)
273         return v;
274
275     return 0;
276 }
277
278 #ifndef DEFAULT_XKB_VARIANT
279 #define DEFAULT_XKB_VARIANT NULL
280 #endif
281
282 #ifndef DEFAULT_XKB_OPTIONS
283 #define DEFAULT_XKB_OPTIONS NULL
284 #endif
285
286 /**
287  * Create a new context.
288  */
289 XKB_EXPORT struct xkb_context *
290 xkb_context_new(enum xkb_context_flags flags)
291 {
292     const char *env;
293     struct xkb_context *ctx = calloc(1, sizeof(*ctx));
294
295     if (!ctx)
296         return NULL;
297
298     ctx->refcnt = 1;
299     ctx->log_fn = default_log_fn;
300     ctx->log_level = XKB_LOG_LEVEL_ERROR;
301     ctx->log_verbosity = 0;
302
303     /* Environment overwrites defaults. */
304     env = getenv("XKB_LOG_LEVEL");
305     if (env)
306         xkb_context_set_log_level(ctx, log_level(env));
307
308     env = getenv("XKB_LOG_VERBOSITY");
309     if (env)
310         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
311
312     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
313         !xkb_context_include_path_append_default(ctx)) {
314         log_err(ctx, "failed to add default include path %s\n",
315                 DFLT_XKB_CONFIG_ROOT);
316         xkb_context_unref(ctx);
317         return NULL;
318     }
319
320     ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
321
322     ctx->atom_table = atom_table_new();
323     if (!ctx->atom_table) {
324         xkb_context_unref(ctx);
325         return NULL;
326     }
327
328     return ctx;
329 }
330
331 xkb_atom_t
332 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
333 {
334     return atom_lookup(ctx->atom_table, string, strlen(string));
335 }
336
337 xkb_atom_t
338 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
339 {
340     return atom_intern(ctx->atom_table, string, len, false);
341 }
342
343 xkb_atom_t
344 xkb_atom_steal(struct xkb_context *ctx, char *string)
345 {
346     return atom_intern(ctx->atom_table, string, strlen(string), true);
347 }
348
349 char *
350 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
351 {
352     return atom_strdup(ctx->atom_table, atom);
353 }
354
355 const char *
356 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
357 {
358     return atom_text(ctx->atom_table, atom);
359 }
360
361 void
362 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
363         const char *fmt, ...)
364 {
365     va_list args;
366
367     va_start(args, fmt);
368     ctx->log_fn(ctx, level, fmt, args);
369     va_end(args);
370 }
371
372 XKB_EXPORT void
373 xkb_context_set_log_fn(struct xkb_context *ctx,
374                        void (*log_fn)(struct xkb_context *ctx,
375                                       enum xkb_log_level level,
376                                       const char *fmt, va_list args))
377 {
378     ctx->log_fn = (log_fn ? log_fn : default_log_fn);
379 }
380
381 XKB_EXPORT enum xkb_log_level
382 xkb_context_get_log_level(struct xkb_context *ctx)
383 {
384     return ctx->log_level;
385 }
386
387 XKB_EXPORT void
388 xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
389 {
390     ctx->log_level = level;
391 }
392
393 XKB_EXPORT int
394 xkb_context_get_log_verbosity(struct xkb_context *ctx)
395 {
396     return ctx->log_verbosity;
397 }
398
399 XKB_EXPORT void
400 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
401 {
402     ctx->log_verbosity = verbosity;
403 }
404
405 XKB_EXPORT void *
406 xkb_context_get_user_data(struct xkb_context *ctx)
407 {
408     if (ctx)
409         return ctx->user_data;
410     return NULL;
411 }
412
413 XKB_EXPORT void
414 xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
415 {
416     ctx->user_data = user_data;
417 }
418
419 char *
420 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
421 {
422     char *rtrn;
423
424     if (size >= sizeof(ctx->text_buffer))
425         return NULL;
426
427     if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
428         ctx->text_next = 0;
429
430     rtrn = &ctx->text_buffer[ctx->text_next];
431     ctx->text_next += size;
432
433     return rtrn;
434 }
435
436 const char *
437 xkb_context_get_default_rules(struct xkb_context *ctx)
438 {
439     const char *env = NULL;
440
441     if (ctx->use_environment_names)
442         env = getenv("XKB_DEFAULT_RULES");
443
444     return env ? env : DEFAULT_XKB_RULES;
445 }
446
447 const char *
448 xkb_context_get_default_model(struct xkb_context *ctx)
449 {
450     const char *env = NULL;
451
452     if (ctx->use_environment_names)
453         env = getenv("XKB_DEFAULT_MODEL");
454
455     return env ? env : DEFAULT_XKB_MODEL;
456 }
457
458 const char *
459 xkb_context_get_default_layout(struct xkb_context *ctx)
460 {
461     const char *env = NULL;
462
463     if (ctx->use_environment_names)
464         env = getenv("XKB_DEFAULT_LAYOUT");
465
466     return env ? env : DEFAULT_XKB_LAYOUT;
467 }
468
469 const char *
470 xkb_context_get_default_variant(struct xkb_context *ctx)
471 {
472     const char *env = NULL;
473     const char *layout = getenv("XKB_DEFAULT_VARIANT");
474
475     /* We don't want to inherit the variant if they haven't also set a
476      * layout, since they're so closely paired. */
477     if (layout && ctx->use_environment_names)
478         env = getenv("XKB_DEFAULT_VARIANT");
479
480     return env ? env : DEFAULT_XKB_VARIANT;
481 }
482
483 const char *
484 xkb_context_get_default_options(struct xkb_context *ctx)
485 {
486     const char *env = NULL;
487
488     if (ctx->use_environment_names)
489         env = getenv("XKB_DEFAULT_OPTIONS");
490
491     return env ? env : DEFAULT_XKB_OPTIONS;
492 }