expr: Remove ExprResolveFloat
[platform/upstream/libxkbcommon.git] / src / xkbcomp / expr.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
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.
15  *
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.
24  *
25  ********************************************************/
26
27 #include "expr.h"
28
29 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
30                                 xkb_atom_t field, unsigned type,
31                                 ExprResult *val_rtrn);
32
33 const char *
34 exprOpText(unsigned type)
35 {
36     static char buf[32];
37
38     switch (type) {
39     case ExprValue:
40         strcpy(buf, "literal");
41         break;
42     case ExprIdent:
43         strcpy(buf, "identifier");
44         break;
45     case ExprActionDecl:
46         strcpy(buf, "action declaration");
47         break;
48     case ExprFieldRef:
49         strcpy(buf, "field reference");
50         break;
51     case ExprArrayRef:
52         strcpy(buf, "array reference");
53         break;
54     case ExprKeysymList:
55         strcpy(buf, "list of keysyms");
56         break;
57     case ExprActionList:
58         strcpy(buf, "list of actions");
59         break;
60     case OpAdd:
61         strcpy(buf, "addition");
62         break;
63     case OpSubtract:
64         strcpy(buf, "subtraction");
65         break;
66     case OpMultiply:
67         strcpy(buf, "multiplication");
68         break;
69     case OpDivide:
70         strcpy(buf, "division");
71         break;
72     case OpAssign:
73         strcpy(buf, "assignment");
74         break;
75     case OpNot:
76         strcpy(buf, "logical not");
77         break;
78     case OpNegate:
79         strcpy(buf, "arithmetic negation");
80         break;
81     case OpInvert:
82         strcpy(buf, "bitwise inversion");
83         break;
84     case OpUnaryPlus:
85         strcpy(buf, "plus sign");
86         break;
87     default:
88         snprintf(buf, sizeof(buf), "illegal(%d)", type);
89         break;
90     }
91     return buf;
92 }
93
94 static const char *
95 exprTypeText(unsigned type)
96 {
97     static char buf[20];
98
99     switch (type) {
100     case TypeUnknown:
101         strcpy(buf, "unknown");
102         break;
103     case TypeBoolean:
104         strcpy(buf, "boolean");
105         break;
106     case TypeInt:
107         strcpy(buf, "int");
108         break;
109     case TypeString:
110         strcpy(buf, "string");
111         break;
112     case TypeAction:
113         strcpy(buf, "action");
114         break;
115     case TypeKeyName:
116         strcpy(buf, "keyname");
117         break;
118     default:
119         snprintf(buf, sizeof(buf), "illegal(%d)", type);
120         break;
121     }
122     return buf;
123 }
124
125 int
126 ExprResolveLhs(struct xkb_keymap *keymap, ExprDef *expr,
127                ExprResult *elem_rtrn, ExprResult *field_rtrn,
128                ExprDef **index_rtrn)
129 {
130     switch (expr->op) {
131     case ExprIdent:
132         elem_rtrn->str = NULL;
133         field_rtrn->str = xkb_atom_strdup(keymap->ctx,
134                                           expr->value.str);
135         *index_rtrn = NULL;
136         return true;
137     case ExprFieldRef:
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);
142         *index_rtrn = NULL;
143         return true;
144     case ExprArrayRef:
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;
150         return true;
151     }
152     log_wsgo(keymap->ctx, "Unexpected operator %d in ResolveLhs\n", expr->op);
153     return false;
154 }
155
156 static bool
157 SimpleLookup(struct xkb_context *ctx, const void *priv,
158              xkb_atom_t field, unsigned type, ExprResult *val_rtrn)
159 {
160     const LookupEntry *entry;
161     const char *str;
162
163     if ((priv == NULL) || (field == XKB_ATOM_NONE) || (type != TypeInt))
164         return false;
165
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;
170             return true;
171         }
172     }
173
174     return false;
175 }
176
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 },
187     { NULL, 0 }
188 };
189
190 bool
191 LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
192                unsigned type, ExprResult *val_rtrn)
193 {
194     return SimpleLookup(ctx, modIndexNames, field, type, val_rtrn);
195 }
196
197 bool
198 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
199               unsigned type, ExprResult *val_rtrn)
200 {
201     const char *str;
202     bool ret = true;
203
204     if (type != TypeInt)
205         return false;
206     str = xkb_atom_text(ctx, field);
207     if (str == NULL)
208         return false;
209     if (strcasecmp(str, "all") == 0)
210         val_rtrn->uval = 0xff;
211     else if (strcasecmp(str, "none") == 0)
212         val_rtrn->uval = 0;
213     else if (LookupModIndex(ctx, priv, field, type, val_rtrn))
214         val_rtrn->uval = (1 << val_rtrn->uval);
215     else
216         ret = false;
217     return ret;
218 }
219
220 int
221 ExprResolveBoolean(struct xkb_context *ctx, ExprDef *expr,
222                    ExprResult *val_rtrn)
223 {
224     int ok = 0;
225     const char *bogus = NULL;
226
227     switch (expr->op) {
228     case ExprValue:
229         if (expr->type != TypeBoolean) {
230             log_err(ctx,
231                     "Found constant of type %s where boolean was expected\n",
232                     exprTypeText(expr->type));
233             return false;
234         }
235         val_rtrn->ival = expr->value.ival;
236         return true;
237
238     case ExprIdent:
239         bogus = xkb_atom_text(ctx, expr->value.str);
240         if (bogus) {
241             if ((strcasecmp(bogus, "true") == 0) ||
242                 (strcasecmp(bogus, "yes") == 0) ||
243                 (strcasecmp(bogus, "on") == 0)) {
244                 val_rtrn->uval = 1;
245                 return true;
246             }
247             else if ((strcasecmp(bogus, "false") == 0) ||
248                      (strcasecmp(bogus, "no") == 0) ||
249                      (strcasecmp(bogus, "off") == 0)) {
250                 val_rtrn->uval = 0;
251                 return true;
252             }
253         }
254         log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
255                 xkb_atom_text(ctx, expr->value.str));
256         return false;
257
258     case ExprFieldRef:
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));
262         return false;
263
264     case OpInvert:
265     case OpNot:
266         ok = ExprResolveBoolean(ctx, expr, val_rtrn);
267         if (ok)
268             val_rtrn->uval = !val_rtrn->uval;
269         return ok;
270     case OpAdd:
271         if (bogus == NULL)
272             bogus = "Addition";
273     case OpSubtract:
274         if (bogus == NULL)
275             bogus = "Subtraction";
276     case OpMultiply:
277         if (bogus == NULL)
278             bogus = "Multiplication";
279     case OpDivide:
280         if (bogus == NULL)
281             bogus = "Division";
282     case OpAssign:
283         if (bogus == NULL)
284             bogus = "Assignment";
285     case OpNegate:
286         if (bogus == NULL)
287             bogus = "Negation";
288         log_err(ctx, "%s of boolean values not permitted\n", bogus);
289         break;
290
291     case OpUnaryPlus:
292         log_err(ctx,
293                 "Unary \"+\" operator not permitted for boolean values\n");
294         break;
295
296     default:
297         log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n", expr->op);
298         break;
299     }
300     return false;
301 }
302
303 int
304 ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr,
305                    ExprResult *val_rtrn)
306 {
307     ExprResult leftRtrn, rightRtrn;
308     ExprDef *left, *right;
309
310     switch (expr->op) {
311     case ExprValue:
312         if (expr->type != TypeInt) {
313             log_err(ctx,
314                     "Found constant of type %s where an int was expected\n",
315                     exprTypeText(expr->type));
316             return false;
317         }
318         val_rtrn->uval = expr->value.uval;
319         return true;
320
321     case OpAdd:
322     case OpSubtract:
323     case OpMultiply:
324     case OpDivide:
325         left = expr->value.binary.left;
326         right = expr->value.binary.right;
327         if (ExprResolveKeyCode(ctx, left, &leftRtrn) &&
328             ExprResolveKeyCode(ctx, right, &rightRtrn)) {
329             switch (expr->op) {
330             case OpAdd:
331                 val_rtrn->uval = leftRtrn.uval + rightRtrn.uval;
332                 break;
333             case OpSubtract:
334                 val_rtrn->uval = leftRtrn.uval - rightRtrn.uval;
335                 break;
336             case OpMultiply:
337                 val_rtrn->uval = leftRtrn.uval * rightRtrn.uval;
338                 break;
339             case OpDivide:
340                 val_rtrn->uval = leftRtrn.uval / rightRtrn.uval;
341                 break;
342             }
343             return true;
344         }
345         return false;
346
347     case OpNegate:
348         left = expr->value.child;
349         if (ExprResolveKeyCode(ctx, left, &leftRtrn)) {
350             val_rtrn->uval = ~leftRtrn.uval;
351             return true;
352         }
353         return false;
354
355     case OpUnaryPlus:
356         left = expr->value.child;
357         return ExprResolveKeyCode(ctx, left, val_rtrn);
358
359     default:
360         log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
361         break;
362     }
363     return false;
364 }
365
366 /**
367  * This function returns ... something.  It's a bit of a guess, really.
368  *
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.
373  *
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.
377  *
378  * Cool.
379  */
380 static int
381 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
382                          ExprResult *val_rtrn, IdentLookupFunc lookup,
383                          const void *lookupPriv)
384 {
385     int ok = 0;
386     ExprResult leftRtrn, rightRtrn;
387     ExprDef *left, *right;
388
389     switch (expr->op) {
390     case ExprValue:
391         if (expr->type == TypeString) {
392             const char *str;
393             str = xkb_atom_text(ctx, expr->value.str);
394             if (str != NULL)
395                 switch (strlen(str)) {
396                 case 0:
397                     val_rtrn->uval = 0;
398                     return true;
399                 case 1:
400                     val_rtrn->uval = str[0];
401                     return true;
402                 default:
403                     break;
404                 }
405         }
406         if (expr->type != TypeInt) {
407             log_err(ctx,
408                     "Found constant of type %s where an int was expected\n",
409                     exprTypeText(expr->type));
410             return false;
411         }
412         val_rtrn->ival = expr->value.ival;
413         return true;
414
415     case ExprIdent:
416         if (lookup)
417             ok = lookup(ctx, lookupPriv, expr->value.str,
418                         TypeInt, val_rtrn);
419         if (!ok)
420             log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
421                     xkb_atom_text(ctx, expr->value.str));
422         return ok;
423
424     case ExprFieldRef:
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));
428         return false;
429
430     case OpAdd:
431     case OpSubtract:
432     case OpMultiply:
433     case OpDivide:
434         left = expr->value.binary.left;
435         right = expr->value.binary.right;
436         if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
437                                      lookupPriv) &&
438             ExprResolveIntegerLookup(ctx, right, &rightRtrn, lookup,
439                                      lookupPriv)) {
440             switch (expr->op) {
441             case OpAdd:
442                 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
443                 break;
444             case OpSubtract:
445                 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
446                 break;
447             case OpMultiply:
448                 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
449                 break;
450             case OpDivide:
451                 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
452                 break;
453             }
454             return true;
455         }
456         return false;
457
458     case OpAssign:
459         log_wsgo(ctx, "Assignment operator not implemented yet\n");
460         break;
461
462     case OpNot:
463         log_err(ctx, "The ! operator cannot be applied to an integer\n");
464         return false;
465
466     case OpInvert:
467     case OpNegate:
468         left = expr->value.child;
469         if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
470                                      lookupPriv)) {
471             if (expr->op == OpNegate)
472                 val_rtrn->ival = -leftRtrn.ival;
473             else
474                 val_rtrn->ival = ~leftRtrn.ival;
475             return true;
476         }
477         return false;
478
479     case OpUnaryPlus:
480         left = expr->value.child;
481         return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
482                                         lookupPriv);
483
484     default:
485         log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
486         break;
487     }
488     return false;
489 }
490
491 int
492 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr,
493                    ExprResult *val_rtrn)
494 {
495     return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
496 }
497
498 int
499 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
500                  ExprResult *val_rtrn)
501 {
502     int ret;
503     static const LookupEntry group_names[] = {
504         { "group1", 1 },
505         { "group2", 2 },
506         { "group3", 3 },
507         { "group4", 4 },
508         { "group5", 5 },
509         { "group6", 6 },
510         { "group7", 7 },
511         { "group8", 8 },
512         { NULL, 0 }
513     };
514
515     ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
516                                    group_names);
517     if (ret == false)
518         return ret;
519
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);
523         return false;
524     }
525
526     return true;
527 }
528
529 int
530 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
531                  ExprResult *val_rtrn)
532 {
533     int ret;
534     static const LookupEntry level_names[] = {
535         { "level1", 1 },
536         { "level2", 2 },
537         { "level3", 3 },
538         { "level4", 4 },
539         { "level5", 5 },
540         { "level6", 6 },
541         { "level7", 7 },
542         { "level8", 8 },
543         { NULL, 0 }
544     };
545
546     ret = ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
547                                    level_names);
548     if (ret == false)
549         return ret;
550
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);
554         return false;
555     }
556
557     return true;
558 }
559
560 int
561 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr,
562                   ExprResult *val_rtrn)
563 {
564     static const LookupEntry button_names[] = {
565         { "button1", 1 },
566         { "button2", 2 },
567         { "button3", 3 },
568         { "button4", 4 },
569         { "button5", 5 },
570         { "default", 0 },
571         { NULL, 0 }
572     };
573
574     return ExprResolveIntegerLookup(ctx, expr, val_rtrn, SimpleLookup,
575                                     button_names);
576 }
577
578 int
579 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
580                   ExprResult *val_rtrn)
581 {
582     ExprResult leftRtrn, rightRtrn;
583     ExprDef *left;
584     ExprDef *right;
585     const char *bogus = NULL;
586
587     switch (expr->op) {
588     case ExprValue:
589         if (expr->type != TypeString) {
590             log_err(ctx, "Found constant of type %s, expected a string\n",
591                     exprTypeText(expr->type));
592             return false;
593         }
594         val_rtrn->str = xkb_atom_strdup(ctx, expr->value.str);
595         if (val_rtrn->str == NULL)
596             val_rtrn->str = strdup("");
597         return true;
598
599     case ExprIdent:
600         log_err(ctx, "Identifier \"%s\" of type string not found\n",
601                 xkb_atom_text(ctx, expr->value.str));
602         return false;
603
604     case ExprFieldRef:
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));
608         return false;
609
610     case OpAdd:
611         left = expr->value.binary.left;
612         right = expr->value.binary.right;
613         if (ExprResolveString(ctx, left, &leftRtrn) &&
614             ExprResolveString(ctx, right, &rightRtrn)) {
615             int len;
616             char *new;
617             len = strlen(leftRtrn.str) + strlen(rightRtrn.str) + 1;
618             new = malloc(len);
619             if (new) {
620                 sprintf(new, "%s%s", leftRtrn.str, rightRtrn.str);
621                 free(leftRtrn.str);
622                 free(rightRtrn.str);
623                 val_rtrn->str = new;
624                 return true;
625             }
626             free(leftRtrn.str);
627             free(rightRtrn.str);
628         }
629         return false;
630
631     case OpSubtract:
632         if (bogus == NULL)
633             bogus = "Subtraction";
634     case OpMultiply:
635         if (bogus == NULL)
636             bogus = "Multiplication";
637     case OpDivide:
638         if (bogus == NULL)
639             bogus = "Division";
640     case OpAssign:
641         if (bogus == NULL)
642             bogus = "Assignment";
643     case OpNegate:
644         if (bogus == NULL)
645             bogus = "Negation";
646     case OpInvert:
647         if (bogus == NULL)
648             bogus = "Bitwise complement";
649         log_err(ctx, "%s of string values not permitted\n", bogus);
650         return false;
651
652     case OpNot:
653         log_err(ctx, "The ! operator cannot be applied to a string\n");
654         return false;
655
656     case OpUnaryPlus:
657         log_err(ctx, "The + operator cannot be applied to a string\n");
658         return false;
659
660     default:
661         log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
662         break;
663     }
664     return false;
665 }
666
667 int
668 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
669                    ExprResult *val_rtrn)
670 {
671     const char *bogus = NULL;
672
673     switch (expr->op) {
674     case ExprValue:
675         if (expr->type != TypeKeyName) {
676             log_err(ctx, "Found constant of type %s, expected a key name\n",
677                     exprTypeText(expr->type));
678             return false;
679         }
680         memcpy(val_rtrn->name, expr->value.keyName, XkbKeyNameLength);
681         return true;
682
683     case ExprIdent:
684         log_err(ctx, "Identifier \"%s\" of type string not found\n",
685                 xkb_atom_text(ctx, expr->value.str));
686         return false;
687
688     case ExprFieldRef:
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));
692         return false;
693
694     case OpAdd:
695         if (bogus == NULL)
696             bogus = "Addition";
697     case OpSubtract:
698         if (bogus == NULL)
699             bogus = "Subtraction";
700     case OpMultiply:
701         if (bogus == NULL)
702             bogus = "Multiplication";
703     case OpDivide:
704         if (bogus == NULL)
705             bogus = "Division";
706     case OpAssign:
707         if (bogus == NULL)
708             bogus = "Assignment";
709     case OpNegate:
710         if (bogus == NULL)
711             bogus = "Negation";
712     case OpInvert:
713         if (bogus == NULL)
714             bogus = "Bitwise complement";
715         log_err(ctx, "%s of key name values not permitted\n", bogus);
716         return false;
717
718     case OpNot:
719         log_err(ctx, "The ! operator cannot be applied to a key name\n");
720         return false;
721
722     case OpUnaryPlus:
723         log_err(ctx, "The + operator cannot be applied to a key name\n");
724         return false;
725
726     default:
727         log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
728         break;
729     }
730     return false;
731 }
732
733 /***====================================================================***/
734
735 int
736 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
737                 ExprResult *val_rtrn, const LookupEntry *values)
738 {
739     if (expr->op != ExprIdent) {
740         log_err(ctx, "Found a %s where an enumerated value was expected\n",
741                 exprOpText(expr->op));
742         return false;
743     }
744     if (!SimpleLookup(ctx, values, expr->value.str, TypeInt, val_rtrn)) {
745         int nOut = 0;
746         log_err(ctx, "Illegal identifier %s (expected one of: ",
747                 xkb_atom_text(ctx, expr->value.str));
748         while (values && values->name)
749         {
750             if (nOut != 0)
751                 log_info(ctx, ", %s", values->name);
752             else
753                 log_info(ctx, "%s", values->name);
754             values++;
755             nOut++;
756         }
757         log_info(ctx, ")\n");
758         return false;
759     }
760     return true;
761 }
762
763 static int
764 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
765                       ExprResult *val_rtrn, IdentLookupFunc lookup,
766                       const void *lookupPriv)
767 {
768     int ok = 0;
769     ExprResult leftRtrn, rightRtrn;
770     ExprDef *left, *right;
771     const char *bogus = NULL;
772
773     switch (expr->op) {
774     case ExprValue:
775         if (expr->type != TypeInt) {
776             log_err(ctx,
777                     "Found constant of type %s where a mask was expected\n",
778                     exprTypeText(expr->type));
779             return false;
780         }
781         val_rtrn->ival = expr->value.ival;
782         return true;
783
784     case ExprIdent:
785         ok = lookup(ctx, lookupPriv, expr->value.str, TypeInt, val_rtrn);
786         if (!ok)
787             log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
788                     xkb_atom_text(ctx, expr->value.str));
789         return ok;
790
791     case ExprFieldRef:
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));
795         return false;
796
797     case ExprArrayRef:
798         bogus = "array reference";
799
800     case ExprActionDecl:
801         if (bogus == NULL)
802             bogus = "function use";
803         log_err(ctx,
804                 "Unexpected %s in mask expression; Expression Ignored\n",
805                 bogus);
806         return false;
807
808     case OpAdd:
809     case OpSubtract:
810     case OpMultiply:
811     case OpDivide:
812         left = expr->value.binary.left;
813         right = expr->value.binary.right;
814         if (ExprResolveMaskLookup(ctx, left, &leftRtrn, lookup,
815                                   lookupPriv) &&
816             ExprResolveMaskLookup(ctx, right, &rightRtrn, lookup,
817                                   lookupPriv)) {
818             switch (expr->op) {
819             case OpAdd:
820                 val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
821                 break;
822             case OpSubtract:
823                 val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
824                 break;
825             case OpMultiply:
826             case OpDivide:
827                 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
828                         (expr->op == OpDivide ? "divide" : "multiply"));
829                 return false;
830             }
831             return true;
832         }
833         return false;
834
835     case OpAssign:
836         log_wsgo(ctx, "Assignment operator not implemented yet\n");
837         break;
838
839     case OpInvert:
840         left = expr->value.child;
841         if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
842                                      lookupPriv)) {
843             val_rtrn->ival = ~leftRtrn.ival;
844             return true;
845         }
846         return false;
847
848     case OpUnaryPlus:
849     case OpNegate:
850     case OpNot:
851         left = expr->value.child;
852         if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
853                                      lookupPriv)) {
854             log_err(ctx, "The %s operator cannot be used with a mask\n",
855                     (expr->op == OpNegate ? "-" : "!"));
856         }
857         return false;
858
859     default:
860         log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
861         break;
862     }
863     return false;
864 }
865
866 int
867 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
868                 ExprResult *val_rtrn, const LookupEntry *values)
869 {
870     return ExprResolveMaskLookup(ctx, expr, val_rtrn, SimpleLookup, values);
871 }
872
873 int
874 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
875                    ExprResult *val_rtrn)
876 {
877     return ExprResolveMaskLookup(ctx, expr, val_rtrn, LookupModMask, NULL);
878 }
879
880 int
881 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
882                     ExprResult *val_rtrn)
883 {
884     return ExprResolveMaskLookup(keymap->ctx, expr, val_rtrn, LookupVModMask,
885                                  keymap);
886 }
887
888 int
889 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
890                   ExprResult *val_rtrn)
891 {
892     int ok = 0;
893     xkb_keysym_t sym;
894
895     if (expr->op == ExprIdent) {
896         const char *str;
897         str = xkb_atom_text(ctx, expr->value.str);
898         if (str) {
899             sym = xkb_keysym_from_name(str);
900             if (sym != XKB_KEY_NoSymbol) {
901                 val_rtrn->uval = sym;
902                 return true;
903             }
904         }
905     }
906     ok = ExprResolveInteger(ctx, expr, val_rtrn);
907     if ((ok) && (val_rtrn->uval < 10))
908         val_rtrn->uval += '0';
909     return ok;
910 }