Fix bug #9221 with memory leak in bidi display.
[bpt/emacs.git] / src / xrdb.c
CommitLineData
f3a0bf5c 1/* Deal with the X Resource Manager.
73b0cd50 2 Copyright (C) 1990, 1993-1994, 2000-2011 Free Software Foundation, Inc.
f3a0bf5c 3
90324359
GM
4Author: Joseph Arceneaux
5Created: 4/90
6
3b7ad313
EN
7This file is part of GNU Emacs.
8
9ec0b715 9GNU Emacs is free software: you can redistribute it and/or modify
f3a0bf5c 10it under the terms of the GNU General Public License as published by
9ec0b715
GM
11the Free Software Foundation, either version 3 of the License, or
12(at your option) any later version.
f3a0bf5c 13
3b7ad313 14GNU Emacs is distributed in the hope that it will be useful,
f3a0bf5c
JB
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
9ec0b715 20along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
f3a0bf5c 21
18160b98 22#include <config.h>
a2a4d43e 23
dfcf069d 24#include <unistd.h>
42a2c622 25#include <errno.h>
57bda87a 26#include <epaths.h>
012c3e71 27
837255fb 28#include <stdio.h>
d7306fe6 29#include <setjmp.h>
837255fb 30
b4716021
PE
31#include "lisp.h"
32
33/* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35#include "xterm.h"
36
f3a0bf5c
JB
37#include <X11/Xlib.h>
38#include <X11/Xatom.h>
f3a0bf5c
JB
39#include <X11/X.h>
40#include <X11/Xutil.h>
41#include <X11/Xresource.h>
5b9c0a1d 42#ifdef HAVE_PWD_H
f3a0bf5c 43#include <pwd.h>
531ff254 44#endif
f3a0bf5c
JB
45#include <sys/stat.h>
46
dee186b6
J
47#ifdef USE_MOTIF
48/* For Vdouble_click_time. */
49#include "keyboard.h"
50#endif
51
971de7fb 52extern char *getenv (const char *);
3d5d61ae 53
3e0be4d0
RS
54extern struct passwd *getpwuid (uid_t);
55extern struct passwd *getpwnam (const char *);
f3a0bf5c 56
eec47d6b
DN
57char *x_get_string_resource (XrmDatabase rdb, const char *name,
58 const char *class);
59static int file_p (const char *filename);
837255fb
JB
60
61\f
62/* X file search path processing. */
63
64
65/* The string which gets substituted for the %C escape in XFILESEARCHPATH
66 and friends, or zero if none was specified. */
ac64929e 67static char *x_customization_string;
837255fb
JB
68
69
70/* Return the value of the emacs.customization (Emacs.Customization)
71 resource, for later use in search path decoding. If we find no
72 such resource, return zero. */
2f7c71a1
AS
73static char *
74x_get_customization_string (XrmDatabase db, const char *name,
75 const char *class)
837255fb
JB
76{
77 char *full_name
78 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
79 char *full_class
80 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
81 char *result;
82
83 sprintf (full_name, "%s.%s", name, "customization");
84 sprintf (full_class, "%s.%s", class, "Customization");
85
86 result = x_get_string_resource (db, full_name, full_class);
87
88 if (result)
abfc2e5f 89 {
1b6d8cf0 90 char *copy = (char *) xmalloc (strlen (result) + 1);
abfc2e5f
RS
91 strcpy (copy, result);
92 return copy;
93 }
837255fb
JB
94 else
95 return 0;
96}
97
98
99/* Expand all the Xt-style %-escapes in STRING, whose length is given
100 by STRING_LEN. Here are the escapes we're supposed to recognize:
101
102 %N The value of the application's class name
103 %T The value of the type parameter ("app-defaults" in this
104 context)
105 %S The value of the suffix parameter ("" in this context)
106 %L The language string associated with the specified display
107 (We use the "LANG" environment variable here, if it's set.)
108 %l The language part of the display's language string
109 (We treat this just like %L. If someone can tell us what
110 we're really supposed to do, dandy.)
111 %t The territory part of the display's language string
112 (This never gets used.)
113 %c The codeset part of the display's language string
114 (This never gets used either.)
115 %C The customization string retrieved from the resource
116 database associated with display.
117 (This is x_customization_string.)
118
119 Return the expanded file name if it exists and is readable, and
120 refers to %L only when the LANG environment variable is set, or
121 otherwise provided by X.
122
28154962
PE
123 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
124 %-escapes in ESCAPED_SUFFIX are expanded.
837255fb
JB
125
126 Return NULL otherwise. */
127
128static char *
28154962
PE
129magic_file_p (const char *string, EMACS_INT string_len, const char *class,
130 const char *escaped_suffix)
837255fb
JB
131{
132 char *lang = getenv ("LANG");
133
28154962 134 ptrdiff_t path_size = 100;
1b6d8cf0 135 char *path = (char *) xmalloc (path_size);
28154962 136 ptrdiff_t path_len = 0;
837255fb 137
eec47d6b 138 const char *p = string;
837255fb
JB
139
140 while (p < string + string_len)
141 {
142 /* The chunk we're about to stick on the end of result. */
eec47d6b 143 const char *next = NULL;
28154962 144 ptrdiff_t next_len;
837255fb
JB
145
146 if (*p == '%')
147 {
148 p++;
149
150 if (p >= string + string_len)
151 next_len = 0;
152 else
153 switch (*p)
154 {
155 case '%':
156 next = "%";
157 next_len = 1;
158 break;
159
160 case 'C':
161 next = (x_customization_string
162 ? x_customization_string
163 : "");
164 next_len = strlen (next);
165 break;
166
167 case 'N':
168 next = class;
169 next_len = strlen (class);
170 break;
171
172 case 'T':
173 next = "app-defaults";
174 next_len = strlen (next);
175 break;
176
177 default:
178 case 'S':
179 next_len = 0;
180 break;
181
182 case 'L':
183 case 'l':
184 if (! lang)
185 {
1b6d8cf0 186 xfree (path);
837255fb
JB
187 return NULL;
188 }
427ec082 189
837255fb
JB
190 next = lang;
191 next_len = strlen (next);
192 break;
427ec082 193
837255fb
JB
194 case 't':
195 case 'c':
1b6d8cf0 196 xfree (path);
837255fb
JB
197 return NULL;
198 }
199 }
200 else
201 next = p, next_len = 1;
202
203 /* Do we have room for this component followed by a '\0' ? */
28154962 204 if (path_size - path_len <= next_len)
837255fb 205 {
28154962
PE
206 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
207 memory_full (SIZE_MAX);
837255fb 208 path_size = (path_len + next_len + 1) * 2;
1b6d8cf0 209 path = (char *) xrealloc (path, path_size);
837255fb 210 }
427ec082 211
72af86bd 212 memcpy (path + path_len, next, next_len);
837255fb
JB
213 path_len += next_len;
214
215 p++;
216
217 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
218 if (p >= string + string_len && escaped_suffix)
219 {
220 string = escaped_suffix;
221 string_len = strlen (string);
222 p = string;
223 escaped_suffix = NULL;
224 }
225 }
226
837255fb
JB
227 path[path_len] = '\0';
228
229 if (! file_p (path))
230 {
1b6d8cf0 231 xfree (path);
837255fb
JB
232 return NULL;
233 }
234
235 return path;
236}
237
238
f3a0bf5c 239static char *
971de7fb 240gethomedir (void)
f3a0bf5c 241{
f3a0bf5c
JB
242 struct passwd *pw;
243 char *ptr;
837255fb 244 char *copy;
f3a0bf5c
JB
245
246 if ((ptr = getenv ("HOME")) == NULL)
247 {
1cac1f6f
KH
248 if ((ptr = getenv ("LOGNAME")) != NULL
249 || (ptr = getenv ("USER")) != NULL)
f3a0bf5c
JB
250 pw = getpwnam (ptr);
251 else
1cac1f6f 252 pw = getpwuid (getuid ());
837255fb 253
f3a0bf5c
JB
254 if (pw)
255 ptr = pw->pw_dir;
f3a0bf5c
JB
256 }
257
427ec082 258 if (ptr == NULL)
8fd0f424 259 return xstrdup ("/");
f3a0bf5c 260
1b6d8cf0 261 copy = (char *) xmalloc (strlen (ptr) + 2);
837255fb
JB
262 strcpy (copy, ptr);
263 strcat (copy, "/");
f3a0bf5c 264
837255fb 265 return copy;
f3a0bf5c
JB
266}
267
837255fb 268
f3a0bf5c 269static int
eec47d6b 270file_p (const char *filename)
f3a0bf5c
JB
271{
272 struct stat status;
273
aa7e4660
RS
274 return (access (filename, 4) == 0 /* exists and is readable */
275 && stat (filename, &status) == 0 /* get the status */
0f2cd61f 276 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
f3a0bf5c
JB
277}
278
f3a0bf5c 279
837255fb 280/* Find the first element of SEARCH_PATH which exists and is readable,
427ec082 281 after expanding the %-escapes. Return 0 if we didn't find any, and
837255fb 282 the path name of the one we found otherwise. */
f3a0bf5c 283
837255fb 284static char *
28154962
PE
285search_magic_path (const char *search_path, const char *class,
286 const char *escaped_suffix)
f3a0bf5c 287{
eec47d6b 288 const char *s, *p;
d4327fec 289
837255fb 290 for (s = search_path; *s; s = p)
f3a0bf5c 291 {
837255fb
JB
292 for (p = s; *p && *p != ':'; p++)
293 ;
427ec082 294
0f2cd61f 295 if (p > s)
f3a0bf5c 296 {
28154962 297 char *path = magic_file_p (s, p - s, class, escaped_suffix);
837255fb
JB
298 if (path)
299 return path;
f3a0bf5c 300 }
0f2cd61f 301 else if (*p == ':')
f3a0bf5c 302 {
0f2cd61f
RS
303 char *path;
304
305 s = "%N%S";
28154962 306 path = magic_file_p (s, strlen (s), class, escaped_suffix);
837255fb
JB
307 if (path)
308 return path;
f3a0bf5c
JB
309 }
310
837255fb
JB
311 if (*p == ':')
312 p++;
f3a0bf5c
JB
313 }
314
315 return 0;
316}
317\f
837255fb
JB
318/* Producing databases for individual sources. */
319
f3a0bf5c 320static XrmDatabase
eec47d6b 321get_system_app (const char *class)
f3a0bf5c 322{
837255fb 323 XrmDatabase db = NULL;
42ca4633
J
324 const char *path;
325 char *p;
f3a0bf5c 326
837255fb 327 path = getenv ("XFILESEARCHPATH");
58c7da0c 328 if (! path) path = PATH_X_DEFAULTS;
f3a0bf5c 329
28154962 330 p = search_magic_path (path, class, 0);
42ca4633 331 if (p)
837255fb 332 {
42ca4633
J
333 db = XrmGetFileDatabase (p);
334 xfree (p);
837255fb 335 }
f3a0bf5c 336
f3a0bf5c
JB
337 return db;
338}
339
837255fb 340
f3a0bf5c 341static XrmDatabase
971de7fb 342get_fallback (Display *display)
f3a0bf5c 343{
f3a0bf5c
JB
344 return NULL;
345}
346
837255fb 347
f3a0bf5c 348static XrmDatabase
eec47d6b 349get_user_app (const char *class)
f3a0bf5c 350{
42ca4633 351 const char *path;
837255fb 352 char *file = 0;
3fa18fb2 353 char *free_it = 0;
837255fb
JB
354
355 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
356 names, not directories. */
357 if (((path = getenv ("XUSERFILESEARCHPATH"))
28154962 358 && (file = search_magic_path (path, class, 0)))
837255fb
JB
359
360 /* Check for APPLRESDIR; it is a path of directories. In each,
361 we have to search for LANG/CLASS and then CLASS. */
362 || ((path = getenv ("XAPPLRESDIR"))
28154962
PE
363 && ((file = search_magic_path (path, class, "/%L/%N"))
364 || (file = search_magic_path (path, class, "/%N"))))
427ec082 365
837255fb
JB
366 /* Check in the home directory. This is a bit of a hack; let's
367 hope one's home directory doesn't contain any %-escapes. */
3fa18fb2 368 || (free_it = gethomedir (),
28154962
PE
369 ((file = search_magic_path (free_it, class, "%L/%N"))
370 || (file = search_magic_path (free_it, class, "%N")))))
f3a0bf5c 371 {
837255fb 372 XrmDatabase db = XrmGetFileDatabase (file);
1b6d8cf0
DN
373 xfree (file);
374 xfree (free_it);
837255fb 375 return db;
f3a0bf5c 376 }
3fa18fb2 377
1b6d8cf0 378 xfree (free_it);
3fa18fb2 379 return NULL;
f3a0bf5c
JB
380}
381
837255fb 382
f3a0bf5c 383static XrmDatabase
971de7fb 384get_user_db (Display *display)
f3a0bf5c
JB
385{
386 XrmDatabase db;
387 char *xdefs;
388
2c8d1900 389#ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
b631f177
JB
390 xdefs = XResourceManagerString (display);
391#else
a2a4d43e 392 xdefs = display->xdefaults;
b631f177
JB
393#endif
394
f3a0bf5c
JB
395 if (xdefs != NULL)
396 db = XrmGetStringDatabase (xdefs);
397 else
398 {
837255fb
JB
399 char *home;
400 char *xdefault;
f3a0bf5c 401
837255fb 402 home = gethomedir ();
1b6d8cf0 403 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
837255fb 404 strcpy (xdefault, home);
f3a0bf5c
JB
405 strcat (xdefault, ".Xdefaults");
406 db = XrmGetFileDatabase (xdefault);
1b6d8cf0
DN
407 xfree (home);
408 xfree (xdefault);
f3a0bf5c
JB
409 }
410
d8717d15 411#ifdef HAVE_XSCREENRESOURCESTRING
9b37b1c2
JB
412 /* Get the screen-specific resources too. */
413 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
414 if (xdefs != NULL)
fdce0b39
JB
415 {
416 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
417 XFree (xdefs);
418 }
9b37b1c2
JB
419#endif
420
f3a0bf5c
JB
421 return db;
422}
423
424static XrmDatabase
971de7fb 425get_environ_db (void)
f3a0bf5c
JB
426{
427 XrmDatabase db;
428 char *p;
8ea90aa3
DN
429 char *path = 0, *home = 0;
430 const char *host;
f3a0bf5c
JB
431
432 if ((p = getenv ("XENVIRONMENT")) == NULL)
433 {
837255fb 434 home = gethomedir ();
e29ad342 435 host = get_system_name ();
1b6d8cf0 436 path = (char *) xmalloc (strlen (home)
837255fb
JB
437 + sizeof (".Xdefaults-")
438 + strlen (host));
439 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
f3a0bf5c
JB
440 p = path;
441 }
442
443 db = XrmGetFileDatabase (p);
837255fb 444
1b6d8cf0
DN
445 xfree (path);
446 xfree (home);
837255fb 447
f3a0bf5c
JB
448 return db;
449}
450\f
837255fb
JB
451/* External interface. */
452
f3a0bf5c
JB
453/* Types of values that we can find in a database */
454
455#define XrmStringType "String" /* String representation */
ac64929e 456static XrmRepresentation x_rm_string; /* Quark representation */
f3a0bf5c
JB
457
458/* Load X resources based on the display and a possible -xrm option. */
459
460XrmDatabase
eec47d6b
DN
461x_load_resources (Display *display, const char *xrm_string,
462 const char *myname, const char *myclass)
f3a0bf5c 463{
837255fb 464 XrmDatabase user_database;
f3a0bf5c
JB
465 XrmDatabase rdb;
466 XrmDatabase db;
4da4f201 467 char line[256];
6ca30ba4 468
72391843 469#if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
3565b346 470 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
72391843
PE
471#endif
472
473#ifdef USE_MOTIF
eec47d6b 474 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
8d56e206 475#endif
f3a0bf5c
JB
476
477 x_rm_string = XrmStringToQuark (XrmStringType);
082dc211
RS
478#ifndef USE_X_TOOLKIT
479 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
480 I suspect it's because the toolkit version does this elsewhere. */
f3a0bf5c 481 XrmInitialize ();
082dc211 482#endif
f3a0bf5c
JB
483 rdb = XrmGetStringDatabase ("");
484
4da4f201
GM
485 /* Add some font defaults. If the font `helv' doesn't exist, widgets
486 will use some other default font. */
487#ifdef USE_MOTIF
427ec082 488
cf9b4b0b 489 sprintf (line, "%s.pane.background: grey75", myclass);
06bbf618 490 XrmPutLineResource (&rdb, line);
cf9b4b0b 491 sprintf (line, "%s*fontList: %s", myclass, helv);
4da4f201 492 XrmPutLineResource (&rdb, line);
cf9b4b0b 493 sprintf (line, "%s*menu*background: grey75", myclass);
4da4f201 494 XrmPutLineResource (&rdb, line);
b6a15240 495 sprintf (line, "%s*menubar*background: grey75", myclass);
4da4f201 496 XrmPutLineResource (&rdb, line);
cf9b4b0b 497 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
4da4f201 498 XrmPutLineResource (&rdb, line);
cf9b4b0b 499 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
06bbf618 500 XrmPutLineResource (&rdb, line);
cf9b4b0b 501 sprintf (line, "%s.dialog*.background: grey75", myclass);
4da4f201 502 XrmPutLineResource (&rdb, line);
cf9b4b0b 503 sprintf (line, "%s*fsb.Text.background: white", myclass);
4da4f201 504 XrmPutLineResource (&rdb, line);
cf9b4b0b 505 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
4da4f201 506 XrmPutLineResource (&rdb, line);
cf9b4b0b 507 sprintf (line, "%s*fsb*DirList.background: white", myclass);
4da4f201 508 XrmPutLineResource (&rdb, line);
cf9b4b0b 509 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
4da4f201 510 XrmPutLineResource (&rdb, line);
cf9b4b0b 511 sprintf (line, "%s*fsb*background: grey75", myclass);
4da4f201 512 XrmPutLineResource (&rdb, line);
cf9b4b0b 513 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
4da4f201 514 XrmPutLineResource (&rdb, line);
cf9b4b0b 515 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
4da4f201 516 XrmPutLineResource (&rdb, line);
cf9b4b0b 517 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
4da4f201 518 XrmPutLineResource (&rdb, line);
cf9b4b0b 519 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
4da4f201
GM
520 XrmPutLineResource (&rdb, line);
521
522 /* Set double click time of list boxes in the file selection
523 dialog from `double-click-time'. */
524 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
525 {
c2982e87 526 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
cf9b4b0b 527 myclass, XFASTINT (Vdouble_click_time));
4da4f201 528 XrmPutLineResource (&rdb, line);
c2982e87 529 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
cf9b4b0b 530 myclass, XFASTINT (Vdouble_click_time));
4da4f201
GM
531 XrmPutLineResource (&rdb, line);
532 }
533
534#else /* not USE_MOTIF */
427ec082 535
fc6c5dfe 536 sprintf (line, "Emacs.dialog*.background: grey75");
4da4f201 537 XrmPutLineResource (&rdb, line);
3928f2b6
JD
538#if !defined (HAVE_XFT) || !defined (USE_LUCID)
539 sprintf (line, "Emacs.dialog*.font: %s", helv);
540 XrmPutLineResource (&rdb, line);
fc6c5dfe 541 sprintf (line, "*XlwMenu*font: %s", helv);
4da4f201 542 XrmPutLineResource (&rdb, line);
3928f2b6 543#endif
fc6c5dfe 544 sprintf (line, "*XlwMenu*background: grey75");
4da4f201 545 XrmPutLineResource (&rdb, line);
fc6c5dfe 546 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
4da4f201 547 XrmPutLineResource (&rdb, line);
427ec082 548
4da4f201
GM
549#endif /* not USE_MOTIF */
550
837255fb
JB
551 user_database = get_user_db (display);
552
553 /* Figure out what the "customization string" is, so we can use it
554 to decode paths. */
1b6d8cf0 555 xfree (x_customization_string);
837255fb
JB
556 x_customization_string
557 = x_get_customization_string (user_database, myname, myclass);
558
f3a0bf5c
JB
559 /* Get application system defaults */
560 db = get_system_app (myclass);
561 if (db != NULL)
562 XrmMergeDatabases (db, &rdb);
563
564 /* Get Fallback resources */
565 db = get_fallback (display);
566 if (db != NULL)
567 XrmMergeDatabases (db, &rdb);
568
569 /* Get application user defaults */
570 db = get_user_app (myclass);
571 if (db != NULL)
572 XrmMergeDatabases (db, &rdb);
573
574 /* get User defaults */
837255fb
JB
575 if (user_database != NULL)
576 XrmMergeDatabases (user_database, &rdb);
f3a0bf5c
JB
577
578 /* Get Environment defaults. */
579 db = get_environ_db ();
580 if (db != NULL)
581 XrmMergeDatabases (db, &rdb);
427ec082 582
f3a0bf5c
JB
583 /* Last, merge in any specification from the command line. */
584 if (xrm_string != NULL)
585 {
586 db = XrmGetStringDatabase (xrm_string);
587 if (db != NULL)
588 XrmMergeDatabases (db, &rdb);
589 }
590
591 return rdb;
592}
593
837255fb 594
f3a0bf5c
JB
595/* Retrieve the value of the resource specified by NAME with class CLASS
596 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
597
2f7c71a1
AS
598static int
599x_get_resource (XrmDatabase rdb, const char *name, const char *class,
600 XrmRepresentation expected_type, XrmValue *ret_value)
f3a0bf5c
JB
601{
602 XrmValue value;
603 XrmName namelist[100];
604 XrmClass classlist[100];
605 XrmRepresentation type;
606
607 XrmStringToNameList(name, namelist);
608 XrmStringToClassList(class, classlist);
609
610 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
611 && (type == expected_type))
612 {
613 if (type == x_rm_string)
41ab0754 614 ret_value->addr = (char *) value.addr;
f3a0bf5c 615 else
72af86bd 616 memcpy (ret_value->addr, value.addr, ret_value->size);
f3a0bf5c
JB
617
618 return value.size;
619 }
620
621 return 0;
622}
623
624/* Retrieve the string resource specified by NAME with CLASS from
625 database RDB. */
626
627char *
eec47d6b 628x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
f3a0bf5c
JB
629{
630 XrmValue value;
631
8686ac71
JB
632 if (inhibit_x_resources)
633 /* --quick was passed, so this is a no-op. */
634 return NULL;
635
f3a0bf5c
JB
636 if (x_get_resource (rdb, name, class, x_rm_string, &value))
637 return (char *) value.addr;
638
639 return (char *) 0;
640}
641\f
837255fb
JB
642/* Stand-alone test facilities. */
643
f3a0bf5c 644#ifdef TESTRM
837255fb
JB
645
646typedef char **List;
647#define arg_listify(len, list) (list)
648#define car(list) (*(list))
649#define cdr(list) (list + 1)
650#define NIL(list) (! *(list))
651#define free_arglist(list)
652
653static List
1dae0f0a 654member (char *elt, List list)
837255fb
JB
655{
656 List p;
657
658 for (p = list; ! NIL (p); p = cdr (p))
659 if (! strcmp (elt, car (p)))
660 return p;
661
662 return p;
663}
f3a0bf5c
JB
664
665static void
1dae0f0a 666fatal (char *msg, char *prog)
f3a0bf5c 667{
1dae0f0a
AS
668 if (errno)
669 perror (prog);
f3a0bf5c 670
1dae0f0a
AS
671 (void) fprintf (stderr, msg, prog);
672 exit (1);
f3a0bf5c
JB
673}
674
1dae0f0a
AS
675int
676main (int argc, char **argv)
f3a0bf5c
JB
677{
678 Display *display;
837255fb 679 char *displayname, *resource_string, *class, *name;
f3a0bf5c 680 XrmDatabase xdb;
837255fb 681 List arg_list, lp;
f3a0bf5c
JB
682
683 arg_list = arg_listify (argc, argv);
684
685 lp = member ("-d", arg_list);
686 if (!NIL (lp))
687 displayname = car (cdr (lp));
688 else
689 displayname = "localhost:0.0";
690
691 lp = member ("-xrm", arg_list);
692 if (! NIL (lp))
693 resource_string = car (cdr (lp));
694 else
695 resource_string = (char *) 0;
696
697 lp = member ("-c", arg_list);
698 if (! NIL (lp))
699 class = car (cdr (lp));
700 else
701 class = "Emacs";
702
837255fb
JB
703 lp = member ("-n", arg_list);
704 if (! NIL (lp))
705 name = car (cdr (lp));
706 else
707 name = "emacs";
f3a0bf5c 708
837255fb 709 free_arglist (arg_list);
f3a0bf5c
JB
710
711 if (!(display = XOpenDisplay (displayname)))
712 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
713
837255fb 714 xdb = x_load_resources (display, resource_string, name, class);
f3a0bf5c 715
f3a0bf5c
JB
716 /* In a real program, you'd want to also do this: */
717 display->db = xdb;
f3a0bf5c
JB
718
719 while (1)
720 {
837255fb
JB
721 char query_name[90];
722 char query_class[90];
723
724 printf ("Name: ");
725 gets (query_name);
f3a0bf5c 726
837255fb 727 if (strlen (query_name))
f3a0bf5c 728 {
837255fb
JB
729 char *value;
730
731 printf ("Class: ");
732 gets (query_class);
733
734 value = x_get_string_resource (xdb, query_name, query_class);
f3a0bf5c
JB
735
736 if (value != NULL)
837255fb 737 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
f3a0bf5c
JB
738 else
739 printf ("\tNo Value.\n\n");
740 }
741 else
742 break;
743 }
744 printf ("\tExit.\n\n");
745
746 XCloseDisplay (display);
1dae0f0a
AS
747
748 return 0;
f3a0bf5c
JB
749}
750#endif /* TESTRM */