_falrm.c 64 KB

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