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.
57 #include <sys/types.h>
59 #include "xkbcomp-priv.h"
66 * The purpose of this file is to map between configuration values that
67 * are easy for a user to specify and understand, and the configuration
68 * values xkbcomp uses and understands.
69 * xkbcomp uses the xkb_component_names struct, which maps directly to
70 * include statements of the appropriate sections, called for short
71 * KcCGST (see keycodes.c, types.c, compat.c, symbols.c; geometry.c was
72 * removed). These are not really intuitive or straight-forward for
74 * Instead, the user passes in a xkb_rule_names struct, which consists
75 * of the name of a rules file (in Linux this is usually "evdev"), a
76 * keyboard model (e.g. "pc105"), a set of layouts (which will end up
77 * in different groups, e.g. "us,fr"), variants (used to alter/augment
78 * the respective layout, e.g. "intl,dvorak"), and a set of options
79 * (used to tweak some general behavior of the keyboard, e.g.
80 * "ctrl:nocaps,compose:menu" to make the Caps Lock key act like Ctrl
81 * and the Menu key like Compose). We call these RMLVO.
85 * The file consists of rule sets, each consisting of rules (one per
86 * line), which match the MLVO values on the left hand side, and, if
87 * the values match to the values the user passed in, results in the
88 * values on the right hand side being added to the resulting KcCGST.
89 * Since some values are related and repeated often, it is possible
90 * to group them together and refer to them by a group name in the
92 * Along with matching values by simple string equality, and for
93 * membership in a group defined previously, rules may also contain
94 * "wildcard" values - "*" - which always match. These usually appear
99 * (It might be helpful to look at a file like rules/evdev along with
100 * this grammer. Comments, whitespace, etc. are not shown.)
102 * File ::= { "!" (Group | RuleSet) }
104 * Group ::= GroupName "=" { GroupElement } "\n"
105 * GroupName ::= "$"<ident>
106 * GroupElement ::= <ident>
108 * RuleSet ::= Mapping { Rule }
110 * Mapping ::= { Mlvo } "=" { Kccgst } "\n"
111 * Mlvo ::= "model" | "option" | ("layout" | "variant") [ Index ]
112 * Index ::= "[" 1..XKB_NUM_GROUPS "]"
113 * Kccgst ::= "keycodes" | "symbols" | "types" | "compat" | "geometry"
115 * Rule ::= { MlvoValue } "=" { KccgstValue } "\n"
116 * MlvoValue ::= "*" | GroupName | <ident>
117 * KccgstValue ::= <ident>
120 * - The order of values in a Rule must be the same as the Mapping it
121 * follows. The mapping line determines the meaning of the values in
122 * the rules which follow in the RuleSet.
123 * - If a Rule is matched, %-expansion is performed on the KccgstValue,
126 * The model, layout or variant, if only one was given (e.g.
127 * %l for "us,il" is invalid).
129 * Layout or variant for the specified group Index, if more than
130 * one was given (e.g. %l[1] for "us" is invalid).
131 * %+m, %+l, %+v, %+l[1], %+v[1]
132 * As above, but prefixed with '+'. Similarly, '|', '-', '_' may be
133 * used instead of '+'.
134 * %(m), %(l), %(l[1]), %(v), %(v[1]):
135 * As above, but prefixed by '(' and suffixed by ')'.
136 * In case the expansion is invalid, as described above, it is
137 * skipped (the rest of the string is still processed); this includes
138 * the prefix and suffix (that's why you shouldn't use e.g. "(%v[1])").
141 /* Scanner / Lexer */
143 /* Point to some substring in the file; used to avoid copying. */
148 typedef darray(struct sval) darray_sval;
151 svaleq(struct sval s1, struct sval s2)
153 return s1.len == s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
157 svaleq_prefix(struct sval s1, struct sval s2)
159 return s1.len <= s2.len && strncmp(s1.start, s2.start, s1.len) == 0;
162 /* Values returned with some tokens, like yylval. */
168 * Holds the location in the file of the last processed token,
180 const char *file_name;
181 struct xkb_context *ctx;
196 scanner_init(struct scanner *s, struct xkb_context *ctx,
197 const char *string, size_t len, const char *file_name)
202 s->line = s->column = 1;
203 s->file_name = file_name;
207 /* C99 is stupid. Just use the 1 variant when there are no args. */
208 #define scanner_error1(scanner, loc, msg) \
209 log_warn(scanner->ctx, "rules/%s:%d:%d: " msg "\n", \
210 scanner->file_name, loc->line, loc->column)
211 #define scanner_error(scanner, loc, fmt, ...) \
212 log_warn(scanner->ctx, "rules/%s:%d:%d: " fmt "\n", \
213 scanner->file_name, loc->line, loc->column, __VA_ARGS__)
216 peek(struct scanner *s)
222 eof(struct scanner *s)
224 return s->s[s->pos] == '\0' || s->pos >= s->len;
228 eol(struct scanner *s)
230 return s->s[s->pos] == '\n';
234 * Use the check_nl variant when the current char might be a new line;
235 * just an optimization.
238 next(struct scanner *s)
241 return s->s[s->pos++];
245 next_check_nl(struct scanner *s)
254 return s->s[s->pos++];
258 chr(struct scanner *s, char ch)
262 s->pos++; s->column++;
267 str(struct scanner *s, const char *string, size_t len)
269 if (strncasecmp(s->s + s->pos, string, len) != 0)
271 s->pos += len; s->column += len;
275 #define lit(s, literal) str(s, literal, sizeof(literal) - 1)
277 static enum rules_token
278 lex(struct scanner *s, union lvalue *val, struct location *loc)
280 skip_more_whitespace_and_comments:
282 while (chr(s, ' ') || chr(s, '\t'));
286 while (!eof(s) && !eol(s)) next(s);
291 while (eol(s)) next_check_nl(s);
292 return TOK_END_OF_LINE;
295 /* Escaped line continuation. */
298 scanner_error1(s, loc,
299 "illegal new line escape; must appear at end of line");
303 goto skip_more_whitespace_and_comments;
306 /* See if we're done. */
307 if (eof(s)) return TOK_END_OF_FILE;
311 loc->column = s->column;
313 /* Operators and punctuation. */
314 if (chr(s, '!')) return TOK_BANG;
315 if (chr(s, '=')) return TOK_EQUALS;
316 if (chr(s, '*')) return TOK_STAR;
320 val->string.start = s->s + s->pos;
322 while (isgraph(peek(s))) {
326 if (val->string.len == 0) {
327 scanner_error1(s, loc,
328 "unexpected character after \'$\'; expected name");
331 return TOK_GROUP_NAME;
335 if (isgraph(peek(s))) {
336 val->string.start = s->s + s->pos;
338 while (isgraph(peek(s))) {
342 return TOK_IDENTIFIER;
345 scanner_error1(s, loc, "unrecognized token");
349 /***====================================================================***/
359 #define SVAL_LIT(literal) { literal, sizeof(literal) - 1 }
361 static const struct sval rules_mlvo_svals[_MLVO_NUM_ENTRIES] = {
362 [MLVO_MODEL] = SVAL_LIT("model"),
363 [MLVO_LAYOUT] = SVAL_LIT("layout"),
364 [MLVO_VARIANT] = SVAL_LIT("variant"),
365 [MLVO_OPTION] = SVAL_LIT("option"),
377 static const struct sval rules_kccgst_svals[_KCCGST_NUM_ENTRIES] = {
378 [KCCGST_KEYCODES] = SVAL_LIT("keycodes"),
379 [KCCGST_TYPES] = SVAL_LIT("types"),
380 [KCCGST_COMPAT] = SVAL_LIT("compat"),
381 [KCCGST_SYMBOLS] = SVAL_LIT("symbols"),
382 [KCCGST_GEOMETRY] = SVAL_LIT("geometry"),
386 * A broken-down version of xkb_rule_names (without the rules,
392 darray_sval variants;
398 darray_sval elements;
402 int mlvo_at_pos[_MLVO_NUM_ENTRIES];
403 unsigned int num_mlvo;
404 unsigned int defined_mlvo_mask;
405 xkb_group_index_t layout_idx, variant_idx;
406 int kccgst_at_pos[_KCCGST_NUM_ENTRIES];
407 unsigned int num_kccgst;
408 unsigned int defined_kccgst_mask;
412 enum mlvo_match_type {
413 MLVO_MATCH_NORMAL = 0,
419 struct sval mlvo_value_at_pos[_MLVO_NUM_ENTRIES];
420 enum mlvo_match_type match_type_at_pos[_MLVO_NUM_ENTRIES];
421 unsigned int num_mlvo_values;
422 struct sval kccgst_value_at_pos[_KCCGST_NUM_ENTRIES];
423 unsigned int num_kccgst_values;
428 * This is the main object used to match a given RMLVO against a rules
429 * file and aggragate the results in a KcCGST. It goes through a simple
430 * matching state machine, with tokens as transitions (see
434 struct xkb_context *ctx;
436 struct rule_names rmlvo;
439 struct scanner scanner;
440 darray(struct group) groups;
441 /* Current mapping. */
442 struct mapping mapping;
446 darray_char kccgst[_KCCGST_NUM_ENTRIES];
450 strip_spaces(struct sval v)
452 while (v.len > 0 && isspace(v.start[0])) { v.len--; v.start++; }
453 while (v.len > 0 && isspace(v.start[v.len - 1])) v.len--;
458 split_comma_separated_string(const char *s)
460 darray_sval arr = darray_new();
461 struct sval val = { NULL, 0 };
464 * Make sure the array returned by this function always includes at
465 * least one value, e.g. "" -> { "" } and "," -> { "", "" }.
469 darray_append(arr, val);
474 val.start = s; val.len = 0;
475 while (*s != '\0' && *s != ',') { s++; val.len++; }
476 darray_append(arr, strip_spaces(val));
477 if (*s == '\0') break;
484 static struct matcher *
485 matcher_new(struct xkb_context *ctx,
486 const struct xkb_rule_names *rmlvo)
488 struct matcher *m = calloc(1, sizeof(*m));
493 m->rmlvo.model.start = rmlvo->model;
494 m->rmlvo.model.len = rmlvo->model ? strlen(rmlvo->model) : 0;
495 m->rmlvo.layouts = split_comma_separated_string(rmlvo->layout);
496 m->rmlvo.variants = split_comma_separated_string(rmlvo->variant);
497 m->rmlvo.options = split_comma_separated_string(rmlvo->options);
503 matcher_free(struct matcher *m)
508 darray_free(m->rmlvo.layouts);
509 darray_free(m->rmlvo.variants);
510 darray_free(m->rmlvo.options);
511 darray_foreach(group, m->groups)
512 darray_free(group->elements);
513 darray_free(m->groups);
517 /* C99 is stupid. Just use the 1 variant when there are no args. */
518 #define matcher_error1(matcher, msg) \
519 log_warn(matcher->ctx, "rules/%s:%d:%d: " msg "\n", \
520 matcher->scanner.file_name, matcher->loc.line, \
522 #define matcher_error(matcher, fmt, ...) \
523 log_warn(matcher->ctx, "rules/%s:%d:%d: " fmt "\n", \
524 matcher->scanner.file_name, matcher->loc.line, \
525 matcher->loc.column, __VA_ARGS__)
528 matcher_group_start_new(struct matcher *m, struct sval name)
530 struct group group = { .name = name, .elements = darray_new() };
531 darray_append(m->groups, group);
535 matcher_group_add_element(struct matcher *m, struct sval element)
537 darray_append(darray_item(m->groups, darray_size(m->groups) - 1).elements,
542 matcher_mapping_start_new(struct matcher *m)
545 for (i = 0; i < _MLVO_NUM_ENTRIES; i++)
546 m->mapping.mlvo_at_pos[i] = -1;
547 for (i = 0; i < _KCCGST_NUM_ENTRIES; i++)
548 m->mapping.kccgst_at_pos[i] = -1;
549 m->mapping.layout_idx = m->mapping.variant_idx = XKB_GROUP_INVALID;
550 m->mapping.num_mlvo = m->mapping.num_kccgst = 0;
551 m->mapping.defined_mlvo_mask = 0;
552 m->mapping.defined_kccgst_mask = 0;
553 m->mapping.skip = false;
557 extract_group_index(const char *s, size_t max_len, xkb_group_index_t *out)
559 /* This function is pretty stupid, but works for now. */
562 if (s[0] != '[' || !isdigit(s[1]) || s[2] != ']')
564 if (s[1] - '0' < 1 || s[1] - '0' > XKB_NUM_GROUPS)
566 /* To zero-based index. */
567 *out = s[1] - '0' - 1;
572 matcher_mapping_set_mlvo(struct matcher *m, struct sval ident)
574 enum rules_mlvo mlvo;
575 struct sval mlvo_sval;
576 xkb_group_index_t idx;
579 for (mlvo = 0; mlvo < _MLVO_NUM_ENTRIES; mlvo++) {
580 mlvo_sval = rules_mlvo_svals[mlvo];
582 if (svaleq_prefix(mlvo_sval, ident))
587 if (mlvo >= _MLVO_NUM_ENTRIES) {
589 "invalid mapping: %.*s is not a valid value here; "
591 ident.len, ident.start);
592 m->mapping.skip = true;
596 if (m->mapping.defined_mlvo_mask & (1 << mlvo)) {
598 "invalid mapping: %.*s appears twice on the same line; "
600 mlvo_sval.len, mlvo_sval.start);
601 m->mapping.skip = true;
605 /* If there are leftovers still, it must be an index. */
606 if (mlvo_sval.len < ident.len) {
607 consumed = extract_group_index(ident.start + mlvo_sval.len,
608 ident.len - mlvo_sval.len, &idx);
609 if ((int) (ident.len - mlvo_sval.len) != consumed) {
611 "invalid mapping:\" %.*s\" may only be followed by a valid group index; "
613 mlvo_sval.len, mlvo_sval.start);
614 m->mapping.skip = true;
618 if (mlvo == MLVO_LAYOUT) {
619 m->mapping.layout_idx = idx;
621 else if (mlvo == MLVO_VARIANT) {
622 m->mapping.variant_idx = idx;
626 "invalid mapping: \"%.*s\" cannot be followed by a group index; "
628 mlvo_sval.len, mlvo_sval.start);
629 m->mapping.skip = true;
634 m->mapping.mlvo_at_pos[m->mapping.num_mlvo] = mlvo;
635 m->mapping.defined_mlvo_mask |= 1 << mlvo;
636 m->mapping.num_mlvo++;
640 matcher_mapping_set_kccgst(struct matcher *m, struct sval ident)
642 enum rules_kccgst kccgst;
643 struct sval kccgst_sval;
645 for (kccgst = 0; kccgst < _KCCGST_NUM_ENTRIES; kccgst++) {
646 kccgst_sval = rules_kccgst_svals[kccgst];
648 if (svaleq(rules_kccgst_svals[kccgst], ident))
653 if (kccgst >= _KCCGST_NUM_ENTRIES) {
655 "invalid mapping: %.*s is not a valid value here; "
657 ident.len, ident.start);
658 m->mapping.skip = true;
662 if (m->mapping.defined_kccgst_mask & (1 << kccgst)) {
664 "invalid mapping: %.*s appears twice on the same line; "
666 kccgst_sval.len, kccgst_sval.start);
667 m->mapping.skip = true;
671 m->mapping.kccgst_at_pos[m->mapping.num_kccgst] = kccgst;
672 m->mapping.defined_kccgst_mask |= 1 << kccgst;
673 m->mapping.num_kccgst++;
677 matcher_mapping_verify(struct matcher *m)
679 if (m->mapping.num_mlvo == 0) {
681 "invalid mapping: must have at least one value on the left hand side; "
682 "ignoring rule set");
686 if (m->mapping.num_kccgst == 0) {
688 "invalid mapping: must have at least one value on the right hand side; "
689 "ignoring rule set");
694 * This following is very stupid, but this is how it works.
695 * See the "Notes" section in the overview above.
698 if (m->mapping.defined_mlvo_mask & (1 << MLVO_LAYOUT)) {
699 if (m->mapping.layout_idx == XKB_GROUP_INVALID) {
700 if (darray_size(m->rmlvo.layouts) > 1)
704 if (darray_size(m->rmlvo.layouts) == 1 ||
705 m->mapping.layout_idx >= darray_size(m->rmlvo.layouts))
710 if (m->mapping.defined_mlvo_mask & (1 << MLVO_VARIANT)) {
711 if (m->mapping.variant_idx == XKB_GROUP_INVALID) {
712 if (darray_size(m->rmlvo.variants) > 1)
716 if (darray_size(m->rmlvo.variants) == 1 ||
717 m->mapping.variant_idx >= darray_size(m->rmlvo.variants))
725 m->mapping.skip = true;
729 matcher_rule_start_new(struct matcher *m)
731 memset(&m->rule, 0, sizeof(m->rule));
732 m->rule.skip = m->mapping.skip;
736 matcher_rule_set_mlvo_common(struct matcher *m, struct sval ident,
737 enum mlvo_match_type match_type)
739 if (m->rule.num_mlvo_values + 1 > m->mapping.num_mlvo) {
741 "invalid rule: has more values than the mapping line; "
746 m->rule.match_type_at_pos[m->rule.num_mlvo_values] = match_type;
747 m->rule.mlvo_value_at_pos[m->rule.num_mlvo_values] = ident;
748 m->rule.num_mlvo_values++;
752 matcher_rule_set_mlvo_wildcard(struct matcher *m)
754 struct sval dummy = { NULL, 0 };
755 matcher_rule_set_mlvo_common(m, dummy, MLVO_MATCH_WILDCARD);
759 matcher_rule_set_mlvo_group(struct matcher *m, struct sval ident)
761 matcher_rule_set_mlvo_common(m, ident, MLVO_MATCH_GROUP);
765 matcher_rule_set_mlvo(struct matcher *m, struct sval ident)
767 matcher_rule_set_mlvo_common(m, ident, MLVO_MATCH_NORMAL);
771 matcher_rule_set_kccgst(struct matcher *m, struct sval ident)
773 if (m->rule.num_kccgst_values + 1 > m->mapping.num_kccgst) {
775 "invalid rule: has more values than the mapping line; "
780 m->rule.kccgst_value_at_pos[m->rule.num_kccgst_values] = ident;
781 m->rule.num_kccgst_values++;
785 match_group(struct matcher *m, struct sval group_name, struct sval to)
788 struct sval *element;
791 darray_foreach(group, m->groups) {
792 if (svaleq(group->name, group_name)) {
800 * rules/evdev intentionally uses some undeclared group names
801 * in rules (e.g. commented group definitions which may be
802 * uncommented if needed). So we continue silently.
807 darray_foreach(element, group->elements)
808 if (svaleq(to, *element))
815 match_value(struct matcher *m, struct sval val, struct sval to,
816 enum mlvo_match_type match_type)
818 if (match_type == MLVO_MATCH_WILDCARD)
820 if (match_type == MLVO_MATCH_GROUP)
821 return match_group(m, val, to);
822 return svaleq(val, to);
826 * This function performs %-expansion on @value (see overview above),
827 * and appends the result to @to.
830 append_expanded_kccgst_value(struct matcher *m, darray_char *to,
834 size_t original_size = darray_size(*to);
835 const char *s = value.start;
836 xkb_group_index_t idx;
839 struct sval expanded;
843 * Appending bar to foo -> foo (not an error if this happens)
844 * Appending +bar to foo -> foo+bar
845 * Appending bar to +foo -> bar+foo
846 * Appending +bar to +foo -> +foo+bar
848 if (!darray_empty(*to) && s[0] != '+' && s[0] != '|') {
849 if (darray_item(*to, 0) == '+' || darray_item(*to, 0) == '|')
850 darray_prepend_items_nullterminate(*to, value.start, value.len);
855 * Some ugly hand-lexing here, but going through the scanner is more
856 * trouble than it's worth, and the format is ugly on its own merit.
858 for (i = 0; i < value.len; ) {
859 /* Check if that's a start of an expansion. */
861 /* Just a normal character. */
862 darray_append_items_nullterminate(*to, &s[i++], 1);
865 if (++i >= value.len) goto error;
869 /* Check for prefix. */
870 if (s[i] == '(' || s[i] == '+' || s[i] == '|' ||
871 s[i] == '_' || s[i] == '-') {
873 if (s[i] == '(') sfx = ')';
874 if (++i >= value.len) goto error;
877 /* Mandatory model/layout/variant specifier. */
879 case 'm': mlv = MLVO_MODEL; break;
880 case 'l': mlv = MLVO_LAYOUT; break;
881 case 'v': mlv = MLVO_VARIANT; break;
885 /* Check for index. */
888 if (mlv != MLVO_LAYOUT && mlv != MLVO_VARIANT) {
890 "invalid index in %%-expansion; "
891 "may only index layout or variant");
895 consumed = extract_group_index(s + i, value.len - i, &idx);
896 if (consumed == -1) goto error;
900 idx = XKB_GROUP_INVALID;
904 /* Check for suffix, if there supposed to be one. */
906 if (i >= value.len) goto error;
907 if (s[i++] != sfx) goto error;
910 /* Get the expanded value. */
913 if (mlv == MLVO_LAYOUT) {
914 if (idx != XKB_GROUP_INVALID &&
915 idx < darray_size(m->rmlvo.layouts) &&
916 darray_size(m->rmlvo.layouts) > 1)
917 expanded = darray_item(m->rmlvo.layouts, idx);
918 else if (idx == XKB_GROUP_INVALID &&
919 darray_size(m->rmlvo.layouts) == 1)
920 expanded = darray_item(m->rmlvo.layouts, 0);
922 else if (mlv == MLVO_VARIANT) {
923 if (idx != XKB_GROUP_INVALID &&
924 idx < darray_size(m->rmlvo.variants) &&
925 darray_size(m->rmlvo.variants) > 1)
926 expanded = darray_item(m->rmlvo.variants, idx);
927 else if (idx == XKB_GROUP_INVALID &&
928 darray_size(m->rmlvo.variants) == 1)
929 expanded = darray_item(m->rmlvo.variants, 0);
931 else if (mlv == MLVO_MODEL) {
932 expanded = m->rmlvo.model;
935 /* If we didn't get one, skip silently. */
936 if (expanded.len <= 0)
940 darray_append_items_nullterminate(*to, &pfx, 1);
941 darray_append_items_nullterminate(*to, expanded.start, expanded.len);
943 darray_append_items_nullterminate(*to, &sfx, 1);
949 matcher_error1(m, "invalid %%-expansion in value; not used");
950 darray_resize(*to, original_size);
955 matcher_rule_verify(struct matcher *m)
957 if (m->rule.num_mlvo_values != m->mapping.num_mlvo ||
958 m->rule.num_kccgst_values != m->mapping.num_kccgst) {
960 "invalid rule: must have same number of values as mapping line;"
967 matcher_rule_apply_if_matches(struct matcher *m)
970 enum rules_mlvo mlvo;
971 enum rules_kccgst kccgst;
972 struct sval value, *option;
973 enum mlvo_match_type match_type;
974 bool matched = false;
975 xkb_group_index_t idx;
977 for (i = 0; i < m->mapping.num_mlvo; i++) {
978 mlvo = m->mapping.mlvo_at_pos[i];
979 value = m->rule.mlvo_value_at_pos[i];
980 match_type = m->rule.match_type_at_pos[i];
982 if (mlvo == MLVO_MODEL) {
983 matched = match_value(m, value, m->rmlvo.model, match_type);
985 else if (mlvo == MLVO_LAYOUT) {
986 idx = m->mapping.layout_idx;
987 idx = (idx == XKB_GROUP_INVALID ? 0 : idx);
988 matched = match_value(m, value,
989 darray_item(m->rmlvo.layouts, idx),
992 else if (mlvo == MLVO_VARIANT) {
993 idx = m->mapping.layout_idx;
994 idx = (idx == XKB_GROUP_INVALID ? 0 : idx);
995 matched = match_value(m, value,
996 darray_item(m->rmlvo.variants, idx),
999 else if (mlvo == MLVO_OPTION) {
1000 darray_foreach(option, m->rmlvo.options) {
1001 matched = match_value(m, value, *option, match_type);
1011 for (i = 0; i < m->mapping.num_kccgst; i++) {
1012 kccgst = m->mapping.kccgst_at_pos[i];
1013 value = m->rule.kccgst_value_at_pos[i];
1014 append_expanded_kccgst_value(m, &m->kccgst[kccgst], value);
1018 * If a rule matches in a rule set, the rest of the set should be
1019 * skipped. However, rule sets matching against options may contain
1020 * several legitimate rules, so they are processed entirely.
1022 if (!(m->mapping.defined_mlvo_mask & (1 << MLVO_OPTION)))
1023 m->mapping.skip = true;
1026 static enum rules_token
1027 gettok(struct matcher *m)
1029 return lex(&m->scanner, &m->val, &m->loc);
1033 matcher_match(struct matcher *m, const char *string, size_t len,
1034 const char *file_name, struct xkb_component_names *out)
1036 enum rules_token tok;
1041 scanner_init(&m->scanner, m->ctx, string, len, file_name);
1044 switch (tok = gettok(m)) {
1047 case TOK_END_OF_LINE:
1049 case TOK_END_OF_FILE:
1056 switch (tok = gettok(m)) {
1057 case TOK_GROUP_NAME:
1058 matcher_group_start_new(m, m->val.string);
1060 case TOK_IDENTIFIER:
1061 matcher_mapping_start_new(m);
1062 matcher_mapping_set_mlvo(m, m->val.string);
1069 switch (tok = gettok(m)) {
1077 switch (tok = gettok(m)) {
1078 case TOK_IDENTIFIER:
1079 matcher_group_add_element(m, m->val.string);
1081 case TOK_END_OF_LINE:
1088 switch (tok = gettok(m)) {
1089 case TOK_IDENTIFIER:
1090 if (!m->mapping.skip)
1091 matcher_mapping_set_mlvo(m, m->val.string);
1094 goto mapping_kccgst;
1100 switch (tok = gettok(m)) {
1101 case TOK_IDENTIFIER:
1102 if (!m->mapping.skip)
1103 matcher_mapping_set_kccgst(m, m->val.string);
1104 goto mapping_kccgst;
1105 case TOK_END_OF_LINE:
1106 if (!m->mapping.skip)
1107 matcher_mapping_verify(m);
1108 goto rule_mlvo_first;
1114 switch (tok = gettok(m)) {
1117 case TOK_END_OF_LINE:
1118 goto rule_mlvo_first;
1119 case TOK_END_OF_FILE:
1122 matcher_rule_start_new(m);
1123 goto rule_mlvo_no_tok;
1130 case TOK_IDENTIFIER:
1132 matcher_rule_set_mlvo(m, m->val.string);
1136 matcher_rule_set_mlvo_wildcard(m);
1138 case TOK_GROUP_NAME:
1140 matcher_rule_set_mlvo_group(m, m->val.string);
1149 switch (tok = gettok(m)) {
1150 case TOK_IDENTIFIER:
1152 matcher_rule_set_kccgst(m, m->val.string);
1154 case TOK_END_OF_LINE:
1156 matcher_rule_verify(m);
1158 matcher_rule_apply_if_matches(m);
1159 goto rule_mlvo_first;
1173 if (darray_empty(m->kccgst[KCCGST_KEYCODES]) ||
1174 darray_empty(m->kccgst[KCCGST_TYPES]) ||
1175 darray_empty(m->kccgst[KCCGST_COMPAT]) ||
1176 /* darray_empty(m->kccgst[KCCGST_GEOMETRY]) || */
1177 darray_empty(m->kccgst[KCCGST_SYMBOLS]))
1180 out->keycodes = darray_mem(m->kccgst[KCCGST_KEYCODES], 0);
1181 out->types = darray_mem(m->kccgst[KCCGST_TYPES], 0);
1182 out->compat = darray_mem(m->kccgst[KCCGST_COMPAT], 0);
1183 /* out->geometry = darray_mem(m->kccgst[KCCGST_GEOMETRY], 0); */
1184 darray_free(m->kccgst[KCCGST_GEOMETRY]);
1185 out->symbols = darray_mem(m->kccgst[KCCGST_SYMBOLS], 0);
1190 matcher_error1(m, "unexpected token");
1196 xkb_components_from_rules(struct xkb_context *ctx,
1197 const struct xkb_rule_names *rmlvo,
1198 struct xkb_component_names *out)
1205 struct stat stat_buf;
1207 struct matcher *matcher;
1209 file = FindFileInXkbPath(ctx, rmlvo->rules, FILE_TYPE_RULES, &path);
1211 log_err(ctx, "Could not find \"%s\" rules in XKB path\n",
1213 log_err(ctx, "%zu include paths searched:\n",
1214 darray_size(ctx->includes));
1215 darray_foreach(include, ctx->includes)
1216 log_err(ctx, "\t%s\n", *include);
1217 log_err(ctx, "%zu include paths could not be added:\n",
1218 darray_size(ctx->failed_includes));
1219 darray_foreach(include, ctx->failed_includes)
1220 log_err(ctx, "\t%s\n", *include);
1226 if (fstat(fd, &stat_buf) != 0) {
1227 log_err(ctx, "Couldn't stat rules file\n");
1231 string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
1233 log_err(ctx, "Couldn't mmap rules file (%zu bytes)\n",
1234 (size_t) stat_buf.st_size);
1238 matcher = matcher_new(ctx, rmlvo);
1239 ret = matcher_match(matcher, string, stat_buf.st_size, rmlvo->rules, out);
1241 log_err(ctx, "No components returned from XKB rules \"%s\"\n", path);
1242 matcher_free(matcher);
1244 munmap(string, stat_buf.st_size);