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