1 /************************************************************
2 * Copyright (c) 1994 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 ********************************************************/
29 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
30 xkb_atom_t field, unsigned type,
31 ExprResult *val_rtrn);
34 exprOpText(unsigned type)
40 strcpy(buf, "literal");
43 strcpy(buf, "identifier");
46 strcpy(buf, "action declaration");
49 strcpy(buf, "field reference");
52 strcpy(buf, "array reference");
55 strcpy(buf, "list of keysyms");
58 strcpy(buf, "list of actions");
61 strcpy(buf, "addition");
64 strcpy(buf, "subtraction");
67 strcpy(buf, "multiplication");
70 strcpy(buf, "division");
73 strcpy(buf, "assignment");
76 strcpy(buf, "logical not");
79 strcpy(buf, "arithmetic negation");
82 strcpy(buf, "bitwise inversion");
85 strcpy(buf, "plus sign");
88 snprintf(buf, sizeof(buf), "illegal(%d)", type);
95 exprTypeText(unsigned type)
101 strcpy(buf, "unknown");
104 strcpy(buf, "boolean");
110 strcpy(buf, "string");
113 strcpy(buf, "action");
116 strcpy(buf, "keyname");
119 snprintf(buf, sizeof(buf), "illegal(%d)", type);
126 ExprResolveLhs(struct xkb_keymap *keymap, ExprDef *expr,
127 ExprResult *elem_rtrn, ExprResult *field_rtrn,
128 ExprDef **index_rtrn)
132 elem_rtrn->str = NULL;
133 field_rtrn->str = xkb_atom_strdup(keymap->ctx,
138 elem_rtrn->str = xkb_atom_strdup(keymap->ctx,
139 expr->value.field.element);
140 field_rtrn->str = xkb_atom_strdup(keymap->ctx,
141 expr->value.field.field);
145 elem_rtrn->str = xkb_atom_strdup(keymap->ctx,
146 expr->value.array.element);
147 field_rtrn->str = xkb_atom_strdup(keymap->ctx,
148 expr->value.array.field);
149 *index_rtrn = expr->value.array.entry;
152 log_wsgo(keymap->ctx, "Unexpected operator %d in ResolveLhs\n", expr->op);
157 SimpleLookup(struct xkb_context *ctx, const void *priv,
158 xkb_atom_t field, unsigned type, ExprResult *val_rtrn)
160 const LookupEntry *entry;
163 if ((priv == NULL) || (field == XKB_ATOM_NONE) || (type != TypeInt))
166 str = xkb_atom_text(ctx, field);
167 for (entry = priv; (entry != NULL) && (entry->name != NULL); entry++) {
168 if (strcasecmp(str, entry->name) == 0) {
169 val_rtrn->uval = entry->result;
177 static const LookupEntry modIndexNames[] = {
178 { "shift", ShiftMapIndex },
179 { "control", ControlMapIndex },
180 { "lock", LockMapIndex },
181 { "mod1", Mod1MapIndex },
182 { "mod2", Mod2MapIndex },
183 { "mod3", Mod3MapIndex },
184 { "mod4", Mod4MapIndex },
185 { "mod5", Mod5MapIndex },
186 { "none", XkbNoModifier },
191 LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
192 unsigned type, ExprResult *val_rtrn)
194 return SimpleLookup(ctx, modIndexNames, field, type, val_rtrn);
198 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
199 unsigned type, ExprResult *val_rtrn)
206 str = xkb_atom_text(ctx, field);
209 if (strcasecmp(str, "all") == 0)
210 val_rtrn->uval = 0xff;
211 else if (strcasecmp(str, "none") == 0)
213 else if (LookupModIndex(ctx, priv, field, type, val_rtrn))
214 val_rtrn->uval = (1 << val_rtrn->uval);
221 ExprResolveBoolean(struct xkb_context *ctx, ExprDef *expr,
222 ExprResult *val_rtrn)
225 const char *bogus = NULL;
229 if (expr->type != TypeBoolean) {
231 "Found constant of type %s where boolean was expected\n",
232 exprTypeText(expr->type));
235 val_rtrn->ival = expr->value.ival;
239 bogus = xkb_atom_text(ctx, expr->value.str);
241 if ((strcasecmp(bogus, "true") == 0) ||
242 (strcasecmp(bogus, "yes") == 0) ||
243 (strcasecmp(bogus, "on") == 0)) {
247 else if ((strcasecmp(bogus, "false") == 0) ||
248 (strcasecmp(bogus, "no") == 0) ||
249 (strcasecmp(bogus, "off") == 0)) {
254 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
255 xkb_atom_text(ctx, expr->value.str));
259 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
260 xkb_atom_text(ctx, expr->value.field.element),
261 xkb_atom_text(ctx, expr->value.field.field));
266 ok = ExprResolveBoolean(ctx, expr, val_rtrn);
268 val_rtrn->uval = !val_rtrn->uval;
275 bogus = "Subtraction";
278 bogus = "Multiplication";
284 bogus = "Assignment";
288 log_err(ctx, "%s of boolean values not permitted\n", bogus);
293 "Unary \"+\" operator not permitted for boolean values\n");
297 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n", expr->op);
304 ExprResolveFloat(struct xkb_context *ctx, ExprDef *expr,
305 ExprResult *val_rtrn)
308 ExprResult leftRtrn, rightRtrn;
309 ExprDef *left, *right;
313 if (expr->type == TypeString) {
315 str = xkb_atom_text(ctx, expr->value.str);
316 if ((str != NULL) && (strlen(str) == 1)) {
317 val_rtrn->uval = str[0] * XkbGeomPtsPerMM;
321 if (expr->type != TypeInt) {
322 log_err(ctx, "Found constant of type %s, expected a number\n",
323 exprTypeText(expr->type));
326 val_rtrn->ival = expr->value.ival;
327 if (expr->type == TypeInt)
328 val_rtrn->ival *= XkbGeomPtsPerMM;
332 log_err(ctx, "Numeric identifier \"%s\" unknown\n",
333 xkb_atom_text(ctx, expr->value.str));
337 log_err(ctx, "Numeric default \"%s.%s\" unknown\n",
338 xkb_atom_text(ctx, expr->value.field.element),
339 xkb_atom_text(ctx, expr->value.field.field));
346 left = expr->value.binary.left;
347 right = expr->value.binary.right;
348 if (ExprResolveFloat(ctx, left, &leftRtrn) &&
349 ExprResolveFloat(ctx, right, &rightRtrn)) {
352 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
356 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
360 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
364 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
372 log_wsgo(ctx, "Assignment operator not implemented yet\n");
376 log_err(ctx, "The ! operator cannot be applied to a number\n");
381 left = expr->value.child;
382 if (ExprResolveFloat(ctx, left, &leftRtrn)) {
383 if (expr->op == OpNegate)
384 val_rtrn->ival = -leftRtrn.ival;
386 val_rtrn->ival = ~leftRtrn.ival;
392 left = expr->value.child;
393 return ExprResolveFloat(ctx, left, val_rtrn);
396 log_wsgo(ctx, "Unknown operator %d in ResolveFloat\n", expr->op);
403 ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr,
404 ExprResult *val_rtrn)
406 ExprResult leftRtrn, rightRtrn;
407 ExprDef *left, *right;
411 if (expr->type != TypeInt) {
413 "Found constant of type %s where an int was expected\n",
414 exprTypeText(expr->type));
417 val_rtrn->uval = expr->value.uval;
424 left = expr->value.binary.left;
425 right = expr->value.binary.right;
426 if (ExprResolveKeyCode(ctx, left, &leftRtrn) &&
427 ExprResolveKeyCode(ctx, right, &rightRtrn)) {
430 val_rtrn->uval = leftRtrn.uval + rightRtrn.uval;
433 val_rtrn->uval = leftRtrn.uval - rightRtrn.uval;
436 val_rtrn->uval = leftRtrn.uval * rightRtrn.uval;
439 val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
447 left = expr->value.child;
448 if (ExprResolveKeyCode(ctx, left, &leftRtrn)) {
449 val_rtrn->uval = ~leftRtrn.uval;
455 left = expr->value.child;
456 return ExprResolveKeyCode(ctx, left, val_rtrn);
459 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
466 * This function returns ... something. It's a bit of a guess, really.
468 * If a string is given in value ctx, its first character will be
469 * returned in uval. If an integer is given in value ctx, it will be
470 * returned in ival. If a float is given in value ctx, it will be
471 * returned as millimetres (rather than points) in ival.
473 * If an ident or field reference is given, the lookup function (if given)
474 * will be called. At the moment, only SimpleLookup use this, and they both
475 * return the results in uval. And don't support field references.
480 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
481 ExprResult *val_rtrn, IdentLookupFunc lookup,
482 const void *lookupPriv)
485 ExprResult leftRtrn, rightRtrn;
486 ExprDef *left, *right;
490 if (expr->type == TypeString) {
492 str = xkb_atom_text(ctx, expr->value.str);
494 switch (strlen(str)) {
499 val_rtrn->uval = str[0];
505 if (expr->type != TypeInt) {
507 "Found constant of type %s where an int was expected\n",
508 exprTypeText(expr->type));
511 val_rtrn->ival = expr->value.ival;
516 ok = lookup(ctx, lookupPriv, expr->value.str,
519 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
520 xkb_atom_text(ctx, expr->value.str));
524 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
525 xkb_atom_text(ctx, expr->value.field.element),
526 xkb_atom_text(ctx, expr->value.field.field));
533 left = expr->value.binary.left;
534 right = expr->value.binary.right;
535 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
537 ExprResolveIntegerLookup(ctx, right, &rightRtrn, lookup,
541 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
544 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
547 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
550 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
558 log_wsgo(ctx, "Assignment operator not implemented yet\n");
562 log_err(ctx, "The ! operator cannot be applied to an integer\n");
567 left = expr->value.child;
568 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
570 if (expr->op == OpNegate)
571 val_rtrn->ival = -leftRtrn.ival;
573 val_rtrn->ival = ~leftRtrn.ival;
579 left = expr->value.child;
580 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
584 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
591 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr,
592 ExprResult *val_rtrn)
594 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
598 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
599 ExprResult *val_rtrn)
602 static const LookupEntry group_names[] = {
614 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
619 if (val_rtrn->uval == 0 || val_rtrn->uval > XkbNumKbdGroups) {
620 log_err(ctx, "Group index %u is out of range (1..%d)\n",
621 val_rtrn->uval, XkbNumKbdGroups);
629 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
630 ExprResult *val_rtrn)
633 static const LookupEntry level_names[] = {
645 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
650 if (val_rtrn->ival < 1 || val_rtrn->ival > XkbMaxShiftLevel) {
651 log_err(ctx, "Shift level %d is out of range (1..%d)\n",
652 val_rtrn->ival, XkbMaxShiftLevel);
660 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr,
661 ExprResult *val_rtrn)
663 static const LookupEntry button_names[] = {
673 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
678 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
679 ExprResult *val_rtrn)
681 ExprResult leftRtrn, rightRtrn;
684 const char *bogus = NULL;
688 if (expr->type != TypeString) {
689 log_err(ctx, "Found constant of type %s, expected a string\n",
690 exprTypeText(expr->type));
693 val_rtrn->str = xkb_atom_strdup(ctx, expr->value.str);
694 if (val_rtrn->str == NULL)
695 val_rtrn->str = strdup("");
699 log_err(ctx, "Identifier \"%s\" of type string not found\n",
700 xkb_atom_text(ctx, expr->value.str));
704 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
705 xkb_atom_text(ctx, expr->value.field.element),
706 xkb_atom_text(ctx, expr->value.field.field));
710 left = expr->value.binary.left;
711 right = expr->value.binary.right;
712 if (ExprResolveString(ctx, left, &leftRtrn) &&
713 ExprResolveString(ctx, right, &rightRtrn)) {
716 len = strlen(leftRtrn.str) + strlen(rightRtrn.str) + 1;
719 sprintf(new, "%s%s", leftRtrn.str, rightRtrn.str);
732 bogus = "Subtraction";
735 bogus = "Multiplication";
741 bogus = "Assignment";
747 bogus = "Bitwise complement";
748 log_err(ctx, "%s of string values not permitted\n", bogus);
752 log_err(ctx, "The ! operator cannot be applied to a string\n");
756 log_err(ctx, "The + operator cannot be applied to a string\n");
760 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
767 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
768 ExprResult *val_rtrn)
770 const char *bogus = NULL;
774 if (expr->type != TypeKeyName) {
775 log_err(ctx, "Found constant of type %s, expected a key name\n",
776 exprTypeText(expr->type));
779 memcpy(val_rtrn->name, expr->value.keyName, XkbKeyNameLength);
783 log_err(ctx, "Identifier \"%s\" of type string not found\n",
784 xkb_atom_text(ctx, expr->value.str));
788 log_err(ctx, "Default \"%s.%s\" of type key name not found\n",
789 xkb_atom_text(ctx, expr->value.field.element),
790 xkb_atom_text(ctx, expr->value.field.field));
798 bogus = "Subtraction";
801 bogus = "Multiplication";
807 bogus = "Assignment";
813 bogus = "Bitwise complement";
814 log_err(ctx, "%s of key name values not permitted\n", bogus);
818 log_err(ctx, "The ! operator cannot be applied to a key name\n");
822 log_err(ctx, "The + operator cannot be applied to a key name\n");
826 log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
832 /***====================================================================***/
835 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
836 ExprResult *val_rtrn, const LookupEntry *values)
838 if (expr->op != ExprIdent) {
839 log_err(ctx, "Found a %s where an enumerated value was expected\n",
840 exprOpText(expr->op));
843 if (!SimpleLookup(ctx, values, expr->value.str, TypeInt, val_rtrn)) {
845 log_err(ctx, "Illegal identifier %s (expected one of: ",
846 xkb_atom_text(ctx, expr->value.str));
847 while (values && values->name)
850 log_info(ctx, ", %s", values->name);
852 log_info(ctx, "%s", values->name);
856 log_info(ctx, ")\n");
863 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
864 ExprResult *val_rtrn, IdentLookupFunc lookup,
865 const void *lookupPriv)
868 ExprResult leftRtrn, rightRtrn;
869 ExprDef *left, *right;
870 const char *bogus = NULL;
874 if (expr->type != TypeInt) {
876 "Found constant of type %s where a mask was expected\n",
877 exprTypeText(expr->type));
880 val_rtrn->ival = expr->value.ival;
884 ok = lookup(ctx, lookupPriv, expr->value.str, TypeInt, val_rtrn);
886 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
887 xkb_atom_text(ctx, expr->value.str));
891 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
892 xkb_atom_text(ctx, expr->value.field.element),
893 xkb_atom_text(ctx, expr->value.field.field));
897 bogus = "array reference";
901 bogus = "function use";
903 "Unexpected %s in mask expression; Expression Ignored\n",
911 left = expr->value.binary.left;
912 right = expr->value.binary.right;
913 if (ExprResolveMaskLookup(ctx, left, &leftRtrn, lookup,
915 ExprResolveMaskLookup(ctx, right, &rightRtrn, lookup,
919 val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
922 val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
926 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
927 (expr->op == OpDivide ? "divide" : "multiply"));
935 log_wsgo(ctx, "Assignment operator not implemented yet\n");
939 left = expr->value.child;
940 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
942 val_rtrn->ival = ~leftRtrn.ival;
950 left = expr->value.child;
951 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
953 log_err(ctx, "The %s operator cannot be used with a mask\n",
954 (expr->op == OpNegate ? "-" : "!"));
959 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
966 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
967 ExprResult *val_rtrn, const LookupEntry *values)
969 return ExprResolveMaskLookup(ctx, expr, val_rtrn, SimpleLookup, values);
973 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
974 ExprResult *val_rtrn)
976 return ExprResolveMaskLookup(ctx, expr, val_rtrn, LookupModMask, NULL);
980 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
981 ExprResult *val_rtrn)
983 return ExprResolveMaskLookup(keymap->ctx, expr, val_rtrn, LookupVModMask,
988 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
989 ExprResult *val_rtrn)
994 if (expr->op == ExprIdent) {
996 str = xkb_atom_text(ctx, expr->value.str);
998 sym = xkb_keysym_from_name(str);
999 if (sym != XKB_KEY_NoSymbol) {
1000 val_rtrn->uval = sym;
1005 ok = ExprResolveInteger(ctx, expr, val_rtrn);
1006 if ((ok) && (val_rtrn->uval < 10))
1007 val_rtrn->uval += '0';