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