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 ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr,
305 ExprResult *val_rtrn)
307 ExprResult leftRtrn, rightRtrn;
308 ExprDef *left, *right;
312 if (expr->type != TypeInt) {
314 "Found constant of type %s where an int was expected\n",
315 exprTypeText(expr->type));
318 val_rtrn->uval = expr->value.uval;
325 left = expr->value.binary.left;
326 right = expr->value.binary.right;
327 if (ExprResolveKeyCode(ctx, left, &leftRtrn) &&
328 ExprResolveKeyCode(ctx, right, &rightRtrn)) {
331 val_rtrn->uval = leftRtrn.uval + rightRtrn.uval;
334 val_rtrn->uval = leftRtrn.uval - rightRtrn.uval;
337 val_rtrn->uval = leftRtrn.uval * rightRtrn.uval;
340 val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
348 left = expr->value.child;
349 if (ExprResolveKeyCode(ctx, left, &leftRtrn)) {
350 val_rtrn->uval = ~leftRtrn.uval;
356 left = expr->value.child;
357 return ExprResolveKeyCode(ctx, left, val_rtrn);
360 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
367 * This function returns ... something. It's a bit of a guess, really.
369 * If a string is given in value ctx, its first character will be
370 * returned in uval. If an integer is given in value ctx, it will be
371 * returned in ival. If a float is given in value ctx, it will be
372 * returned as millimetres (rather than points) in ival.
374 * If an ident or field reference is given, the lookup function (if given)
375 * will be called. At the moment, only SimpleLookup use this, and they both
376 * return the results in uval. And don't support field references.
381 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
382 ExprResult *val_rtrn, IdentLookupFunc lookup,
383 const void *lookupPriv)
386 ExprResult leftRtrn, rightRtrn;
387 ExprDef *left, *right;
391 if (expr->type == TypeString) {
393 str = xkb_atom_text(ctx, expr->value.str);
395 switch (strlen(str)) {
400 val_rtrn->uval = str[0];
406 if (expr->type != TypeInt) {
408 "Found constant of type %s where an int was expected\n",
409 exprTypeText(expr->type));
412 val_rtrn->ival = expr->value.ival;
417 ok = lookup(ctx, lookupPriv, expr->value.str,
420 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
421 xkb_atom_text(ctx, expr->value.str));
425 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
426 xkb_atom_text(ctx, expr->value.field.element),
427 xkb_atom_text(ctx, expr->value.field.field));
434 left = expr->value.binary.left;
435 right = expr->value.binary.right;
436 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
438 ExprResolveIntegerLookup(ctx, right, &rightRtrn, lookup,
442 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
445 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
448 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
451 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
459 log_wsgo(ctx, "Assignment operator not implemented yet\n");
463 log_err(ctx, "The ! operator cannot be applied to an integer\n");
468 left = expr->value.child;
469 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
471 if (expr->op == OpNegate)
472 val_rtrn->ival = -leftRtrn.ival;
474 val_rtrn->ival = ~leftRtrn.ival;
480 left = expr->value.child;
481 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
485 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
492 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr,
493 ExprResult *val_rtrn)
495 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
499 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
500 ExprResult *val_rtrn)
503 static const LookupEntry group_names[] = {
515 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
520 if (val_rtrn->uval == 0 || val_rtrn->uval > XkbNumKbdGroups) {
521 log_err(ctx, "Group index %u is out of range (1..%d)\n",
522 val_rtrn->uval, XkbNumKbdGroups);
530 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
531 ExprResult *val_rtrn)
534 static const LookupEntry level_names[] = {
546 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
551 if (val_rtrn->ival < 1 || val_rtrn->ival > XkbMaxShiftLevel) {
552 log_err(ctx, "Shift level %d is out of range (1..%d)\n",
553 val_rtrn->ival, XkbMaxShiftLevel);
561 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr,
562 ExprResult *val_rtrn)
564 static const LookupEntry button_names[] = {
574 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
579 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
580 ExprResult *val_rtrn)
582 ExprResult leftRtrn, rightRtrn;
585 const char *bogus = NULL;
589 if (expr->type != TypeString) {
590 log_err(ctx, "Found constant of type %s, expected a string\n",
591 exprTypeText(expr->type));
594 val_rtrn->str = xkb_atom_strdup(ctx, expr->value.str);
595 if (val_rtrn->str == NULL)
596 val_rtrn->str = strdup("");
600 log_err(ctx, "Identifier \"%s\" of type string not found\n",
601 xkb_atom_text(ctx, expr->value.str));
605 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
606 xkb_atom_text(ctx, expr->value.field.element),
607 xkb_atom_text(ctx, expr->value.field.field));
611 left = expr->value.binary.left;
612 right = expr->value.binary.right;
613 if (ExprResolveString(ctx, left, &leftRtrn) &&
614 ExprResolveString(ctx, right, &rightRtrn)) {
617 len = strlen(leftRtrn.str) + strlen(rightRtrn.str) + 1;
620 sprintf(new, "%s%s", leftRtrn.str, rightRtrn.str);
633 bogus = "Subtraction";
636 bogus = "Multiplication";
642 bogus = "Assignment";
648 bogus = "Bitwise complement";
649 log_err(ctx, "%s of string values not permitted\n", bogus);
653 log_err(ctx, "The ! operator cannot be applied to a string\n");
657 log_err(ctx, "The + operator cannot be applied to a string\n");
661 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
668 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
669 ExprResult *val_rtrn)
671 const char *bogus = NULL;
675 if (expr->type != TypeKeyName) {
676 log_err(ctx, "Found constant of type %s, expected a key name\n",
677 exprTypeText(expr->type));
680 memcpy(val_rtrn->name, expr->value.keyName, XkbKeyNameLength);
684 log_err(ctx, "Identifier \"%s\" of type string not found\n",
685 xkb_atom_text(ctx, expr->value.str));
689 log_err(ctx, "Default \"%s.%s\" of type key name not found\n",
690 xkb_atom_text(ctx, expr->value.field.element),
691 xkb_atom_text(ctx, expr->value.field.field));
699 bogus = "Subtraction";
702 bogus = "Multiplication";
708 bogus = "Assignment";
714 bogus = "Bitwise complement";
715 log_err(ctx, "%s of key name values not permitted\n", bogus);
719 log_err(ctx, "The ! operator cannot be applied to a key name\n");
723 log_err(ctx, "The + operator cannot be applied to a key name\n");
727 log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
733 /***====================================================================***/
736 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
737 ExprResult *val_rtrn, const LookupEntry *values)
739 if (expr->op != ExprIdent) {
740 log_err(ctx, "Found a %s where an enumerated value was expected\n",
741 exprOpText(expr->op));
744 if (!SimpleLookup(ctx, values, expr->value.str, TypeInt, val_rtrn)) {
746 log_err(ctx, "Illegal identifier %s (expected one of: ",
747 xkb_atom_text(ctx, expr->value.str));
748 while (values && values->name)
751 log_info(ctx, ", %s", values->name);
753 log_info(ctx, "%s", values->name);
757 log_info(ctx, ")\n");
764 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
765 ExprResult *val_rtrn, IdentLookupFunc lookup,
766 const void *lookupPriv)
769 ExprResult leftRtrn, rightRtrn;
770 ExprDef *left, *right;
771 const char *bogus = NULL;
775 if (expr->type != TypeInt) {
777 "Found constant of type %s where a mask was expected\n",
778 exprTypeText(expr->type));
781 val_rtrn->ival = expr->value.ival;
785 ok = lookup(ctx, lookupPriv, expr->value.str, TypeInt, val_rtrn);
787 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
788 xkb_atom_text(ctx, expr->value.str));
792 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
793 xkb_atom_text(ctx, expr->value.field.element),
794 xkb_atom_text(ctx, expr->value.field.field));
798 bogus = "array reference";
802 bogus = "function use";
804 "Unexpected %s in mask expression; Expression Ignored\n",
812 left = expr->value.binary.left;
813 right = expr->value.binary.right;
814 if (ExprResolveMaskLookup(ctx, left, &leftRtrn, lookup,
816 ExprResolveMaskLookup(ctx, right, &rightRtrn, lookup,
820 val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
823 val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
827 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
828 (expr->op == OpDivide ? "divide" : "multiply"));
836 log_wsgo(ctx, "Assignment operator not implemented yet\n");
840 left = expr->value.child;
841 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
843 val_rtrn->ival = ~leftRtrn.ival;
851 left = expr->value.child;
852 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
854 log_err(ctx, "The %s operator cannot be used with a mask\n",
855 (expr->op == OpNegate ? "-" : "!"));
860 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
867 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
868 ExprResult *val_rtrn, const LookupEntry *values)
870 return ExprResolveMaskLookup(ctx, expr, val_rtrn, SimpleLookup, values);
874 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
875 ExprResult *val_rtrn)
877 return ExprResolveMaskLookup(ctx, expr, val_rtrn, LookupModMask, NULL);
881 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
882 ExprResult *val_rtrn)
884 return ExprResolveMaskLookup(keymap->ctx, expr, val_rtrn, LookupVModMask,
889 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
890 ExprResult *val_rtrn)
895 if (expr->op == ExprIdent) {
897 str = xkb_atom_text(ctx, expr->value.str);
899 sym = xkb_keysym_from_name(str);
900 if (sym != XKB_KEY_NoSymbol) {
901 val_rtrn->uval = sym;
906 ok = ExprResolveInteger(ctx, expr, val_rtrn);
907 if ((ok) && (val_rtrn->uval < 10))
908 val_rtrn->uval += '0';