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