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 if (rightRtrn.uval == 0) {
341 log_err(ctx, "Cannot divide by zero: %d / %d\n",
342 leftRtrn.uval, rightRtrn.uval);
345 val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
353 left = expr->value.child;
354 if (ExprResolveKeyCode(ctx, left, &leftRtrn)) {
355 val_rtrn->uval = ~leftRtrn.uval;
361 left = expr->value.child;
362 return ExprResolveKeyCode(ctx, left, val_rtrn);
365 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
372 * This function returns ... something. It's a bit of a guess, really.
374 * If a string is given in value ctx, its first character will be
375 * returned in uval. If an integer is given in value ctx, it will be
376 * returned in ival. If a float is given in value ctx, it will be
377 * returned as millimetres (rather than points) in ival.
379 * If an ident or field reference is given, the lookup function (if given)
380 * will be called. At the moment, only SimpleLookup use this, and they both
381 * return the results in uval. And don't support field references.
386 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
387 ExprResult *val_rtrn, IdentLookupFunc lookup,
388 const void *lookupPriv)
391 ExprResult leftRtrn, rightRtrn;
392 ExprDef *left, *right;
396 if (expr->type == TypeString) {
398 str = xkb_atom_text(ctx, expr->value.str);
400 switch (strlen(str)) {
405 val_rtrn->uval = str[0];
411 if (expr->type != TypeInt) {
413 "Found constant of type %s where an int was expected\n",
414 exprTypeText(expr->type));
417 val_rtrn->ival = expr->value.ival;
422 ok = lookup(ctx, lookupPriv, expr->value.str,
425 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
426 xkb_atom_text(ctx, expr->value.str));
430 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
431 xkb_atom_text(ctx, expr->value.field.element),
432 xkb_atom_text(ctx, expr->value.field.field));
439 left = expr->value.binary.left;
440 right = expr->value.binary.right;
441 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
443 ExprResolveIntegerLookup(ctx, right, &rightRtrn, lookup,
447 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
450 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
453 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
456 if (rightRtrn.ival == 0) {
457 log_err(ctx, "Cannot divide by zero: %d / %d\n",
458 leftRtrn.ival, rightRtrn.ival);
461 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
469 log_wsgo(ctx, "Assignment operator not implemented yet\n");
473 log_err(ctx, "The ! operator cannot be applied to an integer\n");
478 left = expr->value.child;
479 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
481 if (expr->op == OpNegate)
482 val_rtrn->ival = -leftRtrn.ival;
484 val_rtrn->ival = ~leftRtrn.ival;
490 left = expr->value.child;
491 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
495 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
502 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr,
503 ExprResult *val_rtrn)
505 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
509 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
510 ExprResult *val_rtrn)
513 static const LookupEntry group_names[] = {
525 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
530 if (val_rtrn->uval == 0 || val_rtrn->uval > XkbNumKbdGroups) {
531 log_err(ctx, "Group index %u is out of range (1..%d)\n",
532 val_rtrn->uval, XkbNumKbdGroups);
540 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
541 ExprResult *val_rtrn)
544 static const LookupEntry level_names[] = {
556 ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
561 if (val_rtrn->ival < 1 || val_rtrn->ival > XkbMaxShiftLevel) {
562 log_err(ctx, "Shift level %d is out of range (1..%d)\n",
563 val_rtrn->ival, XkbMaxShiftLevel);
571 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr,
572 ExprResult *val_rtrn)
574 static const LookupEntry button_names[] = {
584 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
589 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
590 ExprResult *val_rtrn)
592 ExprResult leftRtrn, rightRtrn;
595 const char *bogus = NULL;
599 if (expr->type != TypeString) {
600 log_err(ctx, "Found constant of type %s, expected a string\n",
601 exprTypeText(expr->type));
604 val_rtrn->str = xkb_atom_strdup(ctx, expr->value.str);
605 if (val_rtrn->str == NULL)
606 val_rtrn->str = strdup("");
610 log_err(ctx, "Identifier \"%s\" of type string not found\n",
611 xkb_atom_text(ctx, expr->value.str));
615 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
616 xkb_atom_text(ctx, expr->value.field.element),
617 xkb_atom_text(ctx, expr->value.field.field));
621 left = expr->value.binary.left;
622 right = expr->value.binary.right;
623 if (ExprResolveString(ctx, left, &leftRtrn) &&
624 ExprResolveString(ctx, right, &rightRtrn)) {
627 len = strlen(leftRtrn.str) + strlen(rightRtrn.str) + 1;
630 sprintf(new, "%s%s", leftRtrn.str, rightRtrn.str);
643 bogus = "Subtraction";
646 bogus = "Multiplication";
652 bogus = "Assignment";
658 bogus = "Bitwise complement";
659 log_err(ctx, "%s of string values not permitted\n", bogus);
663 log_err(ctx, "The ! operator cannot be applied to a string\n");
667 log_err(ctx, "The + operator cannot be applied to a string\n");
671 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
678 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
679 ExprResult *val_rtrn)
681 const char *bogus = NULL;
685 if (expr->type != TypeKeyName) {
686 log_err(ctx, "Found constant of type %s, expected a key name\n",
687 exprTypeText(expr->type));
690 memcpy(val_rtrn->name, expr->value.keyName, XkbKeyNameLength);
694 log_err(ctx, "Identifier \"%s\" of type string not found\n",
695 xkb_atom_text(ctx, expr->value.str));
699 log_err(ctx, "Default \"%s.%s\" of type key name not found\n",
700 xkb_atom_text(ctx, expr->value.field.element),
701 xkb_atom_text(ctx, expr->value.field.field));
709 bogus = "Subtraction";
712 bogus = "Multiplication";
718 bogus = "Assignment";
724 bogus = "Bitwise complement";
725 log_err(ctx, "%s of key name values not permitted\n", bogus);
729 log_err(ctx, "The ! operator cannot be applied to a key name\n");
733 log_err(ctx, "The + operator cannot be applied to a key name\n");
737 log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
743 /***====================================================================***/
746 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
747 ExprResult *val_rtrn, const LookupEntry *values)
749 if (expr->op != ExprIdent) {
750 log_err(ctx, "Found a %s where an enumerated value was expected\n",
751 exprOpText(expr->op));
754 if (!SimpleLookup(ctx, values, expr->value.str, TypeInt, val_rtrn)) {
756 log_err(ctx, "Illegal identifier %s (expected one of: ",
757 xkb_atom_text(ctx, expr->value.str));
758 while (values && values->name)
761 log_info(ctx, ", %s", values->name);
763 log_info(ctx, "%s", values->name);
767 log_info(ctx, ")\n");
774 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
775 ExprResult *val_rtrn, IdentLookupFunc lookup,
776 const void *lookupPriv)
779 ExprResult leftRtrn, rightRtrn;
780 ExprDef *left, *right;
781 const char *bogus = NULL;
785 if (expr->type != TypeInt) {
787 "Found constant of type %s where a mask was expected\n",
788 exprTypeText(expr->type));
791 val_rtrn->ival = expr->value.ival;
795 ok = lookup(ctx, lookupPriv, expr->value.str, TypeInt, val_rtrn);
797 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
798 xkb_atom_text(ctx, expr->value.str));
802 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
803 xkb_atom_text(ctx, expr->value.field.element),
804 xkb_atom_text(ctx, expr->value.field.field));
808 bogus = "array reference";
812 bogus = "function use";
814 "Unexpected %s in mask expression; Expression Ignored\n",
822 left = expr->value.binary.left;
823 right = expr->value.binary.right;
824 if (ExprResolveMaskLookup(ctx, left, &leftRtrn, lookup,
826 ExprResolveMaskLookup(ctx, right, &rightRtrn, lookup,
830 val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
833 val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
837 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
838 (expr->op == OpDivide ? "divide" : "multiply"));
846 log_wsgo(ctx, "Assignment operator not implemented yet\n");
850 left = expr->value.child;
851 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
853 val_rtrn->ival = ~leftRtrn.ival;
861 left = expr->value.child;
862 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
864 log_err(ctx, "The %s operator cannot be used with a mask\n",
865 (expr->op == OpNegate ? "-" : "!"));
870 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
877 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
878 ExprResult *val_rtrn, const LookupEntry *values)
880 return ExprResolveMaskLookup(ctx, expr, val_rtrn, SimpleLookup, values);
884 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
885 ExprResult *val_rtrn)
887 return ExprResolveMaskLookup(ctx, expr, val_rtrn, LookupModMask, NULL);
891 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
892 ExprResult *val_rtrn)
894 return ExprResolveMaskLookup(keymap->ctx, expr, val_rtrn, LookupVModMask,
899 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
900 ExprResult *val_rtrn)
905 if (expr->op == ExprIdent) {
907 str = xkb_atom_text(ctx, expr->value.str);
909 sym = xkb_keysym_from_name(str);
910 if (sym != XKB_KEY_NoSymbol) {
911 val_rtrn->uval = sym;
916 ok = ExprResolveInteger(ctx, expr, val_rtrn);
917 if ((ok) && (val_rtrn->uval < 10))
918 val_rtrn->uval += '0';