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