summaryrefslogtreecommitdiff
path: root/expr.c
blob: b5e722293890e8a51c3746e5bc82c8070279b2a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/************************************************************
 Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.

 Permission to use, copy, modify, and distribute this
 software and its documentation for any purpose and without
 fee is hereby granted, provided that the above copyright
 notice appear in all copies and that both that copyright
 notice and this permission notice appear in supporting
 documentation, and that the name of Silicon Graphics not be
 used in advertising or publicity pertaining to distribution
 of the software without specific prior written permission.
 Silicon Graphics makes no representation about the suitability
 of this software for any purpose. It is provided "as is"
 without any express or implied warranty.

 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 THE USE OR PERFORMANCE OF THIS SOFTWARE.

 ********************************************************/

#include "xkbcomp.h"
#include "tokens.h"
#include "expr.h"

#include <ctype.h>

/***====================================================================***/

char *
exprOpText(unsigned type)
{
    static char buf[32];

    switch (type)
    {
    case ExprValue:
        strcpy(buf, "literal");
        break;
    case ExprIdent:
        strcpy(buf, "identifier");
        break;
    case ExprActionDecl:
        strcpy(buf, "action declaration");
        break;
    case ExprFieldRef:
        strcpy(buf, "field reference");
        break;
    case ExprArrayRef:
        strcpy(buf, "array reference");
        break;
    case ExprKeysymList:
        strcpy(buf, "list of keysyms");
        break;
    case ExprActionList:
        strcpy(buf, "list of actions");
        break;
    case OpAdd:
        strcpy(buf, "addition");
        break;
    case OpSubtract:
        strcpy(buf, "subtraction");
        break;
    case OpMultiply:
        strcpy(buf, "multiplication");
        break;
    case OpDivide:
        strcpy(buf, "division");
        break;
    case OpAssign:
        strcpy(buf, "assignment");
        break;
    case OpNot:
        strcpy(buf, "logical not");
        break;
    case OpNegate:
        strcpy(buf, "arithmetic negation");
        break;
    case OpInvert:
        strcpy(buf, "bitwise inversion");
        break;
    case OpUnaryPlus:
        strcpy(buf, "plus sign");
        break;
    default:
        snprintf(buf, sizeof(buf), "illegal(%d)", type);
        break;
    }
    return buf;
}

char *
exprTypeText(unsigned type)
{
    static char buf[20];

    switch (type)
    {
    case TypeUnknown:
        strcpy(buf, "unknown");
        break;
    case TypeBoolean:
        strcpy(buf, "boolean");
        break;
    case TypeInt:
        strcpy(buf, "int");
        break;
    case TypeString:
        strcpy(buf, "string");
        break;
    case TypeAction:
        strcpy(buf, "action");
        break;
    case TypeKeyName:
        strcpy(buf, "keyname");
        break;
    default:
        snprintf(buf, sizeof(buf), "illegal(%d)", type);
        break;
    }
    return buf;
}

int
ExprResolveLhs(ExprDef * expr,
               ExprResult * elem_rtrn,
               ExprResult * field_rtrn, ExprDef ** index_rtrn)
{
    switch (expr->op)
    {
    case ExprIdent:
        elem_rtrn->str = NULL;
        field_rtrn->str = XkbAtomGetString(NULL, expr->value.str);
        *index_rtrn = NULL;
        return True;
    case ExprFieldRef:
        elem_rtrn->str = XkbAtomGetString(NULL, expr->value.field.element);
        field_rtrn->str = XkbAtomGetString(NULL, expr->value.field.field);
        *index_rtrn = NULL;
        return True;
    case ExprArrayRef:
        elem_rtrn->str = XkbAtomGetString(NULL, expr->value.array.element);
        field_rtrn->str = XkbAtomGetString(NULL, expr->value.array.field);
        *index_rtrn = expr->value.array.entry;
        return True;
    }
    WSGO("Unexpected operator %d in ResolveLhs\n", expr->op);
    return False;
}

