Remove check for $srcdir being configured. This pretty much works now.
[bpt/emacs.git] / src / xrdb.c
CommitLineData
f3a0bf5c 1/* Deal with the X Resource Manager.
c6c5df7f 2 Copyright (C) 1990, 1993 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
JB
20#ifdef emacs
21#include "config.h"
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
58extern char *getenv ();
3d5d61ae
JB
59
60/* This does cause trouble on AIX. I'm going to take the comment at
61 face value. */
62#if 0
dfc35f5f
JB
63extern short getuid (); /* If this causes portability problems,
64 I think we should just delete it; it'll
65 default to `int' anyway. */
3d5d61ae
JB
66#endif
67
73fc0935 68#if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
3e0be4d0
RS
69extern struct passwd *getpwuid (uid_t);
70extern struct passwd *getpwnam (const char *);
71#else
f3a0bf5c
JB
72extern struct passwd *getpwuid ();
73extern struct passwd *getpwnam ();
3e0be4d0 74#endif
f3a0bf5c 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
JB
284{
285 int uid;
286 struct passwd *pw;
287 char *ptr;
837255fb 288 char *copy;
f3a0bf5c
JB
289
290 if ((ptr = getenv ("HOME")) == NULL)
291 {
292 if ((ptr = getenv ("USER")) != NULL)
293 pw = getpwnam (ptr);
294 else
295 {
296 uid = getuid ();
297 pw = getpwuid (uid);
298 }
837255fb 299
f3a0bf5c
JB
300 if (pw)
301 ptr = pw->pw_dir;
f3a0bf5c
JB
302 }
303
837255fb
JB
304 if (ptr == NULL)
305 return "/";
f3a0bf5c 306
837255fb
JB
307 copy = (char *) malloc (strlen (ptr) + 2);
308 strcpy (copy, ptr);
309 strcat (copy, "/");
f3a0bf5c 310
837255fb 311 return copy;
f3a0bf5c
JB
312}
313
837255fb 314
f3a0bf5c
JB
315static int
316file_p (path)
317 char *path;
318{
319 struct stat status;
320
dfc35f5f 321 return (access (path, 4) == 0 /* exists and is readable */
f3a0bf5c
JB
322 && stat (path, &status) == 0 /* get the status */
323 && (status.st_mode & S_IFDIR) == 0); /* not a directory */
324}
325
f3a0bf5c 326
837255fb
JB
327/* Find the first element of SEARCH_PATH which exists and is readable,
328 after expanding the %-escapes. Return 0 if we didn't find any, and
329 the path name of the one we found otherwise. */
f3a0bf5c 330
837255fb
JB
331static char *
332search_magic_path (search_path, class, escaped_suffix, suffix)
333 char *search_path, *class, *escaped_suffix, *suffix;
f3a0bf5c 334{
837255fb 335 register char *s, *p;
d4327fec 336
837255fb 337 for (s = search_path; *s; s = p)
f3a0bf5c 338 {
837255fb
JB
339 for (p = s; *p && *p != ':'; p++)
340 ;
341
f3a0bf5c
JB
342 if (*p == ':' && *(p + 1) == ':')
343 {
837255fb
JB
344 char *path;
345
346 s = "%N%S";
347 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
348 if (path)
349 return path;
f3a0bf5c
JB
350
351 s = p + 1;
352 continue;
353 }
354
355 if (p > s)
356 {
837255fb
JB
357 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
358 if (path)
359 return path;
f3a0bf5c
JB
360 }
361
837255fb
JB
362 if (*p == ':')
363 p++;
f3a0bf5c
JB
364 }
365
366 return 0;
367}
368\f
837255fb
JB
369/* Producing databases for individual sources. */
370
371#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"
372
f3a0bf5c
JB
373static XrmDatabase
374get_system_app (class)
375 char *class;
376{
837255fb
JB
377 XrmDatabase db = NULL;
378 char *path;
f3a0bf5c 379
837255fb
JB
380 path = getenv ("XFILESEARCHPATH");
381 if (! path) path = X_DEFAULT_SEARCH_PATH;
f3a0bf5c 382
837255fb
JB
383 path = search_magic_path (path, class, 0, 0);
384 if (path)
385 {
386 db = XrmGetFileDatabase (path);
387 free (path);
388 }
f3a0bf5c 389
f3a0bf5c
JB
390 return db;
391}
392
837255fb 393
f3a0bf5c
JB
394static XrmDatabase
395get_fallback (display)
396 Display *display;
397{
398 XrmDatabase db;
399
400 return NULL;
401}
402
837255fb 403
f3a0bf5c
JB
404static XrmDatabase
405get_user_app (class)
406 char *class;
407{
837255fb
JB
408 char *path;
409 char *file = 0;
410
411 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
412 names, not directories. */
413 if (((path = getenv ("XUSERFILESEARCHPATH"))
414 && (file = search_magic_path (path, class, 0, 0)))
415
416 /* Check for APPLRESDIR; it is a path of directories. In each,
417 we have to search for LANG/CLASS and then CLASS. */
418 || ((path = getenv ("XAPPLRESDIR"))
419 && ((file = search_magic_path (path, class, "/%L/%N", 0))
420 || (file = search_magic_path (path, class, "/%N", 0))))
421
422 /* Check in the home directory. This is a bit of a hack; let's
423 hope one's home directory doesn't contain any %-escapes. */
424 || (path = gethomedir (),
425 ((file = search_magic_path (path, class, "%L/%N", 0))
426 || (file = search_magic_path (path, class, "%N", 0)))))
f3a0bf5c 427 {
837255fb
JB
428 XrmDatabase db = XrmGetFileDatabase (file);
429 free (file);
430 return db;
f3a0bf5c 431 }
837255fb 432 else
f3a0bf5c 433 return NULL;
f3a0bf5c
JB
434}
435
837255fb 436
f3a0bf5c
JB
437static XrmDatabase
438get_user_db (display)
439 Display *display;
440{
441 XrmDatabase db;
442 char *xdefs;
443
2c8d1900 444#ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
b631f177
JB
445 xdefs = XResourceManagerString (display);
446#else
a2a4d43e 447 xdefs = display->xdefaults;
b631f177
JB
448#endif
449
f3a0bf5c
JB
450 if (xdefs != NULL)
451 db = XrmGetStringDatabase (xdefs);
452 else
453 {
837255fb
JB
454 char *home;
455 char *xdefault;
f3a0bf5c 456
837255fb
JB
457 home = gethomedir ();
458 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
459 strcpy (xdefault, home);
f3a0bf5c
JB
460 strcat (xdefault, ".Xdefaults");
461 db = XrmGetFileDatabase (xdefault);
837255fb
JB
462 free (home);
463 free (xdefault);
f3a0bf5c
JB
464 }
465
d8717d15 466#ifdef HAVE_XSCREENRESOURCESTRING
9b37b1c2
JB
467 /* Get the screen-specific resources too. */
468 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
469 if (xdefs != NULL)
fdce0b39
JB
470 {
471 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
472 XFree (xdefs);
473 }
9b37b1c2
JB
474#endif
475
f3a0bf5c
JB
476 return db;
477}
478
479static XrmDatabase
480get_environ_db ()
481{
482 XrmDatabase db;
483 char *p;
837255fb 484 char *path = 0, *home = 0, *host = 0;
f3a0bf5c
JB
485
486 if ((p = getenv ("XENVIRONMENT")) == NULL)
487 {
837255fb
JB
488 home = gethomedir ();
489
490 {
491 int host_size = 100;
492 host = (char *) malloc (host_size);
493
494 for (;;)
495 {
496 host[host_size - 1] = '\0';
497 gethostname (host, host_size - 1);
498 if (strlen (host) < host_size - 1)
499 break;
500 host = (char *) realloc (host, host_size *= 2);
501 }
502 }
503
504 path = (char *) malloc (strlen (home)
505 + sizeof (".Xdefaults-")
506 + strlen (host));
507 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
f3a0bf5c
JB
508 p = path;
509 }
510
511 db = XrmGetFileDatabase (p);
837255fb
JB
512
513 if (path) free (path);
514 if (home) free (home);
515 if (host) free (host);
516
f3a0bf5c
JB
517 return db;
518}
519\f
837255fb
JB
520/* External interface. */
521
f3a0bf5c
JB
522/* Types of values that we can find in a database */
523
524#define XrmStringType "String" /* String representation */
525XrmRepresentation x_rm_string; /* Quark representation */
526
527/* Load X resources based on the display and a possible -xrm option. */
528
529XrmDatabase
837255fb 530x_load_resources (display, xrm_string, myname, myclass)
f3a0bf5c 531 Display *display;
837255fb 532 char *xrm_string, *myname, *myclass;
f3a0bf5c
JB
533{
534 char *xdefs;
837255fb 535 XrmDatabase user_database;
f3a0bf5c
JB
536 XrmDatabase rdb;
537 XrmDatabase db;
538
539 x_rm_string = XrmStringToQuark (XrmStringType);
540 XrmInitialize ();
541 rdb = XrmGetStringDatabase ("");
542
837255fb
JB
543 user_database = get_user_db (display);
544
545 /* Figure out what the "customization string" is, so we can use it
546 to decode paths. */
547 if (x_customization_string)
548 free (x_customization_string);
549 x_customization_string
550 = x_get_customization_string (user_database, myname, myclass);
551
f3a0bf5c
JB
552 /* Get application system defaults */
553 db = get_system_app (myclass);
554 if (db != NULL)
555 XrmMergeDatabases (db, &rdb);
556
557 /* Get Fallback resources */
558 db = get_fallback (display);
559 if (db != NULL)
560 XrmMergeDatabases (db, &rdb);
561
562 /* Get application user defaults */
563 db = get_user_app (myclass);
564 if (db != NULL)
565 XrmMergeDatabases (db, &rdb);
566
567 /* get User defaults */
837255fb
JB
568 if (user_database != NULL)
569 XrmMergeDatabases (user_database, &rdb);
f3a0bf5c
JB
570
571 /* Get Environment defaults. */
572 db = get_environ_db ();
573 if (db != NULL)
574 XrmMergeDatabases (db, &rdb);
575
576 /* Last, merge in any specification from the command line. */
577 if (xrm_string != NULL)
578 {
579 db = XrmGetStringDatabase (xrm_string);
580 if (db != NULL)
581 XrmMergeDatabases (db, &rdb);
582 }
583
584 return rdb;
585}
586
837255fb 587
f3a0bf5c
JB
588/* Retrieve the value of the resource specified by NAME with class CLASS
589 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
590
591int
592x_get_resource (rdb, name, class, expected_type, ret_value)
593 XrmDatabase rdb;
594 char *name, *class;
595 XrmRepresentation expected_type;
596 XrmValue *ret_value;
597{
598 XrmValue value;
599 XrmName namelist[100];
600 XrmClass classlist[100];
601 XrmRepresentation type;
602
603 XrmStringToNameList(name, namelist);
604 XrmStringToClassList(class, classlist);
605
606 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
607 && (type == expected_type))
608 {
609 if (type == x_rm_string)
41ab0754 610 ret_value->addr = (char *) value.addr;
f3a0bf5c
JB
611 else
612 bcopy (value.addr, ret_value->addr, ret_value->size);
613
614 return value.size;
615 }
616
617 return 0;
618}
619
620/* Retrieve the string resource specified by NAME with CLASS from
621 database RDB. */
622
623char *
624x_get_string_resource (rdb, name, class)
625 XrmDatabase rdb;
626 char *name, *class;
627{
628 XrmValue value;
629
630 if (x_get_resource (rdb, name, class, x_rm_string, &value))
631 return (char *) value.addr;
632
633 return (char *) 0;
634}
635\f
837255fb
JB
636/* Stand-alone test facilities. */
637
f3a0bf5c 638#ifdef TESTRM
837255fb
JB
639
640typedef char **List;
641#define arg_listify(len, list) (list)
642#define car(list) (*(list))
643#define cdr(list) (list + 1)
644#define NIL(list) (! *(list))
645#define free_arglist(list)
646
647static List
648member (elt, list)
649 char *elt;
650 List list;
651{
652 List p;
653
654 for (p = list; ! NIL (p); p = cdr (p))
655 if (! strcmp (elt, car (p)))
656 return p;
657
658 return p;
659}
f3a0bf5c
JB
660
661static void
662fatal (msg, prog, x1, x2, x3, x4, x5)
663 char *msg, *prog;
664 int x1, x2, x3, x4, x5;
665{
666 extern int errno;
667
668 if (errno)
669 perror (prog);
670
671 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
672 exit (1);
673}
674
675main (argc, argv)
676 int argc;
677 char **argv;
678{
679 Display *display;
837255fb 680 char *displayname, *resource_string, *class, *name;
f3a0bf5c 681 XrmDatabase xdb;
837255fb 682 List arg_list, lp;
f3a0bf5c
JB
683
684 arg_list = arg_listify (argc, argv);
685
686 lp = member ("-d", arg_list);
687 if (!NIL (lp))
688 displayname = car (cdr (lp));
689 else
690 displayname = "localhost:0.0";
691
692 lp = member ("-xrm", arg_list);
693 if (! NIL (lp))
694 resource_string = car (cdr (lp));
695 else
696 resource_string = (char *) 0;
697
698 lp = member ("-c", arg_list);
699 if (! NIL (lp))
700 class = car (cdr (lp));
701 else
702 class = "Emacs";
703
837255fb
JB
704 lp = member ("-n", arg_list);
705 if (! NIL (lp))
706 name = car (cdr (lp));
707 else
708 name = "emacs";
f3a0bf5c 709
837255fb 710 free_arglist (arg_list);
f3a0bf5c
JB
711
712 if (!(display = XOpenDisplay (displayname)))
713 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
714
837255fb 715 xdb = x_load_resources (display, resource_string, name, class);
f3a0bf5c 716
f3a0bf5c
JB
717 /* In a real program, you'd want to also do this: */
718 display->db = xdb;
f3a0bf5c
JB
719
720 while (1)
721 {
837255fb
JB
722 char query_name[90];
723 char query_class[90];
724
725 printf ("Name: ");
726 gets (query_name);
f3a0bf5c 727
837255fb 728 if (strlen (query_name))
f3a0bf5c 729 {
837255fb
JB
730 char *value;
731
732 printf ("Class: ");
733 gets (query_class);
734
735 value = x_get_string_resource (xdb, query_name, query_class);
f3a0bf5c
JB
736
737 if (value != NULL)
837255fb 738 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
f3a0bf5c
JB
739 else
740 printf ("\tNo Value.\n\n");
741 }
742 else
743 break;
744 }
745 printf ("\tExit.\n\n");
746
747 XCloseDisplay (display);
748}
749#endif /* TESTRM */