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