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