aboutsummaryrefslogtreecommitdiff
path: root/xkbcomp/compat.c
blob: 00eb72b810b18e96fba894813be6f879540020af (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
/* $Xorg: compat.c,v 1.3 2000/08/17 19:54:30 cpqbld Exp $ */
/************************************************************
 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.

 ********************************************************/
/* $XFree86: xc/programs/xkbcomp/compat.c,v 3.3 2001/01/17 23:45:43 dawes Exp $ */

#include <X11/Xos.h>
#include "xkbcomp.h"
#include "tokens.h"
#include "expr.h"
#include "vmod.h"
#include "misc.h"
#include "indicators.h"
#include "action.h"

typedef struct _SymInterpInfo {
    CommonInfo		defs;
    XkbSymInterpretRec	interp;
} SymInterpInfo;

#define	_SI_VirtualMod		(1<<0)
#define	_SI_Action		(1<<1)
#define	_SI_AutoRepeat		(1<<2)
#define	_SI_LockingKey		(1<<3)
#define	_SI_LevelOneOnly	(1<<4)

typedef struct _GroupCompatInfo {
    unsigned char	fileID;
    unsigned char	merge;
    unsigned char	real_mods;
    unsigned short	vmods;
} GroupCompatInfo;

typedef struct _CompatInfo {
    char *		name;
    unsigned 		fileID;
    int			errorCount;
    int			nInterps;
    SymInterpInfo *	interps;
    SymInterpInfo 	dflt;
    LEDInfo		ledDflt;
    GroupCompatInfo	groupCompat[XkbNumKbdGroups];
    LEDInfo *		leds;
    VModInfo		vmods;
    ActionInfo *	act;
    XkbDescPtr		xkb;
} CompatInfo;

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

#define	ReportSINotArray(si,f,i) \
	ReportNotArray("symbol interpretation",(f),siText((si),(i)))
#define	ReportSIBadType(si,f,w,i) \
	ReportBadType("symbol interpretation",(f),siText((si),(i)),(w))

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

static char *
siText(SymInterpInfo *	si,CompatInfo *	info)
{
static char buf[128];

    if (si==&info->dflt) {
	sprintf(buf,"default");
    }
    else {
	sprintf(buf,"%s+%s(%s)",XkbKeysymText(si->interp.sym,XkbMessage),
				XkbSIMatchText(si->interp.match,XkbMessage),
				XkbModMaskText(si->interp.mods,XkbMessage));
    }
    return buf;
}

static void
InitCompatInfo(CompatInfo *info,XkbDescPtr xkb)
{
register int i;

    info->xkb= xkb;
    info->name= NULL;
    info->fileID= 0;
    info->errorCount= 0;
    info->nInterps= 0;
    info->interps= NULL;
    info->act= NULL;
    info->dflt.defs.fileID= info->fileID;
    info->dflt.defs.defined= 0;
    info->dflt.defs.merge= MergeOverride;
    info->dflt.interp.flags=	0;
    info->dflt.interp.virtual_mod= XkbNoModifier;
    info->dflt.interp.act.type= XkbSA_NoAction;
    for (i=0;i<XkbAnyActionDataSize;i++) {
	info->dflt.interp.act.data[i]= 0;
    }
    ClearIndicatorMapInfo(xkb->dpy,&info->ledDflt);
    info->ledDflt.defs.fileID= info->fileID;
    info->ledDflt.defs.defined= 0;
    info->ledDflt.defs.merge= MergeOverride;
    bzero((char *)&info->groupCompat[0],XkbNumKbdGroups*sizeof(GroupCompatInfo));
    info->leds= NULL;
    InitVModInfo(&info->vmods,xkb);
    return;
}

static void
ClearCompatInfo(CompatInfo *info,XkbDescPtr xkb)
{
register int i;

    if (info->name!=NULL)
	uFree(info->name);
    info->name= NULL;
    info->dflt.defs.defined= 0;
    info->dflt.defs.merge= MergeAugment;
    info->dflt.interp.flags= 0;
    info->dflt.interp.virtual_mod= XkbNoModifier;
    info->dflt.interp.act.type= XkbSA_NoAction;
    for (i=0;i<XkbAnyActionDataSize;i++) {
	info->dflt.interp.act.data[i]= 0;
    }
    ClearIndicatorMapInfo(xkb->dpy,&info->ledDflt);
    info->nInterps= 0;
    info->interps= (SymInterpInfo *)ClearCommonInfo(&info->interps->defs);
    bzero((char *)&info->groupCompat[0],XkbNumKbdGroups*sizeof(GroupCompatInfo));
    info->leds= (LEDInfo *)ClearCommonInfo(&info->leds->defs);
    /* 3/30/94 (ef) -- XXX! Should free action info here */
    ClearVModInfo(&info->vmods,xkb);
    return;
}

static SymInterpInfo *
NextInterp(CompatInfo *info)
{
SymInterpInfo *	si;

    si= uTypedAlloc(SymInterpInfo);
    if (si) {
	bzero((char *)si,sizeof(SymInterpInfo));
	info->interps= (SymInterpInfo *)AddCommonInfo(&info->interps->defs,
							(CommonInfo *)si);
	info->nInterps++;
    }
    return si;
}

static SymInterpInfo *
FindMatchingInterp(CompatInfo *info,SymInterpInfo *new)
{
SymInterpInfo *	old;

    for (old= info->interps;old!=NULL;old=(SymInterpInfo *)old->defs.next) {
	if ((old->interp.sym==new->interp.sym)&&
				(old->interp.mods==new->interp.mods)&&
				(old->interp.match==new->interp.match)) {
	    return  old;
	}
    }
    return NULL;
}

static Bool
AddInterp(CompatInfo *info,SymInterpInfo *new)
{
unsigned		collide;
SymInterpInfo	*	old;

    collide= 0;
    old= FindMatchingInterp(info,new);
    if (old!=NULL) {
	if (new->defs.merge==MergeReplace) {
	    SymInterpInfo *next= (SymInterpInfo *)old->defs.next;
	    if (((old->defs.fileID==new->defs.fileID)&&(warningLevel>0))||
		    					(warningLevel>9)) {
		WARN1("Multiple definitions for \"%s\"\n",siText(new,info));
		ACTION("Earlier interpretation ignored\n");
	    }
	    *old= *new;
	    old->defs.next= &next->defs;
	    return True;
	}
	if (UseNewField(_SI_VirtualMod,&old->defs,&new->defs,&collide)) {
	    old->interp.virtual_mod= new->interp.virtual_mod;
	    old->defs.defined|= _SI_VirtualMod;
	}
	if (UseNewField(_SI_Action,&old->defs,&new->defs,&collide)) {
	    old->interp.act= new->interp.act;
	    old->defs.defined|= _SI_Action;
	}
	if (UseNewField(_SI_AutoRepeat,&old->defs,&new->defs,&collide)) {
	    old->interp.flags&= ~XkbSI_AutoRepeat;
	    old->interp.flags|= (new->interp.flags&XkbSI_AutoRepeat);
	    old->defs.defined|= _SI_AutoRepeat;
	}
	if (UseNewField(_SI_LockingKey,&old->defs,&new->defs,&collide)) {
	    old->interp.flags&= ~XkbSI_LockingKey;
	    old->interp.flags|= (new->interp.flags&XkbSI_LockingKey);
	    old->defs.defined|= _SI_LockingKey;
	}
	if (UseNewField(_SI_LevelOneOnly,&old->defs,&new->defs,&collide)) {
	    old->interp.match&= ~XkbSI_LevelOneOnly;
	    old->interp.match|= (new->interp.match&XkbSI_LevelOneOnly);
	    old->defs.defined|= _SI_LevelOneOnly;
	}
	if (collide) {
	    WARN1("Multiple interpretations of \"%s\"\n",siText(new,info));
	    ACTION1("Using %s definition for duplicate fields\n",
				(new->defs.merge!=MergeAugment?"last":"first"));
	}
	return True;
    }
    old= new;
    if ((new= NextInterp(info))==NULL)
	return False;
    *new= *old;
    new->defs.next= NULL;
    return True;
}

static Bool
AddGroupCompat(CompatInfo *info,unsigned group,GroupCompatInfo *newGC)
{
GroupCompatInfo *	gc;
unsigned		merge;

    merge= newGC->merge;
    gc= &info->groupCompat[group];
    if (((gc->real_mods==newGC->real_mods)&&(gc->vmods==newGC->vmods))) {
	return True;
    }
    if (((gc->fileID==newGC->fileID)&&(warningLevel>0))||(warningLevel>9)) {
	WARN1("Compat map for group %d redefined\n",group+1);
	ACTION1("Using %s definition\n",(merge==MergeAugment?"old":"new"));
    }
    if (merge!=MergeAugment)
	*gc= *newGC;
    return True;
}

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

static Bool
ResolveStateAndPredicate(	ExprDef *	expr,
				unsigned *	pred_rtrn,
				unsigned *	mods_rtrn,
				CompatInfo *	info)
{
ExprResult	result;

    if (expr==NULL) {
	*pred_rtrn= XkbSI_AnyOfOrNone;
	*mods_rtrn= ~0;
	return True;
    }

    *pred_rtrn= XkbSI_Exactly;
    if (expr->op==ExprActionDecl) {
	char *pred_txt= XkbAtomText(NULL,expr->value.action.name,XkbMessage);
	if (uStrCaseCmp(pred_txt,"noneof")==0)
	     *pred_rtrn= XkbSI_NoneOf;
	else if (uStrCaseCmp(pred_txt,"anyofornone")==0)
	     *pred_rtrn= XkbSI_AnyOfOrNone;
	else if (uStrCaseCmp(pred_txt,"anyof")==0)
	     *pred_rtrn= XkbSI_AnyOf;
	else if (uStrCaseCmp(pred_txt,"allof")==0)
	     *pred_rtrn= XkbSI_AllOf;
	else if (uStrCaseCmp(pred_txt,"exactly")==0)
	     *pred_rtrn= XkbSI_Exactly;
	else {
	     ERROR1("Illegal modifier predicate \"%s\"\n",pred_txt);
	     ACTION("Ignored\n");
	     return False;
	}
	expr= expr->value.action.args;
    }
    else if (expr->op==ExprIdent) {
	char *pred_txt= XkbAtomText(NULL,expr->value.str,XkbMessage);
	if ((pred_txt)&&(uStrCaseCmp(pred_txt,"any")==0)) {
	    *pred_rtrn= XkbSI_AnyOf;
	    *mods_rtrn= 0xff;
	    return True;
	}
    }

    if (ExprResolveModMask(expr,&result,NULL,NULL)) {
	*mods_rtrn= result.uval;
	return True;
    }
    return False;
}

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

static void
MergeIncludedCompatMaps(	CompatInfo *	into,
				CompatInfo *	from,
				unsigned	merge)
{
SymInterpInfo * 	si;
LEDInfo *		led,*rtrn,*next;
GroupCompatInfo *	gcm;
register int		i;

    if (from->errorCount>0) {
	into->errorCount+= from->errorCount;
	return;
    }
    if (into->name==NULL) {
	into->name= from->name;
	from->name= NULL;
    }
    for (si=from->interps;si;si=(SymInterpInfo *)si->defs.next) {
	if (merge!=MergeDefault)
	    si->defs.merge= merge;
	if (!AddInterp(into,si))
	    into->errorCount++;
    }
    for (i=0,gcm=&from->groupCompat[0];i<XkbNumKbdGroups;i++,gcm++) {
	if (merge!=MergeDefault)
	    gcm->merge= merge;
	if (!AddGroupCompat(into,i,gcm))
	    into->errorCount++;
    }
    for (led=from->leds;led!=NULL;led=next) {
	next= (LEDInfo *)led->defs.next;
	if (merge!=MergeDefault)
	    led->defs.merge= merge;
	rtrn= AddIndicatorMap(into->leds,led);
	if (rtrn!=NULL) 
	     into->leds= rtrn;
	else into->errorCount++;
    }
    return;
}

typedef void	(*FileHandler)(
	XkbFile *	/* rtrn */,
	XkbDescPtr	/* xkb */,
	unsigned	/* merge */,
	CompatInfo *	/* info */
);

static Bool
HandleIncludeCompatMap(	IncludeStmt *	  stmt,
			XkbDescPtr	  xkb,
			CompatInfo *	  info,
			FileHandler	  hndlr)
{
unsigned 	newMerge;
XkbFile	*	rtrn;
CompatInfo	included;
Bool		haveSelf;

    haveSelf= False;
    if ((stmt->file==NULL)&&(stmt->map==NULL)) {
	haveSelf= True;
	included= *info;
	bzero(info,sizeof(CompatInfo));
    }
    else if (ProcessIncludeFile(stmt,XkmCompatMapIndex,&rtrn,&newMerge)) {
	InitCompatInfo(&included,xkb);
	included.fileID= rtrn->id;
	included.dflt= info->dflt;
	included.dflt.defs.fileID= rtrn->id;
	included.dflt.defs.merge= newMerge;
	included.ledDflt.defs.fileID= rtrn->id;
	included.ledDflt.defs.merge= newMerge;
	included.act= info->act;
	(*hndlr)(rtrn,xkb,MergeOverride,&included);
	if (stmt->stmt!=NULL) {
	    if (included.name!=NULL)
		uFree(included.name);
	    included.name= stmt->stmt;
	    stmt->stmt= NULL;
	}
    }
    else {
	info->errorCount+= 10;
	return False;
    }
    if ((stmt->next!=NULL)&&(included.errorCount<1)) {
	IncludeStmt *	next;
	unsigned	op;
	CompatInfo	next_incl;

	for (next=stmt->next;next!=NULL;next=next->next) {
	    if ((next->file==NULL)&&(next->map==NULL)) {
		haveSelf= True;
		MergeIncludedCompatMaps(&included,info,next->merge);
		ClearCompatInfo(info,xkb);
	    }
	    else if (ProcessIncludeFile(next,XkmCompatMapIndex,&rtrn,&op)) {
		InitCompatInfo(&next_incl,xkb);
		next_incl.fileID= rtrn->id;
		next_incl.dflt= info->dflt;
		next_incl.dflt.defs.fileID= rtrn->id;
		next_incl.dflt.defs.merge= op;
		next_incl.ledDflt.defs.fileID= rtrn->id;
		next_incl.ledDflt.defs.merge= op;
		next_incl.act= info->act;
		(*hndlr)(rtrn,xkb,MergeOverride,&next_incl);
		MergeIncludedCompatMaps(&included,&next_incl,op);
		ClearCompatInfo(&next_incl,xkb);
	    }
	    else {
		info->errorCount+= 10;
		return False;
	    }
	}
    }
    if (haveSelf)
	*info= included;
    else {
	MergeIncludedCompatMaps(info,&included,newMerge);
	ClearCompatInfo(&included,xkb);
    }
    return (info->errorCount==0);
}

static LookupEntry useModMapValues[] = {
	{	"levelone",	1	},
	{	"level1",	1	},
	{	"anylevel",	0	},
	{	"any",		0	},
	{	NULL,		0	}
};

static int
SetInterpField(	SymInterpInfo *	si,
		XkbDescPtr	xkb,
		char *		field,
		ExprDef *	arrayNdx,
		ExprDef *	value,
		CompatInfo *	info)
{
int 		ok= 1;
ExprResult	tmp;

    if (uStrCaseCmp(field,"action")==0) {
	if (arrayNdx!=NULL)
	    return ReportSINotArray(si,field,info);
	ok= HandleActionDef(value,xkb,&si->interp.act,si->defs.merge,info->act);
	if (ok) 
	     si->defs.defined|= _SI_Action;
    }
    else if ((uStrCaseCmp(field,"virtualmodifier")==0)||
	     (uStrCaseCmp(field,"virtualmod")==0)) {
	if (arrayNdx!=NULL)
	    return ReportSINotArray(si,field,info);
	ok= ResolveVirtualModifier(value,&tmp,&info->vmods);
	if (ok) {
	    si->interp.virtual_mod= tmp.uval;
	    si->defs.defined|= _SI_VirtualMod;
	}
	else return ReportSIBadType(si,field,"virtual modifier",info);
    }
    else if (uStrCaseCmp(field,"repeat")==0) {
	if (arrayNdx!=NULL)
	    return ReportSINotArray(si,field,info);
	ok= ExprResolveBoolean(value,&tmp,NULL,NULL);
	if (ok) {
	    if (tmp.uval)	si->interp.flags|= XkbSI_AutoRepeat;
	    else		si->interp.flags&= ~XkbSI_AutoRepeat;
	    si->defs.defined|= _SI_AutoRepeat;
	}
	else return ReportSIBadType(si,field,"boolean",info);
    }
    else if (uStrCaseCmp(field,"locking")==0) {
	if (arrayNdx!=NULL)
	    return ReportSINotArray(si,field,info);
	ok= ExprResolveBoolean(value,&tmp,NULL,NULL);
	if (ok) {
	    if (tmp.uval)	si->interp.flags|= XkbSI_LockingKey;
	    else		si->interp.flags&= ~XkbSI_LockingKey;
	    si->defs.defined|= _SI_LockingKey;
	}
	else return ReportSIBadType(si,field,"boolean",info);
    }
    else if ((uStrCaseCmp(field,"usemodmap")==0)||
	 	(uStrCaseCmp(field,"usemodmapmods")==0)) {
	if (arrayNdx!=NULL)
	    return ReportSINotArray(si,field,info);
	ok= ExprResolveEnum(value,&tmp,useModMapValues);
	if (ok) {
	    if (tmp.uval)	si->interp.match|= XkbSI_LevelOneOnly;
	    else		si->interp.match&= ~XkbSI_LevelOneOnly;
	    si->defs.defined|= _SI_LevelOneOnly;
	}
	else return ReportSIBadType(si,field,"level specification",info);
    }
    else {
	ok= ReportBadField("symbol interpretation",field,siText(si,info));
    }
    return ok;
}

LookupEntry groupNames[]= {
	{	"group1",	0x01	},
	{	"group2",	0x02	},
	{	"group3",	0x04	},
	{	"group4",	0x08	},
	{	"group5",	0x10	},
	{	"group6",	0x20	},
	{	"group7",	0x40	},
	{	"group8",	0x80	},
	{	"none",		0x00	},
	{	"all",		0xff	},
	{	NULL,		0	}
};

static int
HandleInterpVar(VarDef *stmt,XkbDescPtr xkb,CompatInfo *info)
{
ExprResult	elem,field;
ExprDef *	ndx;

    if (ExprResolveLhs(stmt->name,&elem,&field,&ndx)==0) 
	return 0; /* internal error, already reported */
    if (elem.str&&(uStrCaseCmp(elem.str,"interpret")==0))
	return SetInterpField(&info->dflt,xkb,field.str,ndx,stmt->value,info);	
    if (elem.str&&(uStrCaseCmp(elem.str,"indicator")==0)) {
	return SetIndicatorMapField(&info->ledDflt,xkb,field.str,ndx,
								stmt->value);
    }
    return SetActionField(xkb,elem.str,field.str,ndx,stmt->value,&info->act);
}

static int
HandleInterpBody(VarDef *def,XkbDescPtr xkb,SymInterpInfo *si,CompatInfo *info)
{
int		ok= 1;
ExprResult	tmp,field;
ExprDef *	arrayNdx;

    for (;def!=NULL;def= (VarDef *)def->common.next) {
	if ((def->name)&&(def->name->type==ExprFieldRef)) {
	    ok= HandleInterpVar(def,xkb,info);
	    continue;
	}
	ok= ExprResolveLhs(def->name,&tmp,&field,&arrayNdx);
	if (ok)
	    ok= SetInterpField(si,xkb,field.str,arrayNdx,def->value,info);
    }
    return ok;
}

static int
HandleInterpDef(InterpDef *def,XkbDescPtr xkb,unsigned merge,CompatInfo *info)
{
unsigned		pred,mods;
SymInterpInfo		si;
 
    if (!ResolveStateAndPredicate(def->match,&pred,&mods,info)) {
	ERROR("Couldn't determine matching modifiers\n");
	ACTION("Symbol interpretation ignored\n");
	return False;
    }
    if (def->merge!=MergeDefault)
	merge= def->merge;

    si= info->dflt;
    si.defs.merge= merge;
    si.interp.sym= def->sym;
    si.interp.match= pred & XkbSI_OpMask;
    si.interp.mods= mods;
    if (!HandleInterpBody(def->def,xkb,&si,info)) {
	info->errorCount++;
	return False;
    }

    if (!AddInterp(info,&si)) {
	info->errorCount++;
	return False;
    }
    return True;
}

static int
HandleGroupCompatDef(	GroupCompatDef *	def,
			XkbDescPtr		xkb,
			unsigned 		merge,
			CompatInfo *		info)
{
ExprResult	val;
GroupCompatInfo	tmp;

    if (def->merge!=MergeDefault)
	merge= def->merge;
    if (!XkbIsLegalGroup(def->group-1)) {
	ERROR1("Keyboard group must be in the range 1..%d\n",XkbNumKbdGroups+1);
	ACTION1("Compatibility map for illegal group %d ignored\n",def->group);
	return False;
    }
    tmp.fileID= info->fileID;
    tmp.merge= merge;
    if (!ExprResolveModMask(def->def,&val,LookupVModMask,(XPointer)xkb)) {
	ERROR("Expected a modifier mask in group compatibility definition\n");
	ACTION1("Ignoring illegal compatibility map for group %d\n",def->group);
	return False;
    }
    tmp.real_mods= val.uval&0xff;
    tmp.vmods= (val.uval>>8)&0xffff;
    return AddGroupCompat(info,def->group-1,&tmp);
}

static void
HandleCompatMapFile(	XkbFile	*	file,
			XkbDescPtr 	 xkb,
			unsigned	 merge,
			CompatInfo *	info)
{
ParseCommon	*stmt;

    if (merge==MergeDefault)
	merge= MergeAugment;
    info->name= uStringDup(file->name);
    stmt= file->defs;
    while (stmt) {
	switch (stmt->stmtType) {
	    case StmtInclude:
		if (!HandleIncludeCompatMap((IncludeStmt *)stmt,xkb,info,
						HandleCompatMapFile))
		    info->errorCount++;
		break;
	    case StmtInterpDef:
		if (!HandleInterpDef((InterpDef *)stmt,xkb,merge,info))
		    info->errorCount++;
		break;
	    case StmtGroupCompatDef:
		if (!HandleGroupCompatDef((GroupCompatDef*)stmt,xkb,merge,info))
		    info->errorCount++;
		break;
	    case StmtIndicatorMapDef:
		{
		    LEDInfo *rtrn;
		    rtrn= HandleIndicatorMapDef((IndicatorMapDef *)stmt,xkb,
						&info->ledDflt,info->leds,
						merge);
		    if (rtrn!=NULL)
		 	 info->leds= rtrn;
		    else info->errorCount++;
		}
		break;
	    case StmtVarDef:
		if (!HandleInterpVar((VarDef *)stmt,xkb,info))
		    info->errorCount++;
		break;
	    case StmtVModDef:
		if (!HandleVModDef((VModDef *)stmt,merge,&info->vmods))
		    info->errorCount++;
		break;
	    case StmtKeycodeDef:
		ERROR("Interpretation files may not include other types\n");
		ACTION("Ignoring definition of key name\n");
		info->errorCount++;
		break;
	    default:
		WSGO1("Unexpected statement type %d in HandleCompatMapFile\n",
								stmt->stmtType);
		break;
	}
	stmt= stmt->next;
	if (info->errorCount>10) {
#ifdef NOISY
	    ERROR("Too many errors\n");
#endif
	    ACTION1("Abandoning compatibility map \"%s\"\n",file->topName);
	    break;
	}
    }
    return;
}

static void
CopyInterps(	CompatInfo *	info,
		XkbCompatMapPtr	compat,
		Bool		needSymbol,
		unsigned	pred)
{
SymInterpInfo *		si;

    for (si=info->interps;si;si=(SymInterpInfo *)si->defs.next) {
	if (((si->interp.match&XkbSI_OpMask)!=pred)||
		(needSymbol&&(si->interp.sym==NoSymbol))||
		((!needSymbol)&&(si->interp.sym!=NoSymbol)))
	    continue;
	if (compat->num_si>=compat->size_si) {
	    WSGO("No room to merge symbol interpretations\n");
	    ACTION("Symbol interpretations lost\n");
	    return;
	}
	compat->sym_interpret[compat->num_si++]= si->interp;
    }
    return;
}

Bool
CompileCompatMap(	XkbFile *	file,
			XkbFileInfo *	result,
			unsigned	merge,
			LEDInfo **	unboundLEDs)
{
int			i;
CompatInfo		info;
XkbDescPtr		xkb;
GroupCompatInfo *	gcm;

    xkb= result->xkb;
    InitCompatInfo(&info,xkb);
    info.dflt.defs.merge= merge;
    info.ledDflt.defs.merge= merge;
    HandleCompatMapFile(file,xkb,merge,&info);

    if (info.errorCount==0) {
	int size;
	if (XkbAllocCompatMap(xkb,XkbAllCompatMask,info.nInterps)!=Success) {
	    WSGO("Couldn't allocate compatibility map\n");
	    ACTION("Exiting\n");
	    return False;
	}
	if (info.name!=NULL) {
	    if (XkbAllocNames(xkb,XkbCompatNameMask,0,0)==Success)
		xkb->names->compat= XkbInternAtom(xkb->dpy,info.name,False);
	    else {
		WSGO("Couldn't allocate space for compat name\n");
		ACTION2("Name \"%s\" (from %s) NOT assigned\n",scanFile,
								info.name);
	    }
	}
	size= info.nInterps*sizeof(XkbSymInterpretRec);
	if (size>0) {
	    CopyInterps(&info,xkb->compat,True,XkbSI_Exactly);
	    CopyInterps(&info,xkb->compat,True,XkbSI_AllOf|XkbSI_NoneOf);
	    CopyInterps(&info,xkb->compat,True,XkbSI_AnyOf);
	    CopyInterps(&info,xkb->compat,True,XkbSI_AnyOfOrNone);
	    CopyInterps(&info,xkb->compat,False,XkbSI_Exactly);
	    CopyInterps(&info,xkb->compat,False,XkbSI_AllOf|XkbSI_NoneOf);
	    CopyInterps(&info,xkb->compat,False,XkbSI_AnyOf);
	    CopyInterps(&info,xkb->compat,False,XkbSI_AnyOfOrNone);
	}
	for (i=0,gcm=&info.groupCompat[0];i<XkbNumKbdGroups;i++,gcm++) {
	    if ((gcm->fileID!=0)||(gcm->real_mods!=0)||(gcm->vmods!=0)) {
	 	xkb->compat->groups[i].mask= gcm->real_mods;
	 	xkb->compat->groups[i].real_mods= gcm->real_mods;
	 	xkb->compat->groups[i].vmods= gcm->vmods;
	    }
	}
	if (info.leds!=NULL) {
	    if (!CopyIndicatorMapDefs(result,info.leds,unboundLEDs))
		info.errorCount++;
	    info.leds= NULL;
	}
	ClearCompatInfo(&info,xkb);
	return True;
    }
    if (info.interps!=NULL)
	uFree(info.interps);
    return False;
}