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