| 1 |
/* |
| 2 |
* For HPND: |
| 3 |
* Copyright (c) 1996 by Silicon Graphics Computer Systems, Inc. |
| 4 |
* |
| 5 |
* For MIT: |
| 6 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: HPND AND MIT |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <limits.h> |
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#include "xkbcommon/xkbcommon.h" |
| 18 |
#include "xkbcomp-priv.h" |
| 19 |
#include "context.h" |
| 20 |
#include "keymap.h" |
| 21 |
#include "messages-codes.h" |
| 22 |
#include "context.h" |
| 23 |
#include "rules.h" |
| 24 |
#include "rmlvo.h" |
| 25 |
#include "include.h" |
| 26 |
#include "scanner-utils.h" |
| 27 |
#include "darray.h" |
| 28 |
#include "utils.h" |
| 29 |
#include "utils-numbers.h" |
| 30 |
#include "utils-paths.h" |
| 31 |
|
| 32 |
#define MAX_INCLUDE_DEPTH 5 |
| 33 |
|
| 34 |
/* Scanner / Lexer */ |
| 35 |
|
| 36 |
/* Values returned with some tokens, like yylval. */ |
| 37 |
union lvalue { |
| 38 |
struct sval string; |
| 39 |
}; |
| 40 |
|
| 41 |
enum rules_token { |
| 42 |
TOK_END_OF_FILE = 0, |
| 43 |
TOK_END_OF_LINE, |
| 44 |
TOK_IDENTIFIER, |
| 45 |
TOK_GROUP_NAME, |
| 46 |
TOK_BANG, |
| 47 |
TOK_EQUALS, |
| 48 |
TOK_WILD_CARD_STAR, |
| 49 |
TOK_WILD_CARD_NONE, |
| 50 |
TOK_WILD_CARD_SOME, |
| 51 |
TOK_WILD_CARD_ANY, |
| 52 |
TOK_INCLUDE, |
| 53 |
TOK_ERROR |
| 54 |
}; |
| 55 |
|
| 56 |
static inline bool |
| 57 |
is_ident(char ch) |
| 58 |
{ |
| 59 |
return is_graph(ch) && ch != '\\'; |
| 60 |
} |
| 61 |
|
| 62 |
static enum rules_token |
| 63 |
lex(struct scanner *s, union lvalue *val) |
| 64 |
{ |
| 65 |
skip_more_whitespace_and_comments: |
| 66 |
/* Skip spaces. */ |
| 67 |
while (scanner_chr(s, ' ') || scanner_chr(s, '\t') || scanner_chr(s, '\r')); |
| 68 |
|
| 69 |
/* Skip comments. */ |
| 70 |
if (scanner_lit(s, "//")) { |
| 71 |
scanner_skip_to_eol(s); |
| 72 |
} |
| 73 |
|
| 74 |
/* New line. */ |
| 75 |
if (scanner_eol(s)) { |
| 76 |
while (scanner_eol(s)) scanner_next(s); |
| 77 |
return TOK_END_OF_LINE; |
| 78 |
} |
| 79 |
|
| 80 |
/* Escaped line continuation. */ |
| 81 |
if (scanner_chr(s, '\\')) { |
| 82 |
/* Optional \r. */ |
| 83 |
scanner_chr(s, '\r'); |
| 84 |
if (!scanner_eol(s)) { |
| 85 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 86 |
"illegal new line escape; must appear at end of line"); |
| 87 |
return TOK_ERROR; |
| 88 |
} |
| 89 |
scanner_next(s); |
| 90 |
goto skip_more_whitespace_and_comments; |
| 91 |
} |
| 92 |
|
| 93 |
/* See if we're done. */ |
| 94 |
if (scanner_eof(s)) return TOK_END_OF_FILE; |
| 95 |
|
| 96 |
/* New token. */ |
| 97 |
s->token_pos = s->pos; |
| 98 |
|
| 99 |
/* Operators and punctuation. */ |
| 100 |
if (scanner_chr(s, '!')) return TOK_BANG; |
| 101 |
if (scanner_chr(s, '=')) return TOK_EQUALS; |
| 102 |
|
| 103 |
/* Wild cards */ |
| 104 |
if (scanner_chr(s, '*')) return TOK_WILD_CARD_STAR; |
| 105 |
if (scanner_lit(s, "<none>")) return TOK_WILD_CARD_NONE; |
| 106 |
if (scanner_lit(s, "<some>")) return TOK_WILD_CARD_SOME; |
| 107 |
if (scanner_lit(s, "<any>")) return TOK_WILD_CARD_ANY; |
| 108 |
|
| 109 |
/* Group name. */ |
| 110 |
if (scanner_chr(s, '$')) { |
| 111 |
val->string.start = s->s + s->pos; |
| 112 |
val->string.len = 0; |
| 113 |
while (is_ident(scanner_peek(s))) { |
| 114 |
scanner_next(s); |
| 115 |
val->string.len++; |
| 116 |
} |
| 117 |
if (val->string.len == 0) { |
| 118 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 119 |
"unexpected character after \'$\'; expected name"); |
| 120 |
return TOK_ERROR; |
| 121 |
} |
| 122 |
return TOK_GROUP_NAME; |
| 123 |
} |
| 124 |
|
| 125 |
/* Include statement. */ |
| 126 |
if (scanner_lit(s, "include")) |
| 127 |
return TOK_INCLUDE; |
| 128 |
|
| 129 |
/* Identifier. */ |
| 130 |
/* Ensure that we can parse KcCGST values with merge modes */ |
| 131 |
assert(is_ident(MERGE_OVERRIDE_PREFIX)); |
| 132 |
assert(is_ident(MERGE_AUGMENT_PREFIX)); |
| 133 |
assert(is_ident(MERGE_REPLACE_PREFIX)); |
| 134 |
if (is_ident(scanner_peek(s))) { |
| 135 |
val->string.start = s->s + s->pos; |
| 136 |
val->string.len = 0; |
| 137 |
while (is_ident(scanner_peek(s))) { |
| 138 |
scanner_next(s); |
| 139 |
val->string.len++; |
| 140 |
} |
| 141 |
return TOK_IDENTIFIER; |
| 142 |
} |
| 143 |
|
| 144 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 145 |
"unrecognized token"); |
| 146 |
return TOK_ERROR; |
| 147 |
} |
| 148 |
|
| 149 |
/***====================================================================***/ |
| 150 |
|
| 151 |
enum rules_mlvo { |
| 152 |
MLVO_MODEL, |
| 153 |
MLVO_LAYOUT, |
| 154 |
MLVO_VARIANT, |
| 155 |
MLVO_OPTION, |
| 156 |
_MLVO_NUM_ENTRIES |
| 157 |
}; |
| 158 |
|
| 159 |
typedef uint8_t mlvo_index_t; |
| 160 |
typedef uint8_t mlvo_mask_t; |
| 161 |
|
| 162 |
static const struct sval rules_mlvo_svals[_MLVO_NUM_ENTRIES] = { |
| 163 |
[MLVO_MODEL] = SVAL_INIT("model"), |
| 164 |
[MLVO_LAYOUT] = SVAL_INIT("layout"), |
| 165 |
[MLVO_VARIANT] = SVAL_INIT("variant"), |
| 166 |
[MLVO_OPTION] = SVAL_INIT("option"), |
| 167 |
}; |
| 168 |
|
| 169 |
enum rules_kccgst { |
| 170 |
KCCGST_KEYCODES, |
| 171 |
KCCGST_TYPES, |
| 172 |
KCCGST_COMPAT, |
| 173 |
KCCGST_SYMBOLS, |
| 174 |
KCCGST_GEOMETRY, |
| 175 |
_KCCGST_NUM_ENTRIES |
| 176 |
}; |
| 177 |
|
| 178 |
typedef uint8_t kccgst_index_t; |
| 179 |
typedef uint8_t kccgst_mask_t; |
| 180 |
|
| 181 |
static const struct sval rules_kccgst_svals[_KCCGST_NUM_ENTRIES] = { |
| 182 |
[KCCGST_KEYCODES] = SVAL_INIT("keycodes"), |
| 183 |
[KCCGST_TYPES] = SVAL_INIT("types"), |
| 184 |
[KCCGST_COMPAT] = SVAL_INIT("compat"), |
| 185 |
[KCCGST_SYMBOLS] = SVAL_INIT("symbols"), |
| 186 |
[KCCGST_GEOMETRY] = SVAL_INIT("geometry"), |
| 187 |
}; |
| 188 |
|
| 189 |
static_assert(XKB_MAX_GROUPS < (1u << 30), |
| 190 |
"Layout index does not fix in matched_sval::matched_layouts"); |
| 191 |
#define OPTIONS_MATCH_ALL_GROUPS XKB_MAX_GROUPS |
| 192 |
|
| 193 |
/* We use this to keep score whether an mlvo was matched or not; if not, |
| 194 |
* we warn the user that his preference was ignored. */ |
| 195 |
struct matched_sval { |
| 196 |
struct sval sval; |
| 197 |
bool matched:1; |
| 198 |
/* Used for layout-specific options */ |
| 199 |
xkb_layout_index_t layout:31; |
| 200 |
}; |
| 201 |
typedef darray(struct matched_sval) darray_matched_sval; |
| 202 |
|
| 203 |
/* |
| 204 |
* A broken-down version of xkb_rule_names (without the rules, |
| 205 |
* obviously). |
| 206 |
*/ |
| 207 |
struct rule_names { |
| 208 |
struct matched_sval model; |
| 209 |
darray_matched_sval layouts; |
| 210 |
darray_matched_sval variants; |
| 211 |
darray_matched_sval options; |
| 212 |
}; |
| 213 |
|
| 214 |
struct group { |
| 215 |
struct sval name; |
| 216 |
darray_sval elements; |
| 217 |
}; |
| 218 |
|
| 219 |
struct mapping { |
| 220 |
enum rules_mlvo mlvo_at_pos[_MLVO_NUM_ENTRIES]; |
| 221 |
mlvo_index_t num_mlvo; |
| 222 |
mlvo_mask_t defined_mlvo_mask; |
| 223 |
bool has_layout_idx_range; |
| 224 |
/* This member has 2 uses: |
| 225 |
* • Keep track of layout and variant indices while parsing MLVO headers. |
| 226 |
* • Store layout/variant range afterwards. |
| 227 |
* Thus provide 2 structs to reflect these semantics in the code. */ |
| 228 |
union { |
| 229 |
struct { xkb_layout_index_t layout_idx, variant_idx; }; |
| 230 |
struct { xkb_layout_index_t layout_idx_min, layout_idx_max; }; |
| 231 |
}; |
| 232 |
/* This member has 2 uses: |
| 233 |
* • Check if the mapping is active by interpreting the value as a boolean. |
| 234 |
* • Keep track of the remaining layout indices to match. |
| 235 |
* Thus provide 2 names to reflect these semantics in the code. */ |
| 236 |
union { |
| 237 |
xkb_layout_mask_t active; |
| 238 |
xkb_layout_mask_t layouts_candidates_mask; |
| 239 |
}; |
| 240 |
enum rules_kccgst kccgst_at_pos[_KCCGST_NUM_ENTRIES]; |
| 241 |
kccgst_index_t num_kccgst; |
| 242 |
kccgst_mask_t defined_kccgst_mask; |
| 243 |
}; |
| 244 |
|
| 245 |
enum mlvo_match_type { |
| 246 |
/** Match the given plain value */ |
| 247 |
MLVO_MATCH_NORMAL = 0, |
| 248 |
/** Match depending on the value of `wildcard_match_type` */ |
| 249 |
MLVO_MATCH_WILDCARD_LEGACY, |
| 250 |
/** Match empty value */ |
| 251 |
MLVO_MATCH_WILDCARD_NONE, |
| 252 |
/** Match non-empty value */ |
| 253 |
MLVO_MATCH_WILDCARD_SOME, |
| 254 |
/** Match any value, optionally empty */ |
| 255 |
MLVO_MATCH_WILDCARD_ANY, |
| 256 |
/** Match any entry in a group */ |
| 257 |
MLVO_MATCH_GROUP, |
| 258 |
}; |
| 259 |
|
| 260 |
enum wildcard_match_type { |
| 261 |
/** ‘*’ matches only non-empty strings */ |
| 262 |
WILDCARD_MATCH_NONEMPTY = 0, |
| 263 |
/** ‘*’ matches all strings */ |
| 264 |
WILDCARD_MATCH_ALL, |
| 265 |
}; |
| 266 |
|
| 267 |
struct rule { |
| 268 |
struct sval mlvo_value_at_pos[_MLVO_NUM_ENTRIES]; |
| 269 |
enum mlvo_match_type match_type_at_pos[_MLVO_NUM_ENTRIES]; |
| 270 |
mlvo_index_t num_mlvo_values; |
| 271 |
struct sval kccgst_value_at_pos[_KCCGST_NUM_ENTRIES]; |
| 272 |
kccgst_index_t num_kccgst_values; |
| 273 |
bool skip; |
| 274 |
}; |
| 275 |
|
| 276 |
#define SLICE_KCCGST_BIT_FIELD_SIZE 4 |
| 277 |
static_assert(!((1u << (SLICE_KCCGST_BIT_FIELD_SIZE - 1)) & |
| 278 |
(_KCCGST_NUM_ENTRIES - 1)), |
| 279 |
"Cannot encode KcCGST enum safely (need space for the sign)"); |
| 280 |
struct kccgst_buffer_slice { |
| 281 |
uint32_t length:(32 - SLICE_KCCGST_BIT_FIELD_SIZE); |
| 282 |
enum rules_kccgst kccgst:SLICE_KCCGST_BIT_FIELD_SIZE; |
| 283 |
xkb_layout_index_t layout; |
| 284 |
}; |
| 285 |
|
| 286 |
/* Buffer for pending KcCGST values */ |
| 287 |
struct kccgst_buffer { |
| 288 |
darray_char buffer; |
| 289 |
/* Slice corresponding to each value in the buffer */ |
| 290 |
darray(struct kccgst_buffer_slice) slices; |
| 291 |
}; |
| 292 |
|
| 293 |
/* |
| 294 |
* This is the main object used to match a given RMLVO against a rules |
| 295 |
* file and aggregate the results in a KcCGST. It goes through a simple |
| 296 |
* matching state machine, with tokens as transitions (see |
| 297 |
* matcher_match()). |
| 298 |
*/ |
| 299 |
struct matcher { |
| 300 |
struct xkb_context *ctx; |
| 301 |
/* Input.*/ |
| 302 |
struct rule_names rmlvo; |
| 303 |
union lvalue val; |
| 304 |
darray(struct group) groups; |
| 305 |
/* Current mapping. */ |
| 306 |
struct mapping mapping; |
| 307 |
/* Current rule. */ |
| 308 |
struct rule rule; |
| 309 |
/* |
| 310 |
* Buffers for pending KcCGST values. Required in case of using layout |
| 311 |
* index ranges, to ensure that the values are merged in the expected order. |
| 312 |
* See the note: “Layout index ranges and merging KcCGST values”. |
| 313 |
*/ |
| 314 |
struct kccgst_buffer pending_kccgst; |
| 315 |
/* Output. */ |
| 316 |
darray_char kccgst[_KCCGST_NUM_ENTRIES]; |
| 317 |
}; |
| 318 |
|
| 319 |
static struct sval |
| 320 |
strip_spaces(struct sval v) |
| 321 |
{ |
| 322 |
while (v.len > 0 && is_space(v.start[0])) { v.len--; v.start++; } |
| 323 |
while (v.len > 0 && is_space(v.start[v.len - 1])) v.len--; |
| 324 |
return v; |
| 325 |
} |
| 326 |
|
| 327 |
static darray_matched_sval |
| 328 |
split_comma_separated_mlvo(struct xkb_context *ctx, |
| 329 |
enum rules_mlvo mlvo, const char *s) |
| 330 |
{ |
| 331 |
darray_matched_sval arr = darray_new(); |
| 332 |
|
| 333 |
/* |
| 334 |
* Make sure the array returned by this function always includes at |
| 335 |
* least one value, e.g. "" -> { "" } and "," -> { "", "" }. |
| 336 |
*/ |
| 337 |
|
| 338 |
if (!s) { |
| 339 |
struct matched_sval val = { .sval = { NULL, 0 } }; |
| 340 |
darray_append(arr, val); |
| 341 |
return arr; |
| 342 |
} |
| 343 |
|
| 344 |
while (true) { |
| 345 |
struct matched_sval val = { |
| 346 |
.sval = { s, 0 }, |
| 347 |
.matched = false, |
| 348 |
/* NOTE: Cannot store XKB_LAYOUT_INVALID */ |
| 349 |
.layout = OPTIONS_MATCH_ALL_GROUPS |
| 350 |
}; |
| 351 |
while (*s != '\0' && *s != ',' && *s != OPTIONS_GROUP_SPECIFIER_PREFIX) { |
| 352 |
s++; |
| 353 |
val.sval.len++; |
| 354 |
} |
| 355 |
val.sval = strip_spaces(val.sval); |
| 356 |
|
| 357 |
if (*s == OPTIONS_GROUP_SPECIFIER_PREFIX) { |
| 358 |
/* Handle numeric layout index */ |
| 359 |
s++; |
| 360 |
const char* const layout_start = s; |
| 361 |
xkb_layout_index_t layout = XKB_LAYOUT_INVALID; |
| 362 |
|
| 363 |
int count = parse_dec_to_uint32_t(s, SIZE_MAX, &layout); |
| 364 |
if (count > 0) { |
| 365 |
/* Note: 1-indexed layout */ |
| 366 |
s += count; |
| 367 |
if (layout == 0 || layout > XKB_MAX_GROUPS) { |
| 368 |
log_err(ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX, |
| 369 |
"Invalid layout index %"PRIu32" " |
| 370 |
"for the RMVLO component: \"%.*s\"\n", layout, |
| 371 |
(unsigned int) val.sval.len, val.sval.start); |
| 372 |
} else if (mlvo != MLVO_OPTION) { |
| 373 |
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 374 |
"Layout index %"PRIu32" is not supported for " |
| 375 |
"the RMLVO component: \"%.*s\"\n", layout, |
| 376 |
(unsigned int) val.sval.len, val.sval.start); |
| 377 |
} else { |
| 378 |
val.layout = layout - 1; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/* Consume until reaching next item or end of string */ |
| 383 |
const char* const layout_index_end = s; |
| 384 |
while (*s != '\0' && *s != ',') { s++; } |
| 385 |
if (count <= 0 || layout_index_end != s) { |
| 386 |
log_err(ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX, |
| 387 |
"Invalid layout index \"%.*s\" for the RMLVO " |
| 388 |
"component \"%.*s\"; discarding specifier.\n", |
| 389 |
(unsigned int) (s - layout_start), layout_start, |
| 390 |
(unsigned int) val.sval.len, val.sval.start); |
| 391 |
val.layout = OPTIONS_MATCH_ALL_GROUPS; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
darray_append(arr, val); |
| 396 |
|
| 397 |
if (*s == '\0') break; |
| 398 |
if (*s == ',') s++; |
| 399 |
} |
| 400 |
|
| 401 |
return arr; |
| 402 |
} |
| 403 |
|
| 404 |
static struct matcher * |
| 405 |
matcher_new_from_rmlvo(const struct xkb_rmlvo_builder *rmlvo, const char **rules) |
| 406 |
{ |
| 407 |
struct matcher *m = calloc(1, sizeof(*m)); |
| 408 |
if (!m) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
m->ctx = rmlvo->ctx; |
| 412 |
|
| 413 |
/* Sanitize */ |
| 414 |
struct xkb_rule_names names = { |
| 415 |
.rules = rmlvo->rules, |
| 416 |
.model = rmlvo->model, |
| 417 |
/* Initial values only used to detect missing entries */ |
| 418 |
.layout = (darray_empty(rmlvo->layouts)) ? NULL : "x", |
| 419 |
.variant = (darray_empty(rmlvo->layouts)) ? NULL : "x", |
| 420 |
.options = (darray_empty(rmlvo->options)) ? NULL : "x", |
| 421 |
}; |
| 422 |
const enum RMLVO changed = xkb_context_sanitize_rule_names(rmlvo->ctx, |
| 423 |
&names); |
| 424 |
|
| 425 |
/* Initialize matcher with sanitized names, if relevant */ |
| 426 |
if (changed & RMLVO_RULES) { |
| 427 |
*rules = names.rules; |
| 428 |
} else { |
| 429 |
*rules = rmlvo->rules; |
| 430 |
} |
| 431 |
|
| 432 |
if (changed & RMLVO_MODEL) { |
| 433 |
m->rmlvo.model.sval.start = names.model; |
| 434 |
} else { |
| 435 |
m->rmlvo.model.sval.start = rmlvo->model; |
| 436 |
} |
| 437 |
m->rmlvo.model.sval.len = strlen_safe(rmlvo->model); |
| 438 |
m->rmlvo.model.layout = OPTIONS_MATCH_ALL_GROUPS; |
| 439 |
|
| 440 |
assert((changed & RMLVO_LAYOUT) || !(changed & RMLVO_VARIANT)); |
| 441 |
if (changed & RMLVO_LAYOUT) { |
| 442 |
/* Layout and variant are tied together */ |
| 443 |
m->rmlvo.layouts = split_comma_separated_mlvo(rmlvo->ctx, MLVO_LAYOUT, |
| 444 |
names.layout); |
| 445 |
m->rmlvo.variants = split_comma_separated_mlvo(rmlvo->ctx, MLVO_VARIANT, |
| 446 |
names.variant); |
| 447 |
if (darray_size(m->rmlvo.layouts) > darray_size(m->rmlvo.variants)) { |
| 448 |
/* Do not warn if no variants was provided */ |
| 449 |
if (!isempty(names.variant)) |
| 450 |
log_warn(m->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 451 |
"More layouts than variants: \"%s\" vs. \"%s\".\n", |
| 452 |
names.layout ? names.layout : "(none)", |
| 453 |
names.variant ? names.variant : "(none)"); |
| 454 |
darray_resize0(m->rmlvo.variants, darray_size(m->rmlvo.layouts)); |
| 455 |
} else if (darray_size(m->rmlvo.layouts) < darray_size(m->rmlvo.variants)) { |
| 456 |
log_err(m->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 457 |
"Less layouts than variants: \"%s\" vs. \"%s\".\n", |
| 458 |
names.layout ? names.layout : "(none)", |
| 459 |
names.variant ? names.variant : "(none)"); |
| 460 |
darray_resize(m->rmlvo.variants, darray_size(m->rmlvo.layouts)); |
| 461 |
darray_shrink(m->rmlvo.variants); |
| 462 |
} |
| 463 |
} else { |
| 464 |
struct xkb_rmlvo_builder_layout *layout; |
| 465 |
darray_foreach(layout, rmlvo->layouts) { |
| 466 |
struct matched_sval val = { |
| 467 |
.sval = { |
| 468 |
.start = layout->layout, |
| 469 |
.len = strlen_safe(layout->layout) |
| 470 |
}, |
| 471 |
.layout = OPTIONS_MATCH_ALL_GROUPS, |
| 472 |
.matched = false |
| 473 |
}; |
| 474 |
darray_append(m->rmlvo.layouts, val); |
| 475 |
val.sval.start = layout->variant; |
| 476 |
val.sval.len = strlen_safe(layout->variant); |
| 477 |
darray_append(m->rmlvo.variants, val); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if (changed & RMLVO_OPTIONS) { |
| 482 |
m->rmlvo.options = split_comma_separated_mlvo(rmlvo->ctx, MLVO_OPTION, |
| 483 |
names.options); |
| 484 |
} else { |
| 485 |
struct xkb_rmlvo_builder_option *option; |
| 486 |
darray_foreach(option, rmlvo->options) { |
| 487 |
struct matched_sval val = { |
| 488 |
.sval = { |
| 489 |
.start = option->option, |
| 490 |
.len = strlen_safe(option->option), |
| 491 |
}, |
| 492 |
.layout = (option->layout) == XKB_LAYOUT_INVALID |
| 493 |
? OPTIONS_MATCH_ALL_GROUPS |
| 494 |
: option->layout, |
| 495 |
.matched = false |
| 496 |
}; |
| 497 |
darray_append(m->rmlvo.options, val); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
return m; |
| 502 |
} |
| 503 |
|
| 504 |
static struct matcher * |
| 505 |
matcher_new_from_names(struct xkb_context *ctx, |
| 506 |
const struct xkb_rule_names *rmlvo) |
| 507 |
{ |
| 508 |
struct matcher *m = calloc(1, sizeof(*m)); |
| 509 |
if (!m) |
| 510 |
return NULL; |
| 511 |
|
| 512 |
m->ctx = ctx; |
| 513 |
m->rmlvo.model.sval.start = rmlvo->model; |
| 514 |
m->rmlvo.model.sval.len = strlen_safe(rmlvo->model); |
| 515 |
m->rmlvo.model.layout = OPTIONS_MATCH_ALL_GROUPS; |
| 516 |
m->rmlvo.layouts = split_comma_separated_mlvo(ctx, MLVO_LAYOUT, rmlvo->layout); |
| 517 |
m->rmlvo.variants = split_comma_separated_mlvo(ctx, MLVO_VARIANT, rmlvo->variant); |
| 518 |
m->rmlvo.options = split_comma_separated_mlvo(ctx, MLVO_OPTION, rmlvo->options); |
| 519 |
|
| 520 |
if (darray_size(m->rmlvo.layouts) > darray_size(m->rmlvo.variants)) { |
| 521 |
/* Do not warn if no variants was provided */ |
| 522 |
if (!isempty(rmlvo->variant)) |
| 523 |
log_warn(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 524 |
"More layouts than variants: \"%s\" vs. \"%s\".\n", |
| 525 |
rmlvo->layout ? rmlvo->layout : "(none)", |
| 526 |
rmlvo->variant ? rmlvo->variant : "(none)"); |
| 527 |
darray_resize0(m->rmlvo.variants, darray_size(m->rmlvo.layouts)); |
| 528 |
} else if (darray_size(m->rmlvo.layouts) < darray_size(m->rmlvo.variants)) { |
| 529 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 530 |
"Less layouts than variants: \"%s\" vs. \"%s\".\n", |
| 531 |
rmlvo->layout ? rmlvo->layout : "(none)", |
| 532 |
rmlvo->variant ? rmlvo->variant : "(none)"); |
| 533 |
darray_resize(m->rmlvo.variants, darray_size(m->rmlvo.layouts)); |
| 534 |
darray_shrink(m->rmlvo.variants); |
| 535 |
} |
| 536 |
|
| 537 |
return m; |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
matcher_free(struct matcher *m) |
| 542 |
{ |
| 543 |
if (!m) |
| 544 |
return; |
| 545 |
darray_free(m->rmlvo.layouts); |
| 546 |
darray_free(m->rmlvo.variants); |
| 547 |
darray_free(m->rmlvo.options); |
| 548 |
struct group *group; |
| 549 |
darray_foreach(group, m->groups) |
| 550 |
darray_free(group->elements); |
| 551 |
darray_free(m->pending_kccgst.buffer); |
| 552 |
darray_free(m->pending_kccgst.slices); |
| 553 |
for (kccgst_index_t i = 0; i < (kccgst_index_t) _KCCGST_NUM_ENTRIES; i++) |
| 554 |
darray_free(m->kccgst[i]); |
| 555 |
darray_free(m->groups); |
| 556 |
free(m); |
| 557 |
} |
| 558 |
|
| 559 |
static void |
| 560 |
matcher_group_start_new(struct matcher *m, struct sval name) |
| 561 |
{ |
| 562 |
struct group group = { .name = name, .elements = darray_new() }; |
| 563 |
darray_append(m->groups, group); |
| 564 |
} |
| 565 |
|
| 566 |
static void |
| 567 |
matcher_group_add_element(struct matcher *m, struct scanner *s, |
| 568 |
struct sval element) |
| 569 |
{ |
| 570 |
darray_append(darray_item(m->groups, darray_size(m->groups) - 1).elements, |
| 571 |
element); |
| 572 |
} |
| 573 |
|
| 574 |
static bool |
| 575 |
read_rules_file(struct xkb_context *ctx, |
| 576 |
struct matcher *matcher, |
| 577 |
unsigned int include_depth, |
| 578 |
FILE *file, |
| 579 |
const char *path); |
| 580 |
|
| 581 |
static void |
| 582 |
matcher_include(struct matcher *m, struct scanner *parent_scanner, |
| 583 |
unsigned int include_depth, |
| 584 |
struct sval inc) |
| 585 |
{ |
| 586 |
if (include_depth >= MAX_INCLUDE_DEPTH) { |
| 587 |
scanner_err(parent_scanner, XKB_LOG_MESSAGE_NO_ID, |
| 588 |
"maximum include depth (%u) exceeded; " |
| 589 |
"maybe there is an include loop?", |
| 590 |
MAX_INCLUDE_DEPTH); |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
const char *stmt_file = inc.start; |
| 595 |
size_t stmt_file_len = inc.len; |
| 596 |
|
| 597 |
/* Process %-expansion, if any */ |
| 598 |
char buf[PATH_MAX] = {0}; |
| 599 |
const ssize_t expanded = expand_path(m->ctx, parent_scanner->file_name, |
| 600 |
stmt_file, stmt_file_len, |
| 601 |
FILE_TYPE_RULES, |
| 602 |
buf, sizeof(buf)); |
| 603 |
if (expanded < 0) { |
| 604 |
/* Error */ |
| 605 |
return; |
| 606 |
} else if (expanded > 0) { |
| 607 |
/* %-expanded */ |
| 608 |
stmt_file = buf; |
| 609 |
stmt_file_len = (size_t) expanded; |
| 610 |
assert(stmt_file[stmt_file_len] == '\0'); |
| 611 |
} |
| 612 |
|
| 613 |
/* Lookup the first candidate */ |
| 614 |
FILE* file; |
| 615 |
unsigned int offset = 0; |
| 616 |
const bool absolute_path = is_absolute_path(stmt_file); |
| 617 |
if (absolute_path) { |
| 618 |
/* Absolute path: no need for lookup in XKB paths */ |
| 619 |
if (!expanded) { |
| 620 |
/* No %expansion: ensure it’s NULL-terminated */ |
| 621 |
if (stmt_file_len < sizeof(buf)) { |
| 622 |
memcpy(buf, stmt_file, stmt_file_len); |
| 623 |
buf[stmt_file_len] = '\0'; |
| 624 |
stmt_file = buf; |
| 625 |
} else { |
| 626 |
log_err(m->ctx, XKB_ERROR_INVALID_PATH, |
| 627 |
"Path is too long: %zu > %zu, got raw path: %.*s\n", |
| 628 |
stmt_file_len, sizeof(buf), |
| 629 |
(unsigned int) stmt_file_len, stmt_file); |
| 630 |
return; |
| 631 |
} |
| 632 |
} else { |
| 633 |
/* %-expansion is always NULL-terminated */ |
| 634 |
assert(stmt_file[stmt_file_len] == '\0'); |
| 635 |
} |
| 636 |
file = fopen(stmt_file, "rb"); |
| 637 |
} else { |
| 638 |
/* Relative path: lookup the first XKB path */ |
| 639 |
if (unlikely(expanded)) { |
| 640 |
/* |
| 641 |
* Relative path after expansion |
| 642 |
* |
| 643 |
* Unlikely to happen, because %-expansion is meant to use absolute |
| 644 |
* paths. Considering that: |
| 645 |
* - we do not resolve paths before expansion, leading to unexpected |
| 646 |
* result here; |
| 647 |
* - we need the buffer afterwards, but it currently contains the |
| 648 |
* expanded path; |
| 649 |
* - this is an edge case; |
| 650 |
* we simply make the lookup fail. |
| 651 |
*/ |
| 652 |
file = NULL; |
| 653 |
} else { |
| 654 |
file = FindFileInXkbPath(m->ctx, parent_scanner->file_name, |
| 655 |
stmt_file, stmt_file_len, FILE_TYPE_RULES, |
| 656 |
buf, sizeof(buf), &offset, true); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
while (file) { |
| 661 |
assert(strlen_safe(buf) < sizeof(buf)); |
| 662 |
bool ret = read_rules_file(m->ctx, m, include_depth + 1, file, buf); |
| 663 |
fclose(file); |
| 664 |
if (ret) |
| 665 |
return; |
| 666 |
/* Failed to parse rules or get all the components */ |
| 667 |
log_err(m->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 668 |
"No components returned from included XKB rules \"%s\"\n", |
| 669 |
buf); |
| 670 |
|
| 671 |
if (absolute_path) { |
| 672 |
/* There is no point to search further if the path is absolute */ |
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
/* Try next XKB path */ |
| 677 |
offset++; |
| 678 |
file = FindFileInXkbPath(m->ctx, parent_scanner->file_name, |
| 679 |
stmt_file, stmt_file_len, FILE_TYPE_RULES, |
| 680 |
buf, sizeof(buf), &offset, true); |
| 681 |
} |
| 682 |
|
| 683 |
log_err(m->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 684 |
"Failed to open included XKB rules \"%.*s\"\n", |
| 685 |
(unsigned int) stmt_file_len, stmt_file); |
| 686 |
} |
| 687 |
|
| 688 |
static void |
| 689 |
matcher_mapping_start_new(struct matcher *m) |
| 690 |
{ |
| 691 |
for (mlvo_index_t i = 0; i < (mlvo_index_t) _MLVO_NUM_ENTRIES; i++) |
| 692 |
m->mapping.mlvo_at_pos[i] = _MLVO_NUM_ENTRIES; |
| 693 |
for (kccgst_index_t i = 0; i < (kccgst_index_t) _KCCGST_NUM_ENTRIES; i++) |
| 694 |
m->mapping.kccgst_at_pos[i] = _KCCGST_NUM_ENTRIES; |
| 695 |
m->mapping.has_layout_idx_range = false; |
| 696 |
m->mapping.layout_idx = m->mapping.variant_idx = XKB_LAYOUT_INVALID; |
| 697 |
m->mapping.num_mlvo = m->mapping.num_kccgst = 0; |
| 698 |
m->mapping.defined_mlvo_mask = 0; |
| 699 |
m->mapping.defined_kccgst_mask = 0; |
| 700 |
m->mapping.active = true; |
| 701 |
} |
| 702 |
|
| 703 |
static int |
| 704 |
parse_layout_int_index(const char *s, size_t max_len, xkb_layout_index_t *out) |
| 705 |
{ |
| 706 |
/* We expect a NULL-terminated string of at least length 3 */ |
| 707 |
assert(max_len >= 3); |
| 708 |
uint32_t val = 0; |
| 709 |
const int count = parse_dec_to_uint32_t(&s[1], max_len - 2, &val); |
| 710 |
if (count <= 0 || s[1 + count] != ']' || val == 0 || val > XKB_MAX_GROUPS) |
| 711 |
return -1; |
| 712 |
/* To zero-based index. */ |
| 713 |
*out = val - 1; |
| 714 |
return count + 2; /* == length "[index]" */ |
| 715 |
} |
| 716 |
|
| 717 |
/* Parse Kccgst layout index: |
| 718 |
* "[%i]" or "[n]", where "n" is a decimal number */ |
| 719 |
static int |
| 720 |
extract_layout_index(const char *s, size_t max_len, xkb_layout_index_t *out) |
| 721 |
{ |
| 722 |
/* This function is pretty stupid, but works for now. */ |
| 723 |
*out = XKB_LAYOUT_INVALID; |
| 724 |
if (max_len < 3 || s[0] != '[') |
| 725 |
return -1; |
| 726 |
if (max_len > 3 && s[1] == '%' && s[2] == 'i' && s[3] == ']') { |
| 727 |
/* Special index: %i */ |
| 728 |
return 4; /* == length "[%i]" */ |
| 729 |
} |
| 730 |
/* Numeric index */ |
| 731 |
return parse_layout_int_index(s, max_len, out); |
| 732 |
} |
| 733 |
|
| 734 |
/* Special layout indices */ |
| 735 |
enum layout_index_ranges { |
| 736 |
LAYOUT_INDEX_SINGLE = XKB_LAYOUT_INVALID - 4, |
| 737 |
LAYOUT_INDEX_FIRST = XKB_LAYOUT_INVALID - 3, |
| 738 |
LAYOUT_INDEX_LATER = XKB_LAYOUT_INVALID - 2, |
| 739 |
LAYOUT_INDEX_ANY = XKB_LAYOUT_INVALID - 1 |
| 740 |
}; |
| 741 |
|
| 742 |
static_assert((xkb_layout_index_t) XKB_MAX_GROUPS < |
| 743 |
(xkb_layout_index_t) LAYOUT_INDEX_SINGLE, |
| 744 |
"Cannot define special indices"); |
| 745 |
static_assert((xkb_layout_index_t) LAYOUT_INDEX_SINGLE < |
| 746 |
(xkb_layout_index_t) LAYOUT_INDEX_FIRST && |
| 747 |
(xkb_layout_index_t) LAYOUT_INDEX_FIRST < |
| 748 |
(xkb_layout_index_t) LAYOUT_INDEX_LATER && |
| 749 |
(xkb_layout_index_t) LAYOUT_INDEX_LATER < |
| 750 |
(xkb_layout_index_t) LAYOUT_INDEX_ANY && |
| 751 |
(xkb_layout_index_t) LAYOUT_INDEX_ANY < |
| 752 |
(xkb_layout_index_t) XKB_LAYOUT_INVALID, |
| 753 |
"Special indices must respect certain order"); |
| 754 |
|
| 755 |
/* Parse index of layout/variant in MLVO mapping */ |
| 756 |
static int |
| 757 |
extract_mapping_layout_index(const char *s, size_t max_len, |
| 758 |
xkb_layout_index_t *out) |
| 759 |
{ |
| 760 |
static const struct { |
| 761 |
const char* name; |
| 762 |
int length; |
| 763 |
enum layout_index_ranges range; |
| 764 |
} names[] = { |
| 765 |
{ "single]", 7, LAYOUT_INDEX_SINGLE }, |
| 766 |
{ "first]" , 6, LAYOUT_INDEX_FIRST }, |
| 767 |
{ "later]" , 6, LAYOUT_INDEX_LATER }, |
| 768 |
{ "any]" , 4, LAYOUT_INDEX_ANY }, |
| 769 |
}; |
| 770 |
|
| 771 |
/* Check for minimal `[` + index + `]` */ |
| 772 |
if (max_len < 3 || s[0] != '[') { |
| 773 |
*out = XKB_LAYOUT_INVALID; |
| 774 |
return -1; |
| 775 |
} |
| 776 |
|
| 777 |
/* Try named indices ranges */ |
| 778 |
for (unsigned int k = 0; k < ARRAY_SIZE(names); k++) { |
| 779 |
if (strncmp(&s[1], names[k].name, names[k].length) == 0) { |
| 780 |
*out = (xkb_layout_index_t) names[k].range; |
| 781 |
return names[k].length + 1; /* == length "[index]" */ |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
/* Try numeric index */ |
| 786 |
*out = XKB_LAYOUT_INVALID; |
| 787 |
return parse_layout_int_index(s, max_len, out); |
| 788 |
} |
| 789 |
|
| 790 |
static inline bool |
| 791 |
is_mlvo_mask_defined(struct matcher *m, enum rules_mlvo mlvo) |
| 792 |
{ |
| 793 |
return m->mapping.defined_mlvo_mask & (1u << mlvo); |
| 794 |
} |
| 795 |
|
| 796 |
static void |
| 797 |
matcher_mapping_set_mlvo(struct matcher *m, struct scanner *s, |
| 798 |
struct sval ident) |
| 799 |
{ |
| 800 |
enum rules_mlvo mlvo; |
| 801 |
struct sval mlvo_sval; |
| 802 |
|
| 803 |
for (mlvo = 0; mlvo < _MLVO_NUM_ENTRIES; mlvo++) { |
| 804 |
mlvo_sval = rules_mlvo_svals[mlvo]; |
| 805 |
|
| 806 |
if (svaleq_prefix(mlvo_sval, ident)) |
| 807 |
break; |
| 808 |
} |
| 809 |
|
| 810 |
/* Not found. */ |
| 811 |
if (mlvo >= _MLVO_NUM_ENTRIES) { |
| 812 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 813 |
"invalid mapping: \"%.*s\" is not a valid value here; " |
| 814 |
"ignoring rule set", |
| 815 |
(unsigned int) ident.len, ident.start); |
| 816 |
m->mapping.active = false; |
| 817 |
return; |
| 818 |
} |
| 819 |
|
| 820 |
if (is_mlvo_mask_defined(m, mlvo)) { |
| 821 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 822 |
"invalid mapping: \"%.*s\" appears twice on the same line; " |
| 823 |
"ignoring rule set", |
| 824 |
(unsigned int) mlvo_sval.len, mlvo_sval.start); |
| 825 |
m->mapping.active = false; |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
/* If there are leftovers still, it must be an index. */ |
| 830 |
if (mlvo_sval.len < ident.len) { |
| 831 |
xkb_layout_index_t idx; |
| 832 |
int consumed = extract_mapping_layout_index(ident.start + mlvo_sval.len, |
| 833 |
ident.len - mlvo_sval.len, |
| 834 |
&idx); |
| 835 |
if ((int) (ident.len - mlvo_sval.len) != consumed) { |
| 836 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 837 |
"invalid mapping: \"%.*s\" may only be followed by a " |
| 838 |
"valid group index; ignoring rule set", |
| 839 |
(unsigned int) mlvo_sval.len, mlvo_sval.start); |
| 840 |
m->mapping.active = false; |
| 841 |
return; |
| 842 |
} |
| 843 |
|
| 844 |
if (mlvo == MLVO_LAYOUT) { |
| 845 |
m->mapping.layout_idx = idx; |
| 846 |
} |
| 847 |
else if (mlvo == MLVO_VARIANT) { |
| 848 |
m->mapping.variant_idx = idx; |
| 849 |
} |
| 850 |
else { |
| 851 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 852 |
"invalid mapping: \"%.*s\" cannot be followed by a group " |
| 853 |
"index; ignoring rule set", |
| 854 |
(unsigned int) mlvo_sval.len, mlvo_sval.start); |
| 855 |
m->mapping.active = false; |
| 856 |
return; |
| 857 |
} |
| 858 |
} else if (mlvo == MLVO_LAYOUT) { |
| 859 |
m->mapping.layout_idx = (xkb_layout_index_t) LAYOUT_INDEX_SINGLE; |
| 860 |
} else if (mlvo == MLVO_VARIANT) { |
| 861 |
m->mapping.variant_idx = (xkb_layout_index_t) LAYOUT_INDEX_SINGLE; |
| 862 |
} |
| 863 |
|
| 864 |
/* Check that if both layout and variant are defined, then they must have |
| 865 |
* the same index */ |
| 866 |
if (((mlvo == MLVO_LAYOUT && is_mlvo_mask_defined(m, MLVO_VARIANT)) || |
| 867 |
(mlvo == MLVO_VARIANT && is_mlvo_mask_defined(m, MLVO_LAYOUT))) && |
| 868 |
m->mapping.layout_idx != m->mapping.variant_idx) { |
| 869 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 870 |
"invalid mapping: \"layout\" index must be the same as the " |
| 871 |
"\"variant\" index"); |
| 872 |
m->mapping.active = false; |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
m->mapping.mlvo_at_pos[m->mapping.num_mlvo] = mlvo; |
| 877 |
m->mapping.defined_mlvo_mask |= (mlvo_mask_t) 1u << mlvo; |
| 878 |
m->mapping.num_mlvo++; |
| 879 |
} |
| 880 |
|
| 881 |
static void |
| 882 |
matcher_mapping_set_layout_bounds(struct matcher *m) |
| 883 |
{ |
| 884 |
/* Handle case where one of the index is XKB_LAYOUT_INVALID */ |
| 885 |
xkb_layout_index_t idx = MIN(m->mapping.layout_idx, m->mapping.variant_idx); |
| 886 |
switch (idx) { |
| 887 |
case XKB_LAYOUT_INVALID: |
| 888 |
/* No layout nor variant */ |
| 889 |
assert(!is_mlvo_mask_defined(m, MLVO_LAYOUT) && |
| 890 |
!is_mlvo_mask_defined(m, MLVO_VARIANT)); |
| 891 |
m->mapping.has_layout_idx_range = false; |
| 892 |
m->mapping.layout_idx_min = XKB_LAYOUT_INVALID; |
| 893 |
m->mapping.layout_idx_max = XKB_LAYOUT_INVALID; |
| 894 |
m->mapping.layouts_candidates_mask = 0x1; |
| 895 |
break; |
| 896 |
case LAYOUT_INDEX_LATER: |
| 897 |
m->mapping.has_layout_idx_range = true; |
| 898 |
m->mapping.layout_idx_min = 1; |
| 899 |
m->mapping.layout_idx_max = MIN(XKB_MAX_GROUPS, |
| 900 |
darray_size(m->rmlvo.layouts)); |
| 901 |
m->mapping.layouts_candidates_mask = (xkb_layout_mask_t) |
| 902 |
/* All but the first layout */ |
| 903 |
((UINT64_C(1) << m->mapping.layout_idx_max) - UINT64_C(1)) & |
| 904 |
~UINT64_C(1); |
| 905 |
break; |
| 906 |
case LAYOUT_INDEX_ANY: |
| 907 |
m->mapping.has_layout_idx_range = true; |
| 908 |
m->mapping.layout_idx_min = 0; |
| 909 |
m->mapping.layout_idx_max = MIN(XKB_MAX_GROUPS, |
| 910 |
darray_size(m->rmlvo.layouts)); |
| 911 |
m->mapping.layouts_candidates_mask = (xkb_layout_mask_t) |
| 912 |
/* All layouts */ |
| 913 |
(UINT64_C(1) << m->mapping.layout_idx_max) - UINT64_C(1); |
| 914 |
break; |
| 915 |
case LAYOUT_INDEX_SINGLE: |
| 916 |
case LAYOUT_INDEX_FIRST: |
| 917 |
/* No index or first index */ |
| 918 |
idx = 0; |
| 919 |
/* fallthrough */ |
| 920 |
default: |
| 921 |
/* Mere layout index */ |
| 922 |
m->mapping.has_layout_idx_range = false; |
| 923 |
m->mapping.layout_idx_min = idx; |
| 924 |
m->mapping.layout_idx_max = idx + 1; |
| 925 |
m->mapping.layouts_candidates_mask = UINT32_C(1) << idx; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
static void |
| 930 |
matcher_mapping_set_kccgst(struct matcher *m, struct scanner *s, struct sval ident) |
| 931 |
{ |
| 932 |
enum rules_kccgst kccgst; |
| 933 |
struct sval kccgst_sval; |
| 934 |
|
| 935 |
for (kccgst = 0; kccgst < _KCCGST_NUM_ENTRIES; kccgst++) { |
| 936 |
kccgst_sval = rules_kccgst_svals[kccgst]; |
| 937 |
|
| 938 |
if (svaleq(rules_kccgst_svals[kccgst], ident)) |
| 939 |
break; |
| 940 |
} |
| 941 |
|
| 942 |
/* Not found. */ |
| 943 |
if (kccgst >= _KCCGST_NUM_ENTRIES) { |
| 944 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 945 |
"invalid mapping: \"%.*s\" is not a valid value here; " |
| 946 |
"ignoring rule set", |
| 947 |
(unsigned int) ident.len, ident.start); |
| 948 |
m->mapping.active = false; |
| 949 |
return; |
| 950 |
} |
| 951 |
|
| 952 |
if (m->mapping.defined_kccgst_mask & (1u << kccgst)) { |
| 953 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 954 |
"invalid mapping: \"%.*s\" appears twice on the same line; " |
| 955 |
"ignoring rule set", |
| 956 |
(unsigned int) kccgst_sval.len, kccgst_sval.start); |
| 957 |
m->mapping.active = false; |
| 958 |
return; |
| 959 |
} |
| 960 |
|
| 961 |
m->mapping.kccgst_at_pos[m->mapping.num_kccgst] = kccgst; |
| 962 |
m->mapping.defined_kccgst_mask |= (kccgst_mask_t) 1u << kccgst; |
| 963 |
m->mapping.num_kccgst++; |
| 964 |
} |
| 965 |
|
| 966 |
static bool |
| 967 |
matcher_mapping_verify(struct matcher *m, struct scanner *s) |
| 968 |
{ |
| 969 |
if (m->mapping.num_mlvo == 0) { |
| 970 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 971 |
"invalid mapping: must have at least one value on the left " |
| 972 |
"hand side; ignoring rule set"); |
| 973 |
goto skip; |
| 974 |
} |
| 975 |
|
| 976 |
if (m->mapping.num_kccgst == 0) { |
| 977 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 978 |
"invalid mapping: must have at least one value on the right " |
| 979 |
"hand side; ignoring rule set"); |
| 980 |
goto skip; |
| 981 |
} |
| 982 |
|
| 983 |
/* |
| 984 |
* This following is very stupid, but this is how it works. |
| 985 |
* See the "Notes" section in the overview above. |
| 986 |
*/ |
| 987 |
|
| 988 |
if (is_mlvo_mask_defined(m, MLVO_LAYOUT)) { |
| 989 |
assert(m->mapping.layout_idx != XKB_LAYOUT_INVALID); |
| 990 |
switch (m->mapping.layout_idx) { |
| 991 |
case LAYOUT_INDEX_SINGLE: |
| 992 |
/* Layout rule without index matches when |
| 993 |
* exactly 1 layout is specified */ |
| 994 |
if (darray_size(m->rmlvo.layouts) > 1) |
| 995 |
goto skip; |
| 996 |
break; |
| 997 |
case LAYOUT_INDEX_ANY: |
| 998 |
case LAYOUT_INDEX_LATER: |
| 999 |
case LAYOUT_INDEX_FIRST: |
| 1000 |
/* No restrictions */ |
| 1001 |
break; |
| 1002 |
default: |
| 1003 |
/* Layout rule with index matches when at least 2 layouts are |
| 1004 |
* specified. Index must be in valid range. */ |
| 1005 |
if (darray_size(m->rmlvo.layouts) < 2 || |
| 1006 |
m->mapping.layout_idx >= darray_size(m->rmlvo.layouts)) |
| 1007 |
goto skip; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (is_mlvo_mask_defined(m, MLVO_VARIANT)) { |
| 1012 |
assert(m->mapping.variant_idx != XKB_LAYOUT_INVALID); |
| 1013 |
switch (m->mapping.variant_idx) { |
| 1014 |
case LAYOUT_INDEX_SINGLE: |
| 1015 |
/* Variant rule without index matches |
| 1016 |
* when exactly 1 variant is specified */ |
| 1017 |
if (darray_size(m->rmlvo.variants) > 1) |
| 1018 |
goto skip; |
| 1019 |
break; |
| 1020 |
case LAYOUT_INDEX_ANY: |
| 1021 |
case LAYOUT_INDEX_LATER: |
| 1022 |
case LAYOUT_INDEX_FIRST: |
| 1023 |
/* No restriction */ |
| 1024 |
break; |
| 1025 |
default: |
| 1026 |
/* Variant rule with index matches when at least 2 variants are |
| 1027 |
* specified. Index must be in valid range. */ |
| 1028 |
if (darray_size(m->rmlvo.variants) < 2 || |
| 1029 |
m->mapping.variant_idx >= darray_size(m->rmlvo.variants)) |
| 1030 |
goto skip; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
return true; |
| 1035 |
|
| 1036 |
skip: |
| 1037 |
m->mapping.active = false; |
| 1038 |
return false; |
| 1039 |
} |
| 1040 |
|
| 1041 |
static void |
| 1042 |
matcher_rule_start_new(struct matcher *m) |
| 1043 |
{ |
| 1044 |
memset(&m->rule, 0, sizeof(m->rule)); |
| 1045 |
m->rule.skip = !m->mapping.active; |
| 1046 |
} |
| 1047 |
|
| 1048 |
static void |
| 1049 |
matcher_rule_set_mlvo_common(struct matcher *m, struct scanner *s, |
| 1050 |
struct sval ident, |
| 1051 |
enum mlvo_match_type match_type) |
| 1052 |
{ |
| 1053 |
if (m->rule.num_mlvo_values >= m->mapping.num_mlvo) { |
| 1054 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1055 |
"invalid rule: has more values than the mapping line; " |
| 1056 |
"ignoring rule"); |
| 1057 |
m->rule.skip = true; |
| 1058 |
return; |
| 1059 |
} |
| 1060 |
m->rule.match_type_at_pos[m->rule.num_mlvo_values] = match_type; |
| 1061 |
m->rule.mlvo_value_at_pos[m->rule.num_mlvo_values] = ident; |
| 1062 |
m->rule.num_mlvo_values++; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static void |
| 1066 |
matcher_rule_set_mlvo_wildcard(struct matcher *m, struct scanner *s, |
| 1067 |
enum mlvo_match_type match_type) |
| 1068 |
{ |
| 1069 |
struct sval dummy = { NULL, 0 }; |
| 1070 |
matcher_rule_set_mlvo_common(m, s, dummy, match_type); |
| 1071 |
} |
| 1072 |
|
| 1073 |
static void |
| 1074 |
matcher_rule_set_mlvo_group(struct matcher *m, struct scanner *s, |
| 1075 |
struct sval ident) |
| 1076 |
{ |
| 1077 |
matcher_rule_set_mlvo_common(m, s, ident, MLVO_MATCH_GROUP); |
| 1078 |
} |
| 1079 |
|
| 1080 |
static void |
| 1081 |
matcher_rule_set_mlvo(struct matcher *m, struct scanner *s, |
| 1082 |
struct sval ident) |
| 1083 |
{ |
| 1084 |
matcher_rule_set_mlvo_common(m, s, ident, MLVO_MATCH_NORMAL); |
| 1085 |
} |
| 1086 |
|
| 1087 |
static void |
| 1088 |
matcher_rule_set_kccgst(struct matcher *m, struct scanner *s, |
| 1089 |
struct sval ident) |
| 1090 |
{ |
| 1091 |
if (m->rule.num_kccgst_values >= m->mapping.num_kccgst) { |
| 1092 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1093 |
"invalid rule: has more values than the mapping line; " |
| 1094 |
"ignoring rule"); |
| 1095 |
m->rule.skip = true; |
| 1096 |
return; |
| 1097 |
} |
| 1098 |
m->rule.kccgst_value_at_pos[m->rule.num_kccgst_values] = ident; |
| 1099 |
m->rule.num_kccgst_values++; |
| 1100 |
} |
| 1101 |
|
| 1102 |
static bool |
| 1103 |
match_group(struct matcher *m, struct sval group_name, struct sval to) |
| 1104 |
{ |
| 1105 |
struct group *group = NULL; |
| 1106 |
struct sval *element; |
| 1107 |
bool found = false; |
| 1108 |
|
| 1109 |
darray_foreach(group, m->groups) { |
| 1110 |
if (svaleq(group->name, group_name)) { |
| 1111 |
found = true; |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (!found) { |
| 1117 |
/* |
| 1118 |
* rules/evdev intentionally uses some undeclared group names |
| 1119 |
* in rules (e.g. commented group definitions which may be |
| 1120 |
* uncommented if needed). So we continue silently. |
| 1121 |
*/ |
| 1122 |
return false; |
| 1123 |
} |
| 1124 |
|
| 1125 |
darray_foreach(element, group->elements) |
| 1126 |
if (svaleq(to, *element)) |
| 1127 |
return true; |
| 1128 |
|
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
static bool |
| 1133 |
match_value(struct matcher *m, struct sval val, struct sval to, |
| 1134 |
enum mlvo_match_type match_type, |
| 1135 |
enum wildcard_match_type wildcard_type) |
| 1136 |
{ |
| 1137 |
switch (match_type) { |
| 1138 |
case MLVO_MATCH_WILDCARD_LEGACY: |
| 1139 |
/* Match empty values only if explicitly required */ |
| 1140 |
return wildcard_type == WILDCARD_MATCH_ALL || !!to.len; |
| 1141 |
case MLVO_MATCH_WILDCARD_NONE: |
| 1142 |
return !to.len; |
| 1143 |
case MLVO_MATCH_WILDCARD_SOME: |
| 1144 |
return !!to.len; |
| 1145 |
case MLVO_MATCH_WILDCARD_ANY: |
| 1146 |
/* Contrary to the legacy ‘*’, this wild card *always* matches */ |
| 1147 |
return true; |
| 1148 |
case MLVO_MATCH_GROUP: |
| 1149 |
return match_group(m, val, to); |
| 1150 |
default: |
| 1151 |
assert(match_type == MLVO_MATCH_NORMAL); |
| 1152 |
return svaleq(val, to); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
static bool |
| 1157 |
match_value_and_mark(struct matcher *m, struct sval val, |
| 1158 |
struct matched_sval *to, enum mlvo_match_type match_type, |
| 1159 |
enum wildcard_match_type wildcard_type) |
| 1160 |
{ |
| 1161 |
bool matched = match_value(m, val, to->sval, match_type, wildcard_type); |
| 1162 |
if (matched) |
| 1163 |
to->matched = true; |
| 1164 |
return matched; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* |
| 1168 |
* This function performs %-expansion on @value (see overview above), |
| 1169 |
* and appends the result to @expanded. |
| 1170 |
*/ |
| 1171 |
static bool |
| 1172 |
expand_rmlvo_in_kccgst_value(struct matcher *m, struct scanner *s, |
| 1173 |
struct sval value, xkb_layout_index_t layout_idx, |
| 1174 |
darray_char *expanded, size_t *i) |
| 1175 |
{ |
| 1176 |
const char *str = value.start; |
| 1177 |
|
| 1178 |
/* |
| 1179 |
* Some ugly hand-lexing here, but going through the scanner is more |
| 1180 |
* trouble than it's worth, and the format is ugly on its own merit. |
| 1181 |
*/ |
| 1182 |
enum rules_mlvo mlv; |
| 1183 |
xkb_layout_index_t idx; |
| 1184 |
char pfx, sfx; |
| 1185 |
struct matched_sval *expanded_value; |
| 1186 |
|
| 1187 |
/* %i not as layout/variant index "%l[%i]" but as qualifier ":%i" */ |
| 1188 |
if (str[*i] == 'i' && |
| 1189 |
(*i + 1 == value.len || is_merge_mode_prefix(str[*i + 1]))) |
| 1190 |
{ |
| 1191 |
if (layout_idx == XKB_LAYOUT_INVALID) { |
| 1192 |
scanner_err( |
| 1193 |
s, XKB_ERROR_RULES_INVALID_LAYOUT_INDEX_PERCENT_EXPANSION, |
| 1194 |
"Invalid %%i in %%-expansion: there is no corresponding " |
| 1195 |
"layout nor variant in the MLVO fields of the rules header." |
| 1196 |
); |
| 1197 |
goto error; |
| 1198 |
} |
| 1199 |
(*i)++; |
| 1200 |
char index_str[MAX_LAYOUT_INDEX_STR_LENGTH + 1]; |
| 1201 |
int count = snprintf(index_str, sizeof(index_str), "%"PRIu32, |
| 1202 |
layout_idx + 1); |
| 1203 |
darray_appends_nullterminate(*expanded, index_str, count); |
| 1204 |
return true; |
| 1205 |
} |
| 1206 |
|
| 1207 |
pfx = sfx = 0; |
| 1208 |
|
| 1209 |
/* Check for prefix. */ |
| 1210 |
if (str[*i] == '(' || |
| 1211 |
is_merge_mode_prefix(str[*i]) || |
| 1212 |
str[*i] == '_' || str[*i] == '-') { |
| 1213 |
pfx = str[*i]; |
| 1214 |
if (str[*i] == '(') sfx = ')'; |
| 1215 |
if (++(*i) >= value.len) goto error; |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* Mandatory model/layout/variant specifier. */ |
| 1219 |
switch (str[(*i)++]) { |
| 1220 |
case 'm': mlv = MLVO_MODEL; break; |
| 1221 |
case 'l': mlv = MLVO_LAYOUT; break; |
| 1222 |
case 'v': mlv = MLVO_VARIANT; break; |
| 1223 |
default: goto error; |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* Check for index. */ |
| 1227 |
idx = XKB_LAYOUT_INVALID; |
| 1228 |
bool expanded_index = false; |
| 1229 |
if (*i < value.len && str[*i] == '[') { |
| 1230 |
if (mlv != MLVO_LAYOUT && mlv != MLVO_VARIANT) { |
| 1231 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1232 |
"invalid index in %%-expansion; " |
| 1233 |
"may only index layout or variant"); |
| 1234 |
goto error; |
| 1235 |
} |
| 1236 |
|
| 1237 |
int consumed = extract_layout_index(str + (*i), value.len - (*i), &idx); |
| 1238 |
if (consumed == -1) goto error; |
| 1239 |
if (idx == XKB_LAYOUT_INVALID) { |
| 1240 |
/* %i encountered */ |
| 1241 |
assert(layout_idx != XKB_LAYOUT_INVALID); |
| 1242 |
idx = layout_idx; |
| 1243 |
expanded_index = true; |
| 1244 |
} |
| 1245 |
*i += consumed; |
| 1246 |
} |
| 1247 |
|
| 1248 |
/* Check for suffix, if there supposed to be one. */ |
| 1249 |
if (sfx != 0) { |
| 1250 |
if (*i >= value.len) goto error; |
| 1251 |
if (str[(*i)++] != sfx) goto error; |
| 1252 |
} |
| 1253 |
|
| 1254 |
/* Get the expanded value. */ |
| 1255 |
expanded_value = NULL; |
| 1256 |
|
| 1257 |
if (mlv == MLVO_LAYOUT) { |
| 1258 |
if (idx == XKB_LAYOUT_INVALID) { |
| 1259 |
/* No index provided: match only if single layout */ |
| 1260 |
if (darray_size(m->rmlvo.layouts) == 1) |
| 1261 |
expanded_value = &darray_item(m->rmlvo.layouts, 0); |
| 1262 |
/* Some index provided: expand only if it is %i or |
| 1263 |
* if there are multiple layouts */ |
| 1264 |
} else if (idx < darray_size(m->rmlvo.layouts) && |
| 1265 |
(expanded_index || darray_size(m->rmlvo.layouts) > 1)) { |
| 1266 |
expanded_value = &darray_item(m->rmlvo.layouts, idx); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
else if (mlv == MLVO_VARIANT) { |
| 1270 |
if (idx == XKB_LAYOUT_INVALID) { |
| 1271 |
/* No index provided: match only if single variant */ |
| 1272 |
if (darray_size(m->rmlvo.variants) == 1) |
| 1273 |
expanded_value = &darray_item(m->rmlvo.variants, 0); |
| 1274 |
/* Some index provided: expand only if it is %i or |
| 1275 |
* if there are multiple variants */ |
| 1276 |
} else if (idx < darray_size(m->rmlvo.variants) && |
| 1277 |
(expanded_index || darray_size(m->rmlvo.variants) > 1)) { |
| 1278 |
expanded_value = &darray_item(m->rmlvo.variants, idx); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
else if (mlv == MLVO_MODEL) { |
| 1282 |
expanded_value = &m->rmlvo.model; |
| 1283 |
} |
| 1284 |
|
| 1285 |
/* If we didn't get one, skip silently. */ |
| 1286 |
if (!expanded_value || expanded_value->sval.len == 0) { |
| 1287 |
return true; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (pfx != 0) |
| 1291 |
darray_appends_nullterminate(*expanded, &pfx, 1); |
| 1292 |
darray_appends_nullterminate(*expanded, |
| 1293 |
expanded_value->sval.start, |
| 1294 |
(darray_size_t) expanded_value->sval.len); |
| 1295 |
if (sfx != 0) |
| 1296 |
darray_appends_nullterminate(*expanded, &sfx, 1); |
| 1297 |
expanded_value->matched = true; |
| 1298 |
|
| 1299 |
return true; |
| 1300 |
|
| 1301 |
error: |
| 1302 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1303 |
"invalid %%-expansion in value; not used"); |
| 1304 |
return false; |
| 1305 |
} |
| 1306 |
|
| 1307 |
/* |
| 1308 |
* This function performs :all replacement on @value (see overview above), |
| 1309 |
* and appends the result to @expanded. |
| 1310 |
*/ |
| 1311 |
static void |
| 1312 |
expand_qualifier_in_kccgst_value( |
| 1313 |
struct matcher *m, struct scanner *s, |
| 1314 |
struct sval value, darray_char *expanded, |
| 1315 |
bool has_layout_idx_range, bool has_separator, |
| 1316 |
darray_size_t prefix_idx, size_t *i) |
| 1317 |
{ |
| 1318 |
const char *str = value.start; |
| 1319 |
|
| 1320 |
/* “all” followed by nothing or by a layout separator */ |
| 1321 |
if ((*i + 3 <= value.len || is_merge_mode_prefix(str[*i + 3])) && |
| 1322 |
str[*i] == 'a' && str[*i+1] == 'l' && str[*i+2] == 'l') { |
| 1323 |
if (has_layout_idx_range) |
| 1324 |
scanner_vrb(s, XKB_LOG_VERBOSITY_DETAILED, XKB_LOG_MESSAGE_NO_ID, |
| 1325 |
"Using :all qualifier with indices range " |
| 1326 |
"is not recommended."); |
| 1327 |
/* Add at least one layout */ |
| 1328 |
darray_appends_nullterminate(*expanded, "1", 1); |
| 1329 |
/* Check for more layouts (slow path) */ |
| 1330 |
if (darray_size(m->rmlvo.layouts) > 1) { |
| 1331 |
char layout_index[MAX_LAYOUT_INDEX_STR_LENGTH + 1]; |
| 1332 |
const darray_size_t prefix_length = |
| 1333 |
darray_size(*expanded) - prefix_idx - 1; |
| 1334 |
for (xkb_layout_index_t l = 1; |
| 1335 |
l < MIN(XKB_MAX_GROUPS, darray_size(m->rmlvo.layouts)); |
| 1336 |
l++) |
| 1337 |
{ |
| 1338 |
if (!has_separator) |
| 1339 |
darray_append(*expanded, MERGE_DEFAULT_PREFIX); |
| 1340 |
/* Append prefix */ |
| 1341 |
darray_appends_nullterminate(*expanded, |
| 1342 |
&darray_item(*expanded, prefix_idx), |
| 1343 |
prefix_length); |
| 1344 |
/* Append index */ |
| 1345 |
int count = snprintf(layout_index, sizeof(layout_index), |
| 1346 |
"%"PRIu32, l + 1); |
| 1347 |
darray_appends_nullterminate(*expanded, layout_index, count); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
*i += 3; |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
static inline void |
| 1355 |
#ifdef _MSC_VER |
| 1356 |
concat_kccgst(darray_char *into, darray_size_t size, _In_reads_(size) const char* from) |
| 1357 |
#else |
| 1358 |
concat_kccgst(darray_char *into, darray_size_t size, const char from[static size]) |
| 1359 |
#endif |
| 1360 |
{ |
| 1361 |
/* |
| 1362 |
* Appending bar to foo -> foo (not an error if this happens) |
| 1363 |
* Appending +bar to foo -> foo+bar |
| 1364 |
* Appending bar to +foo -> bar+foo |
| 1365 |
* Appending +bar to +foo -> +foo+bar |
| 1366 |
*/ |
| 1367 |
const bool from_plus = is_merge_mode_prefix(from[0]); |
| 1368 |
if (from_plus || darray_empty(*into)) { |
| 1369 |
darray_appends_nullterminate(*into, from, size); |
| 1370 |
} else { |
| 1371 |
const char ch = |
| 1372 |
(char) (darray_empty(*into) ? '\0' : darray_item(*into, 0)); |
| 1373 |
const bool into_plus = is_merge_mode_prefix(ch); |
| 1374 |
if (into_plus) |
| 1375 |
darray_prepends_nullterminate(*into, from, size); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
|
| 1379 |
/* |
| 1380 |
* This function performs %-expansion and :all-expansion on @value |
| 1381 |
* (see overview above), and appends the result to @to. |
| 1382 |
*/ |
| 1383 |
static bool |
| 1384 |
append_expanded_kccgst_value(struct matcher *m, struct scanner *s, |
| 1385 |
bool merge, darray_char *to, struct sval value, |
| 1386 |
xkb_layout_index_t layout_idx) |
| 1387 |
{ |
| 1388 |
const char *str = value.start; |
| 1389 |
darray_char expanded = darray_new(); |
| 1390 |
darray_size_t last_item_idx = 0; |
| 1391 |
bool has_separator = false; |
| 1392 |
|
| 1393 |
for (size_t i = 0; i < value.len; ) { |
| 1394 |
/* Check if that's a start of an expansion or qualifier */ |
| 1395 |
switch (str[i]) { |
| 1396 |
/* Qualifier */ |
| 1397 |
case ':': |
| 1398 |
darray_appends_nullterminate(expanded, &str[i++], 1); |
| 1399 |
expand_qualifier_in_kccgst_value(m, s, value, &expanded, |
| 1400 |
m->mapping.has_layout_idx_range, |
| 1401 |
has_separator, |
| 1402 |
last_item_idx, &i); |
| 1403 |
break; |
| 1404 |
/* Expansion */ |
| 1405 |
case '%': |
| 1406 |
i++; |
| 1407 |
if (i >= value.len || |
| 1408 |
!expand_rmlvo_in_kccgst_value(m, s, value, layout_idx, |
| 1409 |
&expanded, &i)) |
| 1410 |
goto error; |
| 1411 |
break; |
| 1412 |
/* New item */ |
| 1413 |
case MERGE_OVERRIDE_PREFIX: |
| 1414 |
case MERGE_AUGMENT_PREFIX: |
| 1415 |
case MERGE_REPLACE_PREFIX: |
| 1416 |
darray_appends_nullterminate(expanded, &str[i++], 1); |
| 1417 |
last_item_idx = darray_size(expanded) - 1; |
| 1418 |
has_separator = true; |
| 1419 |
break; |
| 1420 |
/* Just a normal character. */ |
| 1421 |
default: |
| 1422 |
darray_appends_nullterminate(expanded, &str[i++], 1); |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
/* See note: “Layout index ranges and merging KcCGST values” */ |
| 1427 |
if (merge) { |
| 1428 |
if (!darray_empty(expanded)) |
| 1429 |
concat_kccgst(to, darray_size(expanded), darray_items(expanded)); |
| 1430 |
} else { |
| 1431 |
darray_concat(*to, expanded); |
| 1432 |
} |
| 1433 |
darray_free(expanded); |
| 1434 |
return true; |
| 1435 |
|
| 1436 |
error: |
| 1437 |
darray_free(expanded); |
| 1438 |
return false; |
| 1439 |
} |
| 1440 |
|
| 1441 |
static bool |
| 1442 |
matcher_append_pending_kccgst(struct matcher *m) |
| 1443 |
{ |
| 1444 |
if (!m->mapping.has_layout_idx_range) |
| 1445 |
return true; |
| 1446 |
/* |
| 1447 |
* Handle pending KcCGST values |
| 1448 |
* See note: “Layout index ranges and merging KcCGST values” |
| 1449 |
*/ |
| 1450 |
for (kccgst_index_t i = 0; i < m->mapping.num_kccgst; i++) { |
| 1451 |
const enum rules_kccgst kccgst = m->mapping.kccgst_at_pos[i]; |
| 1452 |
/* For each relevant layout, append the relevant KcCGST values to |
| 1453 |
* the output. */ |
| 1454 |
for (xkb_layout_index_t layout = m->mapping.layout_idx_min; |
| 1455 |
layout < m->mapping.layout_idx_max; |
| 1456 |
layout++) { |
| 1457 |
/* There may be multiple values to add if the rule set involved |
| 1458 |
* options. Process them sequentially. */ |
| 1459 |
register const struct kccgst_buffer* const buf = &m->pending_kccgst; |
| 1460 |
size_t offset = 0; |
| 1461 |
for (darray_size_t k = 0; k < darray_size(buf->slices); k++) { |
| 1462 |
register const struct kccgst_buffer_slice * const slice = |
| 1463 |
&darray_item(buf->slices, k); |
| 1464 |
if (slice->kccgst == kccgst && slice->layout == layout && |
| 1465 |
slice->length) |
| 1466 |
concat_kccgst(&m->kccgst[kccgst], slice->length, |
| 1467 |
darray_items(buf->buffer) + offset); |
| 1468 |
offset += slice->length; |
| 1469 |
} |
| 1470 |
} |
| 1471 |
} |
| 1472 |
/* Ensure we won’t come here before the next relevant rule set */ |
| 1473 |
m->mapping.has_layout_idx_range = false; |
| 1474 |
return true; |
| 1475 |
} |
| 1476 |
|
| 1477 |
static void |
| 1478 |
matcher_rule_verify(struct matcher *m, struct scanner *s) |
| 1479 |
{ |
| 1480 |
if (m->rule.num_mlvo_values != m->mapping.num_mlvo || |
| 1481 |
m->rule.num_kccgst_values != m->mapping.num_kccgst) { |
| 1482 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1483 |
"invalid rule: must have same number of values " |
| 1484 |
"as mapping line; ignoring rule"); |
| 1485 |
m->rule.skip = true; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
static void |
| 1490 |
matcher_rule_apply_if_matches(struct matcher *m, struct scanner *s) |
| 1491 |
{ |
| 1492 |
/* Initial candidates (used if m->mapping.has_layout_idx_range == true) */ |
| 1493 |
xkb_layout_mask_t candidate_layouts = m->mapping.layouts_candidates_mask; |
| 1494 |
xkb_layout_index_t idx; |
| 1495 |
/* Loop over MLVO pattern components */ |
| 1496 |
for (mlvo_index_t i = 0; i < m->mapping.num_mlvo; i++) { |
| 1497 |
enum rules_mlvo mlvo = m->mapping.mlvo_at_pos[i]; |
| 1498 |
struct sval value = m->rule.mlvo_value_at_pos[i]; |
| 1499 |
enum mlvo_match_type match_type = m->rule.match_type_at_pos[i]; |
| 1500 |
struct matched_sval *to; |
| 1501 |
bool matched = false; |
| 1502 |
|
| 1503 |
/* NOTE: Wild card * matches empty values only for model and options, as |
| 1504 |
* implemented in libxkbfile and xserver. The reason for such different |
| 1505 |
* treatment is not documented. */ |
| 1506 |
if (mlvo == MLVO_MODEL) { |
| 1507 |
to = &m->rmlvo.model; |
| 1508 |
matched = match_value_and_mark(m, value, to, match_type, |
| 1509 |
WILDCARD_MATCH_ALL); |
| 1510 |
} else if (m->mapping.has_layout_idx_range) { |
| 1511 |
/* Special index: loop over the index range */ |
| 1512 |
for (idx = m->mapping.layout_idx_min; |
| 1513 |
idx < m->mapping.layout_idx_max && candidate_layouts; |
| 1514 |
idx++) |
| 1515 |
{ |
| 1516 |
/* Process only if index not skipped */ |
| 1517 |
const xkb_layout_mask_t mask = UINT32_C(1) << idx; |
| 1518 |
if (candidate_layouts & mask) { |
| 1519 |
switch (mlvo) { |
| 1520 |
case MLVO_LAYOUT: |
| 1521 |
to = &darray_item(m->rmlvo.layouts, idx); |
| 1522 |
if (match_value_and_mark(m, value, to, match_type, |
| 1523 |
WILDCARD_MATCH_NONEMPTY)) { |
| 1524 |
/* Mark matched, keep index */ |
| 1525 |
matched = true; |
| 1526 |
} else { |
| 1527 |
/* Not matched, remove index */ |
| 1528 |
candidate_layouts &= ~mask; |
| 1529 |
} |
| 1530 |
break; |
| 1531 |
case MLVO_VARIANT: |
| 1532 |
to = &darray_item(m->rmlvo.variants, idx); |
| 1533 |
if (match_value_and_mark(m, value, to, match_type, |
| 1534 |
WILDCARD_MATCH_NONEMPTY)) { |
| 1535 |
/* Mark matched, keep index */ |
| 1536 |
matched = true; |
| 1537 |
} else { |
| 1538 |
/* Not matched, remove index */ |
| 1539 |
candidate_layouts &= ~mask; |
| 1540 |
} |
| 1541 |
break; |
| 1542 |
default: |
| 1543 |
assert(mlvo == MLVO_OPTION); |
| 1544 |
bool found_option = false; |
| 1545 |
darray_foreach(to, m->rmlvo.options) { |
| 1546 |
/* |
| 1547 |
* Skip if layout-specific option and the target |
| 1548 |
* layout does not match. |
| 1549 |
*/ |
| 1550 |
if (to->layout != OPTIONS_MATCH_ALL_GROUPS && |
| 1551 |
to->layout != idx) |
| 1552 |
continue; |
| 1553 |
if (match_value_and_mark(m, value, to, match_type, |
| 1554 |
WILDCARD_MATCH_ALL)) { |
| 1555 |
/* Mark matched, keep index */ |
| 1556 |
matched = true; |
| 1557 |
found_option = true; |
| 1558 |
break; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
if (!found_option) { |
| 1562 |
/* Not matched, remove index */ |
| 1563 |
candidate_layouts &= ~mask; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
} |
| 1567 |
} |
| 1568 |
} else { |
| 1569 |
/* Numeric index or no index */ |
| 1570 |
switch (mlvo) { |
| 1571 |
case MLVO_LAYOUT: |
| 1572 |
to = &darray_item(m->rmlvo.layouts, |
| 1573 |
m->mapping.layout_idx_min); |
| 1574 |
matched = match_value_and_mark(m, value, to, match_type, |
| 1575 |
WILDCARD_MATCH_NONEMPTY); |
| 1576 |
break; |
| 1577 |
case MLVO_VARIANT: |
| 1578 |
to = &darray_item(m->rmlvo.variants, |
| 1579 |
m->mapping.layout_idx_min); |
| 1580 |
matched = match_value_and_mark(m, value, to, match_type, |
| 1581 |
WILDCARD_MATCH_NONEMPTY); |
| 1582 |
break; |
| 1583 |
default: |
| 1584 |
assert(mlvo == MLVO_OPTION); |
| 1585 |
darray_foreach(to, m->rmlvo.options) { |
| 1586 |
/* |
| 1587 |
* Skip if it is a layout-specific option and either: |
| 1588 |
* - the rule has no layout nor variant field |
| 1589 |
* (layout_idx_min == XKB_LAYOUT_INVALID), or |
| 1590 |
* - the target layout index does not match. |
| 1591 |
*/ |
| 1592 |
if (to->layout != OPTIONS_MATCH_ALL_GROUPS && |
| 1593 |
to->layout != m->mapping.layout_idx_min) |
| 1594 |
continue; |
| 1595 |
matched = match_value_and_mark(m, value, to, match_type, |
| 1596 |
WILDCARD_MATCH_ALL); |
| 1597 |
if (matched) |
| 1598 |
break; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
if (!matched) |
| 1604 |
return; |
| 1605 |
} |
| 1606 |
|
| 1607 |
if (m->mapping.has_layout_idx_range) { |
| 1608 |
/* Special index: loop over the index range */ |
| 1609 |
for (idx = m->mapping.layout_idx_min; |
| 1610 |
idx < m->mapping.layout_idx_max; |
| 1611 |
idx++) |
| 1612 |
{ |
| 1613 |
if (candidate_layouts & (UINT32_C(1) << idx)) { |
| 1614 |
for (kccgst_index_t i = 0; i < m->mapping.num_kccgst; i++) { |
| 1615 |
const enum rules_kccgst kccgst = m->mapping.kccgst_at_pos[i]; |
| 1616 |
const struct sval value = m->rule.kccgst_value_at_pos[i]; |
| 1617 |
/* |
| 1618 |
* [NOTE] Layout index ranges and merging KcCGST values |
| 1619 |
* |
| 1620 |
* Layout indices match following first the order of the |
| 1621 |
* rules in the file, then their natural order. So do not |
| 1622 |
* merge with the output for now but buffer the resulting |
| 1623 |
* KcCGST value and wait reaching the end of the rule set. |
| 1624 |
* |
| 1625 |
* Because the rule set may also involve options, it may |
| 1626 |
* match multiple times for the *same* layout index. So |
| 1627 |
* buffer the result of *each* match. |
| 1628 |
* |
| 1629 |
* When the end of the rule set is reached, merge buffered |
| 1630 |
* KcCGST sequentially, following first the layouts order, |
| 1631 |
* then the order of the rules in the file. |
| 1632 |
* |
| 1633 |
* Example: |
| 1634 |
* |
| 1635 |
* ! model = symbols |
| 1636 |
* * = pc |
| 1637 |
* ! layout[any] option = symbols |
| 1638 |
* C 1 = +c1:%i |
| 1639 |
* C 2 = +c2:%i |
| 1640 |
* B 3 = skip |
| 1641 |
* B 4 = +b:%i |
| 1642 |
* |
| 1643 |
* The result of {layout: "A,B,C", options: "4,3,2,1"} is: |
| 1644 |
* symbols = pc+b:2+c1:3+c2:3. |
| 1645 |
* |
| 1646 |
* - `skip` was dropped because it has no explicit merge |
| 1647 |
* mode; |
| 1648 |
* - although every rule was matched in order, the resulting |
| 1649 |
* order of the symbols follows the order of the layouts, |
| 1650 |
* so `+b` appears before `+c1` and `+c2`. |
| 1651 |
* - the relative order of the options for layout C follows |
| 1652 |
* the order within the rule set, not the order of RMLVO. |
| 1653 |
*/ |
| 1654 |
register struct kccgst_buffer * const buf = |
| 1655 |
&m->pending_kccgst; |
| 1656 |
const darray_size_t prev_buffer_length = |
| 1657 |
darray_size(buf->buffer); |
| 1658 |
append_expanded_kccgst_value(m, s, false, &buf->buffer, |
| 1659 |
value, idx); |
| 1660 |
const uint32_t length = (uint32_t) (darray_size(buf->buffer) |
| 1661 |
- prev_buffer_length); |
| 1662 |
const struct kccgst_buffer_slice slice = { |
| 1663 |
.length = length, |
| 1664 |
.kccgst = kccgst, |
| 1665 |
.layout = idx |
| 1666 |
}; |
| 1667 |
darray_append(buf->slices, slice); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
} |
| 1671 |
} else { |
| 1672 |
/* Numeric index or no index */ |
| 1673 |
for (kccgst_index_t i = 0; i < m->mapping.num_kccgst; i++) { |
| 1674 |
enum rules_kccgst kccgst = m->mapping.kccgst_at_pos[i]; |
| 1675 |
struct sval value = m->rule.kccgst_value_at_pos[i]; |
| 1676 |
append_expanded_kccgst_value(m, s, true, &m->kccgst[kccgst], value, |
| 1677 |
m->mapping.layout_idx_min); |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
/* |
| 1682 |
* If a rule matches in a rule set, the rest of the set should be |
| 1683 |
* skipped. However, rule sets matching against options may contain |
| 1684 |
* several legitimate rules, so they are processed entirely. |
| 1685 |
*/ |
| 1686 |
if (!(is_mlvo_mask_defined(m, MLVO_OPTION))) { |
| 1687 |
m->mapping.layouts_candidates_mask &= ~candidate_layouts; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
static enum rules_token |
| 1692 |
gettok(struct matcher *m, struct scanner *s) |
| 1693 |
{ |
| 1694 |
return lex(s, &m->val); |
| 1695 |
} |
| 1696 |
|
| 1697 |
static bool |
| 1698 |
matcher_match(struct matcher *m, struct scanner *s, |
| 1699 |
unsigned int include_depth, |
| 1700 |
const char *string, size_t len, |
| 1701 |
const char *file_name) |
| 1702 |
{ |
| 1703 |
enum rules_token tok; |
| 1704 |
|
| 1705 |
if (!m) |
| 1706 |
return false; |
| 1707 |
|
| 1708 |
initial: |
| 1709 |
switch (tok = gettok(m, s)) { |
| 1710 |
case TOK_BANG: |
| 1711 |
goto bang; |
| 1712 |
case TOK_END_OF_LINE: |
| 1713 |
goto initial; |
| 1714 |
case TOK_END_OF_FILE: |
| 1715 |
goto finish; |
| 1716 |
default: |
| 1717 |
goto unexpected; |
| 1718 |
} |
| 1719 |
|
| 1720 |
bang: |
| 1721 |
switch (tok = gettok(m, s)) { |
| 1722 |
case TOK_GROUP_NAME: |
| 1723 |
matcher_group_start_new(m, m->val.string); |
| 1724 |
goto group_name; |
| 1725 |
case TOK_INCLUDE: |
| 1726 |
goto include_statement; |
| 1727 |
case TOK_IDENTIFIER: |
| 1728 |
matcher_mapping_start_new(m); |
| 1729 |
matcher_mapping_set_mlvo(m, s, m->val.string); |
| 1730 |
goto mapping_mlvo; |
| 1731 |
default: |
| 1732 |
goto unexpected; |
| 1733 |
} |
| 1734 |
|
| 1735 |
group_name: |
| 1736 |
switch (tok = gettok(m, s)) { |
| 1737 |
case TOK_EQUALS: |
| 1738 |
goto group_element; |
| 1739 |
default: |
| 1740 |
goto unexpected; |
| 1741 |
} |
| 1742 |
|
| 1743 |
group_element: |
| 1744 |
switch (tok = gettok(m, s)) { |
| 1745 |
case TOK_IDENTIFIER: |
| 1746 |
matcher_group_add_element(m, s, m->val.string); |
| 1747 |
goto group_element; |
| 1748 |
case TOK_END_OF_LINE: |
| 1749 |
goto initial; |
| 1750 |
default: |
| 1751 |
goto unexpected; |
| 1752 |
} |
| 1753 |
|
| 1754 |
include_statement: |
| 1755 |
switch (tok = gettok(m, s)) { |
| 1756 |
case TOK_IDENTIFIER: |
| 1757 |
matcher_include(m, s, include_depth, m->val.string); |
| 1758 |
goto include_statement_end; |
| 1759 |
default: |
| 1760 |
goto unexpected; |
| 1761 |
} |
| 1762 |
|
| 1763 |
include_statement_end: |
| 1764 |
switch (tok = gettok(m, s)) { |
| 1765 |
case TOK_END_OF_LINE: |
| 1766 |
goto initial; |
| 1767 |
default: |
| 1768 |
goto unexpected; |
| 1769 |
} |
| 1770 |
|
| 1771 |
mapping_mlvo: |
| 1772 |
switch (tok = gettok(m, s)) { |
| 1773 |
case TOK_IDENTIFIER: |
| 1774 |
if (m->mapping.active) |
| 1775 |
matcher_mapping_set_mlvo(m, s, m->val.string); |
| 1776 |
goto mapping_mlvo; |
| 1777 |
case TOK_EQUALS: |
| 1778 |
goto mapping_kccgst; |
| 1779 |
default: |
| 1780 |
goto unexpected; |
| 1781 |
} |
| 1782 |
|
| 1783 |
mapping_kccgst: |
| 1784 |
switch (tok = gettok(m, s)) { |
| 1785 |
case TOK_IDENTIFIER: |
| 1786 |
if (m->mapping.active) |
| 1787 |
matcher_mapping_set_kccgst(m, s, m->val.string); |
| 1788 |
goto mapping_kccgst; |
| 1789 |
case TOK_END_OF_LINE: |
| 1790 |
if (m->mapping.active && matcher_mapping_verify(m, s)) { |
| 1791 |
matcher_mapping_set_layout_bounds(m); |
| 1792 |
if (m->mapping.has_layout_idx_range) { |
| 1793 |
/* Lazily reset buffers for layout index ranges. |
| 1794 |
* We’ll reuse the allocations. */ |
| 1795 |
darray_size(m->pending_kccgst.buffer) = 0; |
| 1796 |
darray_size(m->pending_kccgst.slices) = 0; |
| 1797 |
} |
| 1798 |
} |
| 1799 |
goto rule_mlvo_first; |
| 1800 |
default: |
| 1801 |
goto unexpected; |
| 1802 |
} |
| 1803 |
|
| 1804 |
rule_mlvo_first: |
| 1805 |
switch (tok = gettok(m, s)) { |
| 1806 |
case TOK_BANG: |
| 1807 |
matcher_append_pending_kccgst(m); |
| 1808 |
goto bang; |
| 1809 |
case TOK_END_OF_LINE: |
| 1810 |
goto rule_mlvo_first; |
| 1811 |
case TOK_END_OF_FILE: |
| 1812 |
matcher_append_pending_kccgst(m); |
| 1813 |
goto finish; |
| 1814 |
default: |
| 1815 |
matcher_rule_start_new(m); |
| 1816 |
goto rule_mlvo_no_tok; |
| 1817 |
} |
| 1818 |
|
| 1819 |
rule_mlvo: |
| 1820 |
tok = gettok(m, s); |
| 1821 |
rule_mlvo_no_tok: |
| 1822 |
switch (tok) { |
| 1823 |
case TOK_IDENTIFIER: |
| 1824 |
if (!m->rule.skip) { |
| 1825 |
if (m->val.string.len == 1 && m->val.string.start[0] == '+') |
| 1826 |
matcher_rule_set_mlvo_wildcard(m, s, MLVO_MATCH_WILDCARD_SOME); |
| 1827 |
else |
| 1828 |
matcher_rule_set_mlvo(m, s, m->val.string); |
| 1829 |
} |
| 1830 |
goto rule_mlvo; |
| 1831 |
case TOK_WILD_CARD_STAR: |
| 1832 |
if (!m->rule.skip) |
| 1833 |
matcher_rule_set_mlvo_wildcard(m, s, MLVO_MATCH_WILDCARD_LEGACY); |
| 1834 |
goto rule_mlvo; |
| 1835 |
case TOK_WILD_CARD_NONE: |
| 1836 |
if (!m->rule.skip) |
| 1837 |
matcher_rule_set_mlvo_wildcard(m, s, MLVO_MATCH_WILDCARD_NONE); |
| 1838 |
goto rule_mlvo; |
| 1839 |
case TOK_WILD_CARD_SOME: |
| 1840 |
if (!m->rule.skip) |
| 1841 |
matcher_rule_set_mlvo_wildcard(m, s, MLVO_MATCH_WILDCARD_SOME); |
| 1842 |
goto rule_mlvo; |
| 1843 |
case TOK_WILD_CARD_ANY: |
| 1844 |
if (!m->rule.skip) |
| 1845 |
matcher_rule_set_mlvo_wildcard(m, s, MLVO_MATCH_WILDCARD_ANY); |
| 1846 |
goto rule_mlvo; |
| 1847 |
case TOK_GROUP_NAME: |
| 1848 |
if (!m->rule.skip) |
| 1849 |
matcher_rule_set_mlvo_group(m, s, m->val.string); |
| 1850 |
goto rule_mlvo; |
| 1851 |
case TOK_EQUALS: |
| 1852 |
goto rule_kccgst; |
| 1853 |
default: |
| 1854 |
goto unexpected; |
| 1855 |
} |
| 1856 |
|
| 1857 |
rule_kccgst: |
| 1858 |
switch (tok = gettok(m, s)) { |
| 1859 |
case TOK_IDENTIFIER: |
| 1860 |
if (!m->rule.skip) |
| 1861 |
matcher_rule_set_kccgst(m, s, m->val.string); |
| 1862 |
goto rule_kccgst; |
| 1863 |
case TOK_END_OF_LINE: |
| 1864 |
if (!m->rule.skip) |
| 1865 |
matcher_rule_verify(m, s); |
| 1866 |
if (!m->rule.skip) |
| 1867 |
matcher_rule_apply_if_matches(m, s); |
| 1868 |
goto rule_mlvo_first; |
| 1869 |
default: |
| 1870 |
goto unexpected; |
| 1871 |
} |
| 1872 |
|
| 1873 |
unexpected: |
| 1874 |
switch (tok) { |
| 1875 |
case TOK_ERROR: |
| 1876 |
goto error; |
| 1877 |
default: |
| 1878 |
goto state_error; |
| 1879 |
} |
| 1880 |
|
| 1881 |
finish: |
| 1882 |
return true; |
| 1883 |
|
| 1884 |
state_error: |
| 1885 |
scanner_err(s, XKB_ERROR_INVALID_RULES_SYNTAX, |
| 1886 |
"unexpected token"); |
| 1887 |
error: |
| 1888 |
return false; |
| 1889 |
} |
| 1890 |
|
| 1891 |
static bool |
| 1892 |
read_rules_file(struct xkb_context *ctx, |
| 1893 |
struct matcher *matcher, |
| 1894 |
unsigned int include_depth, |
| 1895 |
FILE *file, |
| 1896 |
const char *path) |
| 1897 |
{ |
| 1898 |
bool ret; |
| 1899 |
char *string; |
| 1900 |
size_t size; |
| 1901 |
struct scanner scanner; |
| 1902 |
|
| 1903 |
if (!map_file(file, &string, &size)) { |
| 1904 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 1905 |
"Couldn't read rules file \"%s\": %s\n", |
| 1906 |
path, strerror(errno)); |
| 1907 |
return false; |
| 1908 |
} |
| 1909 |
|
| 1910 |
scanner_init(&scanner, matcher->ctx, string, size, path, NULL); |
| 1911 |
|
| 1912 |
/* Basic detection of wrong character encoding. |
| 1913 |
The first character relevant to the grammar must be ASCII: |
| 1914 |
whitespace, !, / (for comment) */ |
| 1915 |
if (!scanner_check_supported_char_encoding(&scanner)) { |
| 1916 |
scanner_err(&scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 1917 |
"This could be a file encoding issue. " |
| 1918 |
"Supported encodings must be backward compatible with ASCII."); |
| 1919 |
scanner_err(&scanner, XKB_ERROR_INVALID_FILE_ENCODING, |
| 1920 |
"E.g. ISO/CEI 8859 and UTF-8 are supported " |
| 1921 |
"but UTF-16, UTF-32 and CP1026 are not."); |
| 1922 |
unmap_file(string, size); |
| 1923 |
return false; |
| 1924 |
} |
| 1925 |
|
| 1926 |
ret = matcher_match(matcher, &scanner, include_depth, string, size, path); |
| 1927 |
unmap_file(string, size); |
| 1928 |
return ret; |
| 1929 |
} |
| 1930 |
|
| 1931 |
/** |
| 1932 |
* XKB extension: append all *.pre or *.post optional partial rules files. |
| 1933 |
* This is a lightweight extension mechanism to enable *simple* rules files |
| 1934 |
* composition: rules files are just processed sequentially using the matcher |
| 1935 |
* resulting from the previous file. |
| 1936 |
*/ |
| 1937 |
static bool |
| 1938 |
xkb_resolve_partial_rules(struct xkb_context *ctx, char *path, size_t path_size, |
| 1939 |
const char* rules, const char* suffix, |
| 1940 |
struct matcher *matcher) |
| 1941 |
{ |
| 1942 |
/* Set partial rules filename: canonical rules filename + suffix */ |
| 1943 |
char partial_rules[60]; /* Arbitrary, but we do not expect long names */ |
| 1944 |
if (unlikely(!snprintf_safe(partial_rules, sizeof(partial_rules), |
| 1945 |
"%s%s", rules, suffix))) { |
| 1946 |
log_err(ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 1947 |
"Cannot load XKB rules \"%s%s\"\n", rules, suffix); |
| 1948 |
return false; |
| 1949 |
} |
| 1950 |
|
| 1951 |
/* |
| 1952 |
* Traverse all include paths and resolve all corresponding partial rules. |
| 1953 |
* These files are optional, but required to be valid if present. |
| 1954 |
*/ |
| 1955 |
unsigned int offset = 0; |
| 1956 |
FILE *file = NULL; |
| 1957 |
const size_t len = strlen(partial_rules); |
| 1958 |
while ((file = FindFileInXkbPath(ctx, "(unknown)", |
| 1959 |
partial_rules, len, FILE_TYPE_RULES, |
| 1960 |
path, path_size, &offset, false)) != NULL) { |
| 1961 |
const bool ok = read_rules_file(ctx, matcher, 0, file, path); |
| 1962 |
fclose(file); |
| 1963 |
if (!ok) { |
| 1964 |
log_err(ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 1965 |
"Error while parsing XKB rules \"%s\"\n", path); |
| 1966 |
return false; |
| 1967 |
} |
| 1968 |
offset++; |
| 1969 |
} |
| 1970 |
|
| 1971 |
return true; |
| 1972 |
} |
| 1973 |
|
| 1974 |
static bool |
| 1975 |
xkb_resolve_rules(struct xkb_context *ctx, |
| 1976 |
const char* rules, struct matcher *matcher, |
| 1977 |
struct xkb_component_names *out, |
| 1978 |
xkb_layout_index_t *explicit_layouts) |
| 1979 |
{ |
| 1980 |
bool ret = false; |
| 1981 |
|
| 1982 |
/* First check if the main rules file exists or error early */ |
| 1983 |
unsigned int offset = 0; |
| 1984 |
char path[PATH_MAX]; |
| 1985 |
FILE * const file = FindFileInXkbPath(ctx, "(unknown)", |
| 1986 |
rules, strlen(rules), FILE_TYPE_RULES, |
| 1987 |
path, sizeof(path), &offset, true); |
| 1988 |
if (!file) { |
| 1989 |
log_err(ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 1990 |
"Cannot load XKB rules \"%s\"\n", rules); |
| 1991 |
goto err_out; |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* |
| 1995 |
* Final rule set is equivalent to: |
| 1996 |
* |
| 1997 |
* ! include <include path 1>/rules/<rules>.pre // only if defined |
| 1998 |
* … |
| 1999 |
* ! include <include path n>/rules/<rules>.pre // only if defined |
| 2000 |
* ! include <rules> // main rules file |
| 2001 |
* ! include <include path 1>/rules/<rules>.post // only if defined |
| 2002 |
* … |
| 2003 |
* ! include <include path n>/rules/<rules>.post // only if defined |
| 2004 |
*/ |
| 2005 |
|
| 2006 |
/* XKB extension: resolve optional <rules>.pre files */ |
| 2007 |
ret = xkb_resolve_partial_rules(ctx, path, sizeof(path), |
| 2008 |
rules, ".pre", matcher); |
| 2009 |
if (!ret) |
| 2010 |
goto err_out; |
| 2011 |
|
| 2012 |
/* Resolve main <rules> file */ |
| 2013 |
ret = read_rules_file(ctx, matcher, 0, file, path); |
| 2014 |
if (!ret) { |
| 2015 |
log_err(ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2016 |
"Error while parsing XKB rules \"%s\"\n", path); |
| 2017 |
goto err_out; |
| 2018 |
} |
| 2019 |
|
| 2020 |
/* XKB extension: resolve optional <rules>.post files */ |
| 2021 |
ret = xkb_resolve_partial_rules(ctx, path, sizeof(path), |
| 2022 |
rules, ".post", matcher); |
| 2023 |
if (!ret) |
| 2024 |
goto err_out; |
| 2025 |
|
| 2026 |
/* Check we got required components */ |
| 2027 |
if (darray_empty(matcher->kccgst[KCCGST_KEYCODES]) || |
| 2028 |
darray_empty(matcher->kccgst[KCCGST_TYPES]) || |
| 2029 |
darray_empty(matcher->kccgst[KCCGST_COMPAT]) || |
| 2030 |
/* darray_empty(matcher->kccgst[KCCGST_GEOMETRY]) || */ |
| 2031 |
darray_empty(matcher->kccgst[KCCGST_SYMBOLS])) { |
| 2032 |
log_err(ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2033 |
"No components returned from XKB rules \"%s\"\n", rules); |
| 2034 |
ret = false; |
| 2035 |
goto err_out; |
| 2036 |
} |
| 2037 |
|
| 2038 |
darray_steal(matcher->kccgst[KCCGST_KEYCODES], &out->keycodes, NULL); |
| 2039 |
darray_steal(matcher->kccgst[KCCGST_TYPES], &out->types, NULL); |
| 2040 |
darray_steal(matcher->kccgst[KCCGST_COMPAT], &out->compatibility, NULL); |
| 2041 |
darray_steal(matcher->kccgst[KCCGST_SYMBOLS], &out->symbols, NULL); |
| 2042 |
darray_steal(matcher->kccgst[KCCGST_GEOMETRY], &out->geometry, NULL); |
| 2043 |
|
| 2044 |
struct matched_sval *mval = &matcher->rmlvo.model; |
| 2045 |
if (!mval->matched && mval->sval.len > 0) |
| 2046 |
log_err(matcher->ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2047 |
"Unrecognized RMLVO model \"%.*s\" was ignored\n", |
| 2048 |
(unsigned int) mval->sval.len, mval->sval.start); |
| 2049 |
darray_foreach(mval, matcher->rmlvo.layouts) |
| 2050 |
if (!mval->matched && mval->sval.len > 0) |
| 2051 |
log_err(matcher->ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2052 |
"Unrecognized RMLVO layout \"%.*s\" was ignored\n", |
| 2053 |
(unsigned int) mval->sval.len, mval->sval.start); |
| 2054 |
darray_foreach(mval, matcher->rmlvo.variants) |
| 2055 |
if (!mval->matched && mval->sval.len > 0) |
| 2056 |
log_err(matcher->ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2057 |
"Unrecognized RMLVO variant \"%.*s\" was ignored\n", |
| 2058 |
(unsigned int) mval->sval.len, mval->sval.start); |
| 2059 |
darray_foreach(mval, matcher->rmlvo.options) |
| 2060 |
if (!mval->matched && mval->sval.len > 0) |
| 2061 |
log_err(matcher->ctx, XKB_ERROR_CANNOT_RESOLVE_RMLVO, |
| 2062 |
"Unrecognized RMLVO option \"%.*s\" was ignored\n", |
| 2063 |
(unsigned int) mval->sval.len, mval->sval.start); |
| 2064 |
|
| 2065 |
/* Set the number of explicit layouts */ |
| 2066 |
if (out->symbols != NULL && explicit_layouts != NULL) { |
| 2067 |
*explicit_layouts = 1; /* at least one group */ |
| 2068 |
const char *symbols = out->symbols; |
| 2069 |
/* Take the highest modifier */ |
| 2070 |
while ((symbols = strchr(symbols, ':')) != NULL && symbols[1] != '\0') { |
| 2071 |
xkb_layout_index_t group = 0; |
| 2072 |
const int count = parse_dec_to_uint32_t(++symbols, SIZE_MAX, &group); |
| 2073 |
/* Update only when valid group index, but continue parsing |
| 2074 |
* even on invalid ones, as we do not handle them here. */ |
| 2075 |
if (count > 0 && (symbols[count] == '\0' || |
| 2076 |
is_merge_mode_prefix(symbols[count])) && |
| 2077 |
group > 0 && group <= XKB_MAX_GROUPS) { |
| 2078 |
*explicit_layouts = MAX(*explicit_layouts, group); |
| 2079 |
symbols += count; |
| 2080 |
} |
| 2081 |
} |
| 2082 |
} |
| 2083 |
|
| 2084 |
err_out: |
| 2085 |
if (file) |
| 2086 |
fclose(file); |
| 2087 |
|
| 2088 |
return ret; |
| 2089 |
} |
| 2090 |
|
| 2091 |
bool |
| 2092 |
xkb_components_from_rmlvo_builder(const struct xkb_rmlvo_builder *rmlvo, |
| 2093 |
struct xkb_component_names *out, |
| 2094 |
xkb_layout_index_t *explicit_layouts) |
| 2095 |
{ |
| 2096 |
const char *rules = rmlvo->rules; |
| 2097 |
struct matcher *matcher = matcher_new_from_rmlvo(rmlvo, &rules); |
| 2098 |
if (!matcher) |
| 2099 |
return false; |
| 2100 |
|
| 2101 |
const bool ret = xkb_resolve_rules(rmlvo->ctx, rules, matcher, |
| 2102 |
out, explicit_layouts); |
| 2103 |
|
| 2104 |
matcher_free(matcher); |
| 2105 |
return ret; |
| 2106 |
} |
| 2107 |
|
| 2108 |
|
| 2109 |
bool |
| 2110 |
xkb_components_from_rules_names(struct xkb_context *ctx, |
| 2111 |
const struct xkb_rule_names *rmlvo, |
| 2112 |
struct xkb_component_names *out, |
| 2113 |
xkb_layout_index_t *explicit_layouts) |
| 2114 |
{ |
| 2115 |
struct matcher *matcher = matcher_new_from_names(ctx, rmlvo); |
| 2116 |
if (!matcher) |
| 2117 |
return false; |
| 2118 |
|
| 2119 |
const bool ret = xkb_resolve_rules(ctx, rmlvo->rules, matcher, |
| 2120 |
out, explicit_layouts); |
| 2121 |
|
| 2122 |
matcher_free(matcher); |
| 2123 |
return ret; |
| 2124 |
} |