Bool
SimpleLookup(XPointer priv,
             Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
{
    LookupEntry *entry;
    char *str;

    if ((priv == NULL) ||
        (field == None) || (elem != None) ||
        ((type != TypeInt) && (type != TypeFloat)))
    {
        return False;
    }
    str = XkbAtomGetString(NULL, field);
    for (entry = (LookupEntry *) priv;
         (entry != NULL) && (entry->name != NULL); entry++)
    {
        if (uStrCaseCmp(str, entry->name) == 0)
        {
            val_rtrn->uval = entry->result;
            if (type == TypeFloat)
                val_rtrn->uval *= XkbGeomPtsPerMM;
            return True;
        }
    }
    return False;
}

Bool
RadioLookup(XPointer priv,
            Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
{
    char *str;
    int rg;

    if ((field == None) || (elem != None) || (type != TypeInt))
        return False;
    str = XkbAtomGetString(NULL, field);
    if (str)
    {
        if (uStrCasePrefix("group", str))
            str += strlen("group");
        else if (uStrCasePrefix("radiogroup", str))
            str += strlen("radiogroup");
        else if (uStrCasePrefix("rg", str))
            str += strlen("rg");
        else if (!isdigit(str[0]))
            str = NULL;
    }
    if ((!str) || (sscanf(str, "%i", &rg) < 1) || (rg < 1)
        || (rg > XkbMaxRadioGroups))
        return False;
    val_rtrn->uval = rg;
    return True;
}

int
TableLookup(XPointer priv,
            Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
{
    LookupTable *tbl = (LookupTable *) priv;
    char *str;

    if ((priv == NULL) || (field == None) || (type != TypeInt))
        return False;
    str = XkbAtomGetString(NULL, elem);
    while (tbl)
    {
        if (((str == NULL) && (tbl->element == NULL)) ||
            ((str != NULL) && (tbl->element != NULL) &&
             (uStrCaseCmp(str, tbl->element) == 0)))
        {
            break;
        }
        tbl = tbl->nextElement;
    }
    if (tbl == NULL)            /* didn't find a matching element */
        return False;
    priv = (XPointer) tbl->entries;
    return SimpleLookup(priv, (Atom) None, field, type, val_rtrn);
}

static LookupEntry modIndexNames[] = {
    {"shift", ShiftMapIndex},
    {"control", ControlMapIndex},
    {"lock", LockMapIndex},
    {"mod1", Mod1MapIndex},
    {"mod2", Mod2MapIndex},
    {"mod3", Mod3MapIndex},
    {"mod4", Mod4MapIndex},
    {"mod5", Mod5MapIndex},
    {"none", XkbNoModifier},
    {NULL, 0}
};

int
LookupModIndex(XPointer priv,
               Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
{
    return SimpleLookup((XPointer) modIndexNames, elem, field, type,
                        val_rtrn);
}

int
LookupModMask(XPointer priv,
              Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
{
    char *str;

    if ((elem != None) || (type != TypeInt))
        return False;
    str = XkbAtomGetString(NULL, field);
    if (str == NULL)
        return False;
    if (uStrCaseCmp(str, "all") == 0)
        val_rtrn->uval = 0xff;
    else if (uStrCaseCmp(str, "none") == 0)
        val_rtrn->uval = 0;
    else if (LookupModIndex(priv, elem, field, type, val_rtrn))
        val_rtrn->uval = (1 << val_rtrn->uval);
    else if (priv != NULL)
    {
        LookupPriv *lpriv = (LookupPriv *) priv;
        if ((lpriv->chain == NULL) ||
            (!(*lpriv->chain) (lpriv->chainPriv, elem, field, type,
                               val_rtrn)))
            return False;
    }
    else
        return False;
    return True;
}

int
ExprResolveModIndex(ExprDef * expr,
                    ExprResult * val_rtrn,
                    IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    const char *bogus = NULL;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type != TypeInt)
        {
            ERROR
                ("Found constant of type %s where a modifier mask was expected\n",
                 exprTypeText(expr->type));
            return False;
        }
        else if ((expr->value.ival >= XkbNumModifiers)
                 || (expr->value.ival < 0))
        {
            ERROR("Illegal modifier index (%d, must be 0..%d)\n",
                   expr->value.ival, XkbNumModifiers - 1);
            return False;
        }
        val_rtrn->ival = expr->value.ival;
        return True;
    case ExprIdent:
        if (LookupModIndex(lookupPriv, (Atom) None, expr->value.str,
                           (unsigned) TypeInt, val_rtrn))
        {
            return True;
        }
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeInt, val_rtrn);
        }
        if (!ok)
            ERROR("Cannot determine modifier index for \"%s\"\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        break;
    case ExprFieldRef:
        bogus = "field reference";
        break;
    case ExprArrayRef:
        bogus = "array reference";
        break;
    case ExprActionDecl:
        bogus = "function";
        break;
    case OpAdd:
    case OpSubtract:
    case OpMultiply:
    case OpDivide:
    case OpInvert:
    case OpNegate:
    case OpNot:
    case OpUnaryPlus:
        bogus = "arithmetic operations";
        break;
    case OpAssign:
        bogus = "assignment";
        break;
    default:
        WSGO("Unknown operator %d in ResolveModIndex\n", expr->op);
        return False;
    }
    if (bogus)
    {
        ERROR("Modifier index must be a name or number, %s ignored\n",
               bogus);
        return False;
    }
    return ok;
}

int
ExprResolveModMask(ExprDef * expr,
                   ExprResult * val_rtrn,
                   IdentLookupFunc lookup, XPointer lookupPriv)
{
    LookupPriv priv;

    priv.priv = NULL;
    priv.chain = lookup;
    priv.chainPriv = lookupPriv;
    return ExprResolveMask(expr, val_rtrn, LookupModMask, (XPointer) & priv);
}

int
ExprResolveBoolean(ExprDef * expr,
                   ExprResult * val_rtrn,
                   IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    const char *bogus = NULL;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type != TypeBoolean)
        {
            ERROR
                ("Found constant of type %s where boolean was expected\n",
                 exprTypeText(expr->type));
            return False;
        }
        val_rtrn->ival = expr->value.ival;
        return True;
    case ExprIdent:
        bogus = XkbAtomGetString(NULL, expr->value.str);
        if (bogus)
        {
            if ((uStrCaseCmp(bogus, "true") == 0) ||
                (uStrCaseCmp(bogus, "yes") == 0) ||
                (uStrCaseCmp(bogus, "on") == 0))
            {
                val_rtrn->uval = 1;
                return True;
            }
            else if ((uStrCaseCmp(bogus, "false") == 0) ||
                     (uStrCaseCmp(bogus, "no") == 0) ||
                     (uStrCaseCmp(bogus, "off") == 0))
            {
                val_rtrn->uval = 0;
                return True;
            }
        }
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeBoolean, val_rtrn);
        }
        if (!ok)
            ERROR("Identifier \"%s\" of type int is unknown\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeBoolean, val_rtrn);
        }
        if (!ok)
            ERROR("Default \"%s.%s\" of type boolean is unknown\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case OpInvert:
    case OpNot:
        ok = ExprResolveBoolean(expr, val_rtrn, lookup, lookupPriv);
        if (ok)
            val_rtrn->uval = !val_rtrn->uval;
        return ok;
    case OpAdd:
        bogus = "Addition";
        goto boolean;
    case OpSubtract:
        bogus = "Subtraction";
        goto boolean;
    case OpMultiply:
        bogus = "Multiplication";
        goto boolean;
    case OpDivide:
        bogus = "Division";
        goto boolean;
    case OpAssign:
        bogus = "Assignment";
        goto boolean;
    case OpNegate:
        bogus = "Negation";
        goto boolean;
    boolean:
        ERROR("%s of boolean values not permitted\n", bogus);
        break;
    case OpUnaryPlus:
        ERROR("Unary \"+\" operator not permitted for boolean values\n");
        break;
    default:
        WSGO("Unknown operator %d in ResolveBoolean\n", expr->op);
        break;
    }
    return False;
}

