Move DONT_REOPEN_PTY from src/s to configure
[bpt/emacs.git] / src / xrdb.c
CommitLineData
f3a0bf5c 1/* Deal with the X Resource Manager.
acaf905b 2 Copyright (C) 1990, 1993-1994, 2000-2012 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 76{
38182d90
PE
77 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
78 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
837255fb
JB
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 {
23f86fce 88 char *copy = 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
28154962
PE
121 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
122 %-escapes in ESCAPED_SUFFIX are expanded.
837255fb
JB
123
124 Return NULL otherwise. */
125
126static char *
d311d28c 127magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
28154962 128 const char *escaped_suffix)
837255fb
JB
129{
130 char *lang = getenv ("LANG");
131
28154962 132 ptrdiff_t path_size = 100;
23f86fce 133 char *path = xmalloc (path_size);
28154962 134 ptrdiff_t path_len = 0;
837255fb 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;
28154962 142 ptrdiff_t next_len;
837255fb
JB
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' ? */
28154962 202 if (path_size - path_len <= next_len)
837255fb 203 {
28154962 204 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
0065d054 205 memory_full (SIZE_MAX);
837255fb 206 path_size = (path_len + next_len + 1) * 2;
38182d90 207 path = xrealloc (path, path_size);
837255fb 208 }
427ec082 209
72af86bd 210 memcpy (path + path_len, next, next_len);
837255fb
JB
211 path_len += next_len;
212
213 p++;
214
215 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
216 if (p >= string + string_len && escaped_suffix)
217 {
218 string = escaped_suffix;
219 string_len = strlen (string);
220 p = string;
221 escaped_suffix = NULL;
222 }
223 }
224
837255fb
JB
225 path[path_len] = '\0';
226
227 if (! file_p (path))
228 {
1b6d8cf0 229 xfree (path);
837255fb
JB
230 return NULL;
231 }
232
233 return path;
234}
235
236
f3a0bf5c 237static char *
971de7fb 238gethomedir (void)
f3a0bf5c 239{
f3a0bf5c
JB
240 struct passwd *pw;
241 char *ptr;
837255fb 242 char *copy;
f3a0bf5c
JB
243
244 if ((ptr = getenv ("HOME")) == NULL)
245 {
1cac1f6f
KH
246 if ((ptr = getenv ("LOGNAME")) != NULL
247 || (ptr = getenv ("USER")) != NULL)
f3a0bf5c
JB
248 pw = getpwnam (ptr);
249 else
1cac1f6f 250 pw = getpwuid (getuid ());
837255fb 251
f3a0bf5c
JB
252 if (pw)
253 ptr = pw->pw_dir;
f3a0bf5c
JB
254 }
255
427ec082 256 if (ptr == NULL)
8fd0f424 257 return xstrdup ("/");
f3a0bf5c 258
23f86fce 259 copy = xmalloc (strlen (ptr) + 2);
837255fb
JB
260 strcpy (copy, ptr);
261 strcat (copy, "/");
f3a0bf5c 262
837255fb 263 return copy;
f3a0bf5c
JB
264}
265
837255fb 266
f3a0bf5c 267static int
eec47d6b 268file_p (const char *filename)
f3a0bf5c
JB
269{
270 struct stat status;
271
aa7e4660
RS
272 return (access (filename, 4) == 0 /* exists and is readable */
273 && stat (filename, &status) == 0 /* get the status */
0f2cd61f 274 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
f3a0bf5c
JB
275}
276
f3a0bf5c 277
837255fb 278/* Find the first element of SEARCH_PATH which exists and is readable,
427ec082 279 after expanding the %-escapes. Return 0 if we didn't find any, and
837255fb 280 the path name of the one we found otherwise. */
f3a0bf5c 281
837255fb 282static char *
28154962
PE
283search_magic_path (const char *search_path, const char *class,
284 const char *escaped_suffix)
f3a0bf5c 285{
eec47d6b 286 const char *s, *p;
d4327fec 287
837255fb 288 for (s = search_path; *s; s = p)
f3a0bf5c 289 {
837255fb
JB
290 for (p = s; *p && *p != ':'; p++)
291 ;
427ec082 292
0f2cd61f 293 if (p > s)
f3a0bf5c 294 {
28154962 295 char *path = magic_file_p (s, p - s, class, escaped_suffix);
837255fb
JB
296 if (path)
297 return path;
f3a0bf5c 298 }
0f2cd61f 299 else if (*p == ':')
f3a0bf5c 300 {
0f2cd61f
RS
301 char *path;
302
303 s = "%N%S";
28154962 304 path = magic_file_p (s, strlen (s), class, escaped_suffix);
837255fb
JB
305 if (path)
306 return path;
f3a0bf5c
JB
307 }
308
837255fb
JB
309 if (*p == ':')
310 p++;
f3a0bf5c
JB
311 }
312
313 return 0;
314}
315\f
837255fb
JB
316/* Producing databases for individual sources. */
317
f3a0bf5c 318static XrmDatabase
eec47d6b 319get_system_app (const char *class)
f3a0bf5c 320{
837255fb 321 XrmDatabase db = NULL;
42ca4633
J
322 const char *path;
323 char *p;
f3a0bf5c 324
837255fb 325 path = getenv ("XFILESEARCHPATH");
58c7da0c 326 if (! path) path = PATH_X_DEFAULTS;
f3a0bf5c 327
28154962 328 p = search_magic_path (path, class, 0);
42ca4633 329 if (p)
837255fb 330 {
42ca4633
J
331 db = XrmGetFileDatabase (p);
332 xfree (p);
837255fb 333 }
f3a0bf5c 334
f3a0bf5c
JB
335 return db;
336}
337
837255fb 338
f3a0bf5c 339static XrmDatabase
971de7fb 340get_fallback (Display *display)
f3a0bf5c 341{
f3a0bf5c
JB
342 return NULL;
343}
344
837255fb 345
f3a0bf5c 346static XrmDatabase
eec47d6b 347get_user_app (const char *class)
f3a0bf5c 348{
42ca4633 349 const char *path;
837255fb 350 char *file = 0;
3fa18fb2 351 char *free_it = 0;
837255fb
JB
352
353 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
354 names, not directories. */
355 if (((path = getenv ("XUSERFILESEARCHPATH"))
28154962 356 && (file = search_magic_path (path, class, 0)))
837255fb
JB
357
358 /* Check for APPLRESDIR; it is a path of directories. In each,
359 we have to search for LANG/CLASS and then CLASS. */
360 || ((path = getenv ("XAPPLRESDIR"))
28154962
PE
361 && ((file = search_magic_path (path, class, "/%L/%N"))
362 || (file = search_magic_path (path, class, "/%N"))))
427ec082 363
837255fb
JB
364 /* Check in the home directory. This is a bit of a hack; let's
365 hope one's home directory doesn't contain any %-escapes. */
3fa18fb2 366 || (free_it = gethomedir (),
28154962
PE
367 ((file = search_magic_path (free_it, class, "%L/%N"))
368 || (file = search_magic_path (free_it, class, "%N")))))
f3a0bf5c 369 {
837255fb 370 XrmDatabase db = XrmGetFileDatabase (file);
1b6d8cf0
DN
371 xfree (file);
372 xfree (free_it);
837255fb 373 return db;
f3a0bf5c 374 }
3fa18fb2 375
1b6d8cf0 376 xfree (free_it);
3fa18fb2 377 return NULL;
f3a0bf5c
JB
378}
379
837255fb 380
f3a0bf5c 381static XrmDatabase
971de7fb 382get_user_db (Display *display)
f3a0bf5c
JB
383{
384 XrmDatabase db;
385 char *xdefs;
386
2c8d1900 387#ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
b631f177
JB
388 xdefs = XResourceManagerString (display);
389#else
a2a4d43e 390 xdefs = display->xdefaults;
b631f177
JB
391#endif
392
f3a0bf5c
JB
393 if (xdefs != NULL)
394 db = XrmGetStringDatabase (xdefs);
395 else
396 {
837255fb
JB
397 char *home;
398 char *xdefault;
f3a0bf5c 399
837255fb 400 home = gethomedir ();
38182d90 401 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
837255fb 402 strcpy (xdefault, home);
f3a0bf5c
JB
403 strcat (xdefault, ".Xdefaults");
404 db = XrmGetFileDatabase (xdefault);
1b6d8cf0
DN
405 xfree (home);
406 xfree (xdefault);
f3a0bf5c
JB
407 }
408
d8717d15 409#ifdef HAVE_XSCREENRESOURCESTRING
9b37b1c2
JB
410 /* Get the screen-specific resources too. */
411 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
412 if (xdefs != NULL)
fdce0b39
JB
413 {
414 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
415 XFree (xdefs);
416 }
9b37b1c2
JB
417#endif
418
f3a0bf5c
JB
419 return db;
420}
421
422static XrmDatabase
971de7fb 423get_environ_db (void)
f3a0bf5c
JB
424{
425 XrmDatabase db;
426 char *p;
a3d9c2a4 427 char *path = 0;
f3a0bf5c
JB
428
429 if ((p = getenv ("XENVIRONMENT")) == NULL)
430 {
a3d9c2a4
PE
431 static char const xdefaults[] = ".Xdefaults-";
432 char *home = gethomedir ();
433 char const *host = get_system_name ();
434 ptrdiff_t pathsize = strlen (home) + sizeof xdefaults + strlen (host);
38182d90 435 path = xrealloc (home, pathsize);
a3d9c2a4 436 strcat (strcat (path, xdefaults), host);
f3a0bf5c
JB
437 p = path;
438 }
439
440 db = XrmGetFileDatabase (p);
837255fb 441
1b6d8cf0 442 xfree (path);
837255fb 443
f3a0bf5c
JB
444 return db;
445}
446\f
837255fb
JB
447/* External interface. */
448
f3a0bf5c
JB
449/* Types of values that we can find in a database */
450
451#define XrmStringType "String" /* String representation */
ac64929e 452static XrmRepresentation x_rm_string; /* Quark representation */
f3a0bf5c
JB
453
454/* Load X resources based on the display and a possible -xrm option. */
455
456XrmDatabase
eec47d6b
DN
457x_load_resources (Display *display, const char *xrm_string,
458 const char *myname, const char *myclass)
f3a0bf5c 459{
837255fb 460 XrmDatabase user_database;
f3a0bf5c
JB
461 XrmDatabase rdb;
462 XrmDatabase db;
4da4f201 463 char line[256];
6ca30ba4 464
72391843 465#if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
3565b346 466 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
72391843
PE
467#endif
468
469#ifdef USE_MOTIF
eec47d6b 470 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
8d56e206 471#endif
f3a0bf5c
JB
472
473 x_rm_string = XrmStringToQuark (XrmStringType);
082dc211
RS
474#ifndef USE_X_TOOLKIT
475 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
476 I suspect it's because the toolkit version does this elsewhere. */
f3a0bf5c 477 XrmInitialize ();
082dc211 478#endif
f3a0bf5c
JB
479 rdb = XrmGetStringDatabase ("");
480
4da4f201
GM
481 /* Add some font defaults. If the font `helv' doesn't exist, widgets
482 will use some other default font. */
483#ifdef USE_MOTIF
427ec082 484
cf9b4b0b 485 sprintf (line, "%s.pane.background: grey75", myclass);
06bbf618 486 XrmPutLineResource (&rdb, line);
cf9b4b0b 487 sprintf (line, "%s*fontList: %s", myclass, helv);
4da4f201 488 XrmPutLineResource (&rdb, line);
cf9b4b0b 489 sprintf (line, "%s*menu*background: grey75", myclass);
4da4f201 490 XrmPutLineResource (&rdb, line);
b6a15240 491 sprintf (line, "%s*menubar*background: grey75", myclass);
4da4f201 492 XrmPutLineResource (&rdb, line);
cf9b4b0b 493 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
4da4f201 494 XrmPutLineResource (&rdb, line);
cf9b4b0b 495 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
06bbf618 496 XrmPutLineResource (&rdb, line);
cf9b4b0b 497 sprintf (line, "%s.dialog*.background: grey75", myclass);
4da4f201 498 XrmPutLineResource (&rdb, line);
cf9b4b0b 499 sprintf (line, "%s*fsb.Text.background: white", myclass);
4da4f201 500 XrmPutLineResource (&rdb, line);
cf9b4b0b 501 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
4da4f201 502 XrmPutLineResource (&rdb, line);
cf9b4b0b 503 sprintf (line, "%s*fsb*DirList.background: white", myclass);
4da4f201 504 XrmPutLineResource (&rdb, line);
cf9b4b0b 505 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
4da4f201 506 XrmPutLineResource (&rdb, line);
cf9b4b0b 507 sprintf (line, "%s*fsb*background: grey75", myclass);
4da4f201 508 XrmPutLineResource (&rdb, line);
cf9b4b0b 509 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
4da4f201 510 XrmPutLineResource (&rdb, line);
cf9b4b0b 511 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
4da4f201 512 XrmPutLineResource (&rdb, line);
cf9b4b0b 513 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
4da4f201 514 XrmPutLineResource (&rdb, line);
cf9b4b0b 515 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
4da4f201
GM
516 XrmPutLineResource (&rdb, line);
517
518 /* Set double click time of list boxes in the file selection
519 dialog from `double-click-time'. */
520 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
521 {
c2982e87 522 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
cf9b4b0b 523 myclass, XFASTINT (Vdouble_click_time));
4da4f201 524 XrmPutLineResource (&rdb, line);
c2982e87 525 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
cf9b4b0b 526 myclass, XFASTINT (Vdouble_click_time));
4da4f201
GM
527 XrmPutLineResource (&rdb, line);
528 }
529
530#else /* not USE_MOTIF */
427ec082 531
fc6c5dfe 532 sprintf (line, "Emacs.dialog*.background: grey75");
4da4f201 533 XrmPutLineResource (&rdb, line);
3928f2b6
JD
534#if !defined (HAVE_XFT) || !defined (USE_LUCID)
535 sprintf (line, "Emacs.dialog*.font: %s", helv);
536 XrmPutLineResource (&rdb, line);
fc6c5dfe 537 sprintf (line, "*XlwMenu*font: %s", helv);
4da4f201 538 XrmPutLineResource (&rdb, line);
3928f2b6 539#endif
fc6c5dfe 540 sprintf (line, "*XlwMenu*background: grey75");
4da4f201 541 XrmPutLineResource (&rdb, line);
fc6c5dfe 542 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
4da4f201 543 XrmPutLineResource (&rdb, line);
427ec082 544
4da4f201
GM
545#endif /* not USE_MOTIF */
546
837255fb
JB
547 user_database = get_user_db (display);
548
549 /* Figure out what the "customization string" is, so we can use it
550 to decode paths. */
1b6d8cf0 551 xfree (x_customization_string);
837255fb
JB
552 x_customization_string
553 = x_get_customization_string (user_database, myname, myclass);
554
f3a0bf5c
JB
555 /* Get application system defaults */
556 db = get_system_app (myclass);
557 if (db != NULL)
558 XrmMergeDatabases (db, &rdb);
559
560 /* Get Fallback resources */
561 db = get_fallback (display);
562 if (db != NULL)
563 XrmMergeDatabases (db, &rdb);
564
565 /* Get application user defaults */
566 db = get_user_app (myclass);
567 if (db != NULL)
568 XrmMergeDatabases (db, &rdb);
569
570 /* get User defaults */
837255fb
JB
571 if (user_database != NULL)
572 XrmMergeDatabases (user_database, &rdb);
f3a0bf5c
JB
573
574 /* Get Environment defaults. */
575 db = get_environ_db ();
576 if (db != NULL)
577 XrmMergeDatabases (db, &rdb);
427ec082 578
f3a0bf5c
JB
579 /* Last, merge in any specification from the command line. */
580 if (xrm_string != NULL)
581 {
582 db = XrmGetStringDatabase (xrm_string);
583 if (db != NULL)
584 XrmMergeDatabases (db, &rdb);
585 }
586
587 return rdb;
588}
589
837255fb 590
f3a0bf5c
JB
591/* Retrieve the value of the resource specified by NAME with class CLASS
592 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
593
2f7c71a1
AS
594static int
595x_get_resource (XrmDatabase rdb, const char *name, const char *class,
596 XrmRepresentation expected_type, XrmValue *ret_value)
f3a0bf5c
JB
597{
598 XrmValue value;
599 XrmName namelist[100];
600 XrmClass classlist[100];
601 XrmRepresentation type;
602
5e617bc2
JB
603 XrmStringToNameList (name, namelist);
604 XrmStringToClassList (class, classlist);
f3a0bf5c
JB
605
606 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
607 && (type == expected_type))
608 {
609 if (type == x_rm_string)
41ab0754 610 ret_value->addr = (char *) value.addr;
f3a0bf5c 611 else
72af86bd 612 memcpy (ret_value->addr, value.addr, ret_value->size);
f3a0bf5c
JB
613
614 return value.size;
615 }
616
617 return 0;
618}
619
620/* Retrieve the string resource specified by NAME with CLASS from
621 database RDB. */
622
623char *
eec47d6b 624x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
f3a0bf5c
JB
625{
626 XrmValue value;
627
8686ac71
JB
628 if (inhibit_x_resources)
629 /* --quick was passed, so this is a no-op. */
630 return NULL;
631
f3a0bf5c
JB
632 if (x_get_resource (rdb, name, class, x_rm_string, &value))
633 return (char *) value.addr;
634
635 return (char *) 0;
636}
637\f
837255fb
JB
638/* Stand-alone test facilities. */
639
f3a0bf5c 640#ifdef TESTRM
837255fb
JB
641
642typedef char **List;
643#define arg_listify(len, list) (list)
644#define car(list) (*(list))
645#define cdr(list) (list + 1)
646#define NIL(list) (! *(list))
647#define free_arglist(list)
648
649static List
1dae0f0a 650member (char *elt, List list)
837255fb
JB
651{
652 List p;
653
654 for (p = list; ! NIL (p); p = cdr (p))
655 if (! strcmp (elt, car (p)))
656 return p;
657
658 return p;
659}
f3a0bf5c
JB
660
661static void
1dae0f0a 662fatal (char *msg, char *prog)
f3a0bf5c 663{
1dae0f0a
AS
664 if (errno)
665 perror (prog);
f3a0bf5c 666
1dae0f0a
AS
667 (void) fprintf (stderr, msg, prog);
668 exit (1);
f3a0bf5c
JB
669}
670
1dae0f0a
AS
671int
672main (int argc, char **argv)
f3a0bf5c
JB
673{
674 Display *display;
837255fb 675 char *displayname, *resource_string, *class, *name;
f3a0bf5c 676 XrmDatabase xdb;
837255fb 677 List arg_list, lp;
f3a0bf5c
JB
678
679 arg_list = arg_listify (argc, argv);
680
681 lp = member ("-d", arg_list);
682 if (!NIL (lp))
683 displayname = car (cdr (lp));
684 else
685 displayname = "localhost:0.0";
686
687 lp = member ("-xrm", arg_list);
688 if (! NIL (lp))
689 resource_string = car (cdr (lp));
690 else
691 resource_string = (char *) 0;
692
693 lp = member ("-c", arg_list);
694 if (! NIL (lp))
695 class = car (cdr (lp));
696 else
697 class = "Emacs";
698
837255fb
JB
699 lp = member ("-n", arg_list);
700 if (! NIL (lp))
701 name = car (cdr (lp));
702 else
703 name = "emacs";
f3a0bf5c 704
837255fb 705 free_arglist (arg_list);
f3a0bf5c
JB
706
707 if (!(display = XOpenDisplay (displayname)))
708 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
709
837255fb 710 xdb = x_load_resources (display, resource_string, name, class);
f3a0bf5c 711
f3a0bf5c
JB
712 /* In a real program, you'd want to also do this: */
713 display->db = xdb;
f3a0bf5c
JB
714
715 while (1)
716 {
837255fb
JB
717 char query_name[90];
718 char query_class[90];
719
720 printf ("Name: ");
721 gets (query_name);
f3a0bf5c 722
837255fb 723 if (strlen (query_name))
f3a0bf5c 724 {
837255fb
JB
725 char *value;
726
727 printf ("Class: ");
728 gets (query_class);
729
730 value = x_get_string_resource (xdb, query_name, query_class);
f3a0bf5c
JB
731
732 if (value != NULL)
837255fb 733 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
f3a0bf5c
JB
734 else
735 printf ("\tNo Value.\n\n");
736 }
737 else
738 break;
739 }
740 printf ("\tExit.\n\n");
741
742 XCloseDisplay (display);
1dae0f0a
AS
743
744 return 0;
f3a0bf5c
JB
745}
746#endif /* TESTRM */