(isearch-highlight): Do nothing if not window-system.
[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 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
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 350
05be3f1a
RS
351 /* Skip the first colon. */
352 p++;
f3a0bf5c
JB
353 continue;
354 }
355
356 if (p > s)
357 {
837255fb
JB
358 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
359 if (path)
360 return path;
f3a0bf5c
JB
361 }
362
837255fb
JB
363 if (*p == ':')
364 p++;
f3a0bf5c
JB
365 }
366
367 return 0;
368}
369\f
837255fb
JB
370/* Producing databases for individual sources. */
371
372#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"
373
f3a0bf5c
JB
374static XrmDatabase
375get_system_app (class)
376 char *class;
377{
837255fb
JB
378 XrmDatabase db = NULL;
379 char *path;
f3a0bf5c 380
837255fb
JB
381 path = getenv ("XFILESEARCHPATH");
382 if (! path) path = X_DEFAULT_SEARCH_PATH;
f3a0bf5c 383
837255fb
JB
384 path = search_magic_path (path, class, 0, 0);
385 if (path)
386 {
387 db = XrmGetFileDatabase (path);
388 free (path);
389 }
f3a0bf5c 390
f3a0bf5c
JB
391 return db;
392}
393
837255fb 394
f3a0bf5c
JB
395static XrmDatabase
396get_fallback (display)
397 Display *display;
398{
399 XrmDatabase db;
400
401 return NULL;
402}
403
837255fb 404
f3a0bf5c
JB
405static XrmDatabase
406get_user_app (class)
407 char *class;
408{
837255fb
JB
409 char *path;
410 char *file = 0;
411
412 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
413 names, not directories. */
414 if (((path = getenv ("XUSERFILESEARCHPATH"))
415 && (file = search_magic_path (path, class, 0, 0)))
416
417 /* Check for APPLRESDIR; it is a path of directories. In each,
418 we have to search for LANG/CLASS and then CLASS. */
419 || ((path = getenv ("XAPPLRESDIR"))
420 && ((file = search_magic_path (path, class, "/%L/%N", 0))
421 || (file = search_magic_path (path, class, "/%N", 0))))
422
423 /* Check in the home directory. This is a bit of a hack; let's
424 hope one's home directory doesn't contain any %-escapes. */
425 || (path = gethomedir (),
426 ((file = search_magic_path (path, class, "%L/%N", 0))
427 || (file = search_magic_path (path, class, "%N", 0)))))
f3a0bf5c 428 {
837255fb
JB
429 XrmDatabase db = XrmGetFileDatabase (file);
430 free (file);
431 return db;
f3a0bf5c 432 }
837255fb 433 else
f3a0bf5c 434 return NULL;
f3a0bf5c
JB
435}
436
837255fb 437
f3a0bf5c
JB
438static XrmDatabase
439get_user_db (display)
440 Display *display;
441{
442 XrmDatabase db;
443 char *xdefs;
444
2c8d1900 445#ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
b631f177
JB
446 xdefs = XResourceManagerString (display);
447#else
a2a4d43e 448 xdefs = display->xdefaults;
b631f177
JB
449#endif
450
f3a0bf5c
JB
451 if (xdefs != NULL)
452 db = XrmGetStringDatabase (xdefs);
453 else
454 {
837255fb
JB
455 char *home;
456 char *xdefault;
f3a0bf5c 457
837255fb
JB
458 home = gethomedir ();
459 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
460 strcpy (xdefault, home);
f3a0bf5c
JB
461 strcat (xdefault, ".Xdefaults");
462 db = XrmGetFileDatabase (xdefault);
837255fb
JB
463 free (home);
464 free (xdefault);
f3a0bf5c
JB
465 }
466
d8717d15 467#ifdef HAVE_XSCREENRESOURCESTRING
9b37b1c2
JB
468 /* Get the screen-specific resources too. */
469 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
470 if (xdefs != NULL)
fdce0b39
JB
471 {
472 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
473 XFree (xdefs);
474 }
9b37b1c2
JB
475#endif
476
f3a0bf5c
JB
477 return db;
478}
479
480static XrmDatabase
481get_environ_db ()
482{
483 XrmDatabase db;
484 char *p;
837255fb 485 char *path = 0, *home = 0, *host = 0;
f3a0bf5c
JB
486
487 if ((p = getenv ("XENVIRONMENT")) == NULL)
488 {
837255fb
JB
489 home = gethomedir ();
490
491 {
492 int host_size = 100;
493 host = (char *) malloc (host_size);
494
495 for (;;)
496 {
497 host[host_size - 1] = '\0';
498 gethostname (host, host_size - 1);
499 if (strlen (host) < host_size - 1)
500 break;
501 host = (char *) realloc (host, host_size *= 2);
502 }
503 }
504
505 path = (char *) malloc (strlen (home)
506 + sizeof (".Xdefaults-")
507 + strlen (host));
508 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
f3a0bf5c
JB
509 p = path;
510 }
511
512 db = XrmGetFileDatabase (p);
837255fb
JB
513
514 if (path) free (path);
515 if (home) free (home);
516 if (host) free (host);
517
f3a0bf5c
JB
518 return db;
519}
520\f
837255fb
JB
521/* External interface. */
522
f3a0bf5c
JB
523/* Types of values that we can find in a database */
524
525#define XrmStringType "String" /* String representation */
526XrmRepresentation x_rm_string; /* Quark representation */
527
528/* Load X resources based on the display and a possible -xrm option. */
529
530XrmDatabase
837255fb 531x_load_resources (display, xrm_string, myname, myclass)
f3a0bf5c 532 Display *display;
837255fb 533 char *xrm_string, *myname, *myclass;
f3a0bf5c
JB
534{
535 char *xdefs;
837255fb 536 XrmDatabase user_database;
f3a0bf5c
JB
537 XrmDatabase rdb;
538 XrmDatabase db;
539
540 x_rm_string = XrmStringToQuark (XrmStringType);
541 XrmInitialize ();
542 rdb = XrmGetStringDatabase ("");
543
837255fb
JB
544 user_database = get_user_db (display);
545
546 /* Figure out what the "customization string" is, so we can use it
547 to decode paths. */
548 if (x_customization_string)
549 free (x_customization_string);
550 x_customization_string
551 = x_get_customization_string (user_database, myname, myclass);
552
f3a0bf5c
JB
553 /* Get application system defaults */
554 db = get_system_app (myclass);
555 if (db != NULL)
556 XrmMergeDatabases (db, &rdb);
557
558 /* Get Fallback resources */
559 db = get_fallback (display);
560 if (db != NULL)
561 XrmMergeDatabases (db, &rdb);
562
563 /* Get application user defaults */
564 db = get_user_app (myclass);
565 if (db != NULL)
566 XrmMergeDatabases (db, &rdb);
567
568 /* get User defaults */
837255fb
JB
569 if (user_database != NULL)
570 XrmMergeDatabases (user_database, &rdb);
f3a0bf5c
JB
571
572 /* Get Environment defaults. */
573 db = get_environ_db ();
574 if (db != NULL)
575 XrmMergeDatabases (db, &rdb);
576
577 /* Last, merge in any specification from the command line. */
578 if (xrm_string != NULL)
579 {
580 db = XrmGetStringDatabase (xrm_string);
581 if (db != NULL)
582 XrmMergeDatabases (db, &rdb);
583 }
584
585 return rdb;
586}
587
837255fb 588
f3a0bf5c
JB
589/* Retrieve the value of the resource specified by NAME with class CLASS
590 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
591
592int
593x_get_resource (rdb, name, class, expected_type, ret_value)
594 XrmDatabase rdb;
595 char *name, *class;
596 XrmRepresentation expected_type;
597 XrmValue *ret_value;
598{
599 XrmValue value;
600 XrmName namelist[100];
601 XrmClass classlist[100];
602 XrmRepresentation type;
603
604 XrmStringToNameList(name, namelist);
605 XrmStringToClassList(class, classlist);
606
607 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
608 && (type == expected_type))
609 {
610 if (type == x_rm_string)
41ab0754 611 ret_value->addr = (char *) value.addr;
f3a0bf5c
JB
612 else
613 bcopy (value.addr, ret_value->addr, ret_value->size);
614
615 return value.size;
616 }
617
618 return 0;
619}
620
621/* Retrieve the string resource specified by NAME with CLASS from
622 database RDB. */
623
624char *
625x_get_string_resource (rdb, name, class)
626 XrmDatabase rdb;
627 char *name, *class;
628{
629 XrmValue value;
630
631 if (x_get_resource (rdb, name, class, x_rm_string, &value))
632 return (char *) value.addr;
633
634 return (char *) 0;
635}
636\f
837255fb
JB
637/* Stand-alone test facilities. */
638
f3a0bf5c 639#ifdef TESTRM
837255fb
JB
640
641typedef char **List;
642#define arg_listify(len, list) (list)
643#define car(list) (*(list))
644#define cdr(list) (list + 1)
645#define NIL(list) (! *(list))
646#define free_arglist(list)
647
648static List
649member (elt, list)
650 char *elt;
651 List list;
652{
653 List p;
654
655 for (p = list; ! NIL (p); p = cdr (p))
656 if (! strcmp (elt, car (p)))
657 return p;
658
659 return p;
660}
f3a0bf5c
JB
661
662static void
663fatal (msg, prog, x1, x2, x3, x4, x5)
664 char *msg, *prog;
665 int x1, x2, x3, x4, x5;
666{
667 extern int errno;
668
669 if (errno)
670 perror (prog);
671
672 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
673 exit (1);
674}
675
676main (argc, argv)
677 int argc;
678 char **argv;
679{
680 Display *display;
837255fb 681 char *displayname, *resource_string, *class, *name;
f3a0bf5c 682 XrmDatabase xdb;
837255fb 683 List arg_list, lp;
f3a0bf5c
JB
684
685 arg_list = arg_listify (argc, argv);
686
687 lp = member ("-d", arg_list);
688 if (!NIL (lp))
689 displayname = car (cdr (lp));
690 else
691 displayname = "localhost:0.0";
692
693 lp = member ("-xrm", arg_list);
694 if (! NIL (lp))
695 resource_string = car (cdr (lp));
696 else
697 resource_string = (char *) 0;
698
699 lp = member ("-c", arg_list);
700 if (! NIL (lp))
701 class = car (cdr (lp));
702 else
703 class = "Emacs";
704
837255fb
JB
705 lp = member ("-n", arg_list);
706 if (! NIL (lp))
707 name = car (cdr (lp));
708 else
709 name = "emacs";
f3a0bf5c 710
837255fb 711 free_arglist (arg_list);
f3a0bf5c
JB
712
713 if (!(display = XOpenDisplay (displayname)))
714 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
715
837255fb 716 xdb = x_load_resources (display, resource_string, name, class);
f3a0bf5c 717
f3a0bf5c
JB
718 /* In a real program, you'd want to also do this: */
719 display->db = xdb;
f3a0bf5c
JB
720
721 while (1)
722 {
837255fb
JB
723 char query_name[90];
724 char query_class[90];
725
726 printf ("Name: ");
727 gets (query_name);
f3a0bf5c 728
837255fb 729 if (strlen (query_name))
f3a0bf5c 730 {
837255fb
JB
731 char *value;
732
733 printf ("Class: ");
734 gets (query_class);
735
736 value = x_get_string_resource (xdb, query_name, query_class);
f3a0bf5c
JB
737
738 if (value != NULL)
837255fb 739 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
f3a0bf5c
JB
740 else
741 printf ("\tNo Value.\n\n");
742 }
743 else
744 break;
745 }
746 printf ("\tExit.\n\n");
747
748 XCloseDisplay (display);
749}
750#endif /* TESTRM */