(describe_map): Ignore bindings for all except symbols and integers.
[bpt/emacs.git] / src / xrdb.c
CommitLineData
f3a0bf5c 1/* Deal with the X Resource Manager.
3a22ee35 2 Copyright (C) 1990, 1993, 1994 Free Software Foundation.
f3a0bf5c
JB
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
d4327fec 6the Free Software Foundation; either version 2, or (at your option)
f3a0bf5c
JB
7any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; see the file COPYING. If not, write to
16the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18/* Written by jla, 4/90 */
19
a2a4d43e 20#ifdef emacs
18160b98 21#include <config.h>
a2a4d43e
JB
22#endif
23
837255fb
JB
24#include <stdio.h>
25
a2a4d43e
JB
26#if 1 /* I'd really appreciate it if this code could go away... -JimB */
27/* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
28#ifdef USG5
29#define SYSV
30#include <unistd.h>
31#endif /* USG5 */
32
33#endif /* 1 */
54c908b5
JB
34
35/* This should be included before the X include files; otherwise, we get
36 warnings about redefining NULL under BSD 4.3. */
37#include <sys/param.h>
38
f3a0bf5c
JB
39#include <X11/Xlib.h>
40#include <X11/Xatom.h>
dfc35f5f 41#if 0
f3a0bf5c 42#include <X11/Xos.h>
dfc35f5f 43#endif
f3a0bf5c
JB
44#include <X11/X.h>
45#include <X11/Xutil.h>
46#include <X11/Xresource.h>
531ff254
JB
47#ifdef VMS
48#include "vms-pwd.h"
49#else
f3a0bf5c 50#include <pwd.h>
531ff254 51#endif
f3a0bf5c
JB
52#include <sys/stat.h>
53
a2a4d43e
JB
54#ifndef MAXPATHLEN
55#define MAXPATHLEN 256
f3a0bf5c
JB
56#endif
57
0f2cd61f
RS
58#if !defined(S_ISDIR) && defined(S_IFDIR)
59#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
60#endif
61
f3a0bf5c 62extern char *getenv ();
3d5d61ae
JB
63
64/* This does cause trouble on AIX. I'm going to take the comment at
65 face value. */
66#if 0
dfc35f5f
JB
67extern short getuid (); /* If this causes portability problems,
68 I think we should just delete it; it'll
69 default to `int' anyway. */
3d5d61ae
JB
70#endif
71
73fc0935 72#if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
3e0be4d0
RS
73extern struct passwd *getpwuid (uid_t);
74extern struct passwd *getpwnam (const char *);
75#else
f3a0bf5c
JB
76extern struct passwd *getpwuid ();
77extern struct passwd *getpwnam ();
3e0be4d0 78#endif
f3a0bf5c 79
e29ad342
KH
80extern char *get_system_name ();
81
837255fb
JB
82/* Make sure not to #include anything after these definitions. Let's
83 not step on anyone's prototypes. */
84#ifdef emacs
85#define malloc xmalloc
86#define realloc xrealloc
87#define free xfree
88#endif
89
90char *x_get_string_resource ();
91static int file_p ();
92
93\f
94/* X file search path processing. */
95
96
97/* The string which gets substituted for the %C escape in XFILESEARCHPATH
98 and friends, or zero if none was specified. */
99char *x_customization_string;
100
101
102/* Return the value of the emacs.customization (Emacs.Customization)
103 resource, for later use in search path decoding. If we find no
104 such resource, return zero. */
105char *
106x_get_customization_string (db, name, class)
107 XrmDatabase db;
108 char *name, *class;
109{
110 char *full_name
111 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
112 char *full_class
113 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
114 char *result;
115
116 sprintf (full_name, "%s.%s", name, "customization");
117 sprintf (full_class, "%s.%s", class, "Customization");
118
119 result = x_get_string_resource (db, full_name, full_class);
120
121 if (result)
abfc2e5f
RS
122 {
123 char *copy = (char *) malloc (strlen (result) + 1);
124 strcpy (copy, result);
125 return copy;
126 }
837255fb
JB
127 else
128 return 0;
129}
130
131
132/* Expand all the Xt-style %-escapes in STRING, whose length is given
133 by STRING_LEN. Here are the escapes we're supposed to recognize:
134
135 %N The value of the application's class name
136 %T The value of the type parameter ("app-defaults" in this
137 context)
138 %S The value of the suffix parameter ("" in this context)
139 %L The language string associated with the specified display
140 (We use the "LANG" environment variable here, if it's set.)
141 %l The language part of the display's language string
142 (We treat this just like %L. If someone can tell us what
143 we're really supposed to do, dandy.)
144 %t The territory part of the display's language string
145 (This never gets used.)
146 %c The codeset part of the display's language string
147 (This never gets used either.)
148 %C The customization string retrieved from the resource
149 database associated with display.
150 (This is x_customization_string.)
151
152 Return the expanded file name if it exists and is readable, and
153 refers to %L only when the LANG environment variable is set, or
154 otherwise provided by X.
155
156 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
157 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
158 alone.
159
160 Return NULL otherwise. */
161
162static char *
163magic_file_p (string, string_len, class, escaped_suffix, suffix)
164 char *string;
165 int string_len;
166 char *class, *escaped_suffix, *suffix;
167{
168 char *lang = getenv ("LANG");
169
170 int path_size = 100;
171 char *path = (char *) malloc (path_size);
172 int path_len = 0;
173
174 char *p = string;
175
176 while (p < string + string_len)
177 {
178 /* The chunk we're about to stick on the end of result. */
179 char *next;
180 int next_len;
181
182 if (*p == '%')
183 {
184 p++;
185
186 if (p >= string + string_len)
187 next_len = 0;
188 else
189 switch (*p)
190 {
191 case '%':
192 next = "%";
193 next_len = 1;
194 break;
195
196 case 'C':
197 next = (x_customization_string
198 ? x_customization_string
199 : "");
200 next_len = strlen (next);
201 break;
202
203 case 'N':
204 next = class;
205 next_len = strlen (class);
206 break;
207
208 case 'T':
209 next = "app-defaults";
210 next_len = strlen (next);
211 break;
212
213 default:
214 case 'S':
215 next_len = 0;
216 break;
217
218 case 'L':
219 case 'l':
220 if (! lang)
221 {
222 free (path);
223 return NULL;
224 }
225
226 next = lang;
227 next_len = strlen (next);
228 break;
229
230 case 't':
231 case 'c':
232 free (path);
233 return NULL;
234 }
235 }
236 else
237 next = p, next_len = 1;
238
239 /* Do we have room for this component followed by a '\0' ? */
240 if (path_len + next_len + 1 > path_size)
241 {
242 path_size = (path_len + next_len + 1) * 2;
243 path = (char *) realloc (path, path_size);
244 }
245
246 bcopy (next, path + path_len, next_len);
247 path_len += next_len;
248
249 p++;
250
251 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
252 if (p >= string + string_len && escaped_suffix)
253 {
254 string = escaped_suffix;
255 string_len = strlen (string);
256 p = string;
257 escaped_suffix = NULL;
258 }
259 }
260
261 /* Perhaps we should add the SUFFIX now. */
262 if (suffix)
263 {
264 int suffix_len = strlen (suffix);
265
266 if (path_len + suffix_len + 1 > path_size)
267 {
268 path_size = (path_len + suffix_len + 1);
269 path = (char *) realloc (path, path_size);
270 }
271
272 bcopy (suffix, path + path_len, suffix_len);
273 path_len += suffix_len;
274 }
275
276 path[path_len] = '\0';
277
278 if (! file_p (path))
279 {
280 free (path);
281 return NULL;
282 }
283
284 return path;
285}
286
287
f3a0bf5c 288static char *
837255fb 289gethomedir ()
f3a0bf5c 290{
f3a0bf5c
JB
291 struct passwd *pw;
292 char *ptr;
837255fb 293 char *copy;
f3a0bf5c
JB
294
295 if ((ptr = getenv ("HOME")) == NULL)
296 {
1cac1f6f
KH
297 if ((ptr = getenv ("LOGNAME")) != NULL
298 || (ptr = getenv ("USER")) != NULL)
f3a0bf5c
JB
299 pw = getpwnam (ptr);
300 else
1cac1f6f 301 pw = getpwuid (getuid ());
837255fb 302
f3a0bf5c
JB
303 if (pw)
304 ptr = pw->pw_dir;
f3a0bf5c
JB
305 }
306
837255fb
JB
307 if (ptr == NULL)
308 return "/";
f3a0bf5c 309
837255fb
JB
310 copy = (char *) malloc (strlen (ptr) + 2);
311 strcpy (copy, ptr);
312 strcat (copy, "/");
f3a0bf5c 313
837255fb 314 return copy;
f3a0bf5c
JB
315}
316
837255fb 317
f3a0bf5c
JB
318static int
319file_p (path)
320 char *path;
321{
322 struct stat status;
323
dfc35f5f 324 return (access (path, 4) == 0 /* exists and is readable */
f3a0bf5c 325 && stat (path, &status) == 0 /* get the status */
0f2cd61f 326 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
f3a0bf5c
JB
327}
328
f3a0bf5c 329
837255fb
JB
330/* Find the first element of SEARCH_PATH which exists and is readable,
331 after expanding the %-escapes. Return 0 if we didn't find any, and
332 the path name of the one we found otherwise. */
f3a0bf5c 333
837255fb
JB
334static char *
335search_magic_path (search_path, class, escaped_suffix, suffix)
336 char *search_path, *class, *escaped_suffix, *suffix;
f3a0bf5c 337{
837255fb 338 register char *s, *p;
d4327fec 339
837255fb 340 for (s = search_path; *s; s = p)
f3a0bf5c 341 {
837255fb
JB
342 for (p = s; *p && *p != ':'; p++)
343 ;
344
0f2cd61f 345 if (p > s)
f3a0bf5c 346 {
0f2cd61f 347 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
837255fb
JB
348 if (path)
349 return path;
f3a0bf5c 350 }
0f2cd61f 351 else if (*p == ':')
f3a0bf5c 352 {
0f2cd61f
RS
353 char *path;
354
355 s = "%N%S";
356 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
837255fb
JB
357 if (path)
358 return path;
f3a0bf5c
JB
359 }
360
837255fb
JB
361 if (*p == ':')
362 p++;
f3a0bf5c
JB
363 }
364
365 return 0;
366}
367\f
837255fb
JB
368/* Producing databases for individual sources. */
369
370#define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/%L/%T/%N%C%S:/usr/lib/X11/%l/%T/%N%C%S:/usr/lib/X11/%T/%N%C%S:/usr/lib/X11/%L/%T/%N%S:/usr/lib/X11/%l/%T/%N%S:/usr/lib/X11/%T/%N%S"
371
f3a0bf5c
JB
372static XrmDatabase
373get_system_app (class)
374 char *class;
375{
837255fb
JB
376 XrmDatabase db = NULL;
377 char *path;
f3a0bf5c 378
837255fb
JB
379 path = getenv ("XFILESEARCHPATH");
380 if (! path) path = X_DEFAULT_SEARCH_PATH;
f3a0bf5c 381
837255fb
JB
382 path = search_magic_path (path, class, 0, 0);
383 if (path)
384 {
385 db = XrmGetFileDatabase (path);
386 free (path);
387 }
f3a0bf5c 388
f3a0bf5c
JB
389 return db;
390}
391
837255fb 392
f3a0bf5c
JB
393static XrmDatabase
394get_fallback (display)
395 Display *display;
396{
397 XrmDatabase db;
398
399 return NULL;
400}
401
837255fb 402
f3a0bf5c
JB
403static XrmDatabase
404get_user_app (class)
405 char *class;
406{
837255fb
JB
407 char *path;
408 char *file = 0;
409
410 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
411 names, not directories. */
412 if (((path = getenv ("XUSERFILESEARCHPATH"))
413 && (file = search_magic_path (path, class, 0, 0)))
414
415 /* Check for APPLRESDIR; it is a path of directories. In each,
416 we have to search for LANG/CLASS and then CLASS. */
417 || ((path = getenv ("XAPPLRESDIR"))
418 && ((file = search_magic_path (path, class, "/%L/%N", 0))
419 || (file = search_magic_path (path, class, "/%N", 0))))
420
421 /* Check in the home directory. This is a bit of a hack; let's
422 hope one's home directory doesn't contain any %-escapes. */
423 || (path = gethomedir (),
424 ((file = search_magic_path (path, class, "%L/%N", 0))
425 || (file = search_magic_path (path, class, "%N", 0)))))
f3a0bf5c 426 {
837255fb
JB
427 XrmDatabase db = XrmGetFileDatabase (file);
428 free (file);
429 return db;
f3a0bf5c 430 }
837255fb 431 else
f3a0bf5c 432 return NULL;
f3a0bf5c
JB
433}
434
837255fb 435
f3a0bf5c
JB
436static XrmDatabase
437get_user_db (display)
438 Display *display;
439{
440 XrmDatabase db;
441 char *xdefs;
442
2c8d1900 443#ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
b631f177
JB
444 xdefs = XResourceManagerString (display);
445#else
a2a4d43e 446 xdefs = display->xdefaults;
b631f177
JB
447#endif
448
f3a0bf5c
JB
449 if (xdefs != NULL)
450 db = XrmGetStringDatabase (xdefs);
451 else
452 {
837255fb
JB
453 char *home;
454 char *xdefault;
f3a0bf5c 455
837255fb
JB
456 home = gethomedir ();
457 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
458 strcpy (xdefault, home);
f3a0bf5c
JB
459 strcat (xdefault, ".Xdefaults");
460 db = XrmGetFileDatabase (xdefault);
837255fb
JB
461 free (home);
462 free (xdefault);
f3a0bf5c
JB
463 }
464
d8717d15 465#ifdef HAVE_XSCREENRESOURCESTRING
9b37b1c2
JB
466 /* Get the screen-specific resources too. */
467 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
468 if (xdefs != NULL)
fdce0b39
JB
469 {
470 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
471 XFree (xdefs);
472 }
9b37b1c2
JB
473#endif
474
f3a0bf5c
JB
475 return db;
476}
477
478static XrmDatabase
479get_environ_db ()
480{
481 XrmDatabase db;
482 char *p;
e29ad342 483 char *path = 0, *home = 0, *host;
f3a0bf5c
JB
484
485 if ((p = getenv ("XENVIRONMENT")) == NULL)
486 {
837255fb 487 home = gethomedir ();
e29ad342 488 host = get_system_name ();
837255fb
JB
489 path = (char *) malloc (strlen (home)
490 + sizeof (".Xdefaults-")
491 + strlen (host));
492 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
f3a0bf5c
JB
493 p = path;
494 }
495
496 db = XrmGetFileDatabase (p);
837255fb
JB
497
498 if (path) free (path);
499 if (home) free (home);
837255fb 500
f3a0bf5c
JB
501 return db;
502}
503\f
837255fb
JB
504/* External interface. */
505
f3a0bf5c
JB
506/* Types of values that we can find in a database */
507
508#define XrmStringType "String" /* String representation */
509XrmRepresentation x_rm_string; /* Quark representation */
510
511/* Load X resources based on the display and a possible -xrm option. */
512
513XrmDatabase
837255fb 514x_load_resources (display, xrm_string, myname, myclass)
f3a0bf5c 515 Display *display;
837255fb 516 char *xrm_string, *myname, *myclass;
f3a0bf5c
JB
517{
518 char *xdefs;
837255fb 519 XrmDatabase user_database;
f3a0bf5c
JB
520 XrmDatabase rdb;
521 XrmDatabase db;
522
523 x_rm_string = XrmStringToQuark (XrmStringType);
524 XrmInitialize ();
525 rdb = XrmGetStringDatabase ("");
526
837255fb
JB
527 user_database = get_user_db (display);
528
529 /* Figure out what the "customization string" is, so we can use it
530 to decode paths. */
531 if (x_customization_string)
532 free (x_customization_string);
533 x_customization_string
534 = x_get_customization_string (user_database, myname, myclass);
535
f3a0bf5c
JB
536 /* Get application system defaults */
537 db = get_system_app (myclass);
538 if (db != NULL)
539 XrmMergeDatabases (db, &rdb);
540
541 /* Get Fallback resources */
542 db = get_fallback (display);
543 if (db != NULL)
544 XrmMergeDatabases (db, &rdb);
545
546 /* Get application user defaults */
547 db = get_user_app (myclass);
548 if (db != NULL)
549 XrmMergeDatabases (db, &rdb);
550
551 /* get User defaults */
837255fb
JB
552 if (user_database != NULL)
553 XrmMergeDatabases (user_database, &rdb);
f3a0bf5c
JB
554
555 /* Get Environment defaults. */
556 db = get_environ_db ();
557 if (db != NULL)
558 XrmMergeDatabases (db, &rdb);
559
560 /* Last, merge in any specification from the command line. */
561 if (xrm_string != NULL)
562 {
563 db = XrmGetStringDatabase (xrm_string);
564 if (db != NULL)
565 XrmMergeDatabases (db, &rdb);
566 }
567
568 return rdb;
569}
570
837255fb 571
f3a0bf5c
JB
572/* Retrieve the value of the resource specified by NAME with class CLASS
573 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
574
575int
576x_get_resource (rdb, name, class, expected_type, ret_value)
577 XrmDatabase rdb;
578 char *name, *class;
579 XrmRepresentation expected_type;
580 XrmValue *ret_value;
581{
582 XrmValue value;
583 XrmName namelist[100];
584 XrmClass classlist[100];
585 XrmRepresentation type;
586
587 XrmStringToNameList(name, namelist);
588 XrmStringToClassList(class, classlist);
589
590 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
591 && (type == expected_type))
592 {
593 if (type == x_rm_string)
41ab0754 594 ret_value->addr = (char *) value.addr;
f3a0bf5c
JB
595 else
596 bcopy (value.addr, ret_value->addr, ret_value->size);
597
598 return value.size;
599 }
600
601 return 0;
602}
603
604/* Retrieve the string resource specified by NAME with CLASS from
605 database RDB. */
606
607char *
608x_get_string_resource (rdb, name, class)
609 XrmDatabase rdb;
610 char *name, *class;
611{
612 XrmValue value;
613
614 if (x_get_resource (rdb, name, class, x_rm_string, &value))
615 return (char *) value.addr;
616
617 return (char *) 0;
618}
619\f
837255fb
JB
620/* Stand-alone test facilities. */
621
f3a0bf5c 622#ifdef TESTRM
837255fb
JB
623
624typedef char **List;
625#define arg_listify(len, list) (list)
626#define car(list) (*(list))
627#define cdr(list) (list + 1)
628#define NIL(list) (! *(list))
629#define free_arglist(list)
630
631static List
632member (elt, list)
633 char *elt;
634 List list;
635{
636 List p;
637
638 for (p = list; ! NIL (p); p = cdr (p))
639 if (! strcmp (elt, car (p)))
640 return p;
641
642 return p;
643}
f3a0bf5c
JB
644
645static void
646fatal (msg, prog, x1, x2, x3, x4, x5)
647 char *msg, *prog;
648 int x1, x2, x3, x4, x5;
649{
650 extern int errno;
651
652 if (errno)
653 perror (prog);
654
655 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
656 exit (1);
657}
658
659main (argc, argv)
660 int argc;
661 char **argv;
662{
663 Display *display;
837255fb 664 char *displayname, *resource_string, *class, *name;
f3a0bf5c 665 XrmDatabase xdb;
837255fb 666 List arg_list, lp;
f3a0bf5c
JB
667
668 arg_list = arg_listify (argc, argv);
669
670 lp = member ("-d", arg_list);
671 if (!NIL (lp))
672 displayname = car (cdr (lp));
673 else
674 displayname = "localhost:0.0";
675
676 lp = member ("-xrm", arg_list);
677 if (! NIL (lp))
678 resource_string = car (cdr (lp));
679 else
680 resource_string = (char *) 0;
681
682 lp = member ("-c", arg_list);
683 if (! NIL (lp))
684 class = car (cdr (lp));
685 else
686 class = "Emacs";
687
837255fb
JB
688 lp = member ("-n", arg_list);
689 if (! NIL (lp))
690 name = car (cdr (lp));
691 else
692 name = "emacs";
f3a0bf5c 693
837255fb 694 free_arglist (arg_list);
f3a0bf5c
JB
695
696 if (!(display = XOpenDisplay (displayname)))
697 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
698
837255fb 699 xdb = x_load_resources (display, resource_string, name, class);
f3a0bf5c 700
f3a0bf5c
JB
701 /* In a real program, you'd want to also do this: */
702 display->db = xdb;
f3a0bf5c
JB
703
704 while (1)
705 {
837255fb
JB
706 char query_name[90];
707 char query_class[90];
708
709 printf ("Name: ");
710 gets (query_name);
f3a0bf5c 711
837255fb 712 if (strlen (query_name))
f3a0bf5c 713 {
837255fb
JB
714 char *value;
715
716 printf ("Class: ");
717 gets (query_class);
718
719 value = x_get_string_resource (xdb, query_name, query_class);
f3a0bf5c
JB
720
721 if (value != NULL)
837255fb 722 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
f3a0bf5c
JB
723 else
724 printf ("\tNo Value.\n\n");
725 }
726 else
727 break;
728 }
729 printf ("\tExit.\n\n");
730
731 XCloseDisplay (display);
732}
733#endif /* TESTRM */