SmXrm.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /* $XConsortium: SmXrm.c /main/4 1995/10/30 09:39:33 rswiston $ */
  24. /* *
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  26. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  28. * (c) Copyright 1993, 1994 Novell, Inc. *
  29. */
  30. /*************************************<+>*************************************
  31. *****************************************************************************
  32. **
  33. ** File: SmXrm.c
  34. **
  35. ** Project: DT Session Manager (dtsession)
  36. **
  37. ** Description:
  38. ** -----------
  39. ** This file contains routines to manage an Xrm database
  40. **
  41. ** SmXrmSubtract() - subtract source_db from target_db and return result_db
  42. **
  43. *****************************************************************************
  44. *************************************<+>*************************************/
  45. #ifdef DEBUG
  46. #include <stdio.h>
  47. #endif
  48. #include <X11/Intrinsic.h>
  49. const XrmQuark empty = NULLQUARK;
  50. struct smSubtractState {
  51. XrmDatabase source_db;
  52. XrmDatabase target_db;
  53. XrmDatabase result_db;
  54. XrmBindingList target_bindings;
  55. XrmQuarkList target_quarks;
  56. };
  57. #ifdef DEBUG
  58. static void _PrintDbEntry(
  59. char *,
  60. XrmBindingList,
  61. XrmQuarkList,
  62. XrmRepresentation *,
  63. XrmValue *);
  64. #endif
  65. static Bool _CompareBindingQuarkList(
  66. XrmBindingList bindings1,
  67. XrmQuarkList quarks1,
  68. XrmBindingList bindings2,
  69. XrmQuarkList quarks2);
  70. static Bool _SmCompareSourceAndTarget(
  71. XrmDatabase *db,
  72. XrmBindingList bindings,
  73. XrmQuarkList quarks,
  74. XrmRepresentation *type,
  75. XrmValue *value,
  76. XPointer closure);
  77. static Bool _SmEnumerateSource(
  78. XrmDatabase *db,
  79. XrmBindingList bindings,
  80. XrmQuarkList quarks,
  81. XrmRepresentation *type,
  82. XrmValue *value,
  83. XPointer closure);
  84. #ifndef DEBUG
  85. #define _PrintDbEntry(a,b,c,d,e)
  86. #endif /* !DEBUG */
  87. /*************************************<->*************************************
  88. *
  89. * _CompareBindingQuarkList()
  90. *
  91. * Description:
  92. * -----------
  93. * Compare two binding quark lists and return True if the same
  94. *
  95. * Inputs:
  96. * ------
  97. * bindings1 - list1 bindings
  98. * quarks1 - list1 bindings
  99. * bindings2 - list2 bindings
  100. * quarks2 - list2 bindings
  101. *
  102. * Outputs:
  103. * -------
  104. *
  105. * Return:
  106. * ------
  107. * same - True if binding quark list is the same, False otherwise
  108. *
  109. * Comments:
  110. * --------
  111. *
  112. *************************************<->***********************************/
  113. static
  114. Bool _CompareBindingQuarkList(
  115. XrmBindingList bindings1,
  116. XrmQuarkList quarks1,
  117. XrmBindingList bindings2,
  118. XrmQuarkList quarks2)
  119. {
  120. int i = 0;
  121. Bool rc = False;
  122. /*
  123. * loop through quarks in list1
  124. */
  125. while (quarks1[i] != NULLQUARK)
  126. {
  127. /*
  128. * compare quark in list1 to same element in list2 and
  129. * break out of loop if they differ.
  130. */
  131. if (quarks2[i] == NULLQUARK || quarks2[i] != quarks1[i])
  132. break;
  133. /*
  134. * quarks for this level compare, now compare bindings
  135. */
  136. if (bindings1[i] != bindings2[i])
  137. break;
  138. i++;
  139. }
  140. if (quarks1[i] == NULLQUARK && quarks2[i] == NULLQUARK)
  141. {
  142. /*
  143. * all quarks and bindings in list1 and list2 compare
  144. */
  145. rc = True;
  146. }
  147. return(rc);
  148. }
  149. /*************************************<->*************************************
  150. *
  151. * _PrintDbEntry()
  152. *
  153. * Description:
  154. * -----------
  155. * Print an Xrm database entry (DEBUG only)
  156. *
  157. * Inputs:
  158. * ------
  159. * leader - leading string
  160. * bindings - binding list
  161. * quarks - quark list
  162. * type - element type
  163. * value - element value
  164. *
  165. * Outputs:
  166. * -------
  167. *
  168. * Return:
  169. * ------
  170. *
  171. * Comments:
  172. * --------
  173. *
  174. *************************************<->***********************************/
  175. #ifdef DEBUG
  176. static
  177. void _PrintDbEntry(
  178. char *leader,
  179. XrmBindingList bindings,
  180. XrmQuarkList quarks,
  181. XrmRepresentation *type,
  182. XrmValue * value)
  183. {
  184. char * str;
  185. int i;
  186. FILE *fp = fopen ("/tmp/dtsession.xrm", "a");
  187. str = XrmQuarkToString(type);
  188. fprintf(fp, "%8s ", leader);
  189. i = 0;
  190. while ( quarks[i] != NULLQUARK )
  191. {
  192. str = XrmQuarkToString(quarks[i]);
  193. fprintf(fp, "%s", str);
  194. i++;
  195. if (quarks[i] != NULLQUARK)
  196. {
  197. fprintf(fp, bindings[i] == XrmBindLoosely ? "*" : ".");
  198. }
  199. }
  200. fprintf(fp, ": %s\n",value->addr);
  201. fclose(fp);
  202. }
  203. #endif /* DEBUG */
  204. /*************************************<->*************************************
  205. *
  206. * _SmCompareSourceAndTarget()
  207. *
  208. * Description:
  209. * -----------
  210. * Xrm Enum callback that compares the current database element to the
  211. * current target element.
  212. *
  213. * Inputs:
  214. * ------
  215. * db - source database
  216. * bindings - binding list
  217. * quarks - quark list
  218. * type - element type
  219. * value - element value
  220. * closure - pointer to smSubtractState data
  221. *
  222. * Outputs:
  223. * -------
  224. *
  225. * Return:
  226. * ------
  227. * result - True if same, False if different
  228. *
  229. * Comments:
  230. * --------
  231. *
  232. *************************************<->***********************************/
  233. static
  234. Bool _SmCompareSourceAndTarget(
  235. XrmDatabase *db,
  236. XrmBindingList bindings,
  237. XrmQuarkList quarks,
  238. XrmRepresentation *type,
  239. XrmValue *value,
  240. XPointer closure)
  241. {
  242. struct smSubtractState *state = (struct smSubtractState *)closure;
  243. Bool rc = False;
  244. _PrintDbEntry("source", bindings, quarks, type, value);
  245. if (_CompareBindingQuarkList(bindings, quarks,
  246. state->target_bindings, state->target_quarks))
  247. {
  248. rc = True;
  249. }
  250. return rc;
  251. }
  252. /*************************************<->*************************************
  253. *
  254. * _SmEnumerateSource()
  255. *
  256. * Description:
  257. * -----------
  258. * Xrm Enum callback that enumerates the source database for comparison
  259. * against the current target element.
  260. *
  261. * Inputs:
  262. * ------
  263. * db - target database
  264. * bindings - binding list
  265. * quarks - quark list
  266. * type - element type
  267. * value - element value
  268. * closure - pointer to smSubtractState data
  269. *
  270. * Outputs:
  271. * -------
  272. *
  273. * Return:
  274. * ------
  275. * result - always False
  276. *
  277. * Comments:
  278. * --------
  279. *
  280. *************************************<->***********************************/
  281. static
  282. Bool _SmEnumerateSource(
  283. XrmDatabase *db,
  284. XrmBindingList bindings,
  285. XrmQuarkList quarks,
  286. XrmRepresentation *type,
  287. XrmValue *value,
  288. XPointer closure)
  289. {
  290. struct smSubtractState *state = (struct smSubtractState *)closure;
  291. Bool rc;
  292. _PrintDbEntry("target", bindings, quarks, type, value);
  293. /*
  294. * Enumerate source database and compare each element to current
  295. * target bindings and quarks.
  296. */
  297. state->target_bindings = bindings;
  298. state->target_quarks = quarks;
  299. if (XrmEnumerateDatabase(state->source_db, &empty, &empty, XrmEnumAllLevels,
  300. _SmCompareSourceAndTarget, closure) == False)
  301. {
  302. /*
  303. * Target bindings and quarks don't match any element in source database,
  304. * so copy target element to result db.
  305. */
  306. _PrintDbEntry("nomatch", bindings, quarks, type, value);
  307. XrmQPutResource(&state->result_db, bindings, quarks, *type, value);
  308. }
  309. return False;
  310. }
  311. /*************************************<->*************************************
  312. *
  313. * SmXrmSubtractDatabase()
  314. *
  315. * Description:
  316. * -----------
  317. * Subtracts source database from target database and returns
  318. * the result database.
  319. *
  320. * Inputs:
  321. * ------
  322. * source_db - source database
  323. * target_db - target database
  324. *
  325. * Outputs:
  326. * -------
  327. *
  328. * Return:
  329. * ------
  330. * result_db - result database
  331. *
  332. * Comments:
  333. * --------
  334. *
  335. *************************************<->***********************************/
  336. /*
  337. * Subtracts source database from target database and returns
  338. * the result database.
  339. */
  340. XrmDatabase
  341. SmXrmSubtractDatabase(
  342. XrmDatabase source_db,
  343. XrmDatabase target_db)
  344. {
  345. struct smSubtractState state;
  346. /*
  347. * return if source or target db not specified
  348. */
  349. if (source_db == NULL || target_db == NULL)
  350. return NULL;
  351. /*
  352. * set up state
  353. */
  354. state.source_db = source_db;
  355. state.target_db = target_db;
  356. state.result_db = NULL;
  357. /*
  358. * populate result db by looping through target and
  359. * copying elements that don't also exist in source db
  360. */
  361. XrmEnumerateDatabase(state.target_db, &empty, &empty, XrmEnumAllLevels,
  362. _SmEnumerateSource, (XPointer)&state);
  363. return(state.result_db);
  364. }