int
ExprResolveFloat(ExprDef * expr,
                 ExprResult * val_rtrn,
                 IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    ExprResult leftRtrn, rightRtrn;
    ExprDef *left, *right;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type == TypeString)
        {
            char *str;
            str = XkbAtomGetString(NULL, expr->value.str);
            if ((str != NULL) && (strlen(str) == 1))
            {
                val_rtrn->uval = str[0] * XkbGeomPtsPerMM;
                return True;
            }
        }
        if ((expr->type != TypeInt) && (expr->type != TypeFloat))
        {
            ERROR("Found constant of type %s, expected a number\n",
                   exprTypeText(expr->type));
            return False;
        }
        val_rtrn->ival = expr->value.ival;
        if (expr->type == TypeInt)
            val_rtrn->ival *= XkbGeomPtsPerMM;
        return True;
    case ExprIdent:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeFloat, val_rtrn);
        }
        if (!ok)
            ERROR("Numeric identifier \"%s\" unknown\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeFloat, val_rtrn);
        }
        if (!ok)
            ERROR("Numeric default \"%s.%s\" unknown\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case OpAdd:
    case OpSubtract:
    case OpMultiply:
    case OpDivide:
        left = expr->value.binary.left;
        right = expr->value.binary.right;
        if (ExprResolveFloat(left, &leftRtrn, lookup, lookupPriv) &&
            ExprResolveFloat(right, &rightRtrn, lookup, lookupPriv))
        {
            switch (expr->op)
            {
            case OpAdd:
                val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
                break;
            case OpSubtract:
                val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
                break;
            case OpMultiply:
                val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
                break;
            case OpDivide:
                val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
                break;
            }
            return True;
        }
        return False;
    case OpAssign:
        WSGO("Assignment operator not implemented yet\n");
        break;
    case OpNot:
        left = expr->value.child;
        if (ExprResolveFloat(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The ! operator cannot be applied to a number\n");
        }
        return False;
    case OpInvert:
    case OpNegate:
        left = expr->value.child;
        if (ExprResolveFloat(left, &leftRtrn, lookup, lookupPriv))
        {
            if (expr->op == OpNegate)
                val_rtrn->ival = -leftRtrn.ival;
            else
                val_rtrn->ival = ~leftRtrn.ival;
            return True;
        }
        return False;
    case OpUnaryPlus:
        left = expr->value.child;
        return ExprResolveFloat(left, val_rtrn, lookup, lookupPriv);
    default:
        WSGO("Unknown operator %d in ResolveFloat\n", expr->op);
        break;
    }
    return False;
}

