PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/nx-3.5.0/nx-X11/lib/X11/Xrm.c

#
C | 2663 lines | 2203 code | 154 blank | 306 comment | 402 complexity | e9249c5fef02b25c787573cc93125518 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* $Xorg: Xrm.c,v 1.7 2001/02/09 02:03:39 xorgcvs Exp $ */
  2. /***********************************************************
  3. Copyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and its
  6. documentation for any purpose and without fee is hereby granted,
  7. provided that the above copyright notice appear in all copies and that
  8. both that copyright notice and this permission notice appear in
  9. supporting documentation, and that the name Digital not be
  10. used in advertising or publicity pertaining to distribution of the
  11. software without specific, written prior permission.
  12. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  13. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  14. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  15. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  16. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  17. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  18. SOFTWARE.
  19. ******************************************************************/
  20. /*
  21. Copyright 1987, 1988, 1990, 1998 The Open Group
  22. Permission to use, copy, modify, distribute, and sell this software and its
  23. documentation for any purpose is hereby granted without fee, provided that
  24. the above copyright notice appear in all copies and that both that
  25. copyright notice and this permission notice appear in supporting
  26. documentation.
  27. The above copyright notice and this permission notice shall be included
  28. in all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  33. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  34. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  35. OTHER DEALINGS IN THE SOFTWARE.
  36. Except as contained in this notice, the name of The Open Group shall
  37. not be used in advertising or otherwise to promote the sale, use or
  38. other dealings in this Software without prior written authorization
  39. from The Open Group.
  40. */
  41. /* $XFree86: xc/lib/X11/Xrm.c,v 3.22 2003/07/16 01:38:26 dawes Exp $ */
  42. #ifdef HAVE_CONFIG_H
  43. #include <config.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <ctype.h>
  47. #include "Xlibint.h"
  48. #include <X11/Xresource.h>
  49. #include "Xlcint.h"
  50. #ifdef XTHREADS
  51. #include "locking.h"
  52. #endif
  53. #include "XrmI.h"
  54. #include <X11/Xos.h>
  55. #include "Xresinternal.h"
  56. #include "Xresource.h"
  57. /*
  58. These Xrm routines allow very fast lookup of resources in the resource
  59. database. Several usage patterns are exploited:
  60. (1) Widgets get a lot of resources at one time. Rather than look up each from
  61. scratch, we can precompute the prioritized list of database levels once, then
  62. search for each resource starting at the beginning of the list.
  63. (2) Many database levels don't contain any leaf resource nodes. There is no
  64. point in looking for resources on a level that doesn't contain any. This
  65. information is kept on a per-level basis.
  66. (3) Sometimes the widget instance tree is structured such that you get the same
  67. class name repeated on the fully qualified widget name. This can result in the
  68. same database level occuring multiple times on the search list. The code below
  69. only checks to see if you get two identical search lists in a row, rather than
  70. look back through all database levels, but in practice this removes all
  71. duplicates I've ever observed.
  72. Joel McCormack
  73. */
  74. /*
  75. The Xrm representation has been completely redesigned to substantially reduce
  76. memory and hopefully improve performance.
  77. The database is structured into two kinds of tables: LTables that contain
  78. only values, and NTables that contain only other tables.
  79. Some invariants:
  80. The next pointer of the top-level node table points to the top-level leaf
  81. table, if any.
  82. Within an LTable, for a given name, the tight value always precedes the
  83. loose value, and if both are present the loose value is always right after
  84. the tight value.
  85. Within an NTable, all of the entries for a given name are contiguous,
  86. in the order tight NTable, loose NTable, tight LTable, loose LTable.
  87. Bob Scheifler
  88. */
  89. static XrmQuark XrmQString, XrmQANY;
  90. typedef Bool (*DBEnumProc)(
  91. XrmDatabase* /* db */,
  92. XrmBindingList /* bindings */,
  93. XrmQuarkList /* quarks */,
  94. XrmRepresentation* /* type */,
  95. XrmValue* /* value */,
  96. XPointer /* closure */
  97. );
  98. typedef struct _VEntry {
  99. struct _VEntry *next; /* next in chain */
  100. XrmQuark name; /* name of this entry */
  101. unsigned int tight:1; /* 1 if it is a tight binding */
  102. unsigned int string:1; /* 1 if type is String */
  103. unsigned int size:30; /* size of value */
  104. } VEntryRec, *VEntry;
  105. typedef struct _DEntry {
  106. VEntryRec entry; /* entry */
  107. XrmRepresentation type; /* representation type */
  108. } DEntryRec, *DEntry;
  109. /* the value is right after the structure */
  110. #define StringValue(ve) (XPointer)((ve) + 1)
  111. #define RepType(ve) ((DEntry)(ve))->type
  112. /* the value is right after the structure */
  113. #define DataValue(ve) (XPointer)(((DEntry)(ve)) + 1)
  114. #define RawValue(ve) (char *)((ve)->string ? StringValue(ve) : DataValue(ve))
  115. typedef struct _NTable {
  116. struct _NTable *next; /* next in chain */
  117. XrmQuark name; /* name of this entry */
  118. unsigned int tight:1; /* 1 if it is a tight binding */
  119. unsigned int leaf:1; /* 1 if children are values */
  120. unsigned int hasloose:1; /* 1 if has loose children */
  121. unsigned int hasany:1; /* 1 if has ANY entry */
  122. unsigned int pad:4; /* unused */
  123. unsigned int mask:8; /* hash size - 1 */
  124. unsigned int entries:16; /* number of children */
  125. } NTableRec, *NTable;
  126. /* the buckets are right after the structure */
  127. #define NodeBuckets(ne) ((NTable *)((ne) + 1))
  128. #define NodeHash(ne,q) NodeBuckets(ne)[(q) & (ne)->mask]
  129. /* leaf tables have an extra level of indirection for the buckets,
  130. * so that resizing can be done without invalidating a search list.
  131. * This is completely ugly, and wastes some memory, but the Xlib
  132. * spec doesn't really specify whether invalidation is OK, and the
  133. * old implementation did not invalidate.
  134. */
  135. typedef struct _LTable {
  136. NTableRec table;
  137. VEntry *buckets;
  138. } LTableRec, *LTable;
  139. #define LeafHash(le,q) (le)->buckets[(q) & (le)->table.mask]
  140. /* An XrmDatabase just holds a pointer to the first top-level table.
  141. * The type name is no longer descriptive, but better to not change
  142. * the Xresource.h header file. This type also gets used to define
  143. * XrmSearchList, which is a complete crock, but we'll just leave it
  144. * and caste types as required.
  145. */
  146. typedef struct _XrmHashBucketRec {
  147. NTable table;
  148. XPointer mbstate;
  149. XrmMethods methods;
  150. #ifdef XTHREADS
  151. LockInfoRec linfo;
  152. #endif
  153. } XrmHashBucketRec;
  154. /* closure used in get/put resource */
  155. typedef struct _VClosure {
  156. XrmRepresentation *type; /* type of value */
  157. XrmValuePtr value; /* value itself */
  158. } VClosureRec, *VClosure;
  159. /* closure used in get search list */
  160. typedef struct _SClosure {
  161. LTable *list; /* search list */
  162. int idx; /* index of last filled element */
  163. int limit; /* maximum index */
  164. } SClosureRec, *SClosure;
  165. /* placed in XrmSearchList to indicate next table is loose only */
  166. #define LOOSESEARCH ((LTable)1)
  167. /* closure used in enumerate database */
  168. typedef struct _EClosure {
  169. XrmDatabase db; /* the database */
  170. DBEnumProc proc; /* the user proc */
  171. XPointer closure; /* the user closure */
  172. XrmBindingList bindings; /* binding list */
  173. XrmQuarkList quarks; /* quark list */
  174. int mode; /* XrmEnum<kind> */
  175. } EClosureRec, *EClosure;
  176. /* types for typecasting ETable based functions to NTable based functions */
  177. typedef Bool (*getNTableSProcp)(
  178. NTable table,
  179. XrmNameList names,
  180. XrmClassList classes,
  181. SClosure closure);
  182. typedef Bool (*getNTableVProcp)(
  183. NTable table,
  184. XrmNameList names,
  185. XrmClassList classes,
  186. VClosure closure);
  187. typedef Bool (*getNTableEProcp)(
  188. NTable table,
  189. XrmNameList names,
  190. XrmClassList classes,
  191. register int level,
  192. EClosure closure);
  193. /* predicate to determine when to resize a hash table */
  194. #define GrowthPred(n,m) ((unsigned)(n) > (((m) + 1) << 2))
  195. #define GROW(prev) \
  196. if (GrowthPred((*prev)->entries, (*prev)->mask)) \
  197. GrowTable(prev)
  198. /* pick a reasonable value for maximum depth of resource database */
  199. #define MAXDBDEPTH 100
  200. /* macro used in get/search functions */
  201. /* find an entry named ename, with leafness given by leaf */
  202. #define NFIND(ename) \
  203. q = ename; \
  204. entry = NodeHash(table, q); \
  205. while (entry && entry->name != q) \
  206. entry = entry->next; \
  207. if (leaf && entry && !entry->leaf) { \
  208. entry = entry->next; \
  209. if (entry && !entry->leaf) \
  210. entry = entry->next; \
  211. if (entry && entry->name != q) \
  212. entry = (NTable)NULL; \
  213. }
  214. /* resourceQuarks keeps track of what quarks have been associated with values
  215. * in all LTables. If a quark has never been used in an LTable, we don't need
  216. * to bother looking for it.
  217. */
  218. static unsigned char *resourceQuarks = (unsigned char *)NULL;
  219. static XrmQuark maxResourceQuark = -1;
  220. /* determines if a quark has been used for a value in any database */
  221. #define IsResourceQuark(q) ((q) > 0 && (q) <= maxResourceQuark && \
  222. resourceQuarks[(q) >> 3] & (1 << ((q) & 7)))
  223. typedef unsigned char XrmBits;
  224. #define BSLASH ((XrmBits) (1 << 5))
  225. #define NORMAL ((XrmBits) (1 << 4))
  226. #define EOQ ((XrmBits) (1 << 3))
  227. #define SEP ((XrmBits) (1 << 2))
  228. #define ENDOF ((XrmBits) (1 << 1))
  229. #define SPACE (NORMAL|EOQ|SEP|(XrmBits)0)
  230. #define RSEP (NORMAL|EOQ|SEP|(XrmBits)1)
  231. #define EOS (EOQ|SEP|ENDOF|(XrmBits)0)
  232. #define EOL (EOQ|SEP|ENDOF|(XrmBits)1)
  233. #define BINDING (NORMAL|EOQ)
  234. #define ODIGIT (NORMAL|(XrmBits)1)
  235. #define next_char(ch,str) xrmtypes[(unsigned char)((ch) = *(++(str)))]
  236. #define next_mbchar(ch,len,str) xrmtypes[(unsigned char)(ch = (*db->methods->mbchar)(db->mbstate, str, &len), str += len, ch)]
  237. #define is_space(bits) ((bits) == SPACE)
  238. #define is_EOQ(bits) ((bits) & EOQ)
  239. #define is_EOF(bits) ((bits) == EOS)
  240. #define is_EOL(bits) ((bits) & ENDOF)
  241. #define is_binding(bits) ((bits) == BINDING)
  242. #define is_odigit(bits) ((bits) == ODIGIT)
  243. #define is_separator(bits) ((bits) & SEP)
  244. #define is_nonpcs(bits) (!(bits))
  245. #define is_normal(bits) ((bits) & NORMAL)
  246. #define is_simple(bits) ((bits) & (NORMAL|BSLASH))
  247. #define is_special(bits) ((bits) & (ENDOF|BSLASH))
  248. /* parsing types */
  249. static XrmBits const xrmtypes[256] = {
  250. EOS,0,0,0,0,0,0,0,
  251. 0,SPACE,EOL,0,0,
  252. #if defined(WIN32) || defined(__UNIXOS2__)
  253. EOL, /* treat CR the same as LF, just in case */
  254. #else
  255. 0,
  256. #endif
  257. 0,0,
  258. 0,0,0,0,0,0,0,0,
  259. 0,0,0,0,0,0,0,0,
  260. SPACE,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  261. NORMAL,NORMAL,BINDING,NORMAL,NORMAL,NORMAL,BINDING,NORMAL,
  262. ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,ODIGIT,
  263. NORMAL,NORMAL,RSEP,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  264. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  265. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  266. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  267. NORMAL,NORMAL,NORMAL,NORMAL,BSLASH,NORMAL,NORMAL,NORMAL,
  268. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  269. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  270. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,
  271. NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,NORMAL,0
  272. /* The rest will be automatically initialized to zero. */
  273. };
  274. void XrmInitialize(void)
  275. {
  276. XrmQString = XrmPermStringToQuark("String");
  277. XrmQANY = XrmPermStringToQuark("?");
  278. }
  279. #ifndef _XP_PRINT_SERVER_
  280. XrmDatabase XrmGetDatabase(
  281. Display *display)
  282. {
  283. XrmDatabase retval;
  284. LockDisplay(display);
  285. retval = display->db;
  286. UnlockDisplay(display);
  287. return retval;
  288. }
  289. void XrmSetDatabase(
  290. Display *display,
  291. XrmDatabase database)
  292. {
  293. LockDisplay(display);
  294. /* destroy database if set up imlicitely by XGetDefault() */
  295. if (display->db && (display->flags & XlibDisplayDfltRMDB)) {
  296. XrmDestroyDatabase(display->db);
  297. display->flags &= ~XlibDisplayDfltRMDB;
  298. }
  299. display->db = database;
  300. UnlockDisplay(display);
  301. }
  302. #endif /* !_XP_PRINT_SERVER_ */
  303. void
  304. XrmStringToQuarkList(
  305. register _Xconst char *name,
  306. register XrmQuarkList quarks) /* RETURN */
  307. {
  308. register XrmBits bits;
  309. register Signature sig = 0;
  310. register char ch, *tname;
  311. register int i = 0;
  312. if ((tname = (char *)name)) {
  313. tname--;
  314. while (!is_EOF(bits = next_char(ch, tname))) {
  315. if (is_binding (bits)) {
  316. if (i) {
  317. /* Found a complete name */
  318. *quarks++ = _XrmInternalStringToQuark(name,tname - name,
  319. sig, False);
  320. i = 0;
  321. sig = 0;
  322. }
  323. name = tname+1;
  324. }
  325. else {
  326. sig = (sig << 1) + ch; /* Compute the signature. */
  327. i++;
  328. }
  329. }
  330. *quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
  331. }
  332. *quarks = NULLQUARK;
  333. }
  334. void
  335. XrmStringToBindingQuarkList(
  336. register _Xconst char *name,
  337. register XrmBindingList bindings, /* RETURN */
  338. register XrmQuarkList quarks) /* RETURN */
  339. {
  340. register XrmBits bits;
  341. register Signature sig = 0;
  342. register char ch, *tname;
  343. register XrmBinding binding;
  344. register int i = 0;
  345. if ((tname = (char *)name)) {
  346. tname--;
  347. binding = XrmBindTightly;
  348. while (!is_EOF(bits = next_char(ch, tname))) {
  349. if (is_binding (bits)) {
  350. if (i) {
  351. /* Found a complete name */
  352. *bindings++ = binding;
  353. *quarks++ = _XrmInternalStringToQuark(name, tname - name,
  354. sig, False);
  355. i = 0;
  356. sig = 0;
  357. binding = XrmBindTightly;
  358. }
  359. name = tname+1;
  360. if (ch == '*')
  361. binding = XrmBindLoosely;
  362. }
  363. else {
  364. sig = (sig << 1) + ch; /* Compute the signature. */
  365. i++;
  366. }
  367. }
  368. *bindings = binding;
  369. *quarks++ = _XrmInternalStringToQuark(name, tname - name, sig, False);
  370. }
  371. *quarks = NULLQUARK;
  372. }
  373. #ifdef DEBUG
  374. static void PrintQuarkList(
  375. XrmQuarkList quarks,
  376. FILE *stream)
  377. {
  378. Bool firstNameSeen;
  379. for (firstNameSeen = False; *quarks; quarks++) {
  380. if (firstNameSeen) {
  381. (void) fprintf(stream, ".");
  382. }
  383. firstNameSeen = True;
  384. (void) fputs(XrmQuarkToString(*quarks), stream);
  385. }
  386. } /* PrintQuarkList */
  387. #endif /* DEBUG */
  388. /*
  389. * Fallback methods for Xrm parsing.
  390. * Simulate a C locale. No state needed here.
  391. */
  392. static void
  393. c_mbnoop(
  394. XPointer state)
  395. {
  396. }
  397. static char
  398. c_mbchar(
  399. XPointer state,
  400. const char *str,
  401. int *lenp)
  402. {
  403. *lenp = 1;
  404. return *str;
  405. }
  406. static const char *
  407. c_lcname(
  408. XPointer state)
  409. {
  410. return "C";
  411. }
  412. static const XrmMethodsRec mb_methods = {
  413. c_mbnoop, /* mbinit */
  414. c_mbchar, /* mbchar */
  415. c_mbnoop, /* mbfinish */
  416. c_lcname, /* lcname */
  417. c_mbnoop /* destroy */
  418. };
  419. static XrmDatabase NewDatabase(void)
  420. {
  421. register XrmDatabase db;
  422. db = (XrmDatabase) Xmalloc(sizeof(XrmHashBucketRec));
  423. if (db) {
  424. _XCreateMutex(&db->linfo);
  425. db->table = (NTable)NULL;
  426. db->mbstate = (XPointer)NULL;
  427. #ifdef _XP_PRINT_SERVER_
  428. db->methods = NULL;
  429. #else
  430. db->methods = _XrmInitParseInfo(&db->mbstate);
  431. #endif
  432. if (!db->methods)
  433. db->methods = &mb_methods;
  434. }
  435. return db;
  436. }
  437. /* move all values from ftable to ttable, and free ftable's buckets.
  438. * ttable is quaranteed empty to start with.
  439. */
  440. static void MoveValues(
  441. LTable ftable,
  442. register LTable ttable)
  443. {
  444. register VEntry fentry, nfentry;
  445. register VEntry *prev;
  446. register VEntry *bucket;
  447. register VEntry tentry;
  448. register int i;
  449. for (i = ftable->table.mask, bucket = ftable->buckets; i >= 0; i--) {
  450. for (fentry = *bucket++; fentry; fentry = nfentry) {
  451. prev = &LeafHash(ttable, fentry->name);
  452. tentry = *prev;
  453. *prev = fentry;
  454. /* chain on all with same name, to preserve invariant order */
  455. while ((nfentry = fentry->next) && nfentry->name == fentry->name)
  456. fentry = nfentry;
  457. fentry->next = tentry;
  458. }
  459. }
  460. Xfree((char *)ftable->buckets);
  461. }
  462. /* move all tables from ftable to ttable, and free ftable.
  463. * ttable is quaranteed empty to start with.
  464. */
  465. static void MoveTables(
  466. NTable ftable,
  467. register NTable ttable)
  468. {
  469. register NTable fentry, nfentry;
  470. register NTable *prev;
  471. register NTable *bucket;
  472. register NTable tentry;
  473. register int i;
  474. for (i = ftable->mask, bucket = NodeBuckets(ftable); i >= 0; i--) {
  475. for (fentry = *bucket++; fentry; fentry = nfentry) {
  476. prev = &NodeHash(ttable, fentry->name);
  477. tentry = *prev;
  478. *prev = fentry;
  479. /* chain on all with same name, to preserve invariant order */
  480. while ((nfentry = fentry->next) && nfentry->name == fentry->name)
  481. fentry = nfentry;
  482. fentry->next = tentry;
  483. }
  484. }
  485. Xfree((char *)ftable);
  486. }
  487. /* grow the table, based on current number of entries */
  488. static void GrowTable(
  489. NTable *prev)
  490. {
  491. register NTable table;
  492. register int i;
  493. table = *prev;
  494. i = table->mask;
  495. if (i == 255) /* biggest it gets */
  496. return;
  497. while (i < 255 && GrowthPred(table->entries, i))
  498. i = (i << 1) + 1;
  499. i++; /* i is now the new size */
  500. if (table->leaf) {
  501. register LTable ltable;
  502. LTableRec otable;
  503. ltable = (LTable)table;
  504. /* cons up a copy to make MoveValues look symmetric */
  505. otable = *ltable;
  506. ltable->buckets = (VEntry *)Xmalloc(i * sizeof(VEntry));
  507. if (!ltable->buckets) {
  508. ltable->buckets = otable.buckets;
  509. return;
  510. }
  511. ltable->table.mask = i - 1;
  512. bzero((char *)ltable->buckets, i * sizeof(VEntry));
  513. MoveValues(&otable, ltable);
  514. } else {
  515. register NTable ntable;
  516. ntable = (NTable)Xmalloc(sizeof(NTableRec) + i * sizeof(NTable));
  517. if (!ntable)
  518. return;
  519. *ntable = *table;
  520. ntable->mask = i - 1;
  521. bzero((char *)NodeBuckets(ntable), i * sizeof(NTable));
  522. *prev = ntable;
  523. MoveTables(table, ntable);
  524. }
  525. }
  526. /* merge values from ftable into *pprev, destroy ftable in the process */
  527. static void MergeValues(
  528. LTable ftable,
  529. NTable *pprev,
  530. Bool override)
  531. {
  532. register VEntry fentry, tentry;
  533. register VEntry *prev;
  534. register LTable ttable;
  535. VEntry *bucket;
  536. int i;
  537. register XrmQuark q;
  538. ttable = (LTable)*pprev;
  539. if (ftable->table.hasloose)
  540. ttable->table.hasloose = 1;
  541. for (i = ftable->table.mask, bucket = ftable->buckets;
  542. i >= 0;
  543. i--, bucket++) {
  544. for (fentry = *bucket; fentry; ) {
  545. q = fentry->name;
  546. prev = &LeafHash(ttable, q);
  547. tentry = *prev;
  548. while (tentry && tentry->name != q)
  549. tentry = *(prev = &tentry->next);
  550. /* note: test intentionally uses fentry->name instead of q */
  551. /* permits serendipitous inserts */
  552. while (tentry && tentry->name == fentry->name) {
  553. /* if tentry is earlier, skip it */
  554. if (!fentry->tight && tentry->tight) {
  555. tentry = *(prev = &tentry->next);
  556. continue;
  557. }
  558. if (fentry->tight != tentry->tight) {
  559. /* no match, chain in fentry */
  560. *prev = fentry;
  561. prev = &fentry->next;
  562. fentry = *prev;
  563. *prev = tentry;
  564. ttable->table.entries++;
  565. } else if (override) {
  566. /* match, chain in fentry, splice out and free tentry */
  567. *prev = fentry;
  568. prev = &fentry->next;
  569. fentry = *prev;
  570. *prev = tentry->next;
  571. /* free the overridden entry */
  572. Xfree((char *)tentry);
  573. /* get next tentry */
  574. tentry = *prev;
  575. } else {
  576. /* match, discard fentry */
  577. prev = &tentry->next;
  578. tentry = fentry; /* use as a temp var */
  579. fentry = fentry->next;
  580. /* free the overpowered entry */
  581. Xfree((char *)tentry);
  582. /* get next tentry */
  583. tentry = *prev;
  584. }
  585. if (!fentry)
  586. break;
  587. }
  588. /* at this point, tentry cannot match any fentry named q */
  589. /* chain in all bindings together, preserve invariant order */
  590. while (fentry && fentry->name == q) {
  591. *prev = fentry;
  592. prev = &fentry->next;
  593. fentry = *prev;
  594. *prev = tentry;
  595. ttable->table.entries++;
  596. }
  597. }
  598. }
  599. Xfree((char *)ftable->buckets);
  600. Xfree((char *)ftable);
  601. /* resize if necessary, now that we're all done */
  602. GROW(pprev);
  603. }
  604. /* merge tables from ftable into *pprev, destroy ftable in the process */
  605. static void MergeTables(
  606. NTable ftable,
  607. NTable *pprev,
  608. Bool override)
  609. {
  610. register NTable fentry, tentry;
  611. NTable nfentry;
  612. register NTable *prev;
  613. register NTable ttable;
  614. NTable *bucket;
  615. int i;
  616. register XrmQuark q;
  617. ttable = *pprev;
  618. if (ftable->hasloose)
  619. ttable->hasloose = 1;
  620. if (ftable->hasany)
  621. ttable->hasany = 1;
  622. for (i = ftable->mask, bucket = NodeBuckets(ftable);
  623. i >= 0;
  624. i--, bucket++) {
  625. for (fentry = *bucket; fentry; ) {
  626. q = fentry->name;
  627. prev = &NodeHash(ttable, q);
  628. tentry = *prev;
  629. while (tentry && tentry->name != q)
  630. tentry = *(prev = &tentry->next);
  631. /* note: test intentionally uses fentry->name instead of q */
  632. /* permits serendipitous inserts */
  633. while (tentry && tentry->name == fentry->name) {
  634. /* if tentry is earlier, skip it */
  635. if ((fentry->leaf && !tentry->leaf) ||
  636. (!fentry->tight && tentry->tight &&
  637. (fentry->leaf || !tentry->leaf))) {
  638. tentry = *(prev = &tentry->next);
  639. continue;
  640. }
  641. nfentry = fentry->next;
  642. if (fentry->leaf != tentry->leaf ||
  643. fentry->tight != tentry->tight) {
  644. /* no match, just chain in */
  645. *prev = fentry;
  646. *(prev = &fentry->next) = tentry;
  647. ttable->entries++;
  648. } else {
  649. if (fentry->leaf)
  650. MergeValues((LTable)fentry, prev, override);
  651. else
  652. MergeTables(fentry, prev, override);
  653. /* bump to next tentry */
  654. tentry = *(prev = &(*prev)->next);
  655. }
  656. /* bump to next fentry */
  657. fentry = nfentry;
  658. if (!fentry)
  659. break;
  660. }
  661. /* at this point, tentry cannot match any fentry named q */
  662. /* chain in all bindings together, preserve invariant order */
  663. while (fentry && fentry->name == q) {
  664. *prev = fentry;
  665. prev = &fentry->next;
  666. fentry = *prev;
  667. *prev = tentry;
  668. ttable->entries++;
  669. }
  670. }
  671. }
  672. Xfree((char *)ftable);
  673. /* resize if necessary, now that we're all done */
  674. GROW(pprev);
  675. }
  676. void XrmCombineDatabase(
  677. XrmDatabase from, XrmDatabase *into,
  678. Bool override)
  679. {
  680. register NTable *prev;
  681. register NTable ftable, ttable, nftable;
  682. if (!*into) {
  683. *into = from;
  684. } else if (from) {
  685. _XLockMutex(&from->linfo);
  686. _XLockMutex(&(*into)->linfo);
  687. if ((ftable = from->table)) {
  688. prev = &(*into)->table;
  689. ttable = *prev;
  690. if (!ftable->leaf) {
  691. nftable = ftable->next;
  692. if (ttable && !ttable->leaf) {
  693. /* both have node tables, merge them */
  694. MergeTables(ftable, prev, override);
  695. /* bump to into's leaf table, if any */
  696. ttable = *(prev = &(*prev)->next);
  697. } else {
  698. /* into has no node table, link from's in */
  699. *prev = ftable;
  700. *(prev = &ftable->next) = ttable;
  701. }
  702. /* bump to from's leaf table, if any */
  703. ftable = nftable;
  704. } else {
  705. /* bump to into's leaf table, if any */
  706. if (ttable && !ttable->leaf)
  707. ttable = *(prev = &ttable->next);
  708. }
  709. if (ftable) {
  710. /* if into has a leaf, merge, else insert */
  711. if (ttable)
  712. MergeValues((LTable)ftable, prev, override);
  713. else
  714. *prev = ftable;
  715. }
  716. }
  717. (from->methods->destroy)(from->mbstate);
  718. _XFreeMutex(&from->linfo);
  719. Xfree((char *)from);
  720. _XUnlockMutex(&(*into)->linfo);
  721. }
  722. }
  723. void XrmMergeDatabases(
  724. XrmDatabase from, XrmDatabase *into)
  725. {
  726. XrmCombineDatabase(from, into, True);
  727. }
  728. /* store a value in the database, overriding any existing entry */
  729. static void PutEntry(
  730. XrmDatabase db,
  731. XrmBindingList bindings,
  732. XrmQuarkList quarks,
  733. XrmRepresentation type,
  734. XrmValuePtr value)
  735. {
  736. register NTable *pprev, *prev;
  737. register NTable table;
  738. register XrmQuark q;
  739. register VEntry *vprev;
  740. register VEntry entry;
  741. NTable *nprev, *firstpprev;
  742. #define NEWTABLE(q,i) \
  743. table = (NTable)Xmalloc(sizeof(LTableRec)); \
  744. if (!table) \
  745. return; \
  746. table->name = q; \
  747. table->hasloose = 0; \
  748. table->hasany = 0; \
  749. table->mask = 0; \
  750. table->entries = 0; \
  751. if (quarks[i]) { \
  752. table->leaf = 0; \
  753. nprev = NodeBuckets(table); \
  754. } else { \
  755. table->leaf = 1; \
  756. if (!(nprev = (NTable *)Xmalloc(sizeof(VEntry *)))) \
  757. return; \
  758. ((LTable)table)->buckets = (VEntry *)nprev; \
  759. } \
  760. *nprev = (NTable)NULL; \
  761. table->next = *prev; \
  762. *prev = table
  763. if (!db || !*quarks)
  764. return;
  765. table = *(prev = &db->table);
  766. /* if already at leaf, bump to the leaf table */
  767. if (!quarks[1] && table && !table->leaf)
  768. table = *(prev = &table->next);
  769. pprev = prev;
  770. if (!table || (quarks[1] && table->leaf)) {
  771. /* no top-level node table, create one and chain it in */
  772. NEWTABLE(NULLQUARK,1);
  773. table->tight = 1; /* arbitrary */
  774. prev = nprev;
  775. } else {
  776. /* search along until we need a value */
  777. while (quarks[1]) {
  778. q = *quarks;
  779. table = *(prev = &NodeHash(table, q));
  780. while (table && table->name != q)
  781. table = *(prev = &table->next);
  782. if (!table)
  783. break; /* not found */
  784. if (quarks[2]) {
  785. if (table->leaf)
  786. break; /* not found */
  787. } else {
  788. if (!table->leaf) {
  789. /* bump to leaf table, if any */
  790. table = *(prev = &table->next);
  791. if (!table || table->name != q)
  792. break; /* not found */
  793. if (!table->leaf) {
  794. /* bump to leaf table, if any */
  795. table = *(prev = &table->next);
  796. if (!table || table->name != q)
  797. break; /* not found */
  798. }
  799. }
  800. }
  801. if (*bindings == XrmBindTightly) {
  802. if (!table->tight)
  803. break; /* not found */
  804. } else {
  805. if (table->tight) {
  806. /* bump to loose table, if any */
  807. table = *(prev = &table->next);
  808. if (!table || table->name != q ||
  809. !quarks[2] != table->leaf)
  810. break; /* not found */
  811. }
  812. }
  813. /* found that one, bump to next quark */
  814. pprev = prev;
  815. quarks++;
  816. bindings++;
  817. }
  818. if (!quarks[1]) {
  819. /* found all the way to a leaf */
  820. q = *quarks;
  821. entry = *(vprev = &LeafHash((LTable)table, q));
  822. while (entry && entry->name != q)
  823. entry = *(vprev = &entry->next);
  824. /* if want loose and have tight, bump to next entry */
  825. if (entry && *bindings == XrmBindLoosely && entry->tight)
  826. entry = *(vprev = &entry->next);
  827. if (entry && entry->name == q &&
  828. (*bindings == XrmBindTightly) == entry->tight) {
  829. /* match, need to override */
  830. if ((type == XrmQString) == entry->string &&
  831. entry->size == value->size) {
  832. /* update type if not String, can be different */
  833. if (!entry->string)
  834. RepType(entry) = type;
  835. /* identical size, just overwrite value */
  836. memcpy(RawValue(entry), (char *)value->addr, value->size);
  837. return;
  838. }
  839. /* splice out and free old entry */
  840. *vprev = entry->next;
  841. Xfree((char *)entry);
  842. (*pprev)->entries--;
  843. }
  844. /* this is where to insert */
  845. prev = (NTable *)vprev;
  846. }
  847. }
  848. /* keep the top table, because we may have to grow it */
  849. firstpprev = pprev;
  850. /* iterate until we get to the leaf */
  851. while (quarks[1]) {
  852. /* build a new table and chain it in */
  853. NEWTABLE(*quarks,2);
  854. if (*quarks++ == XrmQANY)
  855. (*pprev)->hasany = 1;
  856. if (*bindings++ == XrmBindTightly) {
  857. table->tight = 1;
  858. } else {
  859. table->tight = 0;
  860. (*pprev)->hasloose = 1;
  861. }
  862. (*pprev)->entries++;
  863. pprev = prev;
  864. prev = nprev;
  865. }
  866. /* now allocate the value entry */
  867. entry = (VEntry)Xmalloc(((type == XrmQString) ?
  868. sizeof(VEntryRec) : sizeof(DEntryRec)) +
  869. value->size);
  870. if (!entry)
  871. return;
  872. entry->name = q = *quarks;
  873. if (*bindings == XrmBindTightly) {
  874. entry->tight = 1;
  875. } else {
  876. entry->tight = 0;
  877. (*pprev)->hasloose = 1;
  878. }
  879. /* chain it in, with a bit of type cast ugliness */
  880. entry->next = *((VEntry *)prev);
  881. *((VEntry *)prev) = entry;
  882. entry->size = value->size;
  883. if (type == XrmQString) {
  884. entry->string = 1;
  885. } else {
  886. entry->string = 0;
  887. RepType(entry) = type;
  888. }
  889. /* save a copy of the value */
  890. memcpy(RawValue(entry), (char *)value->addr, value->size);
  891. (*pprev)->entries++;
  892. /* this is a new leaf, need to remember it for search lists */
  893. if (q > maxResourceQuark) {
  894. unsigned oldsize = (maxResourceQuark + 1) >> 3;
  895. unsigned size = ((q | 0x7f) + 1) >> 3; /* reallocate in chunks */
  896. if (resourceQuarks) {
  897. unsigned char *prevQuarks = resourceQuarks;
  898. resourceQuarks = (unsigned char *)Xrealloc((char *)resourceQuarks,
  899. size);
  900. if (!resourceQuarks) {
  901. Xfree(prevQuarks);
  902. }
  903. } else
  904. resourceQuarks = (unsigned char *)Xmalloc(size);
  905. if (resourceQuarks) {
  906. bzero((char *)&resourceQuarks[oldsize], size - oldsize);
  907. maxResourceQuark = (size << 3) - 1;
  908. } else {
  909. maxResourceQuark = -1;
  910. }
  911. }
  912. if (q > 0 && resourceQuarks)
  913. resourceQuarks[q >> 3] |= 1 << (q & 0x7);
  914. GROW(firstpprev);
  915. #undef NEWTABLE
  916. }
  917. void XrmQPutResource(
  918. XrmDatabase *pdb,
  919. XrmBindingList bindings,
  920. XrmQuarkList quarks,
  921. XrmRepresentation type,
  922. XrmValuePtr value)
  923. {
  924. if (!*pdb) *pdb = NewDatabase();
  925. _XLockMutex(&(*pdb)->linfo);
  926. PutEntry(*pdb, bindings, quarks, type, value);
  927. _XUnlockMutex(&(*pdb)->linfo);
  928. }
  929. void
  930. XrmPutResource(
  931. XrmDatabase *pdb,
  932. _Xconst char *specifier,
  933. _Xconst char *type,
  934. XrmValuePtr value)
  935. {
  936. XrmBinding bindings[MAXDBDEPTH+1];
  937. XrmQuark quarks[MAXDBDEPTH+1];
  938. if (!*pdb) *pdb = NewDatabase();
  939. _XLockMutex(&(*pdb)->linfo);
  940. XrmStringToBindingQuarkList(specifier, bindings, quarks);
  941. PutEntry(*pdb, bindings, quarks, XrmStringToQuark(type), value);
  942. _XUnlockMutex(&(*pdb)->linfo);
  943. }
  944. void
  945. XrmQPutStringResource(
  946. XrmDatabase *pdb,
  947. XrmBindingList bindings,
  948. XrmQuarkList quarks,
  949. _Xconst char *str)
  950. {
  951. XrmValue value;
  952. if (!*pdb) *pdb = NewDatabase();
  953. value.addr = (XPointer) str;
  954. value.size = strlen(str)+1;
  955. _XLockMutex(&(*pdb)->linfo);
  956. PutEntry(*pdb, bindings, quarks, XrmQString, &value);
  957. _XUnlockMutex(&(*pdb)->linfo);
  958. }
  959. /* Function Name: GetDatabase
  960. * Description: Parses a string and stores it as a database.
  961. * Arguments: db - the database.
  962. * str - a pointer to the string containing the database.
  963. * filename - source filename, if any.
  964. * doall - whether to do all lines or just one
  965. */
  966. /*
  967. * This function is highly optimized to inline as much as possible.
  968. * Be very careful with modifications, or simplifications, as they
  969. * may adversely affect the performance.
  970. *
  971. * Chris Peterson, MIT X Consortium 5/17/90.
  972. */
  973. /*
  974. * Xlib spec says max 100 quarks in a lookup, will stop and return if
  975. * return if any single production's lhs has more than 100 components.
  976. */
  977. #define QLIST_SIZE 100
  978. /*
  979. * This should be big enough to handle things like the XKeysymDB or biggish
  980. * ~/.Xdefaults or app-defaults files. Anything bigger will be allocated on
  981. * the heap.
  982. */
  983. #define DEF_BUFF_SIZE 8192
  984. static void GetIncludeFile(
  985. XrmDatabase db,
  986. _Xconst char *base,
  987. _Xconst char *fname,
  988. int fnamelen);
  989. static void GetDatabase(
  990. XrmDatabase db,
  991. _Xconst register char *str,
  992. _Xconst char *filename,
  993. Bool doall)
  994. {
  995. char *rhs;
  996. char *lhs, lhs_s[DEF_BUFF_SIZE];
  997. XrmQuark quarks[QLIST_SIZE + 1]; /* allow for a terminal NullQuark */
  998. XrmBinding bindings[QLIST_SIZE + 1];
  999. register char *ptr;
  1000. register XrmBits bits = 0;
  1001. register char c;
  1002. register Signature sig;
  1003. register char *ptr_max;
  1004. register int num_quarks;
  1005. register XrmBindingList t_bindings;
  1006. int len, alloc_chars;
  1007. unsigned long str_len;
  1008. XrmValue value;
  1009. Bool only_pcs;
  1010. Bool dolines;
  1011. if (!db)
  1012. return;
  1013. /*
  1014. * if strlen (str) < DEF_BUFF_SIZE allocate buffers on the stack for
  1015. * speed otherwise malloc the buffer. From a buffer overflow standpoint
  1016. * we can be sure that neither: a) a component on the lhs, or b) a
  1017. * value on the rhs, will be longer than the overall length of str,
  1018. * i.e. strlen(str).
  1019. *
  1020. * This should give good performance when parsing "*foo: bar" type
  1021. * databases as might be passed with -xrm command line options; but
  1022. * with larger databases, e.g. .Xdefaults, app-defaults, or KeysymDB
  1023. * files, the size of the buffers will be overly large. One way
  1024. * around this would be to double-parse each production with a resulting
  1025. * performance hit. In any event we can be assured that a lhs component
  1026. * name or a rhs value won't be longer than str itself.
  1027. */
  1028. str_len = strlen (str);
  1029. if (DEF_BUFF_SIZE > str_len) lhs = lhs_s;
  1030. else if ((lhs = (char*) Xmalloc (str_len)) == NULL)
  1031. return;
  1032. alloc_chars = DEF_BUFF_SIZE < str_len ? str_len : DEF_BUFF_SIZE;
  1033. if ((rhs = (char*) Xmalloc (alloc_chars)) == NULL) {
  1034. if (lhs != lhs_s) Xfree (lhs);
  1035. return;
  1036. }
  1037. (*db->methods->mbinit)(db->mbstate);
  1038. str--;
  1039. dolines = True;
  1040. while (!is_EOF(bits) && dolines) {
  1041. dolines = doall;
  1042. /*
  1043. * First: Remove extra whitespace.
  1044. */
  1045. do {
  1046. bits = next_char(c, str);
  1047. } while is_space(bits);
  1048. /*
  1049. * Ignore empty lines.
  1050. */
  1051. if (is_EOL(bits))
  1052. continue; /* start a new line. */
  1053. /*
  1054. * Second: check the first character in a line to see if it is
  1055. * "!" signifying a comment, or "#" signifying a directive.
  1056. */
  1057. if (c == '!') { /* Comment, spin to next newline */
  1058. while (is_simple(bits = next_char(c, str))) {}
  1059. if (is_EOL(bits))
  1060. continue;
  1061. while (!is_EOL(bits = next_mbchar(c, len, str))) {}
  1062. str--;
  1063. continue; /* start a new line. */
  1064. }
  1065. if (c == '#') { /* Directive */
  1066. /* remove extra whitespace */
  1067. only_pcs = True;
  1068. while (is_space(bits = next_char(c, str))) {};
  1069. /* only "include" directive is currently defined */
  1070. if (!strncmp(str, "include", 7)) {
  1071. str += (7-1);
  1072. /* remove extra whitespace */
  1073. while (is_space(bits = next_char(c, str))) {};
  1074. /* must have a starting " */
  1075. if (c == '"') {
  1076. _Xconst char *fname = str+1;
  1077. len = 0;
  1078. do {
  1079. if (only_pcs) {
  1080. bits = next_char(c, str);
  1081. if (is_nonpcs(bits))
  1082. only_pcs = False;
  1083. }
  1084. if (!only_pcs)
  1085. bits = next_mbchar(c, len, str);
  1086. } while (c != '"' && !is_EOL(bits));
  1087. /* must have an ending " */
  1088. if (c == '"')
  1089. GetIncludeFile(db, filename, fname, str - len - fname);
  1090. }
  1091. }
  1092. /* spin to next newline */
  1093. if (only_pcs) {
  1094. while (is_simple(bits))
  1095. bits = next_char(c, str);
  1096. if (is_EOL(bits))
  1097. continue;
  1098. }
  1099. while (!is_EOL(bits))
  1100. bits = next_mbchar(c, len, str);
  1101. str--;
  1102. continue; /* start a new line. */
  1103. }
  1104. /*
  1105. * Third: loop through the LHS of the resource specification
  1106. * storing characters and converting this to a Quark.
  1107. */
  1108. num_quarks = 0;
  1109. t_bindings = bindings;
  1110. sig = 0;
  1111. ptr = lhs;
  1112. *t_bindings = XrmBindTightly;
  1113. for(;;) {
  1114. if (!is_binding(bits)) {
  1115. while (!is_EOQ(bits)) {
  1116. *ptr++ = c;
  1117. sig = (sig << 1) + c; /* Compute the signature. */
  1118. bits = next_char(c, str);
  1119. }
  1120. quarks[num_quarks++] =
  1121. _XrmInternalStringToQuark(lhs, ptr - lhs, sig, False);
  1122. if (num_quarks > QLIST_SIZE) {
  1123. Xfree(rhs);
  1124. if (lhs != lhs_s) Xfree (lhs);
  1125. (*db->methods->mbfinish)(db->mbstate);
  1126. return;
  1127. }
  1128. if (is_separator(bits)) {
  1129. if (!is_space(bits))
  1130. break;
  1131. /* Remove white space */
  1132. do {
  1133. *ptr++ = c;
  1134. sig = (sig << 1) + c; /* Compute the signature. */
  1135. } while (is_space(bits = next_char(c, str)));
  1136. /*
  1137. * The spec doesn't permit it, but support spaces
  1138. * internal to resource name/class
  1139. */
  1140. if (is_separator(bits))
  1141. break;
  1142. num_quarks--;
  1143. continue;
  1144. }
  1145. if (c == '.')
  1146. *(++t_bindings) = XrmBindTightly;
  1147. else
  1148. *(++t_bindings) = XrmBindLoosely;
  1149. sig = 0;
  1150. ptr = lhs;
  1151. }
  1152. else {
  1153. /*
  1154. * Magic unspecified feature #254.
  1155. *
  1156. * If two separators appear with no Text between them then
  1157. * ignore them.
  1158. *
  1159. * If anyone of those separators is a '*' then the binding
  1160. * will be loose, otherwise it will be tight.
  1161. */
  1162. if (c == '*')
  1163. *t_bindings = XrmBindLoosely;
  1164. }
  1165. bits = next_char(c, str);
  1166. }
  1167. quarks[num_quarks] = NULLQUARK;
  1168. /*
  1169. * Make sure that there is a ':' in this line.
  1170. */
  1171. if (c != ':') {
  1172. char oldc;
  1173. /*
  1174. * A parsing error has occured, toss everything on the line
  1175. * a new_line can still be escaped with a '\'.
  1176. */
  1177. while (is_normal(bits))
  1178. bits = next_char(c, str);
  1179. if (is_EOL(bits))
  1180. continue;
  1181. bits = next_mbchar(c, len, str);
  1182. do {
  1183. oldc = c;
  1184. bits = next_mbchar(c, len, str);
  1185. } while (c && (c != '\n' || oldc == '\\'));
  1186. str--;
  1187. continue;
  1188. }
  1189. /*
  1190. * I now have a quark and binding list for the entire left hand
  1191. * side. "c" currently points to the ":" separating the left hand
  1192. * side for the right hand side. It is time to begin processing
  1193. * the right hand side.
  1194. */
  1195. /*
  1196. * Fourth: Remove more whitespace
  1197. */
  1198. for(;;) {
  1199. if (is_space(bits = next_char(c, str)))
  1200. continue;
  1201. if (c != '\\')
  1202. break;
  1203. bits = next_char(c, str);
  1204. if (c == '\n')
  1205. continue;
  1206. str--;
  1207. bits = BSLASH;
  1208. c = '\\';
  1209. break;
  1210. }
  1211. /*
  1212. * Fifth: Process the right hand side.
  1213. */
  1214. ptr = rhs;
  1215. ptr_max = ptr + alloc_chars - 4;
  1216. only_pcs = True;
  1217. len = 1;
  1218. for(;;) {
  1219. /*
  1220. * Tight loop for the normal case: Non backslash, non-end of value
  1221. * character that will fit into the allocated buffer.
  1222. */
  1223. if (only_pcs) {
  1224. while (is_normal(bits) && ptr < ptr_max) {
  1225. *ptr++ = c;
  1226. bits = next_char(c, str);
  1227. }
  1228. if (is_EOL(bits))
  1229. break;
  1230. if (is_nonpcs(bits)) {
  1231. only_pcs = False;
  1232. bits = next_mbchar(c, len, str);
  1233. }
  1234. }
  1235. while (!is_special(bits) && ptr + len <= ptr_max) {
  1236. len = -len;
  1237. while (len)
  1238. *ptr++ = str[len++];
  1239. if (*str == '\0') {
  1240. bits = EOS;
  1241. break;
  1242. }
  1243. bits = next_mbchar(c, len, str);
  1244. }
  1245. if (is_EOL(bits)) {
  1246. str--;
  1247. break;
  1248. }
  1249. if (c == '\\') {
  1250. /*
  1251. * We need to do some magic after a backslash.
  1252. */
  1253. Bool read_next = True;
  1254. if (only_pcs) {
  1255. bits = next_char(c, str);
  1256. if (is_nonpcs(bits))
  1257. only_pcs = False;
  1258. }
  1259. if (!only_pcs)
  1260. bits = next_mbchar(c, len, str);
  1261. if (is_EOL(bits)) {
  1262. if (is_EOF(bits))
  1263. continue;
  1264. } else if (c == 'n') {
  1265. /*
  1266. * "\n" means insert a newline.
  1267. */
  1268. *ptr++ = '\n';
  1269. } else if (c == '\\') {
  1270. /*
  1271. * "\\" completes to just one backslash.
  1272. */
  1273. *ptr++ = '\\';
  1274. } else {
  1275. /*
  1276. * pick up to three octal digits after the '\'.
  1277. */
  1278. char temp[3];
  1279. int count = 0;
  1280. while (is_odigit(bits) && count < 3) {
  1281. temp[count++] = c;
  1282. if (only_pcs) {
  1283. bits = next_char(c, str);
  1284. if (is_nonpcs(bits))
  1285. only_pcs = False;
  1286. }
  1287. if (!only_pcs)
  1288. bits = next_mbchar(c, len, str);
  1289. }
  1290. /*
  1291. * If we found three digits then insert that octal code
  1292. * into the value string as a character.
  1293. */
  1294. if (count == 3) {
  1295. *ptr++ = (unsigned char) ((temp[0] - '0') * 0100 +
  1296. (temp[1] - '0') * 010 +
  1297. (temp[2] - '0'));
  1298. }
  1299. else {
  1300. int tcount;
  1301. /*
  1302. * Otherwise just insert those characters into the
  1303. * string, since no special processing is needed on
  1304. * numerics we can skip the special processing.
  1305. */
  1306. for (tcount = 0; tcount < count; tcount++) {
  1307. *ptr++ = temp[tcount]; /* print them in
  1308. the correct order */
  1309. }
  1310. }
  1311. read_next = False;
  1312. }
  1313. if (read_next) {
  1314. if (only_pcs) {
  1315. bits = next_char(c, str);
  1316. if (is_nonpcs(bits))
  1317. only_pcs = False;
  1318. }
  1319. if (!only_pcs)
  1320. bits = next_mbchar(c, len, str);
  1321. }
  1322. }
  1323. /*
  1324. * It is important to make sure that there is room for at least
  1325. * four more characters in the buffer, since I can add that
  1326. * many characters into the buffer after a backslash has occured.
  1327. */
  1328. if (ptr + len > ptr_max) {
  1329. char * temp_str;
  1330. alloc_chars += BUFSIZ/10;
  1331. temp_str = Xrealloc(rhs, sizeof(char) * alloc_chars);
  1332. if (!temp_str) {
  1333. Xfree(rhs);
  1334. if (lhs != lhs_s) Xfree (lhs);
  1335. (*db->methods->mbfinish)(db->mbstate);
  1336. return;
  1337. }
  1338. ptr = temp_str + (ptr - rhs); /* reset pointer. */
  1339. rhs = temp_str;
  1340. ptr_max = rhs + alloc_chars - 4;
  1341. }
  1342. }
  1343. /*
  1344. * Lastly: Terminate the value string, and store this entry
  1345. * into the database.
  1346. */
  1347. *ptr++ = '\0';
  1348. /* Store it in database */
  1349. value.size = ptr - rhs;
  1350. value.addr = (XPointer) rhs;
  1351. PutEntry(db, bindings, quarks, XrmQString, &value);
  1352. }
  1353. if (lhs != lhs_s) Xfree (lhs);
  1354. Xfree (rhs);
  1355. (*db->methods->mbfinish)(db->mbstate);
  1356. }
  1357. void
  1358. XrmPutStringResource(
  1359. XrmDatabase *pdb,
  1360. _Xconst char*specifier,
  1361. _Xconst char*str)
  1362. {
  1363. XrmValue value;
  1364. XrmBinding bindings[MAXDBDEPTH+1];
  1365. XrmQuark quarks[MAXDBDEPTH+1];
  1366. if (!*pdb) *pdb = NewDatabase();
  1367. XrmStringToBindingQuarkList(specifier, bindings, quarks);
  1368. value.addr = (XPointer) str;
  1369. value.size = strlen(str)+1;
  1370. _XLockMutex(&(*pdb)->linfo);
  1371. PutEntry(*pdb, bindings, quarks, XrmQString, &value);
  1372. _XUnlockMutex(&(*pdb)->linfo);
  1373. }
  1374. void
  1375. XrmPutLineResource(
  1376. XrmDatabase *pdb,
  1377. _Xconst char*line)
  1378. {
  1379. if (!*pdb) *pdb = NewDatabase();
  1380. _XLockMutex(&(*pdb)->linfo);
  1381. GetDatabase(*pdb, line, (char *)NULL, False);
  1382. _XUnlockMutex(&(*pdb)->linfo);
  1383. }
  1384. XrmDatabase
  1385. XrmGetStringDatabase(
  1386. _Xconst char *data)
  1387. {
  1388. XrmDatabase db;
  1389. db = NewDatabase();
  1390. _XLockMutex(&db->linfo);
  1391. GetDatabase(db, data, (char *)NULL, True);
  1392. _XUnlockMutex(&db->linfo);
  1393. return db;
  1394. }
  1395. /* Function Name: ReadInFile
  1396. * Description: Reads the file into a buffer.
  1397. * Arguments: filename - the name of the file.
  1398. * Returns: An allocated string containing the contents of the file.
  1399. */
  1400. static char *
  1401. ReadInFile(_Xconst char *filename)
  1402. {
  1403. register int fd, size;
  1404. char * filebuf;
  1405. #ifdef __UNIXOS2__
  1406. filename = __XOS2RedirRoot(filename);
  1407. #endif
  1408. /*
  1409. * MS-Windows and OS/2 note: Default open mode includes O_TEXT
  1410. */
  1411. if ( (fd = _XOpenFile (filename, O_RDONLY)) == -1 )
  1412. return (char *)NULL;
  1413. /*
  1414. * MS-Windows and OS/2 note: depending on how the sources are
  1415. * untarred, the newlines in resource files may or may not have
  1416. * been expanded to CRLF. Either way the size returned by fstat
  1417. * is sufficient to read the file into because in text-mode any
  1418. * CRLFs in a file will be converted to newlines (LF) with the
  1419. * result that the number of bytes actually read with be <=
  1420. * to the size returned by fstat.
  1421. */
  1422. GetSizeOfFile(fd, size);
  1423. if (!(filebuf = Xmalloc(size + 1))) { /* leave room for '\0' */
  1424. close(fd);
  1425. return (char *)NULL;
  1426. }
  1427. size = read (fd, filebuf, size);
  1428. #ifdef __UNIXOS2__
  1429. { /* kill CRLF */
  1430. int i,k;
  1431. for (i=k=0; i<size; i++)
  1432. if (filebuf[i] != 0x0d) {
  1433. filebuf[k++] = filebuf[i];
  1434. }
  1435. filebuf[k] = 0;
  1436. }
  1437. #endif
  1438. if (size < 0) {
  1439. close (fd);
  1440. Xfree(filebuf);
  1441. return (char *)NULL;
  1442. }
  1443. close (fd);
  1444. filebuf[size] = '\0'; /* NULL terminate it. */
  1445. return filebuf;
  1446. }
  1447. static void
  1448. GetIncludeFile(
  1449. XrmDatabase db,
  1450. _Xconst char *base,
  1451. _Xconst char *fname,
  1452. int fnamelen)
  1453. {
  1454. int len;
  1455. char *str;
  1456. char realfname[BUFSIZ];
  1457. if (fnamelen <= 0 || fnamelen >= BUFSIZ)
  1458. return;
  1459. if (*fname != '/' && base && (str = strrchr(base, '/'))) {
  1460. len = str - base + 1;
  1461. if (len + fnamelen >= BUFSIZ)
  1462. return;
  1463. strncpy(realfname, base, len);
  1464. strncpy(realfname + len, fname, fnamelen);
  1465. realfname[len + fnamelen] = '\0';
  1466. } else {
  1467. strncpy(realfname, fname, fnamelen);
  1468. realfname[fnamelen] = '\0';
  1469. }
  1470. if (!(str = ReadInFile(realfname)))
  1471. return;
  1472. GetDatabase(db, str, realfname, True);
  1473. Xfree(str);
  1474. }
  1475. XrmDatabase
  1476. XrmGetFileDatabase(
  1477. _Xconst char *filename)
  1478. {
  1479. XrmDatabase db;
  1480. char *str;
  1481. if (!(str = ReadInFile(filename)))
  1482. return (XrmDatabase)NULL;
  1483. db = NewDatabase();
  1484. _XLockMutex(&db->linfo);
  1485. GetDatabase(db, str, filename, True);
  1486. _XUnlockMutex(&db->linfo);
  1487. Xfree(str);
  1488. return db;
  1489. }
  1490. Status
  1491. XrmCombineFileDatabase(
  1492. _Xconst char *filename,
  1493. XrmDatabase *target,
  1494. Bool override)
  1495. {
  1496. XrmDatabase db;
  1497. char *str;
  1498. if (!(str = ReadInFile(filename)))
  1499. return 0;
  1500. if (override) {
  1501. db = *target;
  1502. if (!db)
  1503. *target = db = NewDatabase();
  1504. } else
  1505. db = NewDatabase();
  1506. _XLockMutex(&db->linfo);
  1507. GetDatabase(db, str, filename, True);
  1508. _XUnlockMutex(&db->linfo);
  1509. Xfree(str);
  1510. if (!override)
  1511. XrmCombineDatabase(db, target, False);
  1512. return 1;
  1513. }
  1514. /* call the user proc for every value in the table, arbitrary order.
  1515. * stop if user proc returns True. level is current depth in database.
  1516. */
  1517. /*ARGSUSED*/
  1518. static Bool EnumLTable(
  1519. LTable table,
  1520. XrmNameList names,
  1521. XrmClassList classes,
  1522. register int level,
  1523. register EClosure closure)
  1524. {
  1525. register VEntry *bucket;
  1526. register int i;
  1527. register VEntry entry;
  1528. XrmValue value;
  1529. XrmRepresentation type;
  1530. Bool tightOk;
  1531. closure->bindings[level] = (table->table.tight ?
  1532. XrmBindTightly : XrmBindLoosely);
  1533. closure->quarks[level] = table->table.name;
  1534. level++;
  1535. tightOk = !*names;
  1536. closure->quarks[level + 1] = NULLQUARK;
  1537. for (i = table->table.mask, bucket = table->buckets;
  1538. i >= 0;
  1539. i--, bucket++) {
  1540. for (entry = *bucket; entry; entry = entry->next) {
  1541. if (entry->tight && !tightOk)
  1542. continue;
  1543. closure->bindings[level] = (entry->tight ?
  1544. XrmBindTightly : XrmBindLoosely);
  1545. closure->quarks[level] = entry->name;
  1546. value.size = entry->size;
  1547. if (entry->string) {
  1548. type = XrmQString;
  1549. value.addr = StringValue(entry);
  1550. } else {
  1551. type = RepType(entry);
  1552. value.addr = DataValue(entry);
  1553. }
  1554. if ((*closure->proc)(&closure->db, closure->bindings+1,
  1555. closure->quarks+1, &type, &value,
  1556. closure->closure))
  1557. return True;
  1558. }
  1559. }
  1560. return False;
  1561. }
  1562. static Bool EnumAllNTable(
  1563. NTable table,
  1564. register int level,
  1565. register EClosure closure)
  1566. {
  1567. register NTable *bucket;
  1568. register int i;
  1569. register NTable entry;
  1570. XrmQuark empty = NULLQUARK;
  1571. if (level >= MAXDBDEPTH)
  1572. return False;
  1573. for (i = table->mask, bucket = NodeBuckets(table);
  1574. i >= 0;
  1575. i--, bucket++) {
  1576. for (entry = *bucket; entry; entry = entry->next) {
  1577. if (entry->leaf) {
  1578. if (EnumLTable((LTable)entry, &empty, &empty, level, closure))
  1579. return True;
  1580. } else {
  1581. closure->bindings[level] = (entry->tight ?
  1582. XrmBindTightly : XrmBindLoosely);
  1583. closure->quarks[level] = entry->name;
  1584. if (EnumAllNTable(entry, level+1, closure))
  1585. return True;
  1586. }
  1587. }
  1588. }
  1589. return False;
  1590. }
  1591. /* recurse on every table in the table, arbitrary order.
  1592. * stop if user proc returns True. level is current depth in database.
  1593. */
  1594. static Bool EnumNTable(
  1595. NTable table,
  1596. XrmNameList names,
  1597. XrmClassList classes,
  1598. register int level,
  1599. register EClosure closure)
  1600. {
  1601. register NTable entry;
  1602. register XrmQuark q;
  1603. register unsigned int leaf;
  1604. Bool (*get)(
  1605. NTable table,
  1606. XrmNameList names,
  1607. XrmClassList classes,
  1608. register int level,
  1609. EClosure closure);
  1610. Bool bilevel;
  1611. /* find entries named ename, leafness leaf, tight or loose, and call get */
  1612. #define ITIGHTLOOSE(ename) \
  1613. NFIND(ename); \
  1614. if (entry) { \
  1615. if (leaf == entry->leaf) { \
  1616. if (!leaf && !entry->tight && entry->next && \
  1617. entry->next->name == q && entry->next->tight && \
  1618. (bilevel || entry->next->hasloose) && \
  1619. EnumLTable((LTable)entry->next, names+1, classes+1, \
  1620. level, closure)) \
  1621. return True; \
  1622. if ((*get)(entry, names+1, classes+1, level, closure)) \
  1623. return True; \
  1624. if (entry->tight && (entry = entry->next) && \
  1625. entry->name == q && leaf == entry->leaf && \
  1626. (*get)(entry, names+1, classes+1, level, closure)) \
  1627. return True; \
  1628. } else if (entry->leaf) { \
  1629. if ((bilevel || entry->hasloose) && \
  1630. EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1631. return True; \
  1632. if (entry->tight && (entry = entry->next) && \
  1633. entry->name == q && (bilevel || entry->hasloose) && \
  1634. EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1635. return True; \
  1636. } \
  1637. }
  1638. /* find entries named ename, leafness leaf, loose only, and call get */
  1639. #define ILOOSE(ename) \
  1640. NFIND(ename); \
  1641. if (entry && entry->tight && (entry = entry->next) && entry->name != q) \
  1642. entry = (NTable)NULL; \
  1643. if (entry) { \
  1644. if (leaf == entry->leaf) { \
  1645. if ((*get)(entry, names+1, classes+1, level, closure)) \
  1646. return True; \
  1647. } else if (entry->leaf && (bilevel || entry->hasloose)) { \
  1648. if (EnumLTable((LTable)entry, names+1, classes+1, level, closure))\
  1649. return True; \
  1650. } \
  1651. }
  1652. if (level >= MAXDBDEPTH)
  1653. return False;
  1654. closure->bindings[level] = (table->tight ?
  1655. XrmBindTightly : XrmBindLoosely);
  1656. closure->quarks[level] = table->name;
  1657. level++;
  1658. if (!*names) {
  1659. if (EnumAllNTable(table, level, closure))
  1660. return True;
  1661. } else {
  1662. if (names[1] || closure->mode == XrmEnumAllLevels) {
  1663. get = EnumNTable; /* recurse */
  1664. leaf = 0;
  1665. bilevel = !names[1];
  1666. } else {
  1667. get = (getNTableEProcp)EnumLTable; /* bottom of recursion */
  1668. leaf = 1;
  1669. bilevel = False;
  1670. }
  1671. if (table->hasloose && closure->mode == XrmEnumAllLevels) {
  1672. NTable *bucket;
  1673. int i;
  1674. XrmQuark empty = NULLQUARK;
  1675. for (i = table->mask, bucket = NodeBuckets(table);
  1676. i >= 0;
  1677. i--, bucket++) {
  1678. q = NULLQUARK;

Large files files are truncated, but you can click here to view the full file