Debian Codegraph @

src/context-priv.c

raw
1 /*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Ran Benita
4 * SPDX-License-Identifier: MIT
5 *
6 * Author: Daniel Stone <[email protected]>
7 */
8
9 #include "config.h"
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13
14 #include "xkbcommon/xkbcommon.h"
15 #include "atom.h"
16 #include "darray.h"
17 #include "context.h"
18 #include "rmlvo.h"
19 #include "utils.h"
20
21 char *
22 xkb_context_getenv(struct xkb_context *ctx, const char *name)
23 {
24 if (ctx->use_secure_getenv) {
25 return secure_getenv(name);
26 } else {
27 return getenv(name);
28 }
29 }
30
31 bool
32 xkb_context_init_includes(struct xkb_context *ctx)
33 {
34 if (ctx->pending_default_includes) {
35 if (darray_empty(ctx->failed_includes)) {
36 if (!xkb_context_include_path_append_default(ctx)) {
37 log_err(ctx, XKB_ERROR_NO_VALID_DEFAULT_INCLUDE_PATH,
38 "Failed to add any default include path "
39 "(system path: %s)\n",
40 xkb_context_include_path_get_system_path(ctx));
41 return false;
42 }
43 ctx->pending_default_includes = false;
44 } else {
45 /*
46 * If there are failed includes then we already tried to load
47 * default include paths, so avoid further attempts.
48 */
49 return false;
50 }
51 }
52 return true;
53 }
54
55 darray_size_t
56 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
57 {
58 return (xkb_context_init_includes(ctx))
59 ? darray_size(ctx->failed_includes)
60 : 0;
61 }
62
63 const char *
64 xkb_context_failed_include_path_get(struct xkb_context *ctx,
65 darray_size_t idx)
66 {
67 if (idx >= xkb_context_num_failed_include_paths(ctx))
68 return NULL;
69
70 return darray_item(ctx->failed_includes, idx);
71 }
72
73 darray_size_t
74 xkb_atom_table_size(struct xkb_context *ctx)
75 {
76 return atom_table_size(ctx->atom_table);
77 }
78
79 xkb_atom_t
80 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
81 {
82 return atom_intern(ctx->atom_table, string, strlen(string), false);
83 }
84
85 xkb_atom_t
86 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
87 {
88 return atom_intern(ctx->atom_table, string, len, true);
89 }
90
91 const char *
92 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
93 {
94 return atom_text(ctx->atom_table, atom);
95 }
96
97 void
98 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
99 const char *fmt, ...)
100 {
101 va_list args;
102
103 if (ctx->log_level < level || ctx->log_verbosity < verbosity)
104 return;
105
106 va_start(args, fmt);
107 ctx->log_fn(ctx, level, fmt, args);
108 va_end(args);
109 }
110
111 char *
112 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
113 {
114 char *rtrn;
115
116 if (size >= sizeof(ctx->text_buffer))
117 return NULL;
118
119 if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
120 ctx->text_next = 0;
121
122 rtrn = &ctx->text_buffer[ctx->text_next];
123 ctx->text_next += size;
124
125 return rtrn;
126 }
127
128 static const char *
129 xkb_context_get_default_rules(struct xkb_context *ctx)
130 {
131 const char *env = NULL;
132
133 if (ctx->use_environment_names)
134 env = xkb_context_getenv(ctx, "XKB_DEFAULT_RULES");
135
136 return env ? env : DEFAULT_XKB_RULES;
137 }
138
139 static const char *
140 xkb_context_get_default_model(struct xkb_context *ctx)
141 {
142 const char *env = NULL;
143
144 if (ctx->use_environment_names)
145 env = xkb_context_getenv(ctx, "XKB_DEFAULT_MODEL");
146
147 return env ? env : DEFAULT_XKB_MODEL;
148 }
149
150 static const char *
151 xkb_context_get_default_layout(struct xkb_context *ctx)
152 {
153 const char *env = NULL;
154
155 if (ctx->use_environment_names)
156 env = xkb_context_getenv(ctx, "XKB_DEFAULT_LAYOUT");
157
158 return env ? env : DEFAULT_XKB_LAYOUT;
159 }
160
161 static const char *
162 xkb_context_get_default_variant(struct xkb_context *ctx)
163 {
164 const char *env = NULL;
165 const char *layout = xkb_context_getenv(ctx, "XKB_DEFAULT_LAYOUT");
166
167 /* We don't want to inherit the variant if they haven't also set a
168 * layout, since they're so closely paired. */
169 if (layout && ctx->use_environment_names)
170 env = xkb_context_getenv(ctx, "XKB_DEFAULT_VARIANT");
171
172 return env ? env : DEFAULT_XKB_VARIANT;
173 }
174
175 static const char *
176 xkb_context_get_default_options(struct xkb_context *ctx)
177 {
178 const char *env = NULL;
179
180 if (ctx->use_environment_names)
181 env = xkb_context_getenv(ctx, "XKB_DEFAULT_OPTIONS");
182
183 return env ? env : DEFAULT_XKB_OPTIONS;
184 }
185
186 enum RMLVO
187 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
188 struct xkb_rule_names *rmlvo)
189 {
190 enum RMLVO modified = 0;
191
192 if (isempty(rmlvo->rules)) {
193 rmlvo->rules = xkb_context_get_default_rules(ctx);
194 modified |= RMLVO_RULES;
195 }
196 if (isempty(rmlvo->model)) {
197 rmlvo->model = xkb_context_get_default_model(ctx);
198 modified |= RMLVO_MODEL;
199 }
200 /* Layout and variant are tied together, so don't try to use one from
201 * the caller and one from the environment. */
202 if (isempty(rmlvo->layout)) {
203 rmlvo->layout = xkb_context_get_default_layout(ctx);
204 modified |= RMLVO_LAYOUT;
205 const char * const variant = xkb_context_get_default_variant(ctx);
206 if (!isempty(rmlvo->variant)) {
207 log_warn(ctx, XKB_LOG_MESSAGE_NO_ID,
208 "Layout not provided, but variant set to \"%s\": "
209 "ignoring variant and using defaults for both: "
210 "layout=\"%s\", variant=\"%s\".\n",
211 rmlvo->variant,
212 rmlvo->layout,
213 variant ? variant : "");
214 }
215 rmlvo->variant = variant;
216 modified |= RMLVO_VARIANT;
217 }
218 /* Options can be empty, so respect that if passed in. */
219 if (rmlvo->options == NULL) {
220 rmlvo->options = xkb_context_get_default_options(ctx);
221 modified |= RMLVO_OPTIONS;
222 }
223
224 return modified;
225 }