int
ExprResolveInteger(ExprDef * expr,
                   ExprResult * val_rtrn,
                   IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    ExprResult leftRtrn, rightRtrn;
    ExprDef *left, *right;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type == TypeString)
        {
            char *str;
            str = XkbAtomGetString(NULL, expr->value.str);
            if (str != NULL)
                switch (strlen(str))
                {
                case 0:
                    val_rtrn->uval = 0;
                    return True;
                case 1:
                    val_rtrn->uval = str[0];
                    return True;
                default:
                    break;
                }
        }
        if ((expr->type != TypeInt) && (expr->type != TypeFloat))
        {
            ERROR
                ("Found constant of type %s where an int was expected\n",
                 exprTypeText(expr->type));
            return False;
        }
        val_rtrn->ival = expr->value.ival;
        if (expr->type == TypeFloat)
            val_rtrn->ival /= XkbGeomPtsPerMM;
        return True;
    case ExprIdent:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeInt, val_rtrn);
        }
        if (!ok)
            ERROR("Identifier \"%s\" of type int is unknown\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeInt, val_rtrn);
        }
        if (!ok)
            ERROR("Default \"%s.%s\" of type int is unknown\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case OpAdd:
    case OpSubtract:
    case OpMultiply:
    case OpDivide:
        left = expr->value.binary.left;
        right = expr->value.binary.right;
        if (ExprResolveInteger(left, &leftRtrn, lookup, lookupPriv) &&
            ExprResolveInteger(right, &rightRtrn, lookup, lookupPriv))
        {
            switch (expr->op)
            {
            case OpAdd:
                val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
                break;
            case OpSubtract:
                val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
                break;
            case OpMultiply:
                val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
                break;
            case OpDivide:
                val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
                break;
            }
            return True;
        }
        return False;
    case OpAssign:
        WSGO("Assignment operator not implemented yet\n");
        break;
    case OpNot:
        left = expr->value.child;
        if (ExprResolveInteger(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The ! operator cannot be applied to an integer\n");
        }
        return False;
    case OpInvert:
    case OpNegate:
        left = expr->value.child;
        if (ExprResolveInteger(left, &leftRtrn, lookup, lookupPriv))
        {
            if (expr->op == OpNegate)
                val_rtrn->ival = -leftRtrn.ival;
            else
                val_rtrn->ival = ~leftRtrn.ival;
            return True;
        }
        return False;
    case OpUnaryPlus:
        left = expr->value.child;
        return ExprResolveInteger(left, val_rtrn, lookup, lookupPriv);
    default:
        WSGO("Unknown operator %d in ResolveInteger\n", expr->op);
        break;
    }
    return False;
}

