1 /************************************************************
2 * Copyright (c) 1996 by Silicon Graphics Computer Systems, Inc.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of Silicon Graphics not be
10 * used in advertising or publicity pertaining to distribution
11 * of the software without specific prior written permission.
12 * Silicon Graphics makes no representation about the suitability
13 * of this software for any purpose. It is provided "as is"
14 * without any express or implied warranty.
16 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
23 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 ********************************************************/
28 * Copyright © 2012 Ran Benita <ran234@gmail.com>
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
37 * The above copyright notice and this permission notice (including the next
38 * paragraph) shall be included in all copies or substantial portions of the
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47 * DEALINGS IN THE SOFTWARE.
50 #include "xkbcomp-priv.h"
53 #include "scanner-utils.h"
58 * The purpose of this file is to map between configuration values that
59 * are easy for a user to specify and understand, and the configuration
60 * values xkbcomp uses and understands.
61 * xkbcomp uses the xkb_component_names struct, which maps directly to
62 * include statements of the appropriate sections, called for short
63 * KcCGST (see keycodes.c, types.c, compat.c, symbols.c; geometry.c was
64 * removed). These are not really intuitive or straight-forward for
66 * Instead, the user passes in a xkb_rule_names struct, which consists
67 * of the name of a rules file (in Linux this is usually "evdev"), a
68 * keyboard model (e.g. "pc105"), a set of layouts (which will end up
69 * in different groups, e.g. "us,fr"), variants (used to alter/augment
70 * the respective layout, e.g. "intl,dvorak"), and a set of options
71 * (used to tweak some general behavior of the keyboard, e.g.
72 * "ctrl:nocaps,compose:menu" to make the Caps Lock key act like Ctrl
73 * and the Menu key like Compose). We call these RMLVO.
77 * The file consists of rule sets, each consisting of rules (one per
78 * line), which match the MLVO values on the left hand side, and, if
79 * the values match to the values the user passed in, results in the
80 * values on the right hand side being added to the resulting KcCGST.
81 * Since some values are related and repeated often, it is possible
82 * to group them together and refer to them by a group name in the
84 * Along with matching values by simple string equality, and for
85 * membership in a group defined previously, rules may also contain
86 * "wildcard" values - "*" - which always match. These usually appear
91 * (It might be helpful to look at a file like rules/evdev along with
92 * this grammer. Comments, whitespace, etc. are not shown.)
94 * File ::= { "!" (Group | RuleSet) }
96 * Group ::= GroupName "=" { GroupElement } "\n"
97 * GroupName ::= "$"<ident>
98 * GroupElement ::= <ident>
100 * RuleSet ::= Mapping { Rule }
102 * Mapping ::= { Mlvo } "=" { Kccgst } "\n"
103 * Mlvo ::= "model" | "option" | ("layout" | "variant") [ Index ]
104 * Index ::= "[" 1..XKB_NUM_GROUPS "]"
105 * Kccgst ::= "keycodes" | "symbols" | "types" | "compat" | "geometry"
107 * Rule ::= { MlvoValue } "=" { KccgstValue } "\n"
108 * MlvoValue ::= "*" | GroupName | <ident>
109 * KccgstValue ::= <ident>
112 * - The order of values in a Rule must be the same as the Mapping it
113 * follows. The mapping line determines the meaning of the values in
114 * the rules which follow in the RuleSet.
115 * - If a Rule is matched, %-expansion is performed on the KccgstValue,
118 * The model, layout or variant, if only one was given (e.g.
119 * %l for "us,il" is invalid).
121 * Layout or variant for the specified group Index, if more than
122 * one was given (e.g. %l[1] for "us" is invalid).
123 * %+m, %+l, %+v, %+l[1], %+v[1]
124 * As above, but prefixed with '+'. Similarly, '|', '-', '_' may be
125 * used instead of '+'.
126 * %(m), %(l), %(l[1]), %(v), %(v[1]):
127 * As above, but prefixed by '(' and suffixed by ')'.
128 * In case the expansion is invalid, as described above, it is
129 * skipped (the rest of the string is still processed); this includes
130 * the prefix and suffix (that's why you shouldn't use e.g. "(%v[1])").
133 /* Scanner / Lexer */
135 /* Values returned with some tokens, like yylval. */
141 * Holds the location in the file of the last processed token,
159 /* C99 is stupid. Just use the 1 variant when there are no args. */
160 #define scanner_error1(scanner, loc, msg) \
161 log_warn((scanner)->ctx, "rules/%s:%d:%d: %s\n", \
162 (scanner)->file_name, (loc)->line, (loc)->column, msg)
163 #define scanner_error(scanner, loc, fmt, ...) \
164 log_warn((scanner)->ctx, "rules/%s:%d:%d: " fmt "\n", \
165 (scanner)->file_name, (loc)->line, (loc)->column, __VA_ARGS__)
170 return is_graph(ch) && ch != '\\';
173 static enum rules_token
174 lex(struct scanner *s, union lvalue *val, struct location *loc)
176 skip_more_whitespace_and_comments:
178 while (chr(s, ' ') || chr(s, '\t'));
182 while (!eof(s) && !eol(s)) next(s);
187 while (eol(s)) next(s);
188 return TOK_END_OF_LINE;
191 /* Escaped line continuation. */
194 scanner_error1(s, loc,
195 "illegal new line escape; must appear at end of line");
199 goto skip_more_whitespace_and_comments;
202 /* See if we're done. */
203 if (eof(s)) return TOK_END_OF_FILE;
207 loc->column = s->column;
209 /* Operators and punctuation. */
210 if (chr(s, '!')) return TOK_BANG;
211 if (chr(s, '=')) return TOK_EQUALS;
212 if (chr(s, '*')) return TOK_STAR;
216 val->string.start = s->s + s->pos;
218 while (is_ident(peek(s))) {
222 if (val->string.len == 0) {
223 scanner_error1(s, loc,
224 "unexpected character after \'$\'; expected name");
227 return TOK_GROUP_NAME;
231 if (is_ident(peek(s))) {
232 val->string.start = s->s + s->pos;
234 while (is_ident(peek(s))) {
238 return TOK_IDENTIFIER;
241 scanner_error1(s, loc, "unrecognized token");
245 /***====================================================================***/
255 #define SVAL_LIT(literal) { literal, sizeof(literal) - 1 }
257 static const struct sval rules_mlvo_svals[_MLVO_NUM_ENTRIES] = {
258 [MLVO_MODEL] = SVAL_LIT("model"),
259 [MLVO_LAYOUT] = SVAL_LIT("layout"),
260 [MLVO_VARIANT] = SVAL_LIT("variant"),
261 [MLVO_OPTION] = SVAL_LIT("option"),
273 static const struct sval rules_kccgst_svals[_KCCGST_NUM_ENTRIES] = {
274 [KCCGST_KEYCODES] = SVAL_LIT("keycodes"),
275 [KCCGST_TYPES] = SVAL_LIT("types"),
276 [KCCGST_COMPAT] = SVAL_LIT("compat"),
277 [KCCGST_SYMBOLS] = SVAL_LIT("symbols"),
278 [KCCGST_GEOMETRY] = SVAL_LIT("geometry"),
282 * A broken-down version of xkb_rule_names (without the rules,
288 darray_sval variants;
294 darray_sval elements;
298 int mlvo_at_pos[_MLVO_NUM_ENTRIES];
299 unsigned int num_mlvo;
300 unsigned int defined_mlvo_mask;
301 xkb_layout_index_t layout_idx, variant_idx;
302 int kccgst_at_pos[_KCCGST_NUM_ENTRIES];
303 unsigned int num_kccgst;
304 unsigned int defined_kccgst_mask;
308 enum mlvo_match_type {
309 MLVO_MATCH_NORMAL = 0,
315 struct sval mlvo_value_at_pos[_MLVO_NUM_ENTRIES];
316 enum mlvo_match_type match_type_at_pos[_MLVO_NUM_ENTRIES];
317 unsigned int num_mlvo_values;
318 struct sval kccgst_value_at_pos[_KCCGST_NUM_ENTRIES];
319 unsigned int num_kccgst_values;
324 * This is the main object used to match a given RMLVO against a rules
325 * file and aggragate the results in a KcCGST. It goes through a simple
326 * matching state machine, with tokens as transitions (see
330 struct xkb_context *ctx;
332 struct rule_names rmlvo;
335 struct scanner scanner;
336 darray(struct group) groups;
337 /* Current mapping. */
338 struct mapping mapping;
342 darray_char kccgst[_KCCGST_NUM_ENTRIES];
346 strip_spaces(struct sval v)
348 while (v.len > 0 && is_space(v.start[0])) { v.len--; v.start++; }
349 while (v.len > 0 && is_space(v.start[v.len - 1])) v.len--;
354 split_comma_separated_string(const char *s)
356 darray_sval arr = darray_new();
359 * Make sure the array returned by this function always includes at
360 * least one value, e.g. "" -> { "" } and "," -> { "", "" }.
364 struct sval val = { NULL, 0 };
365 darray_append(arr, val);
370 struct sval val = { s, 0 };
371 while (*s != '\0' && *s != ',') { s++; val.len++; }
372 darray_append(arr, strip_spaces(val));
373 if (*s == '\0') break;
380 static struct matcher *
381 matcher_new(struct xkb_context *ctx,
382 const struct xkb_rule_names *rmlvo)
384 struct matcher *m = calloc(1, sizeof(*m));
389 m->rmlvo.model.start = rmlvo->model;
390 m->rmlvo.model.len = strlen_safe(rmlvo->model);
391 m->rmlvo.layouts = split_comma_separated_string(rmlvo->layout);
392 m->rmlvo.variants = split_comma_separated_string(rmlvo->variant);
393 m->rmlvo.options = split_comma_separated_string(rmlvo->options);
399 matcher_free(struct matcher *m)
404 darray_free(m->rmlvo.layouts);
405 darray_free(m->rmlvo.variants);
406 darray_free(m->rmlvo.options);
407 darray_foreach(group, m->groups)
408 darray_free(group->elements);
409 darray_free(m->groups);
413 #define matcher_error1(matcher, msg) \
414 scanner_error1(&(matcher)->scanner, &(matcher)->loc, msg)
415 #define matcher_error(matcher, fmt, ...) \
416 scanner_error(&(matcher)->scanner, &(matcher)->loc, fmt, __VA_ARGS__)
419 matcher_group_start_new(struct matcher *m, struct sval name)
421 struct group group = { .name = name, .elements = darray_new() };
422 darray_append(m->groups, group);
426 matcher_group_add_element(struct matcher *m, struct sval element)
428 darray_append(darray_item(m->groups, darray_size(m->groups) - 1).elements,
433 matcher_mapping_start_new(struct matcher *m)
435 for (unsigned i = 0; i < _MLVO_NUM_ENTRIES; i++)
436 m->mapping.mlvo_at_pos[i] = -1;
437 for (unsigned i = 0; i < _KCCGST_NUM_ENTRIES; i++)
438 m->mapping.kccgst_at_pos[i] = -1;
439 m->mapping.layout_idx = m->mapping.variant_idx = XKB_LAYOUT_INVALID;
440 m->mapping.num_mlvo = m->mapping.num_kccgst = 0;
441 m->mapping.defined_mlvo_mask = 0;
442 m->mapping.defined_kccgst_mask = 0;
443 m->mapping.skip = false;
447 extract_layout_index(const char *s, size_t max_len, xkb_layout_index_t *out)
449 /* This function is pretty stupid, but works for now. */
450 *out = XKB_LAYOUT_INVALID;
453 if (s[0] != '[' || !is_digit(s[1]) || s[2] != ']')
455 if (s[1] - '0' < 1 || s[1] - '0' > XKB_MAX_GROUPS)
457 /* To zero-based index. */
458 *out = s[1] - '0' - 1;
463 matcher_mapping_set_mlvo(struct matcher *m, struct sval ident)
465 enum rules_mlvo mlvo;
466 struct sval mlvo_sval;
468 for (mlvo = 0; mlvo < _MLVO_NUM_ENTRIES; mlvo++) {
469 mlvo_sval = rules_mlvo_svals[mlvo];
471 if (svaleq_prefix(mlvo_sval, ident))
476 if (mlvo >= _MLVO_NUM_ENTRIES) {
478 "invalid mapping: %.*s is not a valid value here; "
480 ident.len, ident.start);
481 m->mapping.skip = true;
485 if (m->mapping.defined_mlvo_mask & (1 << mlvo)) {
487 "invalid mapping: %.*s appears twice on the same line; "
489 mlvo_sval.len, mlvo_sval.start);
490 m->mapping.skip = true;
494 /* If there are leftovers still, it must be an index. */
495 if (mlvo_sval.len < ident.len) {
496 xkb_layout_index_t idx;
497 int consumed = extract_layout_index(ident.start + mlvo_sval.len,
498 ident.len - mlvo_sval.len, &idx);
499 if ((int) (ident.len - mlvo_sval.len) != consumed) {
501 "invalid mapping:\" %.*s\" may only be followed by a valid group index; "
503 mlvo_sval.len, mlvo_sval.start);
504 m->mapping.skip = true;
508 if (mlvo == MLVO_LAYOUT) {
509 m->mapping.layout_idx = idx;
511 else if (mlvo == MLVO_VARIANT) {
512 m->mapping.variant_idx = idx;
516 "invalid mapping: \"%.*s\" cannot be followed by a group index; "
518 mlvo_sval.len, mlvo_sval.start);
519 m->mapping.skip = true;
524 m->mapping.mlvo_at_pos[m->mapping.num_mlvo] = mlvo;
525 m->mapping.defined_mlvo_mask |= 1 << mlvo;
526 m->mapping.num_mlvo++;
530 matcher_mapping_set_kccgst(struct matcher *m, struct sval ident)
532 enum rules_kccgst kccgst;
533 struct sval kccgst_sval;
535 for (kccgst = 0; kccgst < _KCCGST_NUM_ENTRIES; kccgst++) {
536 kccgst_sval = rules_kccgst_svals[kccgst];
538 if (svaleq(rules_kccgst_svals[kccgst], ident))
543 if (kccgst >= _KCCGST_NUM_ENTRIES) {
545 "invalid mapping: %.*s is not a valid value here; "
547 ident.len, ident.start);
548 m->mapping.skip = true;
552 if (m->mapping.defined_kccgst_mask & (1 << kccgst)) {
554 "invalid mapping: %.*s appears twice on the same line; "
556 kccgst_sval.len, kccgst_sval.start);
557 m->mapping.skip = true;
561 m->mapping.kccgst_at_pos[m->mapping.num_kccgst] = kccgst;
562 m->mapping.defined_kccgst_mask |= 1 << kccgst;
563 m->mapping.num_kccgst++;
567 matcher_mapping_verify(struct matcher *m)
569 if (m->mapping.num_mlvo == 0) {
571 "invalid mapping: must have at least one value on the left hand side; "
572 "ignoring rule set");
576 if (m->mapping.num_kccgst == 0) {
578 "invalid mapping: must have at least one value on the right hand side; "
579 "ignoring rule set");
584 * This following is very stupid, but this is how it works.
585 * See the "Notes" section in the overview above.
588 if (m->mapping.defined_mlvo_mask & (1 << MLVO_LAYOUT)) {
589 if (m->mapping.layout_idx == XKB_LAYOUT_INVALID) {
590 if (darray_size(m->rmlvo.layouts) > 1)
594 if (darray_size(m->rmlvo.layouts) == 1 ||
595 m->mapping.layout_idx >= darray_size(m->rmlvo.layouts))
600 if (m->mapping.defined_mlvo_mask & (1 << MLVO_VARIANT)) {
601 if (m->mapping.variant_idx == XKB_LAYOUT_INVALID) {
602 if (darray_size(m->rmlvo.variants) > 1)
606 if (darray_size(m->rmlvo.variants) == 1 ||
607 m->mapping.variant_idx >= darray_size(m->rmlvo.variants))
615 m->mapping.skip = true;
619 matcher_rule_start_new(struct matcher *m)
621 memset(&m->rule, 0, sizeof(m->rule));
622 m->rule.skip = m->mapping.skip;
626 matcher_rule_set_mlvo_common(struct matcher *m, struct sval ident,
627 enum mlvo_match_type match_type)
629 if (m->rule.num_mlvo_values + 1 > m->mapping.num_mlvo) {
631 "invalid rule: has more values than the mapping line; "
636 m->rule.match_type_at_pos[m->rule.num_mlvo_values] = match_type;
637 m->rule.mlvo_value_at_pos[m->rule.num_mlvo_values] = ident;
638 m->rule.num_mlvo_values++;
642 matcher_rule_set_mlvo_wildcard(struct matcher *m)
644 struct sval dummy = { NULL, 0 };
645 matcher_rule_set_mlvo_common(m, dummy, MLVO_MATCH_WILDCARD);
649 matcher_rule_set_mlvo_group(struct matcher *m, struct sval ident)
651 matcher_rule_set_mlvo_common(m, ident, MLVO_MATCH_GROUP);
655 matcher_rule_set_mlvo(struct matcher *m, struct sval ident)
657 matcher_rule_set_mlvo_common(m, ident, MLVO_MATCH_NORMAL);
661 matcher_rule_set_kccgst(struct matcher *m, struct sval ident)
663 if (m->rule.num_kccgst_values + 1 > m->mapping.num_kccgst) {
665 "invalid rule: has more values than the mapping line; "
670 m->rule.kccgst_value_at_pos[m->rule.num_kccgst_values] = ident;
671 m->rule.num_kccgst_values++;
675 match_group(struct matcher *m, struct sval group_name, struct sval to)
678 struct sval *element;
681 darray_foreach(group, m->groups) {
682 if (svaleq(group->name, group_name)) {
690 * rules/evdev intentionally uses some undeclared group names
691 * in rules (e.g. commented group definitions which may be
692 * uncommented if needed). So we continue silently.
697 darray_foreach(element, group->elements)
698 if (svaleq(to, *element))
705 match_value(struct matcher *m, struct sval val, struct sval to,
706 enum mlvo_match_type match_type)
708 if (match_type == MLVO_MATCH_WILDCARD)
710 if (match_type == MLVO_MATCH_GROUP)
711 return match_group(m, val, to);
712 return svaleq(val, to);
716 * This function performs %-expansion on @value (see overview above),
717 * and appends the result to @to.
720 append_expanded_kccgst_value(struct matcher *m, darray_char *to,
723 const unsigned original_size = darray_size(*to);
724 const char *s = value.start;
727 * Appending bar to foo -> foo (not an error if this happens)
728 * Appending +bar to foo -> foo+bar
729 * Appending bar to +foo -> bar+foo
730 * Appending +bar to +foo -> +foo+bar
732 if (!darray_empty(*to) && s[0] != '+' && s[0] != '|') {
733 if (darray_item(*to, 0) == '+' || darray_item(*to, 0) == '|')
734 darray_prepends_nullterminate(*to, value.start, value.len);
739 * Some ugly hand-lexing here, but going through the scanner is more
740 * trouble than it's worth, and the format is ugly on its own merit.
742 for (unsigned i = 0; i < value.len; ) {
744 xkb_layout_index_t idx;
746 struct sval expanded;
748 /* Check if that's a start of an expansion. */
750 /* Just a normal character. */
751 darray_appends_nullterminate(*to, &s[i++], 1);
754 if (++i >= value.len) goto error;
758 /* Check for prefix. */
759 if (s[i] == '(' || s[i] == '+' || s[i] == '|' ||
760 s[i] == '_' || s[i] == '-') {
762 if (s[i] == '(') sfx = ')';
763 if (++i >= value.len) goto error;
766 /* Mandatory model/layout/variant specifier. */
768 case 'm': mlv = MLVO_MODEL; break;
769 case 'l': mlv = MLVO_LAYOUT; break;
770 case 'v': mlv = MLVO_VARIANT; break;
774 /* Check for index. */
775 idx = XKB_LAYOUT_INVALID;
776 if (i < value.len && s[i] == '[') {
779 if (mlv != MLVO_LAYOUT && mlv != MLVO_VARIANT) {
781 "invalid index in %%-expansion; "
782 "may only index layout or variant");
786 consumed = extract_layout_index(s + i, value.len - i, &idx);
787 if (consumed == -1) goto error;
791 /* Check for suffix, if there supposed to be one. */
793 if (i >= value.len) goto error;
794 if (s[i++] != sfx) goto error;
797 /* Get the expanded value. */
800 if (mlv == MLVO_LAYOUT) {
801 if (idx != XKB_LAYOUT_INVALID &&
802 idx < darray_size(m->rmlvo.layouts) &&
803 darray_size(m->rmlvo.layouts) > 1)
804 expanded = darray_item(m->rmlvo.layouts, idx);
805 else if (idx == XKB_LAYOUT_INVALID &&
806 darray_size(m->rmlvo.layouts) == 1)
807 expanded = darray_item(m->rmlvo.layouts, 0);
809 else if (mlv == MLVO_VARIANT) {
810 if (idx != XKB_LAYOUT_INVALID &&
811 idx < darray_size(m->rmlvo.variants) &&
812 darray_size(m->rmlvo.variants) > 1)
813 expanded = darray_item(m->rmlvo.variants, idx);
814 else if (idx == XKB_LAYOUT_INVALID &&
815 darray_size(m->rmlvo.variants) == 1)
816 expanded = darray_item(m->rmlvo.variants, 0);
818 else if (mlv == MLVO_MODEL) {
819 expanded = m->rmlvo.model;
822 /* If we didn't get one, skip silently. */
823 if (expanded.len <= 0)
827 darray_appends_nullterminate(*to, &pfx, 1);
828 darray_appends_nullterminate(*to, expanded.start, expanded.len);
830 darray_appends_nullterminate(*to, &sfx, 1);
836 matcher_error1(m, "invalid %%-expansion in value; not used");
837 darray_resize(*to, original_size);
842 matcher_rule_verify(struct matcher *m)
844 if (m->rule.num_mlvo_values != m->mapping.num_mlvo ||
845 m->rule.num_kccgst_values != m->mapping.num_kccgst) {
847 "invalid rule: must have same number of values as mapping line;"
854 matcher_rule_apply_if_matches(struct matcher *m)
856 for (unsigned i = 0; i < m->mapping.num_mlvo; i++) {
857 enum rules_mlvo mlvo = m->mapping.mlvo_at_pos[i];
858 struct sval value = m->rule.mlvo_value_at_pos[i];
859 enum mlvo_match_type match_type = m->rule.match_type_at_pos[i];
860 bool matched = false;
862 if (mlvo == MLVO_MODEL) {
863 matched = match_value(m, value, m->rmlvo.model, match_type);
865 else if (mlvo == MLVO_LAYOUT) {
866 xkb_layout_index_t idx = m->mapping.layout_idx;
867 idx = (idx == XKB_LAYOUT_INVALID ? 0 : idx);
868 matched = match_value(m, value,
869 darray_item(m->rmlvo.layouts, idx),
872 else if (mlvo == MLVO_VARIANT) {
873 xkb_layout_index_t idx = m->mapping.layout_idx;
874 idx = (idx == XKB_LAYOUT_INVALID ? 0 : idx);
875 matched = match_value(m, value,
876 darray_item(m->rmlvo.variants, idx),
879 else if (mlvo == MLVO_OPTION) {
881 darray_foreach(option, m->rmlvo.options) {
882 matched = match_value(m, value, *option, match_type);
892 for (unsigned i = 0; i < m->mapping.num_kccgst; i++) {
893 enum rules_kccgst kccgst = m->mapping.kccgst_at_pos[i];
894 struct sval value = m->rule.kccgst_value_at_pos[i];
895 append_expanded_kccgst_value(m, &m->kccgst[kccgst], value);
899 * If a rule matches in a rule set, the rest of the set should be
900 * skipped. However, rule sets matching against options may contain
901 * several legitimate rules, so they are processed entirely.
903 if (!(m->mapping.defined_mlvo_mask & (1 << MLVO_OPTION)))
904 m->mapping.skip = true;
907 static enum rules_token
908 gettok(struct matcher *m)
910 return lex(&m->scanner, &m->val, &m->loc);
914 matcher_match(struct matcher *m, const char *string, size_t len,
915 const char *file_name, struct xkb_component_names *out)
917 enum rules_token tok;
922 scanner_init(&m->scanner, m->ctx, string, len, file_name);
925 switch (tok = gettok(m)) {
928 case TOK_END_OF_LINE:
930 case TOK_END_OF_FILE:
937 switch (tok = gettok(m)) {
939 matcher_group_start_new(m, m->val.string);
942 matcher_mapping_start_new(m);
943 matcher_mapping_set_mlvo(m, m->val.string);
950 switch (tok = gettok(m)) {
958 switch (tok = gettok(m)) {
960 matcher_group_add_element(m, m->val.string);
962 case TOK_END_OF_LINE:
969 switch (tok = gettok(m)) {
971 if (!m->mapping.skip)
972 matcher_mapping_set_mlvo(m, m->val.string);
981 switch (tok = gettok(m)) {
983 if (!m->mapping.skip)
984 matcher_mapping_set_kccgst(m, m->val.string);
986 case TOK_END_OF_LINE:
987 if (!m->mapping.skip)
988 matcher_mapping_verify(m);
989 goto rule_mlvo_first;
995 switch (tok = gettok(m)) {
998 case TOK_END_OF_LINE:
999 goto rule_mlvo_first;
1000 case TOK_END_OF_FILE:
1003 matcher_rule_start_new(m);
1004 goto rule_mlvo_no_tok;
1011 case TOK_IDENTIFIER:
1013 matcher_rule_set_mlvo(m, m->val.string);
1017 matcher_rule_set_mlvo_wildcard(m);
1019 case TOK_GROUP_NAME:
1021 matcher_rule_set_mlvo_group(m, m->val.string);
1030 switch (tok = gettok(m)) {
1031 case TOK_IDENTIFIER:
1033 matcher_rule_set_kccgst(m, m->val.string);
1035 case TOK_END_OF_LINE:
1037 matcher_rule_verify(m);
1039 matcher_rule_apply_if_matches(m);
1040 goto rule_mlvo_first;
1054 if (darray_empty(m->kccgst[KCCGST_KEYCODES]) ||
1055 darray_empty(m->kccgst[KCCGST_TYPES]) ||
1056 darray_empty(m->kccgst[KCCGST_COMPAT]) ||
1057 /* darray_empty(m->kccgst[KCCGST_GEOMETRY]) || */
1058 darray_empty(m->kccgst[KCCGST_SYMBOLS]))
1061 out->keycodes = darray_mem(m->kccgst[KCCGST_KEYCODES], 0);
1062 out->types = darray_mem(m->kccgst[KCCGST_TYPES], 0);
1063 out->compat = darray_mem(m->kccgst[KCCGST_COMPAT], 0);
1064 /* out->geometry = darray_mem(m->kccgst[KCCGST_GEOMETRY], 0); */
1065 darray_free(m->kccgst[KCCGST_GEOMETRY]);
1066 out->symbols = darray_mem(m->kccgst[KCCGST_SYMBOLS], 0);
1071 matcher_error1(m, "unexpected token");
1077 xkb_components_from_rules(struct xkb_context *ctx,
1078 const struct xkb_rule_names *rmlvo,
1079 struct xkb_component_names *out)
1086 struct matcher *matcher;
1088 file = FindFileInXkbPath(ctx, rmlvo->rules, FILE_TYPE_RULES, &path);
1092 ret = map_file(file, &string, &size);
1094 log_err(ctx, "Couldn't read rules file: %s\n", strerror(errno));
1098 matcher = matcher_new(ctx, rmlvo);
1099 ret = matcher_match(matcher, string, size, rmlvo->rules, out);
1101 log_err(ctx, "No components returned from XKB rules \"%s\"\n", path);
1102 matcher_free(matcher);
1104 unmap_file(string, size);