home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xrm_Core.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  76KB  |  2,724 lines

  1. /* $TOG: Xrm.c /main/92 1997/06/22 07:14:10 kaleb $ */
  2.  
  3. /***********************************************************
  4. Copyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the name Digital not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25. /*
  26.  
  27. Copyright (c) 1987, 1988, 1990  X Consortium
  28.  
  29. Permission is hereby granted, free of charge, to any person obtaining
  30. a copy of this software and associated documentation files (the
  31. "Software"), to deal in the Software without restriction, including
  32. without limitation the rights to use, copy, modify, merge, publish,
  33. distribute, sublicense, and/or sell copies of the Software, and to
  34. permit persons to whom the Software is furnished to do so, subject to
  35. the following conditions:
  36.  
  37. The above copyright notice and this permission notice shall be included
  38. in all copies or substantial portions of the Software.
  39.  
  40. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  41. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  42. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  43. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  44. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  45. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  46. OTHER DEALINGS IN THE SOFTWARE.
  47.  
  48. Except as contained in this notice, the name of the X Consortium shall
  49. not be used in advertising or otherwise to promote the sale, use or
  50. other dealings in this Software without prior written authorization
  51. from the X Consortium.
  52.  
  53. */
  54. /* $XFree86: xc/lib/X11/Xrm.c,v 3.3.4.4 1998/10/04 13:36:22 hohndel Exp $ */
  55.  
  56. #include    <stdio.h>
  57. #include    <ctype.h>
  58. #include    "Xlib_private.h"
  59. #include    <X11/Xresource.h>
  60. #include    "Xlcint.h"
  61. #ifdef XTHREADS
  62. #include    "locking.h"
  63. #endif
  64. #include     "XrmI.h"
  65. #include    <X11/Xos.h>
  66.  
  67. #ifdef __STDC__
  68. #define Const const
  69. #else
  70. #define Const /**/
  71. #endif
  72. #if defined(__STDC__) && !defined(NORCONST)
  73. #define RConst const
  74. #else
  75. #define RConst /**/
  76. #endif
  77.  
  78. /*
  79.  
  80. These Xrm routines allow very fast lookup of resources in the resource
  81. database.  Several usage patterns are exploited:
  82.  
  83. (1) Widgets get a lot of resources at one time.  Rather than look up each from
  84. scratch, we can precompute the prioritized list of database levels once, then
  85. search for each resource starting at the beginning of the list.
  86.  
  87. (2) Many database levels don't contain any leaf resource nodes.  There is no
  88. point in looking for resources on a level that doesn't contain any.  This
  89. information is kept on a per-level basis.
  90.  
  91. (3) Sometimes the widget instance tree is structured such that you get the same
  92. class name repeated on the fully qualified widget name.  This can result in the
  93. same database level occuring multiple times on the search list.  The code below
  94. only checks to see if you get two identical search lists in a row, rather than
  95. look back through all database levels, but in practice this removes all
  96. duplicates I've ever observed.
  97.  
  98. Joel McCormack
  99.  
  100. */
  101.  
  102. /*
  103.  
  104. The Xrm representation has been completely redesigned to substantially reduce
  105. memory and hopefully improve performance.
  106.  
  107. The database is structured into two kinds of tables: LTables that contain
  108. only values, and NTables that contain only other tables.
  109.  
  110. Some invariants:
  111.  
  112. The next pointer of the top-level node table points to the top-level leaf
  113. table, if any.
  114.  
  115. Within an LTable, for a given name, the tight value always precedes the
  116. loose value, and if both are present the loose value is always right after
  117. the tight value.
  118.  
  119. Within an NTable, all of the entries for a given name are contiguous,
  120. in the order tight NTable, loose NTable, tight LTable, loose LTable.
  121.  
  122. Bob Scheifler
  123.  
  124. */
  125.  
  126. typedef unsigned long Signature;
  127.  
  128. static XrmQuark XrmQString, XrmQANY;
  129.  
  130. typedef    Bool (*DBEnumProc)(
  131. #if NeedNestedPrototypes    /* this is Nested on purpose, to match Xlib.h */
  132.     XrmDatabase*    /* db */,
  133.     XrmBindingList    /* bindings */,
  134.     XrmQuarkList    /* quarks */,
  135.     XrmRepresentation*    /* type */,
  136.     XrmValue*        /* value */,
  137.     XPointer        /* closure */
  138. #endif
  139. );
  140.  
  141. typedef struct _VEntry {
  142.     struct _VEntry    *next;        /* next in chain */
  143.     XrmQuark        name;        /* name of this entry */
  144.     unsigned int    tight:1;    /* 1 if it is a tight binding */
  145.     unsigned int    string:1;    /* 1 if type is String */
  146.     unsigned int    size:30;    /* size of value */
  147. } VEntryRec, *VEntry;
  148.  
  149.  
  150. typedef struct _DEntry {
  151.     VEntryRec        entry;        /* entry */
  152.     XrmRepresentation    type;        /* representation type */
  153. } DEntryRec, *DEntry;
  154.  
  155. /* the value is right after the structure */
  156. #define StringValue(ve) (XPointer)((ve) + 1)
  157. #define RepType(ve) ((DEntry)(ve))->type
  158. /* the value is right after the structure */
  159. #define DataValue(ve) (XPointer)(((DEntry)(ve)) + 1)
  160. #define RawValue(ve) (char *)((ve)->string ? StringValue(ve) : DataValue(ve))
  161.  
  162. typedef struct _NTable {
  163.     struct _NTable    *next;        /* next in chain */
  164.     XrmQuark        name;        /* name of this entry */
  165.     unsigned int    tight:1;    /* 1 if it is a tight binding */
  166.     unsigned int    leaf:1;        /* 1 if children are values */
  167.     unsigned int    hasloose:1;    /* 1 if has loose children */
  168.     unsigned int    hasany:1;    /* 1 if has ANY entry */
  169.     unsigned int    pad:4;        /* unused */
  170.     unsigned int    mask:8;        /* hash size - 1 */
  171.     unsigned int    entries:16;    /* number of children */
  172. } NTableRec, *NTable;
  173.  
  174. /* the buckets are right after the structure */
  175. #define NodeBuckets(ne) ((NTable *)((ne) + 1))
  176. #define NodeHash(ne,q) NodeBuckets(ne)[(q) & (ne)->mask]
  177.  
  178. /* leaf tables have an extra level of indirection for the buckets,
  179.  * so that resizing can be done without invalidating a search list.
  180.  * This is completely ugly, and wastes some memory, but the Xlib
  181.  * spec doesn't really specify whether invalidation is OK, and the
  182.  * old implementation did not invalidate.
  183.  */
  184. typedef struct _LTable {
  185.     NTableRec        table;
  186.     VEntry        *buckets;
  187. } LTableRec, *LTable;
  188.  
  189. #define LeafHash(le,q) (le)->buckets[(q) & (le)->table.mask]
  190.  
  191. /* An XrmDatabase just holds a pointer to the first top-level table.
  192.  * The type name is no longer descriptive, but better to not change
  193.  * the Xresource.h header file.  This type also gets used to define
  194.  * XrmSearchList, which is a complete crock, but we'll just leave it
  195.  * and caste types as required.
  196.  */
  197. typedef struct _XrmHashBucketRec {
  198.     NTable table;
  199.     XPointer mbstate;
  200.     XrmMethods methods;
  201. #ifdef XTHREADS
  202.     LockInfoRec linfo;
  203. #endif
  204. } XrmHashBucketRec;
  205.  
  206. /* closure used in get/put resource */
  207. typedef struct _VClosure {
  208.     XrmRepresentation    *type;        /* type of value */
  209.     XrmValuePtr        value;        /* value itself */
  210. } VClosureRec, *VClosure;
  211.  
  212. /* closure used in get search list */
  213. typedef struct _SClosure {
  214.     LTable        *list;        /* search list */
  215.     int            idx;        /* index of last filled element */
  216.     int            limit;        /* maximum index */
  217. } SClosureRec, *SClosure;
  218.  
  219. /* placed in XrmSearchList to indicate next table is loose only */
  220. #define LOOSESEARCH ((LTable)1)
  221.  
  222. /* closure used in enumerate database */
  223. typedef struct _EClosure {
  224.     XrmDatabase db;            /* the database */
  225.     DBEnumProc proc;            /* the user proc */
  226.     XPointer closure;            /* the user closure */
  227.     XrmBindingList bindings;        /* binding list */
  228.     XrmQuarkList quarks;        /* quark list */
  229.     int mode;                /* XrmEnum<kind> */
  230. } EClosureRec, *EClosure;
  231.  
  232. /* predicate to determine when to resize a hash table */
  233. #define GrowthPred(n,m) ((unsigned)(n) > (((m) + 1) << 2))
  234.  
  235. #define GROW(prev) \
  236.     if (GrowthPred((*prev)->entries, (*prev)->mask)) \
  237.     GrowTable(prev)
  238.  
  239. /* pick a reasonable value for maximum depth of resource database */
  240. #define MAXDBDEPTH 100
  241.  
  242. /* macro used in get/search functions */
  243.  
  244. /* find an entry named ename, with leafness given by leaf */
  245. #define NFIND(ename) \
  246.     q = ename; \
  247.     entry = NodeHash(table, q); \
  248.     while (entry && entry->name != q) \
  249.     entry = entry->next; \
  250.     if (leaf && entry && !entry->leaf) { \
  251.     entry = entry->next; \
  252.     if (entry && !entry->leaf) \
  253.         entry = entry->next; \
  254.     if (entry && entry->name != q) \
  255.         entry = (NTable)NULL; \
  256.     }
  257.  
  258. /* resourceQuarks keeps track of what quarks have been associated with values
  259.  * in all LTables.  If a quark has never been used in an LTable, we don't need
  260.  * to bother looking for it.
  261.  */
  262.  
  263. static unsigned char *resourceQuarks = (unsigned char *)NULL;
  264. static XrmQuark maxResourceQuark = -1;
  265.  
  266. /* determines if a quark has been used for a value in any database */
  267. #define IsResourceQuark(q)  ((q) > 0 && (q) <= maxResourceQuark && \
  268.                  resourceQuarks[(q) >> 3] & (1 << ((q) & 7)))
  269.  
  270. typedef unsigned char XrmBits;
  271.  
  272. #define BSLASH  ((XrmBits) (1 << 5))
  273. #define NORMAL    ((XrmBits) (1 << 4))
  274. #define EOQ    ((XrmBits) (1 << 3))
  275. #define SEP    ((XrmBits) (1 << 2))
  276. #define ENDOF    ((XrmBits) (1 << 1))
  277. #define SPACE    (NORMAL|EOQ|SEP|(XrmBits)0)
  278. #define RSEP    (NORMAL|EOQ|SEP|(XrmBits)1)
  279. #define EOS    (EOQ|SEP|ENDOF|(XrmBits)0)
  280. #define EOL    (EOQ|SEP|ENDOF|(XrmBits)1)
  281. #define BINDING    (NORMAL|EOQ)
  282. #define ODIGIT    (NORMAL|(XrmBits)1)
  283.  
  284. #define next_char(ch,str) xrmtypes[(unsigned char)((ch) = *(++(str)))]
  285. #define next_mbchar(ch,len,str) xrmtypes[(unsigned char)(ch = (*db->methods->mbchar)(db->mbstate, str, &len), str += len, ch)]
  286.  
  287. #define is_space(bits)        ((bits) == SPACE)
  288. #define is_EOQ(bits)        ((bits) & EOQ)
  289. #define is_EOF(bits)        ((bits) == EOS)
  290. #define is_EOL(bits)        ((bits) & ENDOF)
  291. #define is_binding(bits)    ((bits) == BINDING)
  292. #define is_odigit(bits)        ((bits) == ODIGIT)
  293. #define is_separator(bits)    ((bits) & SEP)
  294. #define is_nonpcs(bits)        (!(bits))
  295. #define is_normal(bits)        ((bits) & NORMAL)
  296. #define is_simple(bits)        ((bits) & (NORMAL|BSLASH))
  297. #define is_special(bits)    ((bits) & (ENDOF|BSLASH))
  298.  
  299. /* parsing types */
  300. static XrmBits Const xrmtypes[256] = {
  301.     EOS,0,0,0,0,0,0,0,
  302. #ifndef __EMX__
  303.     0,SPACE,EOL,0,0,0,0,0,
  304. #else
  305.     0,SPACE,EOL,0,0,EOL,0,0,
  306. #endif
  307.     0,0,0,0,0,0,0,0,
  308.     0,0,0,0,0,0,0,0,
  309.     SPACE,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  310.     NORMAL,NORMAL,BINDING,NORMAL,NORMAL,NORMAL,BINDING,NORMAL,
  311.     ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,
  312.     NORMAL,NORMAL,RSEP,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  313.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  314.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  315.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  316.     NORMAL,NORMAL,NORMAL,NORMAL,BSLASH,NORMAL,NORMAL,NORMAL,
  317.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  318.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  319.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  320.     NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,0
  321.     /* The rest will be automatically initialized to zero. */
  322. };
  323.  
  324. void XrmInitialize()
  325. {
  326.     DBUG_ENTER("XrmInitialize")
  327.     XrmQString = XrmPermStringToQuark("String");
  328.     XrmQANY = XrmPermStringToQuark("?");
  329.     DBUG_VOID_RETURN;
  330. }
  331.  
  332. XrmDatabase XrmGetDatabase(display)
  333.     Display *display;
  334. {
  335.     DBUG_ENTER("XrmGetDatabase")
  336.     XrmDatabase retval;
  337.     LockDisplay(display);
  338.     retval = display->db;
  339.     UnlockDisplay(display);
  340.     DBUG_RETURN(retval);
  341. }
  342.  
  343. void XrmSetDatabase(display, database)
  344.     Display *display;
  345.     XrmDatabase database;
  346. {
  347.     DBUG_ENTER("XrmSetDatabase")
  348.     LockDisplay(display);
  349.     display->db = database;
  350.     UnlockDisplay(display);
  351.     DBUG_VOID_RETURN;
  352. }
  353.  
  354. #if NeedFunctionPrototypes
  355. void XrmStringToQuarkList(
  356.     register _Xconst char  *name,
  357.     register XrmQuarkList quarks)   /* RETURN */
  358. #else
  359. void XrmStringToQuarkList(name, quarks)
  360.     register char      *name;
  361.     register XrmQuarkList quarks;   /* RETURN */
  362. #endif
  363. {
  364.     DBUG_ENTER("XrmStringToQuarkList")
  365.     register XrmBits        bits;
  366.     register Signature      sig = 0;
  367.     register char           ch, *tname;
  368.     register int         i = 0;
  369.  
  370.     if ((tname = (char *)name)) {
  371.     tname--;
  372.     while (!is_EOF(bits = next_char(ch, tname))) {
  373.         if (is_binding (bits)) {
  374.         if (i) {
  375.             /* Found a complete name */
  376.             *quarks++ = _XrmInternalStringToQuark(name,tname - name,
  377.                               sig, False);
  378.             i = 0;
  379.             sig = 0;
  380.         }
  381.         name = tname+1;
  382.         }
  383.         else {
  384.         sig = (sig << 1) + ch; /* Compute the signature. */
  385.         i++;
  386.         }
  387.     }
  388.     *quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
  389.     }
  390.     *quarks = NULLQUARK;
  391.     DBUG_VOID_RETURN;
  392. }
  393.  
  394. #if NeedFunctionPrototypes
  395. void XrmStringToBindingQuarkList(
  396.     register _Xconst char   *name,
  397.     register XrmBindingList bindings,   /* RETURN */
  398.     register XrmQuarkList   quarks)     /* RETURN */
  399. #else
  400. void XrmStringToBindingQuarkList(name, bindings, quarks)
  401.     register char        *name;
  402.     register XrmBindingList bindings;   /* RETURN */
  403.     register XrmQuarkList   quarks;     /* RETURN */
  404. #endif
  405. {
  406.     DBUG_ENTER("XrmStringToBindingQuarkList")
  407.     register XrmBits        bits;
  408.     register Signature      sig = 0;
  409.     register char           ch, *tname;
  410.     register XrmBinding     binding;
  411.     register int         i = 0;
  412.  
  413.     if ((tname = (char *)name)) {
  414.     tname--;
  415.     binding = XrmBindTightly;
  416.     while (!is_EOF(bits = next_char(ch, tname))) {
  417.         if (is_binding (bits)) {
  418.         if (i) {
  419.             /* Found a complete name */
  420.             *bindings++ = binding;
  421.             *quarks++ = _XrmInternalStringToQuark(name, tname - name,
  422.                               sig, False);
  423.  
  424.             i = 0;
  425.             sig = 0;
  426.             binding = XrmBindTightly;
  427.         }
  428.         name = tname+1;
  429.  
  430.         if (ch == '*')
  431.             binding = XrmBindLoosely;
  432.         }
  433.         else {
  434.         sig = (sig << 1) + ch; /* Compute the signature. */
  435.         i++;
  436.         }
  437.     }
  438.     *bindings = binding;
  439.     *quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
  440.     }
  441.     *quarks = NULLQUARK;
  442.     DBUG_VOID_RETURN;
  443. }
  444.  
  445. #ifdef DEBUG
  446.  
  447. static void PrintQuarkList(quarks, stream)
  448.     XrmQuarkList    quarks;
  449.     FILE        *stream;
  450. {
  451.     DBUG_ENTER("PrintQuarkList")
  452.     Bool        firstNameSeen;
  453.  
  454.     for (firstNameSeen = False; *quarks; quarks++) {
  455.     if (firstNameSeen) {
  456.         (void) fprintf(stream, ".");
  457.     }
  458.     firstNameSeen = True;
  459.     (void) fputs(XrmQuarkToString(*quarks), stream);
  460.     }
  461.     DBUG_VOID_RETURN;
  462. } /* PrintQuarkList */
  463.  
  464. #endif /* DEBUG */
  465.  
  466. /*ARGSUSED*/
  467. static void mbnoop(state)
  468.     XPointer state;
  469. {
  470. }
  471.  
  472. /*ARGSUSED*/
  473. static char mbchar(state, str, lenp)
  474.     XPointer state;
  475.     char *str;
  476.     int *lenp;
  477. {
  478.     DBUG_ENTER("mbchar")
  479.     char result;
  480.     *lenp = 1;
  481.     result = *str;
  482.     DBUG_RETURN(result);
  483. }
  484.  
  485. /*ARGSUSED*/
  486. static char *lcname(state)
  487.     XPointer state;
  488. {
  489.     return "C";
  490. }
  491.  
  492. static RConst XrmMethodsRec mb_methods = {
  493.     mbnoop,
  494.     mbchar,
  495.     mbnoop,
  496.     lcname,
  497.     mbnoop
  498. };
  499.  
  500. static XrmDatabase NewDatabase()
  501. {
  502.     DBUG_ENTER("NewDatabase")
  503.     register XrmDatabase db;
  504.  
  505.     db = (XrmDatabase) Xmalloc(sizeof(XrmHashBucketRec));
  506.     if (db) {
  507.     _XCreateMutex(&db->linfo);
  508.     db->table = (NTable)NULL;
  509.     db->mbstate = (XPointer)NULL;
  510.     db->methods = _XrmInitParseInfo(&db->mbstate);
  511.     if (!db->methods)
  512.         db->methods = (XrmMethods)&mb_methods;
  513.     }
  514.     DBUG_RETURN(db);
  515. }
  516.  
  517. /* move all values from ftable to ttable, and free ftable's buckets.
  518.  * ttable is quaranteed empty to start with.
  519.  */
  520. static void MoveValues(ftable, ttable)
  521.     LTable ftable;
  522.     register LTable ttable;
  523. {
  524.     DBUG_ENTER("MoveValues")
  525.     register VEntry fentry, nfentry;
  526.     register VEntry *prev;
  527.     register VEntry *bucket;
  528.     register VEntry tentry;
  529.     register int i;
  530.  
  531.     for (i = ftable->table.mask, bucket = ftable->buckets; i >= 0; i--) {
  532.     for (fentry = *bucket++; fentry; fentry = nfentry) {
  533.         prev = &LeafHash(ttable, fentry->name);
  534.         tentry = *prev;
  535.         *prev = fentry;
  536.         /* chain on all with same name, to preserve invariant order */
  537.         while ((nfentry = fentry->next) && nfentry->name == fentry->name)
  538.         fentry = nfentry;
  539.         fentry->next = tentry;
  540.     }
  541.     }
  542.     Xfree((char *)ftable->buckets);
  543.     DBUG_VOID_RETURN;
  544. }
  545.  
  546. /* move all tables from ftable to ttable, and free ftable.
  547.  * ttable is quaranteed empty to start with.
  548.  */
  549. static void MoveTables(ftable, ttable)
  550.     NTable ftable;
  551.     register NTable ttable;
  552. {
  553.     DBUG_ENTER("MoveTables")
  554.     register NTable fentry, nfentry;
  555.     register NTable *prev;
  556.     register NTable *bucket;
  557.     register NTable tentry;
  558.     register int i;
  559.  
  560.     for (i = ftable->mask, bucket = NodeBuckets(ftable); i >= 0; i--) {
  561.     for (fentry = *bucket++; fentry; fentry = nfentry) {
  562.         prev = &NodeHash(ttable, fentry->name);
  563.         tentry = *prev;
  564.         *prev = fentry;
  565.         /* chain on all with same name, to preserve invariant order */
  566.         while ((nfentry = fentry->next) && nfentry->name == fentry->name)
  567.         fentry = nfentry;
  568.         fentry->next = tentry;
  569.     }
  570.     }
  571.     Xfree((char *)ftable);
  572.     DBUG_VOID_RETURN;
  573. }
  574.  
  575. /* grow the table, based on current number of entries */
  576. static void GrowTable(prev)
  577.     NTable *prev;
  578. {
  579.     DBUG_ENTER("GrowTable")
  580.     register NTable table;
  581.     register int i;
  582.  
  583.     table = *prev;
  584.     i = table->mask;
  585.     if (i == 255) /* biggest it gets */
  586.     DBUG_VOID_RETURN;
  587.     while (i < 255 && GrowthPred(table->entries, i))
  588.     i = (i << 1) + 1;
  589.     i++; /* i is now the new size */
  590.     if (table->leaf) {
  591.     register LTable ltable;
  592.     LTableRec otable;
  593.  
  594.     ltable = (LTable)table;
  595.     /* cons up a copy to make MoveValues look symmetric */
  596.     otable = *ltable;
  597.     ltable->buckets = (VEntry *)Xmalloc(i * sizeof(VEntry));
  598.     if (!ltable->buckets) {
  599.         ltable->buckets = otable.buckets;
  600.         DBUG_VOID_RETURN;
  601.     }
  602.     ltable->table.mask = i - 1;
  603.     bzero((char *)ltable->buckets, i * sizeof(VEntry));
  604.     MoveValues(&otable, ltable);
  605.     } else {
  606.     register NTable ntable;
  607.  
  608.     ntable = (NTable)Xmalloc(sizeof(NTableRec) + i * sizeof(NTable));
  609.     if (!ntable)
  610.         DBUG_VOID_RETURN;
  611.     *ntable = *table;
  612.     ntable->mask = i - 1;
  613.     bzero((char *)NodeBuckets(ntable), i * sizeof(NTable));
  614.     *prev = ntable;
  615.     MoveTables(table, ntable);
  616.     }
  617.     DBUG_VOID_RETURN;
  618. }
  619.  
  620. /* merge values from ftable into *pprev, destroy ftable in the process */
  621. static void MergeValues(ftable, pprev, override)
  622.     LTable ftable;
  623.     NTable *pprev;
  624.     Bool override;
  625. {
  626.     DBUG_ENTER("MergeValues")
  627.     register VEntry fentry, tentry;
  628.     register VEntry *prev;
  629.     register LTable ttable;
  630.     VEntry *bucket;
  631.     int i;
  632.     register XrmQuark q;
  633.  
  634.     ttable = (LTable)*pprev;
  635.     if (ftable->table.hasloose)
  636.     ttable->table.hasloose = 1;
  637.     for (i = ftable->table.mask, bucket = ftable->buckets;
  638.      i >= 0;
  639.      i--, bucket++) {
  640.     for (fentry = *bucket; fentry; ) {
  641.         q = fentry->name;
  642.         prev = &LeafHash(ttable, q);
  643.         tentry = *prev;
  644.         while (tentry && tentry->name != q)
  645.         tentry = *(prev = &tentry->next);
  646.         /* note: test intentionally uses fentry->name instead of q */
  647.         /* permits serendipitous inserts */
  648.         while (tentry && tentry->name == fentry->name) {
  649.         /* if tentry is earlier, skip it */
  650.         if (!fentry->tight && tentry->tight) {
  651.             tentry = *(prev = &tentry->next);
  652.             continue;
  653.         }
  654.         if (fentry->tight != tentry->tight) {
  655.             /* no match, chain in fentry */
  656.             *prev = fentry;
  657.             prev = &fentry->next;
  658.             fentry = *prev;
  659.             *prev = tentry;
  660.             ttable->table.entries++;
  661.         } else if (override) {
  662.             /* match, chain in fentry, splice out and free tentry */
  663.             *prev = fentry;
  664.             prev = &fentry->next;
  665.             fentry = *prev;
  666.             *prev = tentry->next;
  667.             /* free the overridden entry */
  668.             Xfree((char *)tentry);
  669.             /* get next tentry */
  670.             tentry = *prev;
  671.         } else {
  672.             /* match, discard fentry */
  673.             prev = &tentry->next;
  674.             tentry = fentry; /* use as a temp var */
  675.             fentry = fentry->next;
  676.             /* free the overpowered entry */
  677.             Xfree((char *)tentry);
  678.             /* get next tentry */
  679.             tentry = *prev;
  680.         }
  681.         if (!fentry)
  682.             break;
  683.         }
  684.         /* at this point, tentry cannot match any fentry named q */
  685.         /* chain in all bindings together, preserve invariant order */
  686.         while (fentry && fentry->name == q) {
  687.         *prev = fentry;
  688.         prev = &fentry->next;
  689.         fentry = *prev;
  690.         *prev = tentry;
  691.         ttable->table.entries++;
  692.         }
  693.     }
  694.     }
  695.     Xfree((char *)ftable->buckets);
  696.     Xfree((char *)ftable);
  697.     /* resize if necessary, now that we're all done */
  698.     GROW(pprev);
  699.     DBUG_VOID_RETURN;
  700. }
  701.  
  702. /* merge tables from ftable into *pprev, destroy ftable in the process */
  703. static void MergeTables(ftable, pprev, override)
  704.     NTable ftable;
  705.     NTable *pprev;
  706.     Bool override;
  707. {
  708.     DBUG_ENTER("MergeTables")
  709.     register NTable fentry, tentry;
  710.     NTable nfentry;
  711.     register NTable *prev;
  712.     register NTable ttable;
  713.     NTable *bucket;
  714.     int i;
  715.     register XrmQuark q;
  716.  
  717.     ttable = *pprev;
  718.     if (ftable->hasloose)
  719.     ttable->hasloose = 1;
  720.     if (ftable->hasany)
  721.     ttable->hasany = 1;
  722.     for (i = ftable->mask, bucket = NodeBuckets(ftable);
  723.      i >= 0;
  724.      i--, bucket++) {
  725.     for (fentry = *bucket; fentry; ) {
  726.         q = fentry->name;
  727.         prev = &NodeHash(ttable, q);
  728.         tentry = *prev;
  729.         while (tentry && tentry->name != q)
  730.         tentry = *(prev = &tentry->next);
  731.         /* note: test intentionally uses fentry->name instead of q */
  732.         /* permits serendipitous inserts */
  733.         while (tentry && tentry->name == fentry->name) {
  734.         /* if tentry is earlier, skip it */
  735.         if ((fentry->leaf && !tentry->leaf) ||
  736.             (!fentry->tight && tentry->tight &&
  737.              (fentry->leaf || !tentry->leaf))) {
  738.             tentry = *(prev = &tentry->next);
  739.             continue;
  740.         }
  741.         nfentry = fentry->next;
  742.         if (fentry->leaf != tentry->leaf ||
  743.             fentry->tight != tentry->tight) {
  744.             /* no match, just chain in */
  745.             *prev = fentry;
  746.             *(prev = &fentry->next) = tentry;
  747.             ttable->entries++;
  748.         } else {
  749.             if (fentry->leaf)
  750.             MergeValues((LTable)fentry, prev, override);
  751.             else
  752.             MergeTables(fentry, prev, override);
  753.             /* bump to next tentry */
  754.             tentry = *(prev = &(*prev)->next);
  755.         }
  756.         /* bump to next fentry */
  757.         fentry = nfentry;
  758.         if (!fentry)
  759.             break;
  760.         }
  761.         /* at this point, tentry cannot match any fentry named q */
  762.         /* chain in all bindings together, preserve invariant order */
  763.         while (fentry && fentry->name == q) {
  764.         *prev = fentry;
  765.         prev = &fentry->next;
  766.         fentry = *prev;
  767.         *prev = tentry;
  768.         ttable->entries++;
  769.         }
  770.     }
  771.     }
  772.     Xfree((char *)ftable);
  773.     /* resize if necessary, now that we're all done */
  774.     GROW(pprev);
  775.     DBUG_VOID_RETURN;
  776. }
  777.  
  778. void XrmCombineDatabase(from, into, override)
  779.     XrmDatabase    from, *into;
  780.     Bool override;
  781. {
  782.     DBUG_ENTER("XrmCombineDatabase")
  783.     register NTable *prev;
  784.     register NTable ftable, ttable, nftable;
  785.  
  786.     if (!*into) {
  787.     *into = from;
  788.     } else if (from) {
  789.     _XLockMutex(&from->linfo);
  790.     _XLockMutex(&(*into)->linfo);
  791.     if ((ftable = from->table)) {
  792.         prev = &(*into)->table;
  793.         ttable = *prev;
  794.         if (!ftable->leaf) {
  795.         nftable = ftable->next;
  796.         if (ttable && !ttable->leaf) {
  797.             /* both have node tables, merge them */
  798.             MergeTables(ftable, prev, override);
  799.             /* bump to into's leaf table, if any */
  800.             ttable = *(prev = &(*prev)->next);
  801.         } else {
  802.             /* into has no node table, link from's in */
  803.             *prev = ftable;
  804.             *(prev = &ftable->next) = ttable;
  805.         }
  806.         /* bump to from's leaf table, if any */
  807.         ftable = nftable;
  808.         } else {
  809.         /* bump to into's leaf table, if any */
  810.         if (ttable && !ttable->leaf)
  811.             ttable = *(prev = &ttable->next);
  812.         }
  813.         if (ftable) {
  814.         /* if into has a leaf, merge, else insert */
  815.         if (ttable)
  816.             MergeValues((LTable)ftable, prev, override);
  817.         else
  818.             *prev = ftable;
  819.         }
  820.     }
  821.     (from->methods->destroy)(from->mbstate);
  822.     _XFreeMutex(&from->linfo);
  823.     Xfree((char *)from);
  824.     _XUnlockMutex(&(*into)->linfo);
  825.     }
  826.     DBUG_VOID_RETURN;
  827. }
  828.  
  829. void XrmMergeDatabases(from, into)
  830.     XrmDatabase    from, *into;
  831. {
  832.     DBUG_ENTER("XrmMergeDatabases")
  833.     XrmCombineDatabase(from, into, True);
  834.     DBUG_VOID_RETURN;
  835. }
  836.  
  837. /* store a value in the database, overriding any existing entry */
  838. static void PutEntry(db, bindings, quarks, type, value)
  839.     XrmDatabase        db;
  840.     XrmBindingList    bindings;
  841.     XrmQuarkList    quarks;
  842.     XrmRepresentation    type;
  843.     XrmValuePtr        value;
  844. {
  845.     DBUG_ENTER("PutEntry")
  846.     register NTable *pprev, *prev;
  847.     register NTable table;
  848.     register XrmQuark q;
  849.     register VEntry *vprev;
  850.     register VEntry entry;
  851.     NTable *nprev, *firstpprev;
  852.  
  853. #define NEWTABLE(q,i) \
  854.     table = (NTable)Xmalloc(sizeof(LTableRec)); \
  855.     if (!table) \
  856.     DBUG_VOID_RETURN; \
  857.     table->name = q; \
  858.     table->hasloose = 0; \
  859.     table->hasany = 0; \
  860.     table->mask = 0; \
  861.     table->entries = 0; \
  862.     if (quarks[i]) { \
  863.     table->leaf = 0; \
  864.     nprev = NodeBuckets(table); \
  865.     } else { \
  866.     table->leaf = 1; \
  867.     if (!(nprev = (NTable *)Xmalloc(sizeof(VEntry *)))) \
  868.         DBUG_VOID_RETURN; \
  869.     ((LTable)table)->buckets = (VEntry *)nprev; \
  870.     } \
  871.     *nprev = (NTable)NULL; \
  872.     table->next = *prev; \
  873.     *prev = table
  874.  
  875.     if (!db || !*quarks)
  876.     DBUG_VOID_RETURN;
  877.     table = *(prev = &db->table);
  878.     /* if already at leaf, bump to the leaf table */
  879.     if (!quarks[1] && table && !table->leaf)
  880.     table = *(prev = &table->next);
  881.     pprev = prev;
  882.     if (!table || (quarks[1] && table->leaf)) {
  883.     /* no top-level node table, create one and chain it in */
  884.     NEWTABLE(NULLQUARK,1);
  885.     table->tight = 1; /* arbitrary */
  886.     prev = nprev;
  887.     } else {
  888.     /* search along until we need a value */
  889.     while (quarks[1]) {
  890.         q = *quarks;
  891.         table = *(prev = &NodeHash(table, q));
  892.         while (table && table->name != q)
  893.         table = *(prev = &table->next);
  894.         if (!table)
  895.         break; /* not found */
  896.         if (quarks[2]) {
  897.         if (table->leaf)
  898.             break; /* not found */
  899.         } else {
  900.         if (!table->leaf) {
  901.             /* bump to leaf table, if any */
  902.             table = *(prev = &table->next);
  903.             if (!table || table->name != q)
  904.             break; /* not found */
  905.             if (!table->leaf) {
  906.             /* bump to leaf table, if any */
  907.             table = *(prev = &table->next);
  908.             if (!table || table->name != q)
  909.                 break; /* not found */
  910.             }
  911.         }
  912.         }
  913.         if (*bindings == XrmBindTightly) {
  914.         if (!table->tight)
  915.             break; /* not found */
  916.         } else {
  917.         if (table->tight) {
  918.             /* bump to loose table, if any */
  919.             table = *(prev = &table->next);
  920.             if (!table || table->name != q ||
  921.             !quarks[2] != table->leaf)
  922.             break; /* not found */
  923.         }
  924.         }
  925.         /* found that one, bump to next quark */
  926.         pprev = prev;
  927.         quarks++;
  928.         bindings++;
  929.     }
  930.     if (!quarks[1]) {
  931.         /* found all the way to a leaf */
  932.         q = *quarks;
  933.         entry = *(vprev = &LeafHash((LTable)table, q));
  934.         while (entry && entry->name != q)
  935.         entry = *(vprev = &entry->next);
  936.         /* if want loose and have tight, bump to next entry */
  937.         if (entry && *bindings == XrmBindLoosely && entry->tight)
  938.         entry = *(vprev = &entry->next);
  939.         if (entry && entry->name == q &&
  940.         (*bindings == XrmBindTightly) == entry->tight) {
  941.         /* match, need to override */
  942.         if ((type == XrmQString) == entry->string &&
  943.             entry->size == value->size) {
  944.             /* update type if not String, can be different */
  945.             if (!entry->string)
  946.             RepType(entry) = type;
  947.             /* identical size, just overwrite value */
  948.             memcpy(RawValue(entry), (char *)value->addr, value->size);
  949.             DBUG_VOID_RETURN;
  950.         }
  951.         /* splice out and free old entry */
  952.         *vprev = entry->next;
  953.         Xfree((char *)entry);
  954.         (*pprev)->entries--;
  955.         }
  956.         /* this is where to insert */
  957.         prev = (NTable *)vprev;
  958.     }
  959.     }
  960.     /* keep the top table, because we may have to grow it */
  961.     firstpprev = pprev;
  962.     /* iterate until we get to the leaf */
  963.     while (quarks[1]) {
  964.     /* build a new table and chain it in */
  965.     NEWTABLE(*quarks,2);
  966.     if (*quarks++ == XrmQANY)
  967.         (*pprev)->hasany = 1;
  968.     if (*bindings++ == XrmBindTightly) {
  969.         table->tight = 1;
  970.     } else {
  971.         table->tight = 0;
  972.         (*pprev)->hasloose = 1;
  973.     }
  974.     (*pprev)->entries++;
  975.     pprev = prev;
  976.     prev = nprev;
  977.     }
  978.     /* now allocate the value entry */
  979.     entry = (VEntry)Xmalloc(((type == XrmQString) ?
  980.                  sizeof(VEntryRec) : sizeof(DEntryRec)) +
  981.                 value->size);
  982.     if (!entry)
  983.     DBUG_VOID_RETURN;
  984.     entry->name = q = *quarks;
  985.     if (*bindings == XrmBindTightly) {
  986.     entry->tight = 1;
  987.     } else {
  988.     entry->tight = 0;
  989.     (*pprev)->hasloose = 1;
  990.     }
  991.     /* chain it in, with a bit of type cast ugliness */
  992.     entry->next = *((VEntry *)prev);
  993.     *((VEntry *)prev) = entry;
  994.     entry->size = value->size;
  995.     if (type == XrmQString) {
  996.     entry->string = 1;
  997.     } else {
  998.     entry->string = 0;
  999.     RepType(entry) = type;
  1000.     }
  1001.     /* save a copy of the value */
  1002.     memcpy(RawValue(entry), (char *)value->addr, value->size);
  1003.     (*pprev)->entries++;
  1004.     /* this is a new leaf, need to remember it for search lists */
  1005.     if (q > maxResourceQuark) {
  1006.     unsigned oldsize = (maxResourceQuark + 1) >> 3;
  1007.     unsigned size = ((q | 0x7f) + 1) >> 3; /* reallocate in chunks */
  1008.     if (resourceQuarks) {
  1009.         unsigned char *prevQuarks = resourceQuarks;
  1010.  
  1011.         resourceQuarks = (unsigned char *)Xrealloc((char *)resourceQuarks,
  1012.                                size);
  1013.         if (!resourceQuarks) {
  1014.         Xfree(prevQuarks);
  1015.         }
  1016.     } else
  1017.         resourceQuarks = (unsigned char *)Xmalloc(size);
  1018.     if (resourceQuarks) {
  1019.         bzero((char *)&resourceQuarks[oldsize], size - oldsize);
  1020.         maxResourceQuark = (size << 3) - 1;
  1021.     } else {
  1022.         maxResourceQuark = -1;
  1023.     }
  1024.     }
  1025.     if (q > 0 && resourceQuarks)
  1026.     resourceQuarks[q >> 3] |= 1 << (q & 0x7);
  1027.     GROW(firstpprev);
  1028.  
  1029. #undef NEWTABLE
  1030.     DBUG_VOID_RETURN;
  1031. }
  1032.  
  1033. void XrmQPutResource(pdb, bindings, quarks, type, value)
  1034.     XrmDatabase        *pdb;
  1035.     XrmBindingList      bindings;
  1036.     XrmQuarkList    quarks;
  1037.     XrmRepresentation    type;
  1038.     XrmValuePtr        value;
  1039. {
  1040.     DBUG_ENTER("XrmQPutResource")
  1041.     if (!*pdb) *pdb = NewDatabase();
  1042.     _XLockMutex(&(*pdb)->linfo);
  1043.     PutEntry(*pdb, bindings, quarks, type, value);
  1044.     _XUnlockMutex(&(*pdb)->linfo);
  1045.     DBUG_VOID_RETURN;
  1046. }
  1047.  
  1048. #if NeedFunctionPrototypes
  1049. void XrmPutResource(
  1050.     XrmDatabase     *pdb,
  1051.     _Xconst char    *specifier,
  1052.     _Xconst char    *type,
  1053.     XrmValuePtr        value)
  1054. #else
  1055. void XrmPutResource(pdb, specifier, type, value)
  1056.     XrmDatabase     *pdb;
  1057.     char        *specifier;
  1058.     char        *type;
  1059.     XrmValuePtr        value;
  1060. #endif
  1061. {
  1062.     DBUG_ENTER("XrmPutResource")
  1063.     XrmBinding        bindings[MAXDBDEPTH+1];
  1064.     XrmQuark        quarks[MAXDBDEPTH+1];
  1065.  
  1066.     if (!*pdb) *pdb = NewDatabase();
  1067.     _XLockMutex(&(*pdb)->linfo);
  1068.     XrmStringToBindingQuarkList(specifier, bindings, quarks);
  1069.     PutEntry(*pdb, bindings, quarks, XrmStringToQuark(type), value);
  1070.     _XUnlockMutex(&(*pdb)->linfo);
  1071.     DBUG_VOID_RETURN;
  1072. }
  1073.  
  1074. #if NeedFunctionPrototypes
  1075. void XrmQPutStringResource(
  1076.     XrmDatabase     *pdb,
  1077.     XrmBindingList  bindings,
  1078.     XrmQuarkList    quarks,
  1079.     _Xconst char    *str)
  1080. #else
  1081. void XrmQPutStringResource(pdb, bindings, quarks, str)
  1082.     XrmDatabase     *pdb;
  1083.     XrmBindingList  bindings;
  1084.     XrmQuarkList    quarks;
  1085.     char        *str;
  1086. #endif
  1087. {
  1088.     DBUG_ENTER("XrmQPutStringResource")
  1089.     XrmValue    value;
  1090.  
  1091.     if (!*pdb) *pdb = NewDatabase();
  1092.     value.addr = (XPointer) str;
  1093.     value.size = strlen(str)+1;
  1094.     _XLockMutex(&(*pdb)->linfo);
  1095.     PutEntry(*pdb, bindings, quarks, XrmQString, &value);
  1096.     _XUnlockMutex(&(*pdb)->linfo);
  1097.     DBUG_VOID_RETURN;
  1098. }
  1099.  
  1100. /*    Function Name: GetDatabase
  1101.  *    Description: Parses a string and stores it as a database.
  1102.  *    Arguments: db - the database.
  1103.  *                 str - a pointer to the string containing the database.
  1104.  *                 filename - source filename, if any.
  1105.  *                 doall - whether to do all lines or just one
  1106.  */
  1107.  
  1108. /*
  1109.  * This function is highly optimized to inline as much as possible. 
  1110.  * Be very careful with modifications, or simplifications, as they 
  1111.  * may adversely affect the performance.
  1112.  *
  1113.  * Chris Peterson, MIT X Consortium        5/17/90.
  1114.  */
  1115.  
  1116. /* 
  1117.  * Xlib spec says max 100 quarks in a lookup, will stop and return if
  1118.  * return if any single production's lhs has more than 100 components.
  1119.  */
  1120. #define QLIST_SIZE 100
  1121.  
  1122. /* 
  1123.  * This should be big enough to handle things like the XKeysymDB or biggish
  1124.  * ~/.Xdefaults or app-defaults files. Anything bigger will be allocated on
  1125.  * the heap.
  1126.  */
  1127. #define DEF_BUFF_SIZE 8192
  1128.  
  1129. static void GetIncludeFile();
  1130.  
  1131. static void GetDatabase(db, str, filename, doall)
  1132.     XrmDatabase db;
  1133.     register char *str;
  1134.     char *filename;
  1135.     Bool doall;
  1136. {
  1137.     DBUG_ENTER("GetDatabase")
  1138.     char *rhs;
  1139.     char *lhs, lhs_s[DEF_BUFF_SIZE];
  1140.     XrmQuark quarks[QLIST_SIZE + 1];    /* allow for a terminal NullQuark */
  1141.     XrmBinding bindings[QLIST_SIZE + 1];
  1142.  
  1143.     register char *ptr;
  1144.     register XrmBits bits = 0;
  1145.     register char c;
  1146.     register Signature sig;
  1147.     register char *ptr_max;
  1148.     register int num_quarks;
  1149.     register XrmBindingList t_bindings;
  1150.  
  1151.     int len, alloc_chars;
  1152.     unsigned long str_len;
  1153.     XrmValue value;
  1154.     Bool only_pcs;
  1155.     Bool dolines;
  1156.  
  1157.     if (!db)
  1158.     DBUG_VOID_RETURN;
  1159.  
  1160.     /* 
  1161.      * if strlen (str) < DEF_BUFF_SIZE allocate buffers on the stack for 
  1162.      * speed otherwise malloc the buffer. From a buffer overflow standpoint
  1163.      * we can be sure that neither: a) a component on the lhs, or b) a
  1164.      * value on the rhs, will be longer than the overall length of str,
  1165.      * i.e. strlen(str).
  1166.      *
  1167.      * This should give good performance when parsing "*foo: bar" type 
  1168.      * databases as might be passed with -xrm command line options; but
  1169.      * with larger databases, e.g. .Xdefaults, app-defaults, or KeysymDB
  1170.      * files, the size of the buffers will be overly large. One way
  1171.      * around this would be to double-parse each production with a resulting
  1172.      * performance hit. In any event we can be assured that a lhs component
  1173.      * name or a rhs value won't be longer than str itself.
  1174.      */
  1175.  
  1176.     str_len = strlen (str);
  1177.     if (DEF_BUFF_SIZE > str_len) lhs = lhs_s;
  1178.     else if ((lhs = (char*) Xmalloc (str_len)) == NULL) 
  1179.     DBUG_VOID_RETURN;
  1180.  
  1181.     alloc_chars = DEF_BUFF_SIZE < str_len ? str_len : DEF_BUFF_SIZE;
  1182.     if ((rhs = (char*) Xmalloc (alloc_chars)) == NULL) {
  1183.     if (lhs != lhs_s) Xfree (lhs);
  1184.     DBUG_VOID_RETURN;
  1185.     }
  1186.  
  1187.     (*db->methods->mbinit)(db->mbstate);
  1188.     str--;
  1189.     dolines = True;
  1190.     while (!is_EOF(bits) && dolines) {
  1191.     dolines = doall;
  1192.  
  1193.     /*
  1194.      * First: Remove extra whitespace. 
  1195.      */
  1196.  
  1197.     do {
  1198.         bits = next_char(c, str);
  1199.     } while is_space(bits);
  1200.  
  1201.     /*
  1202.      * Ignore empty lines.
  1203.      */
  1204.  
  1205.     if (is_EOL(bits))
  1206.         continue;        /* start a new line. */
  1207.  
  1208.     /*
  1209.      * Second: check the first character in a line to see if it is
  1210.      * "!" signifying a comment, or "#" signifying a directive.
  1211.      */
  1212.  
  1213.     if (c == '!') { /* Comment, spin to next newline */
  1214.         while (is_simple(bits = next_char(c, str))) {}
  1215.         if (is_EOL(bits))
  1216.         continue;
  1217.         while (!is_EOL(bits = next_mbchar(c, len, str))) {}
  1218.         str--;
  1219.         continue;        /* start a new line. */
  1220.     }
  1221.  
  1222.     if (c == '#') { /* Directive */
  1223.         /* remove extra whitespace */
  1224.         only_pcs = True;
  1225.         while (is_space(bits = next_char(c, str))) {};
  1226.         /* only "include" directive is currently defined */
  1227.         if (!strncmp(str, "include", 7)) {
  1228.         str += (7-1);
  1229.         /* remove extra whitespace */
  1230.         while (is_space(bits = next_char(c, str))) {};
  1231.         /* must have a starting " */
  1232.         if (c == '"') {
  1233.             char *fname = str+1;
  1234.             len = 0;
  1235.             do {
  1236.             if (only_pcs) {
  1237.                 bits = next_char(c, str);
  1238.                 if (is_nonpcs(bits))
  1239.                 only_pcs = False;
  1240.             }
  1241.             if (!only_pcs)
  1242.                 bits = next_mbchar(c, len, str);
  1243.             } while (c != '"' && !is_EOL(bits));
  1244.             /* must have an ending " */
  1245.             if (c == '"')
  1246.             GetIncludeFile(db, filename, fname, str - len - fname);
  1247.         }
  1248.         }
  1249.         /* spin to next newline */
  1250.         if (only_pcs) {
  1251.         while (is_simple(bits))
  1252.             bits = next_char(c, str);
  1253.         if (is_EOL(bits))
  1254.             continue;
  1255.         }
  1256.         while (!is_EOL(bits))
  1257.         bits = next_mbchar(c, len, str);
  1258.         str--;
  1259.         continue;        /* start a new line. */
  1260.     }
  1261.  
  1262.     /*
  1263.      * Third: loop through the LHS of the resource specification
  1264.      * storing characters and converting this to a Quark.
  1265.      */
  1266.     
  1267.     num_quarks = 0;
  1268.     t_bindings = bindings;
  1269.  
  1270.     sig = 0;
  1271.     ptr = lhs;
  1272.     *t_bindings = XrmBindTightly;    
  1273.     for(;;) {
  1274.         if (!is_binding(bits)) {
  1275.         while (!is_EOQ(bits)) {
  1276.             *ptr++ = c;
  1277.             sig = (sig << 1) + c; /* Compute the signature. */
  1278.             bits = next_char(c, str);
  1279.         }
  1280.  
  1281.         quarks[num_quarks++] = 
  1282.             _XrmInternalStringToQuark(lhs, ptr - lhs, sig, False);
  1283.  
  1284.         if (num_quarks > QLIST_SIZE) {
  1285.             Xfree(rhs);
  1286.             if (lhs != lhs_s) Xfree (lhs);
  1287.             (*db->methods->mbfinish)(db->mbstate);
  1288.             DBUG_VOID_RETURN;
  1289.         }
  1290.  
  1291.         if (is_separator(bits))  {
  1292.             if (!is_space(bits))
  1293.             break;
  1294.  
  1295.             /* Remove white space */
  1296.             do {
  1297.             *ptr++ = c;
  1298.             sig = (sig << 1) + c; /* Compute the signature. */
  1299.             } while (is_space(bits = next_char(c, str)));
  1300.  
  1301.             /* 
  1302.              * The spec doesn't permit it, but support spaces
  1303.              * internal to resource name/class 
  1304.              */
  1305.  
  1306.             if (is_separator(bits))
  1307.             break;
  1308.             num_quarks--;
  1309.             continue;
  1310.         }
  1311.  
  1312.         if (c == '.')
  1313.             *(++t_bindings) = XrmBindTightly;
  1314.         else
  1315.             *(++t_bindings) = XrmBindLoosely;
  1316.  
  1317.         sig = 0;
  1318.         ptr = lhs;
  1319.         }
  1320.         else {
  1321.         /*
  1322.          * Magic unspecified feature #254.
  1323.          *
  1324.          * If two separators appear with no Text between them then
  1325.          * ignore them.
  1326.          *
  1327.          * If anyone of those separators is a '*' then the binding 
  1328.          * will be loose, otherwise it will be tight.
  1329.          */
  1330.  
  1331.         if (c == '*')
  1332.             *t_bindings = XrmBindLoosely;
  1333.         }
  1334.  
  1335.         bits = next_char(c, str);
  1336.     }
  1337.  
  1338.     quarks[num_quarks] = NULLQUARK;
  1339.  
  1340.     /*
  1341.      * Make sure that there is a ':' in this line.
  1342.      */
  1343.  
  1344.     if (c != ':') {
  1345.         char oldc;
  1346.  
  1347.         /*
  1348.          * A parsing error has occured, toss everything on the line
  1349.          * a new_line can still be escaped with a '\'.
  1350.          */
  1351.  
  1352.         while (is_normal(bits))
  1353.         bits = next_char(c, str);
  1354.         if (is_EOL(bits))
  1355.         continue;
  1356.         bits = next_mbchar(c, len, str);
  1357.         do {
  1358.         oldc = c;
  1359.         bits = next_mbchar(c, len, str);
  1360.         } while (c && (c != '\n' || oldc == '\\'));
  1361.         str--;
  1362.         continue;
  1363.     }
  1364.  
  1365.     /*
  1366.      * I now have a quark and binding list for the entire left hand
  1367.      * side.  "c" currently points to the ":" separating the left hand
  1368.      * side for the right hand side.  It is time to begin processing
  1369.      * the right hand side.
  1370.      */
  1371.  
  1372.     /* 
  1373.      * Fourth: Remove more whitespace
  1374.      */
  1375.  
  1376.     for(;;) {
  1377.         if (is_space(bits = next_char(c, str)))
  1378.         continue;
  1379.         if (c != '\\')
  1380.         break;
  1381.         bits = next_char(c, str);
  1382.         if (c == '\n')
  1383.         continue;
  1384.         str--;
  1385.         bits = BSLASH;
  1386.         c = '\\';
  1387.         break;
  1388.     }
  1389.  
  1390.     /* 
  1391.      * Fifth: Process the right hand side.
  1392.      */
  1393.  
  1394.     ptr = rhs;
  1395.     ptr_max = ptr + alloc_chars - 4;
  1396.     only_pcs = True;
  1397.     len = 1;
  1398.  
  1399.     for(;;) {
  1400.  
  1401.         /*
  1402.          * Tight loop for the normal case:  Non backslash, non-end of value
  1403.          * character that will fit into the allocated buffer.
  1404.          */
  1405.  
  1406.         if (only_pcs) {
  1407.         while (is_normal(bits) && ptr < ptr_max) {
  1408.             *ptr++ = c;
  1409.             bits = next_char(c, str);
  1410.         }
  1411.         if (is_EOL(bits))
  1412.             break;
  1413.         if (is_nonpcs(bits)) {
  1414.             only_pcs = False;
  1415.             bits = next_mbchar(c, len, str);
  1416.         }
  1417.         }
  1418.         while (!is_special(bits) && ptr + len <= ptr_max) {
  1419.         len = -len;
  1420.         while (len)
  1421.             *ptr++ = str[len++];
  1422.         bits = next_mbchar(c, len, str);
  1423.         }
  1424.  
  1425.         if (is_EOL(bits)) {
  1426.         str--;
  1427.         break;
  1428.         }
  1429.  
  1430.         if (c == '\\') {
  1431.         /*
  1432.          * We need to do some magic after a backslash.
  1433.          */
  1434.         Bool read_next = True;
  1435.  
  1436.         if (only_pcs) {
  1437.             bits = next_char(c, str);
  1438.             if (is_nonpcs(bits))
  1439.             only_pcs = False;
  1440.         }
  1441.         if (!only_pcs)
  1442.             bits = next_mbchar(c, len, str);
  1443.  
  1444.         if (is_EOL(bits)) {
  1445.             if (is_EOF(bits))
  1446.             continue;
  1447.         } else if (c == 'n') {
  1448.             /*
  1449.              * "\n" means insert a newline.
  1450.              */
  1451.             *ptr++ = '\n';
  1452.         } else if (c == '\\') {
  1453.             /*
  1454.              * "\\" completes to just one backslash.
  1455.              */
  1456.             *ptr++ = '\\';
  1457.         } else {
  1458.             /*
  1459.              * pick up to three octal digits after the '\'.
  1460.              */
  1461.             char temp[3];
  1462.             int count = 0;
  1463.             while (is_odigit(bits) && count < 3) {
  1464.             temp[count++] = c;
  1465.             if (only_pcs) {
  1466.                 bits = next_char(c, str);
  1467.                 if (is_nonpcs(bits))
  1468.                 only_pcs = False;
  1469.             }
  1470.             if (!only_pcs)
  1471.                 bits = next_mbchar(c, len, str);
  1472.             }
  1473.  
  1474.             /*
  1475.              * If we found three digits then insert that octal code
  1476.              * into the value string as a character.
  1477.              */
  1478.  
  1479.             if (count == 3) {
  1480.             *ptr++ = (unsigned char) ((temp[0] - '0') * 0100 +
  1481.                           (temp[1] - '0') * 010 +
  1482.                           (temp[2] - '0'));
  1483.             }
  1484.             else {
  1485.             int tcount;
  1486.  
  1487.             /* 
  1488.              * Otherwise just insert those characters into the 
  1489.              * string, since no special processing is needed on
  1490.              * numerics we can skip the special processing.
  1491.              */
  1492.  
  1493.             for (tcount = 0; tcount < count; tcount++) {
  1494.                 *ptr++ = temp[tcount]; /* print them in
  1495.                               the correct order */
  1496.             }
  1497.             }
  1498.             read_next = False;
  1499.         }
  1500.         if (read_next) {
  1501.             if (only_pcs) {
  1502.             bits = next_char(c, str);
  1503.             if (is_nonpcs(bits))
  1504.                 only_pcs = False;
  1505.             }
  1506.             if (!only_pcs)
  1507.             bits = next_mbchar(c, len, str);
  1508.         }
  1509.         }
  1510.  
  1511.         /* 
  1512.          * It is important to make sure that there is room for at least
  1513.          * four more characters in the buffer, since I can add that
  1514.          * many characters into the buffer after a backslash has occured.
  1515.          */
  1516.  
  1517.         if (ptr + len > ptr_max) {
  1518.         char * temp_str;
  1519.  
  1520.         alloc_chars += BUFSIZ/10;        
  1521.         temp_str = Xrealloc(rhs, sizeof(char) * alloc_chars);
  1522.  
  1523.         if (!temp_str) {
  1524.             Xfree(rhs);
  1525.             if (lhs != lhs_s) Xfree (lhs);
  1526.             (*db->methods->mbfinish)(db->mbstate);
  1527.             DBUG_VOID_RETURN;
  1528.         }
  1529.  
  1530.         ptr = temp_str + (ptr - rhs); /* reset pointer. */
  1531.         rhs = temp_str;
  1532.         ptr_max = rhs + alloc_chars - 4;
  1533.         }
  1534.     }
  1535.  
  1536.     /*
  1537.      * Lastly: Terminate the value string, and store this entry 
  1538.      *        into the database.
  1539.      */
  1540.  
  1541.     *ptr++ = '\0';
  1542.  
  1543.     /* Store it in database */
  1544.     value.size = ptr - rhs;
  1545.     value.addr = (XPointer) rhs;
  1546.     
  1547.     PutEntry(db, bindings, quarks, XrmQString, &value);
  1548.     }
  1549.  
  1550.     if (lhs != lhs_s) Xfree (lhs);
  1551.     Xfree (rhs);
  1552.  
  1553.     (*db->methods->mbfinish)(db->mbstate);
  1554.     DBUG_VOID_RETURN;
  1555. }
  1556.  
  1557. #if NeedFunctionPrototypes
  1558. void XrmPutStringResource(
  1559.     XrmDatabase *pdb,
  1560.     _Xconst char*specifier,
  1561.     _Xconst char*str)
  1562. #else
  1563. void XrmPutStringResource(pdb, specifier, str)
  1564.     XrmDatabase *pdb;
  1565.     char    *specifier;
  1566.     char    *str;
  1567. #endif
  1568. {
  1569.     DBUG_ENTER("XrmPutStringResource")
  1570.     XrmValue    value;
  1571.     XrmBinding    bindings[MAXDBDEPTH+1];
  1572.     XrmQuark    quarks[MAXDBDEPTH+1];
  1573.  
  1574.     if (!*pdb) *pdb = NewDatabase();
  1575.     XrmStringToBindingQuarkList(specifier, bindings, quarks);
  1576.     value.addr = (XPointer) str;
  1577.     value.size = strlen(str)+1;
  1578.     _XLockMutex(&(*pdb)->linfo);
  1579.     PutEntry(*pdb, bindings, quarks, XrmQString, &value);
  1580.     _XUnlockMutex(&(*pdb)->linfo);
  1581.     DBUG_VOID_RETURN;
  1582. }
  1583.  
  1584.  
  1585. #if NeedFunctionPrototypes
  1586. void XrmPutLineResource(
  1587.     XrmDatabase *pdb,
  1588.     _Xconst char*line)
  1589. #else
  1590. void XrmPutLineResource(pdb, line)
  1591.     XrmDatabase *pdb;
  1592.     char    *line;
  1593. #endif
  1594. {
  1595.     DBUG_ENTER("XrmPutLineResource")
  1596.     if (!*pdb) *pdb = NewDatabase();
  1597.     _XLockMutex(&(*pdb)->linfo);
  1598.     GetDatabase(*pdb, line, (char *)NULL, False);
  1599.     _XUnlockMutex(&(*pdb)->linfo);
  1600.     DBUG_VOID_RETURN;
  1601. }
  1602.  
  1603. #if NeedFunctionPrototypes
  1604. XrmDatabase XrmGetStringDatabase(
  1605.     _Xconst char    *data)
  1606. #else
  1607. XrmDatabase XrmGetStringDatabase(data)
  1608.     char        *data;
  1609. #endif
  1610. {
  1611.     DBUG_ENTER("XrmGetStringDatabase")
  1612.     XrmDatabase     db;
  1613.  
  1614.     db = NewDatabase();
  1615.     _XLockMutex(&db->linfo);
  1616.     GetDatabase(db, data, (char *)NULL, True);
  1617.     _XUnlockMutex(&db->linfo);
  1618.     DBUG_RETURN(db);
  1619. }
  1620.  
  1621. /*    Function Name: ReadInFile
  1622.  *    Description: Reads the file into a buffer.
  1623.  *    Arguments: filename - the name of the file.
  1624.  *    Returns: An allocated string containing the contents of the file.
  1625.  */
  1626.  
  1627. static char *
  1628. ReadInFile(filename)
  1629. char * filename;
  1630. {
  1631.     DBUG_ENTER("ReadInFile")
  1632.     register int fd, size;
  1633.     char * filebuf;
  1634. #ifdef __EMX__
  1635.     filename = __XOS2RedirRoot(filename);
  1636. #endif
  1637.     if ( (fd = OpenFile(filename)) == -1 )
  1638.     DBUG_RETURN((char *)NULL);
  1639.  
  1640.     GetSizeOfFile(filename, size);
  1641.  
  1642.     if (!(filebuf = Xmalloc(size + 1))) { /* leave room for '\0' */
  1643.     close(fd);
  1644.     DBUG_RETURN((char *)NULL);
  1645.     }
  1646.  
  1647.     size = ReadFile(fd, filebuf, size);
  1648. #ifdef __EMX__
  1649.     { /* kill CRLF */
  1650.       int i,k;
  1651.       for (i=k=0; i<size; i++)
  1652.     if (filebuf[i] != 0x0d) {
  1653.        filebuf[k++] = filebuf[i];
  1654.     }
  1655.     filebuf[k] = 0;
  1656.     }
  1657. #endif
  1658.     if (size < 0) {
  1659.     CloseFile(fd);
  1660.     Xfree(filebuf);
  1661.     DBUG_RETURN((char *)NULL);
  1662.     }
  1663.     CloseFile(fd);
  1664.  
  1665.     filebuf[size] = '\0';    /* NULL terminate it. */
  1666.     DBUG_RETURN(filebuf);
  1667. }
  1668.  
  1669. static void
  1670. GetIncludeFile(db, base, fname, fnamelen)
  1671.     XrmDatabase db;
  1672.     char *base;
  1673.     char *fname;
  1674.     int fnamelen;
  1675. {
  1676.     DBUG_ENTER("GetIncludeFile")
  1677.     int len;
  1678.     char *str;
  1679.     char realfname[BUFSIZ];
  1680.  
  1681.     if (fnamelen <= 0 || fnamelen >= BUFSIZ)
  1682.     DBUG_VOID_RETURN;
  1683.     if (*fname != '/' && base && (str = strrchr(base, '/'))) {
  1684.     len = str - base + 1;
  1685.     if (len + fnamelen >= BUFSIZ)
  1686.         DBUG_VOID_RETURN;
  1687.     strncpy(realfname, base, len);
  1688.     strncpy(realfname + len, fname, fnamelen);
  1689.     realfname[len + fnamelen] = '\0';
  1690.     } else {
  1691.     strncpy(realfname, fname, fnamelen);
  1692.     realfname[fnamelen] = '\0';
  1693.     }
  1694.     if (!(str = ReadInFile(realfname)))
  1695.     DBUG_VOID_RETURN;
  1696.     GetDatabase(db, str, realfname, True);
  1697.     Xfree(str);
  1698.     DBUG_VOID_RETURN;
  1699. }
  1700.  
  1701. #if NeedFunctionPrototypes
  1702. XrmDatabase XrmGetFileDatabase(
  1703.     _Xconst char    *filename)
  1704. #else
  1705. XrmDatabase XrmGetFileDatabase(filename)
  1706.     char         *filename;
  1707. #endif
  1708. {
  1709.     DBUG_ENTER("XrmGetFileDatabase")
  1710.     XrmDatabase db;
  1711.     char *str;
  1712.  
  1713.     if (!(str = ReadInFile(filename)))
  1714.     DBUG_RETURN((XrmDatabase)NULL);
  1715.  
  1716.     db = NewDatabase();
  1717.     _XLockMutex(&db->linfo);
  1718.     GetDatabase(db, str, filename, True);
  1719.     _XUnlockMutex(&db->linfo);
  1720.     Xfree(str);
  1721.     DBUG_RETURN(db);
  1722. }
  1723.  
  1724. #if NeedFunctionPrototypes
  1725. Status XrmCombineFileDatabase(
  1726.     _Xconst char    *filename,
  1727.     XrmDatabase     *target,
  1728.     Bool             override)
  1729. #else
  1730. Status XrmCombineFileDatabase(filename, target, override)
  1731.     char        *filename;
  1732.     XrmDatabase *target;
  1733.     Bool         override;
  1734. #endif
  1735. {
  1736.     DBUG_ENTER("XrmCombileFileDatabase")
  1737.     XrmDatabase db;
  1738.     char *str;
  1739.  
  1740.     if (!(str = ReadInFile(filename)))
  1741.     DBUG_RETURN(0);
  1742.     if (override) {
  1743.     db = *target;
  1744.     if (!db)
  1745.         *target = db = NewDatabase();
  1746.     } else
  1747.     db = NewDatabase();
  1748.     _XLockMutex(&db->linfo);
  1749.     GetDatabase(db, str, filename, True);
  1750.     _XUnlockMutex(&db->linfo);
  1751.     Xfree(str);
  1752.     if (!override)
  1753.     XrmCombineDatabase(db, target, False);
  1754.     DBUG_RETURN(1);
  1755. }
  1756.  
  1757. /* call the user proc for every value in the table, arbitrary order.
  1758.  * stop if user proc returns True.  level is current depth in database.
  1759.  */
  1760. /*ARGSUSED*/
  1761. static Bool EnumLTable(table, names, classes, level, closure)
  1762.     LTable        table;
  1763.     XrmNameList        names;
  1764.     XrmClassList     classes;
  1765.     register int    level;
  1766.     register EClosure    closure;
  1767. {
  1768.     DBUG_ENTER("EnumLTable")
  1769.     register VEntry *bucket;
  1770.     register int i;
  1771.     register VEntry entry;
  1772.     XrmValue value;
  1773.     XrmRepresentation type;
  1774.     Bool tightOk;
  1775.  
  1776.     closure->bindings[level] = (table->table.tight ?
  1777.                 XrmBindTightly : XrmBindLoosely);
  1778.     closure->quarks[level] = table->table.name;
  1779.     level++;
  1780.     tightOk = !*names;
  1781.     closure->quarks[level + 1] = NULLQUARK;
  1782.     for (i = table->table.mask, bucket = table->buckets;
  1783.      i >= 0;
  1784.      i--, bucket++) {
  1785.     for (entry = *bucket; entry; entry = entry->next) {
  1786.         if (entry->tight && !tightOk)
  1787.         continue;
  1788.         closure->bindings[level] = (entry->tight ?
  1789.                     XrmBindTightly : XrmBindLoosely);
  1790.         closure->quarks[level] = entry->name;
  1791.         value.size = entry->size;
  1792.         if (entry->string) {
  1793.         type = XrmQString;
  1794.         value.addr = StringValue(entry);
  1795.         } else {
  1796.         type = RepType(entry);
  1797.         value.addr = DataValue(entry);
  1798.         }
  1799.         if ((*closure->proc)(&closure->db, closure->bindings+1,
  1800.                  closure->quarks+1, &type, &value,
  1801.                  closure->closure))
  1802.         DBUG_RETURN(True);
  1803.     }
  1804.     }
  1805.     DBUG_RETURN(False);
  1806. }
  1807.  
  1808. static Bool EnumAllNTable(table, level, closure)
  1809.     NTable        table;
  1810.     register int    level;
  1811.     register EClosure    closure;
  1812. {
  1813.     DBUG_ENTER("EnumAllNTable")
  1814.     register NTable *bucket;
  1815.     register int i;
  1816.     register NTable entry;
  1817.     XrmQuark empty = NULLQUARK;
  1818.  
  1819.     if (level >= MAXDBDEPTH)
  1820.     return False;
  1821.     for (i = table->mask, bucket = NodeBuckets(table);
  1822.      i >= 0;
  1823.      i--, bucket++) {
  1824.     for (entry = *bucket; entry; entry = entry->next) {
  1825.         if (entry->leaf) {
  1826.         if (EnumLTable((LTable)entry, &empty, &empty, level, closure))
  1827.             DBUG_RETURN(True);
  1828.         } else {
  1829.         closure->bindings[level] = (entry->tight ?
  1830.                         XrmBindTightly : XrmBindLoosely);
  1831.         closure->quarks[level] = entry->name;
  1832.         if (EnumAllNTable(entry, level+1, closure))
  1833.             DBUG_RETURN(True);
  1834.         }
  1835.     }
  1836.     }
  1837.     DBUG_RETURN(False);
  1838. }
  1839.  
  1840. /* recurse on every table in the table, arbitrary order.
  1841.  * stop if user proc returns True.  level is current depth in database.
  1842.  */
  1843. static Bool EnumNTable(table, names, classes, level, closure)
  1844.     NTable        table;
  1845.     XrmNameList        names;
  1846.     XrmClassList     classes;
  1847.     register int    level;
  1848.     register EClosure    closure;
  1849. {
  1850.     DBUG_ENTER("EnumNTable")
  1851.     register NTable    entry;
  1852.     register XrmQuark    q;
  1853.     register unsigned int leaf;
  1854.     Bool (*get)();
  1855.     Bool bilevel;
  1856.  
  1857. /* find entries named ename, leafness leaf, tight or loose, and call get */
  1858. #define ITIGHTLOOSE(ename) \
  1859.     NFIND(ename); \
  1860.     if (entry) { \
  1861.     if (leaf == entry->leaf) { \
  1862.         if (!leaf && !entry->tight && entry->next && \
  1863.         entry->next->name == q && entry->next->tight && \
  1864.         (bilevel || entry->next->hasloose) && \
  1865.         EnumLTable((LTable)entry->next, names+1, classes+1, \
  1866.                level, closure)) \
  1867.         DBUG_RETURN(True); \
  1868.         if ((*get)(entry, names+1, classes+1, level, closure)) \
  1869.         DBUG_RETURN(True); \
  1870.         if (entry->tight && (entry = entry->next) && \
  1871.         entry->name == q && leaf == entry->leaf && \
  1872.         (*get)(entry, names+1, classes+1, level, closure)) \
  1873.         DBUG_RETURN(True); \
  1874.     } else if (entry->leaf) { \
  1875.         if ((bilevel || entry->hasloose) && \
  1876.         EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1877.         DBUG_RETURN(True); \
  1878.         if (entry->tight && (entry = entry->next) && \
  1879.         entry->name == q && (bilevel || entry->hasloose) && \
  1880.         EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1881.         DBUG_RETURN(True); \
  1882.     } \
  1883.     }
  1884.  
  1885. /* find entries named ename, leafness leaf, loose only, and call get */
  1886. #define ILOOSE(ename) \
  1887.     NFIND(ename); \
  1888.     if (entry && entry->tight && (entry = entry->next) && entry->name != q) \
  1889.     entry = (NTable)NULL; \
  1890.     if (entry) { \
  1891.     if (leaf == entry->leaf) { \
  1892.         if ((*get)(entry, names+1, classes+1, level, closure)) \
  1893.         DBUG_RETURN(True); \
  1894.     } else if (entry->leaf && (bilevel || entry->hasloose)) { \
  1895.         if (EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1896.         DBUG_RETURN(True); \
  1897.     } \
  1898.     }
  1899.  
  1900.     if (level >= MAXDBDEPTH)
  1901.     return False;
  1902.     closure->bindings[level] = (table->tight ?
  1903.                 XrmBindTightly : XrmBindLoosely);
  1904.     closure->quarks[level] = table->name;
  1905.     level++;
  1906.     if (!*names) {
  1907.     if (EnumAllNTable(table, level, closure))
  1908.         DBUG_RETURN(True);
  1909.     } else {
  1910.     if (names[1] || closure->mode == XrmEnumAllLevels) {
  1911.         get = EnumNTable; /* recurse */
  1912.         leaf = 0;
  1913.         bilevel = !names[1];
  1914.     } else {
  1915.         get = EnumLTable; /* bottom of recursion */
  1916.         leaf = 1;
  1917.         bilevel = False;
  1918.     }
  1919.     if (table->hasloose && closure->mode == XrmEnumAllLevels) {
  1920.         NTable *bucket;
  1921.         int i;
  1922.         XrmQuark empty = NULLQUARK;
  1923.  
  1924.         for (i = table->mask, bucket = NodeBuckets(table);
  1925.          i >= 0;
  1926.          i--, bucket++) {
  1927.         q = NULLQUARK;
  1928.         for (entry = *bucket; entry; entry = entry->next) {
  1929.             if (!entry->tight && entry->name != q &&
  1930.             entry->name != *names && entry->name != *classes) {
  1931.             q = entry->name;
  1932.             if (entry->leaf) {
  1933.                 if (EnumLTable((LTable)entry, &empty, &empty,
  1934.                        level, closure))
  1935.                 DBUG_RETURN(True);
  1936.             } else {
  1937.                 if (EnumNTable(entry, &empty, &empty,
  1938.                        level, closure))
  1939.                 DBUG_RETURN(True);
  1940.             }
  1941.             }
  1942.         }
  1943.         }
  1944.     }
  1945.  
  1946.     ITIGHTLOOSE(*names);   /* do name, tight and loose */
  1947.     ITIGHTLOOSE(*classes); /* do class, tight and loose */
  1948.     if (table->hasany) {
  1949.         ITIGHTLOOSE(XrmQANY); /* do ANY, tight and loose */
  1950.     }
  1951.     if (table->hasloose) {
  1952.         while (1) {
  1953.         names++;
  1954.         classes++;
  1955.         if (!*names)
  1956.             break;
  1957.         if (!names[1] && closure->mode != XrmEnumAllLevels) {
  1958.             get = EnumLTable; /* bottom of recursion */
  1959.             leaf = 1;
  1960.         }
  1961.         ILOOSE(*names);   /* loose names */
  1962.         ILOOSE(*classes); /* loose classes */
  1963.         if (table->hasany) {
  1964.             ILOOSE(XrmQANY); /* loose ANY */
  1965.         }
  1966.         }
  1967.         names--;
  1968.         classes--;
  1969.     }
  1970.     }
  1971.     /* now look for matching leaf nodes */
  1972.     entry = table->next;
  1973.     if (!entry)
  1974.     DBUG_RETURN(False);
  1975.     if (entry->leaf) {
  1976.     if (entry->tight && !table->tight)
  1977.         entry = entry->next;
  1978.     } else {
  1979.     entry = entry->next;
  1980.     if (!entry || !entry->tight)
  1981.         DBUG_RETURN(False);
  1982.     }
  1983.     if (!entry || entry->name != table->name)
  1984.     DBUG_RETURN(False);
  1985.     /* found one */
  1986.     level--;
  1987.     if ((!*names || entry->hasloose) &&
  1988.     EnumLTable((LTable)entry, names, classes, level, closure))
  1989.     DBUG_RETURN(True);
  1990.     if (entry->tight && entry == table->next && (entry = entry->next) &&
  1991.     entry->name == table->name && (!*names || entry->hasloose))
  1992.     return EnumLTable((LTable)entry, names, classes, level, closure);
  1993.     DBUG_RETURN(False);
  1994.  
  1995. #undef ITIGHTLOOSE
  1996. #undef ILOOSE
  1997. }
  1998.  
  1999. /* call the proc for every value in the database, arbitrary order.
  2000.  * stop if the proc returns True.
  2001.  */
  2002. Bool XrmEnumerateDatabase(db, names, classes, mode, proc, closure)
  2003.     XrmDatabase        db;
  2004.     XrmNameList        names;
  2005.     XrmClassList    classes;
  2006.     int            mode;
  2007.     DBEnumProc        proc;
  2008.     XPointer        closure;
  2009. {
  2010.     DBUG_ENTER("XrmEnumerateDatabase")
  2011.     XrmBinding  bindings[MAXDBDEPTH+2];
  2012.     XrmQuark    quarks[MAXDBDEPTH+2];
  2013.     register NTable table;
  2014.     EClosureRec    eclosure;
  2015.     Bool retval = False;
  2016.  
  2017.     if (!db)
  2018.     DBUG_RETURN(False);
  2019.     _XLockMutex(&db->linfo);
  2020.     eclosure.db = db;
  2021.     eclosure.proc = proc;
  2022.     eclosure.closure = closure;
  2023.     eclosure.bindings = bindings;
  2024.     eclosure.quarks = quarks;
  2025.     eclosure.mode = mode;
  2026.     table = db->table;
  2027.     if (table && !table->leaf && !*names && mode == XrmEnumOneLevel)
  2028.     table = table->next;
  2029.     if (table) {
  2030.     if (!table->leaf)
  2031.         retval = EnumNTable(table, names, classes, 0, &eclosure);
  2032.     else
  2033.         retval = EnumLTable((LTable)table, names, classes, 0, &eclosure);
  2034.     }
  2035.     _XUnlockMutex(&db->linfo);
  2036.     DBUG_RETURN(retval);
  2037. }
  2038.  
  2039. static void PrintBindingQuarkList(bindings, quarks, stream)
  2040.     XrmBindingList      bindings;
  2041.     XrmQuarkList    quarks;
  2042.     FILE        *stream;
  2043. {
  2044.     DBUG_ENTER("PrintBindingQuarkList")
  2045.     Bool    firstNameSeen;
  2046.  
  2047.     for (firstNameSeen = False; *quarks; bindings++, quarks++) {
  2048.     if (*bindings == XrmBindLoosely) {
  2049.         (void) fprintf(stream, "*");
  2050.     } else if (firstNameSeen) {
  2051.         (void) fprintf(stream, ".");
  2052.     }
  2053.     firstNameSeen = True;
  2054.     (void) fputs(XrmQuarkToString(*quarks), stream);
  2055.     }
  2056.     DBUG_VOID_RETURN;
  2057. }
  2058.  
  2059. /* output out the entry in correct file syntax */
  2060. /*ARGSUSED*/
  2061. static Bool DumpEntry(db, bindings, quarks, type, value, data)
  2062.     XrmDatabase        *db;
  2063.     XrmBindingList      bindings;
  2064.     XrmQuarkList    quarks;
  2065.     XrmRepresentation   *type;
  2066.     XrmValuePtr        value;
  2067.     XPointer        data;
  2068. {
  2069.     DBUG_ENTER("DumpEntry")
  2070.     FILE            *stream = (FILE *)data;
  2071.     register unsigned int    i;
  2072.     register char        *s;
  2073.     register char        c;
  2074.  
  2075.     if (*type != XrmQString)
  2076.     (void) putc('!', stream);
  2077.     PrintBindingQuarkList(bindings, quarks, stream);
  2078.     s = value->addr;
  2079.     i = value->size;
  2080.     if (*type == XrmQString) {
  2081.     (void) fputs(":\t", stream);
  2082.     if (i)
  2083.         i--;
  2084.     }
  2085.     else
  2086.     (void) fprintf(stream, "=%s:\t", XrmRepresentationToString(*type));
  2087.     if (i && (*s == ' ' || *s == '\t'))
  2088.     (void) putc('\\', stream); /* preserve leading whitespace */
  2089.     while (i--) {
  2090.     c = *s++;
  2091.     if (c == '\n') {
  2092.         if (i)
  2093.         (void) fputs("\\n\\\n", stream);
  2094.         else
  2095.         (void) fputs("\\n", stream);
  2096.     } else if (c == '\\')
  2097.         (void) fputs("\\\\", stream);
  2098.     else if ((c < ' ' && c != '\t') ||
  2099.          ((unsigned char)c >= 0x7f && (unsigned char)c < 0xa0))
  2100.         (void) fprintf(stream, "\\%03o", (unsigned char)c);
  2101.     else
  2102.         (void) putc(c, stream);
  2103.     }
  2104.     (void) putc('\n', stream);
  2105.     DBUG_RETURN(ferror(stream) != 0);
  2106. }
  2107.  
  2108. #ifdef DEBUG
  2109.  
  2110. void PrintTable(table, file)
  2111.     NTable table;
  2112.     FILE *file;
  2113. {
  2114.     DBUG_ENTER("PrintTable")
  2115.     XrmBinding  bindings[MAXDBDEPTH+1];
  2116.     XrmQuark    quarks[MAXDBDEPTH+1];
  2117.     EClosureRec closure;
  2118.     XrmQuark    empty = NULLQUARK;
  2119.  
  2120.     closure.db = (XrmDatabase)NULL;
  2121.     closure.proc = DumpEntry;
  2122.     closure.closure = (XPointer)file;
  2123.     closure.bindings = bindings;
  2124.     closure.quarks = quarks;
  2125.     closure.mode = XrmEnumAllLevels;
  2126.     if (table->leaf)
  2127.     EnumLTable((LTable)table, &empty, &empty, 0, &closure);
  2128.     else
  2129.     EnumNTable(table, &empty, &empty, 0, &closure);
  2130.     DBUG_VOID_RETURN;
  2131. }
  2132.  
  2133. #endif /* DEBUG */
  2134.  
  2135. #if NeedFunctionPrototypes
  2136. void XrmPutFileDatabase(
  2137.     XrmDatabase db,
  2138.     _Xconst char *fileName)
  2139. #else
  2140. void XrmPutFileDatabase(db, fileName)
  2141.     XrmDatabase db;
  2142.     char     *fileName;
  2143. #endif
  2144. {
  2145.     DBUG_ENTER("XrmPutFileDatabase")
  2146.     FILE    *file;
  2147.     XrmQuark empty = NULLQUARK;
  2148.  
  2149.     if (!db) DBUG_VOID_RETURN;
  2150.     if (!(file = fopen(fileName, "w"))) DBUG_VOID_RETURN;
  2151.     if (XrmEnumerateDatabase(db, &empty, &empty, XrmEnumAllLevels,
  2152.                  DumpEntry, (XPointer) file))
  2153.     unlink((char *)fileName);
  2154.     fclose(file);
  2155.     DBUG_VOID_RETURN;
  2156. }
  2157.  
  2158. /* macros used in get/search functions */
  2159.  
  2160. /* find entries named ename, leafness leaf, tight or loose, and call get */
  2161. #define GTIGHTLOOSE(ename,looseleaf) \
  2162.     NFIND(ename); \
  2163.     if (entry) { \
  2164.     if (leaf == entry->leaf) { \
  2165.         if (!leaf && !entry->tight && entry->next && \
  2166.         entry->next->name == q && entry->next->tight && \
  2167.         entry->next->hasloose && \
  2168.         looseleaf((LTable)entry->next, names+1, classes+1, closure)) \
  2169.         DBUG_RETURN(True); \
  2170.         if ((*get)(entry, names+1, classes+1, closure)) \
  2171.         return True; \
  2172.         if (entry->tight && (entry = entry->next) && \
  2173.         entry->name == q && leaf == entry->leaf && \
  2174.         (*get)(entry, names+1, classes+1, closure)) \
  2175.         DBUG_RETURN(True); \
  2176.     } else if (entry->leaf) { \
  2177.         if (entry->hasloose && \
  2178.         looseleaf((LTable)entry, names+1, classes+1, closure)) \
  2179.         DBUG_RETURN(True); \
  2180.         if (entry->tight && (entry = entry->next) && \
  2181.         entry->name == q && entry->hasloose && \
  2182.         looseleaf((LTable)entry, names+1, classes+1, closure)) \
  2183.         DBUG_RETURN(True); \
  2184.     } \
  2185.     }
  2186.  
  2187. /* find entries named ename, leafness leaf, loose only, and call get */
  2188. #define GLOOSE(ename,looseleaf) \
  2189.     NFIND(ename); \
  2190.     if (entry && entry->tight && (entry = entry->next) && entry->name != q) \
  2191.     entry = (NTable)NULL; \
  2192.     if (entry) { \
  2193.     if (leaf == entry->leaf) { \
  2194.         if ((*get)(entry, names+1, classes+1, closure)) \
  2195.         DBUG_RETURN(True); \
  2196.     } else if (entry->leaf && entry->hasloose) { \
  2197.         if (looseleaf((LTable)entry, names+1, classes+1, closure)) \
  2198.         DBUG_RETURN(True); \
  2199.     } \
  2200.     }
  2201.  
  2202. /* add tight/loose entry to the search list, return True if list is full */
  2203. /*ARGSUSED*/
  2204. static Bool AppendLEntry(table, names, classes, closure)
  2205.     LTable        table;
  2206.     XrmNameList        names;
  2207.     XrmClassList     classes;
  2208.     register SClosure    closure;
  2209. {
  2210.     DBUG_ENTER("AppendLEntry")
  2211.     /* check for duplicate */
  2212.     if (closure->idx >= 0 && closure->list[closure->idx] == table)
  2213.     DBUG_RETURN(False);
  2214.     if (closure->idx == closure->limit)
  2215.     DBUG_RETURN(True);
  2216.     /* append it */
  2217.     closure->idx++;
  2218.     closure->list[closure->idx] = table;
  2219.     DBUG_RETURN(False);
  2220. }
  2221.  
  2222. /* add loose entry to the search list, return True if list is full */
  2223. /*ARGSUSED*/
  2224. static Bool AppendLooseLEntry(table, names, classes, closure)
  2225.     LTable        table;
  2226.     XrmNameList        names;
  2227.     XrmClassList     classes;
  2228.     register SClosure    closure;
  2229. {
  2230.     DBUG_ENTER("AppendLooseLEntry")
  2231.     /* check for duplicate */
  2232.     if (closure->idx >= 0 && closure->list[closure->idx] == table)
  2233.     DBUG_RETURN(False);
  2234.     if (closure->idx >= closure->limit - 1)
  2235.     DBUG_RETURN(True);
  2236.     /* append it */
  2237.     closure->idx++;
  2238.     closure->list[closure->idx] = LOOSESEARCH;
  2239.     closure->idx++;
  2240.     closure->list[closure->idx] = table;
  2241.     DBUG_RETURN(False);
  2242. }
  2243.  
  2244. /* search for a leaf table */
  2245. static Bool SearchNEntry(table, names, classes, closure)
  2246.     NTable        table;
  2247.     XrmNameList        names;
  2248.     XrmClassList     classes;
  2249.     SClosure        closure;
  2250. {
  2251.     DBUG_ENTER("SearchNEntry")
  2252.     register NTable    entry;
  2253.     register XrmQuark    q;
  2254.     register unsigned int leaf;
  2255.     Bool        (*get)();
  2256.  
  2257.     if (names[1]) {
  2258.     get = SearchNEntry; /* recurse */
  2259.     leaf = 0;
  2260.     } else {
  2261.     get = AppendLEntry; /* bottom of recursion */
  2262.     leaf = 1;
  2263.     }
  2264.     GTIGHTLOOSE(*names, AppendLooseLEntry);   /* do name, tight and loose */
  2265.     GTIGHTLOOSE(*classes, AppendLooseLEntry); /* do class, tight and loose */
  2266.     if (table->hasany) {
  2267.     GTIGHTLOOSE(XrmQANY, AppendLooseLEntry); /* do ANY, tight and loose */
  2268.     }
  2269.     if (table->hasloose) {
  2270.     while (1) {
  2271.         names++;
  2272.         classes++;
  2273.         if (!*names)
  2274.         break;
  2275.         if (!names[1]) {
  2276.         get = AppendLEntry; /* bottom of recursion */
  2277.         leaf = 1;
  2278.         }
  2279.         GLOOSE(*names, AppendLooseLEntry);   /* loose names */
  2280.         GLOOSE(*classes, AppendLooseLEntry); /* loose classes */
  2281.         if (table->hasany) {
  2282.         GLOOSE(XrmQANY, AppendLooseLEntry); /* loose ANY */
  2283.         }
  2284.     }
  2285.     }
  2286.     /* now look for matching leaf nodes */
  2287.     entry = table->next;
  2288.     if (!entry)
  2289.     DBUG_RETURN(False);
  2290.     if (entry->leaf) {
  2291.     if (entry->tight && !table->tight)
  2292.         entry = entry->next;
  2293.     } else {
  2294.     entry = entry->next;
  2295.     if (!entry || !entry->tight)
  2296.         DBUG_RETURN(False);
  2297.     }
  2298.     if (!entry || entry->name != table->name)
  2299.     DBUG_RETURN(False);
  2300.     /* found one */
  2301.     if (entry->hasloose &&
  2302.     AppendLooseLEntry((LTable)entry, names, classes, closure))
  2303.     DBUG_RETURN(True);
  2304.     if (entry->tight && entry == table->next && (entry = entry->next) &&
  2305.     entry->name == table->name && entry->hasloose) {
  2306.     Bool result = AppendLooseLEntry((LTable)entry, names, classes, closure);
  2307.     DBUG_RETURN(result);
  2308.     }
  2309.     DBUG_RETURN(False);
  2310. }
  2311.  
  2312. Bool XrmQGetSearchList(db, names, classes, searchList, listLength)
  2313.     XrmDatabase     db;
  2314.     XrmNameList        names;
  2315.     XrmClassList    classes;
  2316.     XrmSearchList   searchList;    /* RETURN */
  2317.     int            listLength;
  2318. {
  2319.     DBUG_ENTER("XrmQGetSearchList")
  2320.     register NTable    table;
  2321.     SClosureRec        closure;
  2322.  
  2323.     if (listLength <= 0)
  2324.     DBUG_RETURN(False);
  2325.     closure.list = (LTable *)searchList;
  2326.     closure.idx = -1;
  2327.     closure.limit = listLength - 2;
  2328.     if (db) {
  2329.     _XLockMutex(&db->linfo);
  2330.     table = db->table;
  2331.     if (*names) {
  2332.         if (table && !table->leaf) {
  2333.         if (SearchNEntry(table, names, classes, &closure)) {
  2334.             _XUnlockMutex(&db->linfo);
  2335.             DBUG_RETURN(False);
  2336.         }
  2337.         } else if (table && table->hasloose &&
  2338.                AppendLooseLEntry((LTable)table, names, classes,
  2339.                      &closure)) {
  2340.         _XUnlockMutex(&db->linfo);
  2341.         DBUG_RETURN(False);
  2342.         }
  2343.     } else {
  2344.         if (table && !table->leaf)
  2345.         table = table->next;
  2346.         if (table && 
  2347.         AppendLEntry((LTable)table, names, classes, &closure)) {
  2348.         _XUnlockMutex(&db->linfo);
  2349.         DBUG_RETURN(False);
  2350.         }
  2351.     }
  2352.     _XUnlockMutex(&db->linfo);
  2353.     }
  2354.     closure.list[closure.idx + 1] = (LTable)NULL;
  2355.     DBUG_RETURN(True);
  2356. }
  2357.  
  2358. Bool XrmQGetSearchResource(searchList, name, class, pType, pValue)
  2359.          XrmSearchList    searchList;
  2360.     register XrmName        name;
  2361.     register XrmClass        class;
  2362.              XrmRepresentation    *pType;  /* RETURN */
  2363.              XrmValue        *pValue; /* RETURN */
  2364. {
  2365.     DBUG_ENTER("XrmQGetSearchResource")
  2366.     register LTable *list;
  2367.     register LTable table;
  2368.     register VEntry entry;
  2369.     int flags;
  2370.  
  2371. /* find tight or loose entry */
  2372. #define VTIGHTLOOSE(q) \
  2373.     entry = LeafHash(table, q); \
  2374.     while (entry && entry->name != q) \
  2375.     entry = entry->next; \
  2376.     if (entry) \
  2377.     break
  2378. /* find loose entry */
  2379. #define VLOOSE(q) \
  2380.     entry = LeafHash(table, q); \
  2381.     while (entry && entry->name != q) \
  2382.     entry = entry->next; \
  2383.     if (entry) { \
  2384.     if (!entry->tight) \
  2385.         break; \
  2386.     if ((entry = entry->next) && entry->name == q) \
  2387.         break; \
  2388.     }
  2389.     list = (LTable *)searchList;
  2390.     /* figure out which combination of name and class we need to search for */
  2391.     flags = 0;
  2392.     if (IsResourceQuark(name))
  2393.     flags = 2;
  2394.     if (IsResourceQuark(class))
  2395.     flags |= 1;
  2396.     if (!flags) {
  2397.     /* neither name nor class has ever been used to name a resource */
  2398.     table = (LTable)NULL;
  2399.     } else if (flags == 3) {
  2400.     /* both name and class */
  2401.     while ((table = *list++)) {
  2402.         if (table != LOOSESEARCH) {
  2403.         VTIGHTLOOSE(name);  /* do name, tight and loose */
  2404.         VTIGHTLOOSE(class); /* do class, tight and loose */
  2405.         } else {
  2406.         table = *list++;
  2407.         VLOOSE(name);  /* do name, loose only */
  2408.         VLOOSE(class); /* do class, loose only */
  2409.         }
  2410.     }
  2411.     } else {
  2412.     /* just one of name or class */
  2413.     if (flags == 1)
  2414.         name = class;
  2415.     while ((table = *list++)) {
  2416.         if (table != LOOSESEARCH) {
  2417.         VTIGHTLOOSE(name); /* tight and loose */
  2418.         } else {
  2419.         table = *list++;
  2420.         VLOOSE(name); /* loose only */
  2421.         }
  2422.     }
  2423.     }
  2424.     if (table) {
  2425.     /* found a match */
  2426.     if (entry->string) {
  2427.         *pType = XrmQString;
  2428.         pValue->addr = StringValue(entry);
  2429.     } else {
  2430.         *pType = RepType(entry);
  2431.         pValue->addr = DataValue(entry);
  2432.     }
  2433.     pValue->size = entry->size;
  2434.     DBUG_RETURN(True);
  2435.     }
  2436.     *pType = NULLQUARK;
  2437.     pValue->addr = (XPointer)NULL;
  2438.     pValue->size = 0;
  2439.     DBUG_RETURN(False);
  2440. #undef VTIGHTLOOSE
  2441. #undef VLOOSE
  2442. }
  2443. /* look for a tight/loose value */
  2444. static Bool GetVEntry(table, names, classes, closure)
  2445.     LTable        table;
  2446.     XrmNameList        names;
  2447.     XrmClassList     classes;
  2448.     VClosure        closure;
  2449. {
  2450.     DBUG_ENTER("GetVEntry")
  2451.     register VEntry entry;
  2452.     register XrmQuark q;
  2453.     /* try name first */
  2454.     q = *names;
  2455.     entry = LeafHash(table, q);
  2456.     while (entry && entry->name != q)
  2457.     entry = entry->next;
  2458.     if (!entry) {
  2459.     /* not found, try class */
  2460.     q = *classes;
  2461.     entry = LeafHash(table, q);
  2462.     while (entry && entry->name != q)
  2463.         entry = entry->next;
  2464.     if (!entry)
  2465.         DBUG_RETURN(False);
  2466.     }
  2467.     if (entry->string) {
  2468.     *closure->type = XrmQString;
  2469.     closure->value->addr = StringValue(entry);
  2470.     } else {
  2471.     *closure->type = RepType(entry);
  2472.     closure->value->addr = DataValue(entry);
  2473.     }
  2474.     closure->value->size = entry->size;
  2475.     DBUG_RETURN(True);
  2476. }
  2477. /* look for a loose value */
  2478. static Bool GetLooseVEntry(table, names, classes, closure)
  2479.     LTable        table;
  2480.     XrmNameList        names;
  2481.     XrmClassList     classes;
  2482.     VClosure        closure;
  2483. {
  2484.     DBUG_ENTER("GetLooseVEntry")
  2485.     register VEntry    entry;
  2486.     register XrmQuark    q;
  2487. #define VLOOSE(ename) \
  2488.     q = ename; \
  2489.     entry = LeafHash(table, q); \
  2490.     while (entry && entry->name != q) \
  2491.     entry = entry->next; \
  2492.     if (entry && entry->tight && (entry = entry->next) && entry->name != q) \
  2493.     entry = (VEntry)NULL;
  2494.     /* bump to last component */
  2495.     while (names[1]) {
  2496.     names++;
  2497.     classes++;
  2498.     }
  2499.     VLOOSE(*names);  /* do name, loose only */
  2500.     if (!entry) {
  2501.     VLOOSE(*classes); /* do class, loose only */
  2502.     if (!entry)
  2503.         DBUG_RETURN(False);
  2504.     }
  2505.     if (entry->string) {
  2506.     *closure->type = XrmQString;
  2507.     closure->value->addr = StringValue(entry);
  2508.     } else {
  2509.     *closure->type = RepType(entry);
  2510.     closure->value->addr = DataValue(entry);
  2511.     }
  2512.     closure->value->size = entry->size;
  2513. #undef VLOOSE
  2514.     DBUG_RETURN(True);
  2515. }
  2516. /* recursive search for a value */
  2517. static Bool GetNEntry(table, names, classes, closure)
  2518.     NTable        table;
  2519.     XrmNameList        names;
  2520.     XrmClassList     classes;
  2521.     VClosure        closure;
  2522. {
  2523.     DBUG_ENTER("GetNEntry")
  2524.     register NTable    entry;
  2525.     register XrmQuark    q;
  2526.     register unsigned int leaf;
  2527.     Bool        (*get)();
  2528.     NTable        otable;
  2529.     if (names[2]) {
  2530.     get = GetNEntry; /* recurse */
  2531.     leaf = 0;
  2532.     } else {
  2533.     get = GetVEntry; /* bottom of recursion */
  2534.     leaf = 1;
  2535.     }
  2536.     GTIGHTLOOSE(*names, GetLooseVEntry);   /* do name, tight and loose */
  2537.     GTIGHTLOOSE(*classes, GetLooseVEntry); /* do class, tight and loose */
  2538.     if (table->hasany) {
  2539.     GTIGHTLOOSE(XrmQANY, GetLooseVEntry); /* do ANY, tight and loose */
  2540.     }
  2541.     if (table->hasloose) {
  2542.     while (1) {
  2543.         names++;
  2544.         classes++;
  2545.         if (!names[1])
  2546.         break;
  2547.         if (!names[2]) {
  2548.         get = GetVEntry; /* bottom of recursion */
  2549.         leaf = 1;
  2550.         }
  2551.         GLOOSE(*names, GetLooseVEntry);   /* do name, loose only */
  2552.         GLOOSE(*classes, GetLooseVEntry); /* do class, loose only */
  2553.         if (table->hasany) {
  2554.         GLOOSE(XrmQANY, GetLooseVEntry); /* do ANY, loose only */
  2555.         }
  2556.     }
  2557.     }
  2558.     /* look for matching leaf tables */
  2559.     otable = table;
  2560.     table = table->next;
  2561.     if (!table)
  2562.     DBUG_RETURN(False);
  2563.     if (table->leaf) {
  2564.     if (table->tight && !otable->tight)
  2565.         table = table->next;
  2566.     } else {
  2567.     table = table->next;
  2568.     if (!table || !table->tight)
  2569.         DBUG_RETURN(False);
  2570.     }
  2571.     if (!table || table->name != otable->name)
  2572.     return False;
  2573.     /* found one */
  2574.     if (table->hasloose &&
  2575.     GetLooseVEntry((LTable)table, names, classes, closure))
  2576.     DBUG_RETURN(True);
  2577.     if (table->tight && table == otable->next) {
  2578.     table = table->next;
  2579.     if (table && table->name == otable->name && table->hasloose) {
  2580.         Bool result = GetLooseVEntry((LTable)table, names, classes, closure);
  2581.         DBUG_RETURN(result);
  2582.     }
  2583.     }
  2584.     DBUG_RETURN(False);
  2585. }
  2586. Bool XrmQGetResource(db, names, classes, pType, pValue)
  2587.     XrmDatabase         db;
  2588.     XrmNameList        names;
  2589.     XrmClassList     classes;
  2590.     XrmRepresentation    *pType;  /* RETURN */
  2591.     XrmValuePtr        pValue;  /* RETURN */
  2592. {
  2593.     DBUG_ENTER("XrmQGetResource")
  2594.     register NTable table;
  2595.     VClosureRec closure;
  2596.     if (db && *names) {
  2597.     _XLockMutex(&db->linfo);
  2598.     closure.type = pType;
  2599.     closure.value = pValue;
  2600.     table = db->table;
  2601.     if (names[1]) {
  2602.         if (table && !table->leaf) {
  2603.         if (GetNEntry(table, names, classes, &closure)) {
  2604.             _XUnlockMutex(&db->linfo);
  2605.             DBUG_RETURN(True);
  2606.         }
  2607.         } else if (table && table->hasloose &&
  2608.             GetLooseVEntry((LTable)table, names, classes, &closure)) {
  2609.         _XUnlockMutex (&db->linfo);
  2610.         DBUG_RETURN(True);
  2611.         }
  2612.     } else {
  2613.         if (table && !table->leaf)
  2614.         table = table->next;
  2615.         if (table && GetVEntry((LTable)table, names, classes, &closure)) {
  2616.         _XUnlockMutex(&db->linfo);
  2617.         DBUG_RETURN(True);
  2618.         }
  2619.     }
  2620.     _XUnlockMutex(&db->linfo);
  2621.     }
  2622.     *pType = NULLQUARK;
  2623.     pValue->addr = (XPointer)NULL;
  2624.     pValue->size = 0;
  2625.     DBUG_RETURN(False);
  2626. }
  2627. #if NeedFunctionPrototypes
  2628. Bool XrmGetResource(db, name_str, class_str, pType_str, pValue)
  2629.     XrmDatabase         db;
  2630.     _Xconst char    *name_str;
  2631.     _Xconst char    *class_str;
  2632.     XrmString        *pType_str;  /* RETURN */
  2633.     XrmValuePtr        pValue;      /* RETURN */
  2634. #else
  2635. Bool XrmGetResource(db, name_str, class_str, pType_str, pValue)
  2636.     XrmDatabase         db;
  2637.     XrmString        name_str;
  2638.     XrmString        class_str;
  2639.     XrmString        *pType_str;  /* RETURN */
  2640.     XrmValuePtr        pValue;      /* RETURN */
  2641. #endif
  2642. {
  2643.     DBUG_ENTER("XrmGetResource")
  2644.     XrmName        names[MAXDBDEPTH+1];
  2645.     XrmClass        classes[MAXDBDEPTH+1];
  2646.     XrmRepresentation   fromType;
  2647.     Bool        result;
  2648.     XrmStringToNameList(name_str, names);
  2649.     XrmStringToClassList(class_str, classes);
  2650.     result = XrmQGetResource(db, names, classes, &fromType, pValue);
  2651.     (*pType_str) = XrmQuarkToString(fromType);
  2652.     DBUG_RETURN(result);
  2653. }
  2654. /* destroy all values, plus table itself */
  2655. static void DestroyLTable(table)
  2656.     LTable table;
  2657. {
  2658.     DBUG_ENTER("DestroyLTable")
  2659.     register int i;
  2660.     register VEntry *buckets;
  2661.     register VEntry entry, next;
  2662.     buckets = table->buckets;
  2663.     for (i = table->table.mask; i >= 0; i--, buckets++) {
  2664.     for (next = *buckets; (entry = next); ) {
  2665.         next = entry->next;
  2666.         Xfree((char *)entry);
  2667.     }
  2668.     }
  2669.     Xfree((char *)table->buckets);
  2670.     Xfree((char *)table);
  2671.     DBUG_VOID_RETURN;
  2672. }
  2673. /* destroy all contained tables, plus table itself */
  2674. static void DestroyNTable(table)
  2675.     NTable table;
  2676. {
  2677.     DBUG_ENTER("DestroyNTable")
  2678.     register int i;
  2679.     register NTable *buckets;
  2680.     register NTable entry, next;
  2681.     buckets = NodeBuckets(table);
  2682.     for (i = table->mask; i >= 0; i--, buckets++) {
  2683.     for (next = *buckets; (entry = next); ) {
  2684.         next = entry->next;
  2685.         if (entry->leaf)
  2686.         DestroyLTable((LTable)entry);
  2687.         else
  2688.         DestroyNTable(entry);
  2689.     }
  2690.     }
  2691.     Xfree((char *)table);
  2692.     DBUG_VOID_RETURN;
  2693. }
  2694. char *XrmLocaleOfDatabase(db)
  2695.     XrmDatabase db;
  2696. {
  2697.     DBUG_ENTER("XrmLocaleOfDatabase")
  2698.     char* retval;
  2699.     _XLockMutex(&db->linfo);
  2700.     retval = (*db->methods->lcname)(db->mbstate);
  2701.     _XUnlockMutex(&db->linfo);
  2702.     DBUG_RETURN(retval);
  2703. }
  2704. void XrmDestroyDatabase(db)
  2705.     XrmDatabase   db;
  2706. {
  2707.     DBUG_ENTER("XrmDestroyDatabase")
  2708.     register NTable table, next;
  2709.     if (db) {
  2710.     _XLockMutex(&db->linfo);
  2711.     for (next = db->table; (table = next); ) {
  2712.         next = table->next;
  2713.         if (table->leaf)
  2714.         DestroyLTable((LTable)table);
  2715.         else
  2716.         DestroyNTable(table);
  2717.     }
  2718.     _XFreeMutex(&db->linfo);
  2719.     (*db->methods->destroy)(db->mbstate);
  2720.     Xfree((char *)db);
  2721.     }
  2722.     DBUG_VOID_RETURN;
  2723. }
  2724.