keysym: properly handle overflow in 0x keysym names
[platform/upstream/libxkbcommon.git] / src / context-priv.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
33 #include "xkbcommon/xkbcommon.h"
34 #include "utils.h"
35 #include "context.h"
36
37 unsigned int
38 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
39 {
40     return darray_size(ctx->failed_includes);
41 }
42
43 const char *
44 xkb_context_failed_include_path_get(struct xkb_context *ctx,
45                                     unsigned int idx)
46 {
47     if (idx >= xkb_context_num_failed_include_paths(ctx))
48         return NULL;
49
50     return darray_item(ctx->failed_includes, idx);
51 }
52
53 xkb_atom_t
54 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
55 {
56     return atom_intern(ctx->atom_table, string, strlen(string), false);
57 }
58
59 xkb_atom_t
60 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
61 {
62     return atom_intern(ctx->atom_table, string, len, true);
63 }
64
65 const char *
66 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
67 {
68     return atom_text(ctx->atom_table, atom);
69 }
70
71 void
72 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
73         const char *fmt, ...)
74 {
75     va_list args;
76
77     if (ctx->log_level < level || ctx->log_verbosity < verbosity)
78         return;
79
80     va_start(args, fmt);
81     ctx->log_fn(ctx, level, fmt, args);
82     va_end(args);
83 }
84
85 char *
86 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
87 {
88     char *rtrn;
89
90     if (size >= sizeof(ctx->text_buffer))
91         return NULL;
92
93     if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
94         ctx->text_next = 0;
95
96     rtrn = &ctx->text_buffer[ctx->text_next];
97     ctx->text_next += size;
98
99     return rtrn;
100 }
101
102 static const char *
103 xkb_context_get_default_rules(struct xkb_context *ctx)
104 {
105     const char *env = NULL;
106
107     if (ctx->use_environment_names)
108         env = secure_getenv("XKB_DEFAULT_RULES");
109
110     return env ? env : DEFAULT_XKB_RULES;
111 }
112
113 static const char *
114 xkb_context_get_default_model(struct xkb_context *ctx)
115 {
116     const char *env = NULL;
117
118     if (ctx->use_environment_names)
119         env = secure_getenv("XKB_DEFAULT_MODEL");
120
121     return env ? env : DEFAULT_XKB_MODEL;
122 }
123
124 static const char *
125 xkb_context_get_default_layout(struct xkb_context *ctx)
126 {
127     const char *env = NULL;
128
129     if (ctx->use_environment_names)
130         env = secure_getenv("XKB_DEFAULT_LAYOUT");
131
132     return env ? env : DEFAULT_XKB_LAYOUT;
133 }
134
135 static const char *
136 xkb_context_get_default_variant(struct xkb_context *ctx)
137 {
138     const char *env = NULL;
139     const char *layout = secure_getenv("XKB_DEFAULT_LAYOUT");
140
141     /* We don't want to inherit the variant if they haven't also set a
142      * layout, since they're so closely paired. */
143     if (layout && ctx->use_environment_names)
144         env = secure_getenv("XKB_DEFAULT_VARIANT");
145
146     return env ? env : DEFAULT_XKB_VARIANT;
147 }
148
149 static const char *
150 xkb_context_get_default_options(struct xkb_context *ctx)
151 {
152     const char *env = NULL;
153
154     if (ctx->use_environment_names)
155         env = secure_getenv("XKB_DEFAULT_OPTIONS");
156
157     return env ? env : DEFAULT_XKB_OPTIONS;
158 }
159
160 void
161 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
162                                 struct xkb_rule_names *rmlvo)
163 {
164     if (isempty(rmlvo->rules))
165         rmlvo->rules = xkb_context_get_default_rules(ctx);
166     if (isempty(rmlvo->model))
167         rmlvo->model = xkb_context_get_default_model(ctx);
168     /* Layout and variant are tied together, so don't try to use one from
169      * the caller and one from the environment. */
170     if (isempty(rmlvo->layout)) {
171         rmlvo->layout = xkb_context_get_default_layout(ctx);
172         rmlvo->variant = xkb_context_get_default_variant(ctx);
173     }
174     /* Options can be empty, so respect that if passed in. */
175     if (rmlvo->options == NULL)
176         rmlvo->options = xkb_context_get_default_options(ctx);
177 }