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