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