-
Notifications
You must be signed in to change notification settings - Fork 2
/
Xrm.c
2692 lines (2442 loc) · 69.1 KB
/
Xrm.c
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
/* $Xorg: Xrm.c,v 1.6 2000/08/17 19:45:08 cpqbld Exp $ */
/***********************************************************
Copyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard
All Rights Reserved
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 Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL 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.
******************************************************************/
/*
Copyright 1987, 1988, 1990, 1998 The Open Group
All Rights Reserved.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/
/* $XFree86: xc/lib/X11/Xrm.c,v 3.15 2001/01/17 19:41:50 dawes Exp $ */
#include <stdio.h>
#include <ctype.h>
/*#include "Xlibint.h"*/
#include "nxlib.h"
#include "X11/Xresource.h"
#include "X11/Xlcint.h"
#ifdef XTHREADS
#include "locking.h"
#endif
#include "X11/XrmI.h"
#include "X11/Xos.h"
#ifdef __STDC__
#define Const const
#else
#define Const /**/
#endif
#if defined(__STDC__) && !defined(NORCONST)
#define RConst const
#else
#define RConst /**/
#endif
extern int _XOpenFile(_Xconst char *path, int flags);
extern XrmQuark _XrmInternalStringToQuark();
/*
These Xrm routines allow very fast lookup of resources in the resource
database. Several usage patterns are exploited:
(1) Widgets get a lot of resources at one time. Rather than look up each from
scratch, we can precompute the prioritized list of database levels once, then
search for each resource starting at the beginning of the list.
(2) Many database levels don't contain any leaf resource nodes. There is no
point in looking for resources on a level that doesn't contain any. This
information is kept on a per-level basis.
(3) Sometimes the widget instance tree is structured such that you get the same
class name repeated on the fully qualified widget name. This can result in the
same database level occuring multiple times on the search list. The code below
only checks to see if you get two identical search lists in a row, rather than
look back through all database levels, but in practice this removes all
duplicates I've ever observed.
Joel McCormack
*/
/*
The Xrm representation has been completely redesigned to substantially reduce
memory and hopefully improve performance.
The database is structured into two kinds of tables: LTables that contain
only values, and NTables that contain only other tables.
Some invariants:
The next pointer of the top-level node table points to the top-level leaf
table, if any.
Within an LTable, for a given name, the tight value always precedes the
loose value, and if both are present the loose value is always right after
the tight value.
Within an NTable, all of the entries for a given name are contiguous,
in the order tight NTable, loose NTable, tight LTable, loose LTable.
Bob Scheifler
*/
typedef unsigned long Signature;
static XrmQuark XrmQString, XrmQANY;
typedef Bool (*DBEnumProc)(
#if NeedNestedPrototypes /* this is Nested on purpose, to match Xlib.h */
XrmDatabase* /* db */,
XrmBindingList /* bindings */,
XrmQuarkList /* quarks */,
XrmRepresentation* /* type */,
XrmValue* /* value */,
XPointer /* closure */
#endif
);
typedef struct _VEntry {
struct _VEntry *next; /* next in chain */
XrmQuark name; /* name of this entry */
unsigned int tight:1; /* 1 if it is a tight binding */
unsigned int string:1; /* 1 if type is String */
unsigned int size:30; /* size of value */
} VEntryRec, *VEntry;
typedef struct _DEntry {
VEntryRec entry; /* entry */
XrmRepresentation type; /* representation type */
} DEntryRec, *DEntry;
/* the value is right after the structure */
#define StringValue(ve) (XPointer)((ve) + 1)
#define RepType(ve) ((DEntry)(ve))->type
/* the value is right after the structure */
#define DataValue(ve) (XPointer)(((DEntry)(ve)) + 1)
#define RawValue(ve) (char *)((ve)->string ? StringValue(ve) : DataValue(ve))
typedef struct _NTable {
struct _NTable *next; /* next in chain */
XrmQuark name; /* name of this entry */
unsigned int tight:1; /* 1 if it is a tight binding */
unsigned int leaf:1; /* 1 if children are values */
unsigned int hasloose:1; /* 1 if has loose children */
unsigned int hasany:1; /* 1 if has ANY entry */
unsigned int pad:4; /* unused */
unsigned int mask:8; /* hash size - 1 */
unsigned int entries:16; /* number of children */
} NTableRec, *NTable;
/* the buckets are right after the structure */
#define NodeBuckets(ne) ((NTable *)((ne) + 1))
#define NodeHash(ne,q) NodeBuckets(ne)[(q) & (ne)->mask]
/* leaf tables have an extra level of indirection for the buckets,
* so that resizing can be done without invalidating a search list.
* This is completely ugly, and wastes some memory, but the Xlib
* spec doesn't really specify whether invalidation is OK, and the
* old implementation did not invalidate.
*/
typedef struct _LTable {
NTableRec table;
VEntry *buckets;
} LTableRec, *LTable;
#define LeafHash(le,q) (le)->buckets[(q) & (le)->table.mask]
/* An XrmDatabase just holds a pointer to the first top-level table.
* The type name is no longer descriptive, but better to not change
* the Xresource.h header file. This type also gets used to define
* XrmSearchList, which is a complete crock, but we'll just leave it
* and caste types as required.
*/
typedef struct _XrmHashBucketRec {
NTable table;
XPointer mbstate;
XrmMethods methods;
#ifdef XTHREADS
LockInfoRec linfo;
#endif
} XrmHashBucketRec;
/* closure used in get/put resource */
typedef struct _VClosure {
XrmRepresentation *type; /* type of value */
XrmValuePtr value; /* value itself */
} VClosureRec, *VClosure;
/* closure used in get search list */
typedef struct _SClosure {
LTable *list; /* search list */
int idx; /* index of last filled element */
int limit; /* maximum index */
} SClosureRec, *SClosure;
/* placed in XrmSearchList to indicate next table is loose only */
#define LOOSESEARCH ((LTable)1)
/* closure used in enumerate database */
typedef struct _EClosure {
XrmDatabase db; /* the database */
DBEnumProc proc; /* the user proc */
XPointer closure; /* the user closure */
XrmBindingList bindings; /* binding list */
XrmQuarkList quarks; /* quark list */
int mode; /* XrmEnum<kind> */
} EClosureRec, *EClosure;
/* predicate to determine when to resize a hash table */
#define GrowthPred(n,m) ((unsigned)(n) > (((m) + 1) << 2))
#define GROW(prev) \
if (GrowthPred((*prev)->entries, (*prev)->mask)) \
GrowTable(prev)
/* pick a reasonable value for maximum depth of resource database */
#define MAXDBDEPTH 100
/* macro used in get/search functions */
/* find an entry named ename, with leafness given by leaf */
#define NFIND(ename) \
q = ename; \
entry = NodeHash(table, q); \
while (entry && entry->name != q) \
entry = entry->next; \
if (leaf && entry && !entry->leaf) { \
entry = entry->next; \
if (entry && !entry->leaf) \
entry = entry->next; \
if (entry && entry->name != q) \
entry = (NTable)NULL; \
}
/* resourceQuarks keeps track of what quarks have been associated with values
* in all LTables. If a quark has never been used in an LTable, we don't need
* to bother looking for it.
*/
static unsigned char *resourceQuarks = (unsigned char *)NULL;
static XrmQuark maxResourceQuark = -1;
/* determines if a quark has been used for a value in any database */
#define IsResourceQuark(q) ((q) > 0 && (q) <= maxResourceQuark && \
resourceQuarks[(q) >> 3] & (1 << ((q) & 7)))
typedef unsigned char XrmBits;
#define BSLASH ((XrmBits) (1 << 5))
#define NORMAL ((XrmBits) (1 << 4))
#define EOQ ((XrmBits) (1 << 3))
#define SEP ((XrmBits) (1 << 2))
#define ENDOF ((XrmBits) (1 << 1))
#define SPACE (NORMAL|EOQ|SEP|(XrmBits)0)
#define RSEP (NORMAL|EOQ|SEP|(XrmBits)1)
#define EOS (EOQ|SEP|ENDOF|(XrmBits)0)
#define EOL (EOQ|SEP|ENDOF|(XrmBits)1)
#define BINDING (NORMAL|EOQ)
#define ODIGIT (NORMAL|(XrmBits)1)
#define next_char(ch,str) xrmtypes[(unsigned char)((ch) = *(++(str)))]
#define next_mbchar(ch,len,str) xrmtypes[(unsigned char)(ch = (*db->methods->mbchar)(db->mbstate, str, &len), str += len, ch)]
#define is_space(bits) ((bits) == SPACE)
#define is_EOQ(bits) ((bits) & EOQ)
#define is_EOF(bits) ((bits) == EOS)
#define is_EOL(bits) ((bits) & ENDOF)
#define is_binding(bits) ((bits) == BINDING)
#define is_odigit(bits) ((bits) == ODIGIT)
#define is_separator(bits) ((bits) & SEP)
#define is_nonpcs(bits) (!(bits))
#define is_normal(bits) ((bits) & NORMAL)
#define is_simple(bits) ((bits) & (NORMAL|BSLASH))
#define is_special(bits) ((bits) & (ENDOF|BSLASH))
/* parsing types */
static XrmBits Const xrmtypes[256] = {
EOS,0,0,0,0,0,0,0,
0,SPACE,EOL,0,0,
#if defined(WIN32) || defined(__EMX__) /* || defined(OS2) */
EOL, /* treat CR the same as LF, just in case */
#else
0,
#endif
0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
SPACE,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,BINDING,NORMAL,NORMAL,NORMAL,BINDING,NORMAL,
ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,
NORMAL,NORMAL,RSEP,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,BSLASH,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,0
/* The rest will be automatically initialized to zero. */
};
void XrmInitialize()
{
XrmQString = XrmPermStringToQuark("String");
XrmQANY = XrmPermStringToQuark("?");
}
XrmDatabase XrmGetDatabase(display)
Display *display;
{
XrmDatabase retval;
LockDisplay(display);
retval = display->db;
UnlockDisplay(display);
return retval;
}
void XrmSetDatabase(display, database)
Display *display;
XrmDatabase database;
{
LockDisplay(display);
display->db = database;
UnlockDisplay(display);
}
#if NeedFunctionPrototypes
void XrmStringToQuarkList(
register _Xconst char *name,
register XrmQuarkList quarks) /* RETURN */
#else
void XrmStringToQuarkList(name, quarks)
register char *name;
register XrmQuarkList quarks; /* RETURN */
#endif
{
register XrmBits bits;
register Signature sig = 0;
register char ch, *tname;
register int i = 0;
if ((tname = (char *)name)) {
tname--;
while (!is_EOF(bits = next_char(ch, tname))) {
if (is_binding (bits)) {
if (i) {
/* Found a complete name */
*quarks++ = _XrmInternalStringToQuark(name,tname - name,
sig, False);
i = 0;
sig = 0;
}
name = tname+1;
}
else {
sig = (sig << 1) + ch; /* Compute the signature. */
i++;
}
}
*quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
}
*quarks = NULLQUARK;
}
#if NeedFunctionPrototypes
void XrmStringToBindingQuarkList(
register _Xconst char *name,
register XrmBindingList bindings, /* RETURN */
register XrmQuarkList quarks) /* RETURN */
#else
void XrmStringToBindingQuarkList(name, bindings, quarks)
register char *name;
register XrmBindingList bindings; /* RETURN */
register XrmQuarkList quarks; /* RETURN */
#endif
{
register XrmBits bits;
register Signature sig = 0;
register char ch, *tname;
register XrmBinding binding;
register int i = 0;
if ((tname = (char *)name)) {
tname--;
binding = XrmBindTightly;
while (!is_EOF(bits = next_char(ch, tname))) {
if (is_binding (bits)) {
if (i) {
/* Found a complete name */
*bindings++ = binding;
*quarks++ = _XrmInternalStringToQuark(name, tname - name,
sig, False);
i = 0;
sig = 0;
binding = XrmBindTightly;
}
name = tname+1;
if (ch == '*')
binding = XrmBindLoosely;
}
else {
sig = (sig << 1) + ch; /* Compute the signature. */
i++;
}
}
*bindings = binding;
*quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
}
*quarks = NULLQUARK;
}
#ifdef DEBUG
static void PrintQuarkList(quarks, stream)
XrmQuarkList quarks;
FILE *stream;
{
Bool firstNameSeen;
for (firstNameSeen = False; *quarks; quarks++) {
if (firstNameSeen) {
(void) fprintf(stream, ".");
}
firstNameSeen = True;
(void) fputs(XrmQuarkToString(*quarks), stream);
}
} /* PrintQuarkList */
#endif /* DEBUG */
/*
* Fallback methods for Xrm parsing.
* Simulate a C locale. No state needed here.
*/
static void
c_mbnoop(
XPointer state)
{
}
static char
c_mbchar(
XPointer state,
const char *str,
int *lenp)
{
*lenp = 1;
return *str;
}
static const char *
c_lcname(
XPointer state)
{
return "C";
}
static const XrmMethodsRec mb_methods = {
c_mbnoop, /* mbinit */
c_mbchar, /* mbchar */
c_mbnoop, /* mbfinish */
c_lcname, /* lcname */
c_mbnoop /* destroy */
};
static XrmDatabase NewDatabase()
{
register XrmDatabase db;
db = (XrmDatabase) Xmalloc(sizeof(XrmHashBucketRec));
if (db) {
_XCreateMutex(&db->linfo);
db->table = (NTable)NULL;
db->mbstate = (XPointer)NULL;
// FIXME db->methods = _XrmInitParseInfo(&db->mbstate);
if (!db->methods)
db->methods = &mb_methods;
}
return db;
}
/* move all values from ftable to ttable, and free ftable's buckets.
* ttable is quaranteed empty to start with.
*/
static void MoveValues(ftable, ttable)
LTable ftable;
register LTable ttable;
{
register VEntry fentry, nfentry;
register VEntry *prev;
register VEntry *bucket;
register VEntry tentry;
register int i;
for (i = ftable->table.mask, bucket = ftable->buckets; i >= 0; i--) {
for (fentry = *bucket++; fentry; fentry = nfentry) {
prev = &LeafHash(ttable, fentry->name);
tentry = *prev;
*prev = fentry;
/* chain on all with same name, to preserve invariant order */
while ((nfentry = fentry->next) && nfentry->name == fentry->name)
fentry = nfentry;
fentry->next = tentry;
}
}
Xfree((char *)ftable->buckets);
}
/* move all tables from ftable to ttable, and free ftable.
* ttable is quaranteed empty to start with.
*/
static void MoveTables(ftable, ttable)
NTable ftable;
register NTable ttable;
{
register NTable fentry, nfentry;
register NTable *prev;
register NTable *bucket;
register NTable tentry;
register int i;
for (i = ftable->mask, bucket = NodeBuckets(ftable); i >= 0; i--) {
for (fentry = *bucket++; fentry; fentry = nfentry) {
prev = &NodeHash(ttable, fentry->name);
tentry = *prev;
*prev = fentry;
/* chain on all with same name, to preserve invariant order */
while ((nfentry = fentry->next) && nfentry->name == fentry->name)
fentry = nfentry;
fentry->next = tentry;
}
}
Xfree((char *)ftable);
}
/* grow the table, based on current number of entries */
static void GrowTable(prev)
NTable *prev;
{
register NTable table;
register int i;
table = *prev;
i = table->mask;
if (i == 255) /* biggest it gets */
return;
while (i < 255 && GrowthPred(table->entries, i))
i = (i << 1) + 1;
i++; /* i is now the new size */
if (table->leaf) {
register LTable ltable;
LTableRec otable;
ltable = (LTable)table;
/* cons up a copy to make MoveValues look symmetric */
otable = *ltable;
ltable->buckets = (VEntry *)Xmalloc(i * sizeof(VEntry));
if (!ltable->buckets) {
ltable->buckets = otable.buckets;
return;
}
ltable->table.mask = i - 1;
bzero((char *)ltable->buckets, i * sizeof(VEntry));
MoveValues(&otable, ltable);
} else {
register NTable ntable;
ntable = (NTable)Xmalloc(sizeof(NTableRec) + i * sizeof(NTable));
if (!ntable)
return;
*ntable = *table;
ntable->mask = i - 1;
bzero((char *)NodeBuckets(ntable), i * sizeof(NTable));
*prev = ntable;
MoveTables(table, ntable);
}
}
/* merge values from ftable into *pprev, destroy ftable in the process */
static void MergeValues(ftable, pprev, override)
LTable ftable;
NTable *pprev;
Bool override;
{
register VEntry fentry, tentry;
register VEntry *prev;
register LTable ttable;
VEntry *bucket;
int i;
register XrmQuark q;
ttable = (LTable)*pprev;
if (ftable->table.hasloose)
ttable->table.hasloose = 1;
for (i = ftable->table.mask, bucket = ftable->buckets;
i >= 0;
i--, bucket++) {
for (fentry = *bucket; fentry; ) {
q = fentry->name;
prev = &LeafHash(ttable, q);
tentry = *prev;
while (tentry && tentry->name != q)
tentry = *(prev = &tentry->next);
/* note: test intentionally uses fentry->name instead of q */
/* permits serendipitous inserts */
while (tentry && tentry->name == fentry->name) {
/* if tentry is earlier, skip it */
if (!fentry->tight && tentry->tight) {
tentry = *(prev = &tentry->next);
continue;
}
if (fentry->tight != tentry->tight) {
/* no match, chain in fentry */
*prev = fentry;
prev = &fentry->next;
fentry = *prev;
*prev = tentry;
ttable->table.entries++;
} else if (override) {
/* match, chain in fentry, splice out and free tentry */
*prev = fentry;
prev = &fentry->next;
fentry = *prev;
*prev = tentry->next;
/* free the overridden entry */
Xfree((char *)tentry);
/* get next tentry */
tentry = *prev;
} else {
/* match, discard fentry */
prev = &tentry->next;
tentry = fentry; /* use as a temp var */
fentry = fentry->next;
/* free the overpowered entry */
Xfree((char *)tentry);
/* get next tentry */
tentry = *prev;
}
if (!fentry)
break;
}
/* at this point, tentry cannot match any fentry named q */
/* chain in all bindings together, preserve invariant order */
while (fentry && fentry->name == q) {
*prev = fentry;
prev = &fentry->next;
fentry = *prev;
*prev = tentry;
ttable->table.entries++;
}
}
}
Xfree((char *)ftable->buckets);
Xfree((char *)ftable);
/* resize if necessary, now that we're all done */
GROW(pprev);
}
/* merge tables from ftable into *pprev, destroy ftable in the process */
static void MergeTables(ftable, pprev, override)
NTable ftable;
NTable *pprev;
Bool override;
{
register NTable fentry, tentry;
NTable nfentry;
register NTable *prev;
register NTable ttable;
NTable *bucket;
int i;
register XrmQuark q;
ttable = *pprev;
if (ftable->hasloose)
ttable->hasloose = 1;
if (ftable->hasany)
ttable->hasany = 1;
for (i = ftable->mask, bucket = NodeBuckets(ftable);
i >= 0;
i--, bucket++) {
for (fentry = *bucket; fentry; ) {
q = fentry->name;
prev = &NodeHash(ttable, q);
tentry = *prev;
while (tentry && tentry->name != q)
tentry = *(prev = &tentry->next);
/* note: test intentionally uses fentry->name instead of q */
/* permits serendipitous inserts */
while (tentry && tentry->name == fentry->name) {
/* if tentry is earlier, skip it */
if ((fentry->leaf && !tentry->leaf) ||
(!fentry->tight && tentry->tight &&
(fentry->leaf || !tentry->leaf))) {
tentry = *(prev = &tentry->next);
continue;
}
nfentry = fentry->next;
if (fentry->leaf != tentry->leaf ||
fentry->tight != tentry->tight) {
/* no match, just chain in */
*prev = fentry;
*(prev = &fentry->next) = tentry;
ttable->entries++;
} else {
if (fentry->leaf)
MergeValues((LTable)fentry, prev, override);
else
MergeTables(fentry, prev, override);
/* bump to next tentry */
tentry = *(prev = &(*prev)->next);
}
/* bump to next fentry */
fentry = nfentry;
if (!fentry)
break;
}
/* at this point, tentry cannot match any fentry named q */
/* chain in all bindings together, preserve invariant order */
while (fentry && fentry->name == q) {
*prev = fentry;
prev = &fentry->next;
fentry = *prev;
*prev = tentry;
ttable->entries++;
}
}
}
Xfree((char *)ftable);
/* resize if necessary, now that we're all done */
GROW(pprev);
}
void XrmCombineDatabase(from, into, override)
XrmDatabase from, *into;
Bool override;
{
register NTable *prev;
register NTable ftable, ttable, nftable;
if (!*into) {
*into = from;
} else if (from) {
_XLockMutex(&from->linfo);
_XLockMutex(&(*into)->linfo);
if ((ftable = from->table)) {
prev = &(*into)->table;
ttable = *prev;
if (!ftable->leaf) {
nftable = ftable->next;
if (ttable && !ttable->leaf) {
/* both have node tables, merge them */
MergeTables(ftable, prev, override);
/* bump to into's leaf table, if any */
ttable = *(prev = &(*prev)->next);
} else {
/* into has no node table, link from's in */
*prev = ftable;
*(prev = &ftable->next) = ttable;
}
/* bump to from's leaf table, if any */
ftable = nftable;
} else {
/* bump to into's leaf table, if any */
if (ttable && !ttable->leaf)
ttable = *(prev = &ttable->next);
}
if (ftable) {
/* if into has a leaf, merge, else insert */
if (ttable)
MergeValues((LTable)ftable, prev, override);
else
*prev = ftable;
}
}
(from->methods->destroy)(from->mbstate);
_XFreeMutex(&from->linfo);
Xfree((char *)from);
_XUnlockMutex(&(*into)->linfo);
}
}
void XrmMergeDatabases(from, into)
XrmDatabase from, *into;
{
XrmCombineDatabase(from, into, True);
}
/* store a value in the database, overriding any existing entry */
static void PutEntry(db, bindings, quarks, type, value)
XrmDatabase db;
XrmBindingList bindings;
XrmQuarkList quarks;
XrmRepresentation type;
XrmValuePtr value;
{
register NTable *pprev, *prev;
register NTable table;
register XrmQuark q;
register VEntry *vprev;
register VEntry entry;
NTable *nprev, *firstpprev;
#define NEWTABLE(q,i) \
table = (NTable)Xmalloc(sizeof(LTableRec)); \
if (!table) \
return; \
table->name = q; \
table->hasloose = 0; \
table->hasany = 0; \
table->mask = 0; \
table->entries = 0; \
if (quarks[i]) { \
table->leaf = 0; \
nprev = NodeBuckets(table); \
} else { \
table->leaf = 1; \
if (!(nprev = (NTable *)Xmalloc(sizeof(VEntry *)))) \
return; \
((LTable)table)->buckets = (VEntry *)nprev; \
} \
*nprev = (NTable)NULL; \
table->next = *prev; \
*prev = table
if (!db || !*quarks)
return;
table = *(prev = &db->table);
/* if already at leaf, bump to the leaf table */
if (!quarks[1] && table && !table->leaf)
table = *(prev = &table->next);
pprev = prev;
if (!table || (quarks[1] && table->leaf)) {
/* no top-level node table, create one and chain it in */
NEWTABLE(NULLQUARK,1);
table->tight = 1; /* arbitrary */
prev = nprev;
} else {
/* search along until we need a value */
while (quarks[1]) {
q = *quarks;
table = *(prev = &NodeHash(table, q));
while (table && table->name != q)
table = *(prev = &table->next);
if (!table)
break; /* not found */
if (quarks[2]) {
if (table->leaf)
break; /* not found */
} else {
if (!table->leaf) {
/* bump to leaf table, if any */
table = *(prev = &table->next);
if (!table || table->name != q)
break; /* not found */
if (!table->leaf) {
/* bump to leaf table, if any */
table = *(prev = &table->next);
if (!table || table->name != q)
break; /* not found */
}
}
}
if (*bindings == XrmBindTightly) {
if (!table->tight)
break; /* not found */
} else {
if (table->tight) {
/* bump to loose table, if any */
table = *(prev = &table->next);
if (!table || table->name != q ||
!quarks[2] != table->leaf)
break; /* not found */
}
}
/* found that one, bump to next quark */
pprev = prev;
quarks++;
bindings++;
}
if (!quarks[1]) {
/* found all the way to a leaf */
q = *quarks;
entry = *(vprev = &LeafHash((LTable)table, q));
while (entry && entry->name != q)
entry = *(vprev = &entry->next);
/* if want loose and have tight, bump to next entry */
if (entry && *bindings == XrmBindLoosely && entry->tight)
entry = *(vprev = &entry->next);
if (entry && entry->name == q &&
(*bindings == XrmBindTightly) == entry->tight) {
/* match, need to override */
if ((type == XrmQString) == entry->string &&
entry->size == value->size) {
/* update type if not String, can be different */
if (!entry->string)
RepType(entry) = type;
/* identical size, just overwrite value */
memcpy(RawValue(entry), (char *)value->addr, value->size);
return;
}
/* splice out and free old entry */
*vprev = entry->next;
Xfree((char *)entry);
(*pprev)->entries--;
}
/* this is where to insert */
prev = (NTable *)vprev;
}
}
/* keep the top table, because we may have to grow it */
firstpprev = pprev;
/* iterate until we get to the leaf */
while (quarks[1]) {
/* build a new table and chain it in */
NEWTABLE(*quarks,2);
if (*quarks++ == XrmQANY)
(*pprev)->hasany = 1;
if (*bindings++ == XrmBindTightly) {
table->tight = 1;
} else {
table->tight = 0;
(*pprev)->hasloose = 1;
}
(*pprev)->entries++;
pprev = prev;
prev = nprev;
}
/* now allocate the value entry */
entry = (VEntry)Xmalloc(((type == XrmQString) ?
sizeof(VEntryRec) : sizeof(DEntryRec)) +
value->size);
if (!entry)
return;
entry->name = q = *quarks;
if (*bindings == XrmBindTightly) {
entry->tight = 1;
} else {
entry->tight = 0;
(*pprev)->hasloose = 1;
}
/* chain it in, with a bit of type cast ugliness */
entry->next = *((VEntry *)prev);
*((VEntry *)prev) = entry;
entry->size = value->size;
if (type == XrmQString) {
entry->string = 1;
} else {
entry->string = 0;
RepType(entry) = type;
}
/* save a copy of the value */
memcpy(RawValue(entry), (char *)value->addr, value->size);
(*pprev)->entries++;
/* this is a new leaf, need to remember it for search lists */
if (q > maxResourceQuark) {
unsigned oldsize = (maxResourceQuark + 1) >> 3;
unsigned size = ((q | 0x7f) + 1) >> 3; /* reallocate in chunks */
if (resourceQuarks) {
unsigned char *prevQuarks = resourceQuarks;
resourceQuarks = (unsigned char *)Xrealloc((char *)resourceQuarks,
size);
if (!resourceQuarks) {
Xfree(prevQuarks);
}
} else
resourceQuarks = (unsigned char *)Xmalloc(size);
if (resourceQuarks) {
bzero((char *)&resourceQuarks[oldsize], size - oldsize);
maxResourceQuark = (size << 3) - 1;
} else {
maxResourceQuark = -1;