int
ExprResolveString(ExprDef * expr,
                  ExprResult * val_rtrn,
                  IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    ExprResult leftRtrn, rightRtrn;
    ExprDef *left;
    ExprDef *right;
    const char *bogus = NULL;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type != TypeString)
        {
            ERROR("Found constant of type %s, expected a string\n",
                   exprTypeText(expr->type));
            return False;
        }
        val_rtrn->str = XkbAtomGetString(NULL, expr->value.str);
        if (val_rtrn->str == NULL)
        {
            static char empty_char = '\0';
            val_rtrn->str = &empty_char;
        }
        return True;
    case ExprIdent:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeString, val_rtrn);
        }
        if (!ok)
            ERROR("Identifier \"%s\" of type string not found\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeString, val_rtrn);
        }
        if (!ok)
            ERROR("Default \"%s.%s\" of type string not found\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case OpAdd:
        left = expr->value.binary.left;
        right = expr->value.binary.right;
        if (ExprResolveString(left, &leftRtrn, lookup, lookupPriv) &&
            ExprResolveString(right, &rightRtrn, lookup, lookupPriv))
        {
            int len;
            char *new;
            len = strlen(leftRtrn.str) + strlen(rightRtrn.str) + 1;
            new = (char *) uAlloc(len);
            if (new)
            {
                snprintf(new, len, "%s%s", leftRtrn.str, rightRtrn.str);
                val_rtrn->str = new;
                return True;
            }
        }
        return False;
    case OpSubtract:
        bogus = "Subtraction";
        goto string;
    case OpMultiply:
        bogus = "Multiplication";
        goto string;
    case OpDivide:
        bogus = "Division";
        goto string;
    case OpAssign:
        bogus = "Assignment";
        goto string;
    case OpNegate:
        bogus = "Negation";
        goto string;
    case OpInvert:
        bogus = "Bitwise complement";
        goto string;
    string:
        ERROR("%s of string values not permitted\n", bogus);
        return False;
    case OpNot:
        left = expr->value.child;
        if (ExprResolveString(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The ! operator cannot be applied to a string\n");
        }
        return False;
    case OpUnaryPlus:
        left = expr->value.child;
        if (ExprResolveString(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The + operator cannot be applied to a string\n");
        }
        return False;
    default:
        WSGO("Unknown operator %d in ResolveString\n", expr->op);
        break;
    }
    return False;
}

int
ExprResolveKeyName(ExprDef * expr,
                   ExprResult * val_rtrn,
                   IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    ExprDef *left;
    ExprResult leftRtrn;
    const char *bogus = NULL;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type != TypeKeyName)
        {
            ERROR("Found constant of type %s, expected a key name\n",
                   exprTypeText(expr->type));
            return False;
        }
        memcpy(val_rtrn->keyName.name, expr->value.keyName, XkbKeyNameLength);
        return True;
    case ExprIdent:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeString, val_rtrn);
        }
        if (!ok)
            ERROR("Identifier \"%s\" of type string not found\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeString, val_rtrn);
        }
        if (!ok)
            ERROR("Default \"%s.%s\" of type key name not found\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case OpAdd:
        bogus = "Addition";
        goto keyname;
    case OpSubtract:
        bogus = "Subtraction";
        goto keyname;
    case OpMultiply:
        bogus = "Multiplication";
        goto keyname;
    case OpDivide:
        bogus = "Division";
        goto keyname;
    case OpAssign:
        bogus = "Assignment";
        goto keyname;
    case OpNegate:
        bogus = "Negation";
        goto keyname;
    case OpInvert:
        bogus = "Bitwise complement";
        goto keyname;
    keyname:
        ERROR("%s of key name values not permitted\n", bogus);
        return False;
    case OpNot:
        left = expr->value.binary.left;
        if (ExprResolveString(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The ! operator cannot be applied to a key name\n");
        }
        return False;
    case OpUnaryPlus:
        left = expr->value.binary.left;
        if (ExprResolveString(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The + operator cannot be applied to a key name\n");
        }
        return False;
    default:
        WSGO("Unknown operator %d in ResolveKeyName\n", expr->op);
        break;
    }
    return False;
}

/***====================================================================***/

int
ExprResolveEnum(ExprDef * expr, ExprResult * val_rtrn, LookupEntry * values)
{
    if (expr->op != ExprIdent)
    {
        ERROR("Found a %s where an enumerated value was expected\n",
               exprOpText(expr->op));
        return False;
    }
    if (!SimpleLookup((XPointer) values, (Atom) None, expr->value.str,
                      (unsigned) TypeInt, val_rtrn))
    {
        int nOut = 0;
        ERROR("Illegal identifier %s (expected one of: ",
               XkbAtomText(NULL, expr->value.str, XkbMessage));
        while (values && values->name)
        {
            if (nOut != 0)
                INFO(", %s", values->name);
            else
                INFO("%s", values->name);
            values++;
            nOut++;
        }
        INFO(")\n");
        return False;
    }
    return True;
}

int
ExprResolveMask(ExprDef * expr,
                ExprResult * val_rtrn,
                IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    ExprResult leftRtrn, rightRtrn;
    ExprDef *left, *right;
    const char *bogus = NULL;

    switch (expr->op)
    {
    case ExprValue:
        if (expr->type != TypeInt)
        {
            ERROR
                ("Found constant of type %s where a mask was expected\n",
                 exprTypeText(expr->type));
            return False;
        }
        val_rtrn->ival = expr->value.ival;
        return True;
    case ExprIdent:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            None, expr->value.str, TypeInt, val_rtrn);
        }
        if (!ok)
            ERROR("Identifier \"%s\" of type int is unknown\n",
                   XkbAtomText(NULL, expr->value.str, XkbMessage));
        return ok;
    case ExprFieldRef:
        if (lookup)
        {
            ok = (*lookup) (lookupPriv,
                            expr->value.field.element,
                            expr->value.field.field, TypeInt, val_rtrn);
        }
        if (!ok)
            ERROR("Default \"%s.%s\" of type int is unknown\n",
                   XkbAtomText(NULL, expr->value.field.element, XkbMessage),
                   XkbAtomText(NULL, expr->value.field.field, XkbMessage));
        return ok;
    case ExprArrayRef:
        bogus = "array reference";
        goto unexpected_mask;
    case ExprActionDecl:
        bogus = "function use";
        goto unexpected_mask;
    unexpected_mask:
        ERROR("Unexpected %s in mask expression\n", bogus);
        ACTION("Expression ignored\n");
        return False;
    case OpAdd:
    case OpSubtract:
    case OpMultiply:
    case OpDivide:
        left = expr->value.binary.left;
        right = expr->value.binary.right;
        if (ExprResolveMask(left, &leftRtrn, lookup, lookupPriv) &&
            ExprResolveMask(right, &rightRtrn, lookup, lookupPriv))
        {
            switch (expr->op)
            {
            case OpAdd:
                val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
                break;
            case OpSubtract:
                val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
                break;
            case OpMultiply:
            case OpDivide:
                ERROR("Cannot %s masks\n",
                       expr->op == OpDivide ? "divide" : "multiply");
                ACTION("Illegal operation ignored\n");
                return False;
            }
            return True;
        }
        return False;
    case OpAssign:
        WSGO("Assignment operator not implemented yet\n");
        break;
    case OpInvert:
        left = expr->value.child;
        if (ExprResolveInteger(left, &leftRtrn, lookup, lookupPriv))
        {
            val_rtrn->ival = ~leftRtrn.ival;
            return True;
        }
        return False;
    case OpUnaryPlus:
    case OpNegate:
    case OpNot:
        left = expr->value.child;
        if (ExprResolveInteger(left, &leftRtrn, lookup, lookupPriv))
        {
            ERROR("The %s operator cannot be used with a mask\n",
                   (expr->op == OpNegate ? "-" : "!"));
        }
        return False;
    default:
        WSGO("Unknown operator %d in ResolveMask\n", expr->op);
        break;
    }
    return False;
}

int
ExprResolveKeySym(ExprDef * expr,
                  ExprResult * val_rtrn,
                  IdentLookupFunc lookup, XPointer lookupPriv)
{
    int ok = 0;
    KeySym sym;

    if (expr->op == ExprIdent)
    {
        char *str;
        str = XkbAtomGetString(NULL, expr->value.str);
        if ((str != NULL) && ((sym = XStringToKeysym(str)) != NoSymbol))
        {
            val_rtrn->uval = sym;
            return True;
        }
    }
    ok = ExprResolveInteger(expr, val_rtrn, lookup, lookupPriv);
    if ((ok) && (val_rtrn->uval < 10))
        val_rtrn->uval += '0';
    return ok;
}