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