1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001 Free Software Foundation.
4 This file is part of GNU Emacs.
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)
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.
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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* Written by jla, 4/90 */
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
37 X11. Don't do it on Solaris, because it breaks compilation with
38 XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
40 #if defined(USG5) && !defined(SOLARIS2)
44 #endif /* USG5 && !SOLARIS2 */
49 #include <X11/Xatom.h>
54 #include <X11/Xutil.h>
55 #include <X11/Xresource.h>
61 #if !defined(S_ISDIR) && defined(S_IFDIR)
62 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
67 extern char *getenv ();
69 /* This does cause trouble on AIX. I'm going to take the comment at
72 extern short getuid (); /* If this causes portability problems,
73 I think we should just delete it; it'll
74 default to `int' anyway. */
77 #ifdef DECLARE_GETPWUID_WITH_UID_T
78 extern struct passwd
*getpwuid (uid_t
);
79 extern struct passwd
*getpwnam (const char *);
81 extern struct passwd
*getpwuid ();
82 extern struct passwd
*getpwnam ();
85 extern char *get_system_name ();
87 /* Make sure not to #include anything after these definitions. Let's
88 not step on anyone's prototypes. */
90 #define malloc xmalloc
91 #define realloc xrealloc
95 char *x_get_string_resource ();
99 /* X file search path processing. */
102 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
103 and friends, or zero if none was specified. */
104 char *x_customization_string
;
107 /* Return the value of the emacs.customization (Emacs.Customization)
108 resource, for later use in search path decoding. If we find no
109 such resource, return zero. */
111 x_get_customization_string (db
, name
, class)
116 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
118 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
121 sprintf (full_name
, "%s.%s", name
, "customization");
122 sprintf (full_class
, "%s.%s", class, "Customization");
124 result
= x_get_string_resource (db
, full_name
, full_class
);
128 char *copy
= (char *) malloc (strlen (result
) + 1);
129 strcpy (copy
, result
);
137 /* Expand all the Xt-style %-escapes in STRING, whose length is given
138 by STRING_LEN. Here are the escapes we're supposed to recognize:
140 %N The value of the application's class name
141 %T The value of the type parameter ("app-defaults" in this
143 %S The value of the suffix parameter ("" in this context)
144 %L The language string associated with the specified display
145 (We use the "LANG" environment variable here, if it's set.)
146 %l The language part of the display's language string
147 (We treat this just like %L. If someone can tell us what
148 we're really supposed to do, dandy.)
149 %t The territory part of the display's language string
150 (This never gets used.)
151 %c The codeset part of the display's language string
152 (This never gets used either.)
153 %C The customization string retrieved from the resource
154 database associated with display.
155 (This is x_customization_string.)
157 Return the expanded file name if it exists and is readable, and
158 refers to %L only when the LANG environment variable is set, or
159 otherwise provided by X.
161 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
162 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
165 Return NULL otherwise. */
168 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
171 char *class, *escaped_suffix
, *suffix
;
173 char *lang
= getenv ("LANG");
176 char *path
= (char *) malloc (path_size
);
181 while (p
< string
+ string_len
)
183 /* The chunk we're about to stick on the end of result. */
191 if (p
>= string
+ string_len
)
202 next
= (x_customization_string
203 ? x_customization_string
205 next_len
= strlen (next
);
210 next_len
= strlen (class);
214 next
= "app-defaults";
215 next_len
= strlen (next
);
232 next_len
= strlen (next
);
242 next
= p
, next_len
= 1;
244 /* Do we have room for this component followed by a '\0' ? */
245 if (path_len
+ next_len
+ 1 > path_size
)
247 path_size
= (path_len
+ next_len
+ 1) * 2;
248 path
= (char *) realloc (path
, path_size
);
251 bcopy (next
, path
+ path_len
, next_len
);
252 path_len
+= next_len
;
256 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
257 if (p
>= string
+ string_len
&& escaped_suffix
)
259 string
= escaped_suffix
;
260 string_len
= strlen (string
);
262 escaped_suffix
= NULL
;
266 /* Perhaps we should add the SUFFIX now. */
269 int suffix_len
= strlen (suffix
);
271 if (path_len
+ suffix_len
+ 1 > path_size
)
273 path_size
= (path_len
+ suffix_len
+ 1);
274 path
= (char *) realloc (path
, path_size
);
277 bcopy (suffix
, path
+ path_len
, suffix_len
);
278 path_len
+= suffix_len
;
281 path
[path_len
] = '\0';
300 if ((ptr
= getenv ("HOME")) == NULL
)
302 if ((ptr
= getenv ("LOGNAME")) != NULL
303 || (ptr
= getenv ("USER")) != NULL
)
306 pw
= getpwuid (getuid ());
315 copy
= (char *) malloc (strlen (ptr
) + 2);
329 return (access (filename
, 4) == 0 /* exists and is readable */
330 && stat (filename
, &status
) == 0 /* get the status */
331 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
335 /* Find the first element of SEARCH_PATH which exists and is readable,
336 after expanding the %-escapes. Return 0 if we didn't find any, and
337 the path name of the one we found otherwise. */
340 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
341 char *search_path
, *class, *escaped_suffix
, *suffix
;
343 register char *s
, *p
;
345 for (s
= search_path
; *s
; s
= p
)
347 for (p
= s
; *p
&& *p
!= ':'; p
++)
352 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
361 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
373 /* Producing databases for individual sources. */
376 get_system_app (class)
379 XrmDatabase db
= NULL
;
382 path
= getenv ("XFILESEARCHPATH");
383 if (! path
) path
= PATH_X_DEFAULTS
;
385 path
= search_magic_path (path
, class, 0, 0);
388 db
= XrmGetFileDatabase (path
);
397 get_fallback (display
)
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)))
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))))
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 || (free_it
= gethomedir (),
426 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
427 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
429 XrmDatabase db
= XrmGetFileDatabase (file
);
443 get_user_db (display
)
449 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
450 xdefs
= XResourceManagerString (display
);
452 xdefs
= display
->xdefaults
;
456 db
= XrmGetStringDatabase (xdefs
);
462 home
= gethomedir ();
463 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
464 strcpy (xdefault
, home
);
465 strcat (xdefault
, ".Xdefaults");
466 db
= XrmGetFileDatabase (xdefault
);
471 #ifdef HAVE_XSCREENRESOURCESTRING
472 /* Get the screen-specific resources too. */
473 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
476 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
489 char *path
= 0, *home
= 0, *host
;
491 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
493 home
= gethomedir ();
494 host
= get_system_name ();
495 path
= (char *) malloc (strlen (home
)
496 + sizeof (".Xdefaults-")
498 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
502 db
= XrmGetFileDatabase (p
);
504 if (path
) free (path
);
505 if (home
) free (home
);
510 /* External interface. */
512 /* Types of values that we can find in a database */
514 #define XrmStringType "String" /* String representation */
515 XrmRepresentation x_rm_string
; /* Quark representation */
517 /* Load X resources based on the display and a possible -xrm option. */
520 x_load_resources (display
, xrm_string
, myname
, myclass
)
522 char *xrm_string
, *myname
, *myclass
;
524 XrmDatabase user_database
;
529 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
532 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
533 extern Lisp_Object Vdouble_click_time
;
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. */
542 rdb
= XrmGetStringDatabase ("");
544 /* Add some font defaults. If the font `helv' doesn't exist, widgets
545 will use some other default font. */
548 sprintf (line
, "%s.pane.background: grey75", myclass
);
549 XrmPutLineResource (&rdb
, line
);
550 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
551 XrmPutLineResource (&rdb
, line
);
552 sprintf (line
, "%s*menu*background: grey75", myclass
);
553 XrmPutLineResource (&rdb
, line
);
554 sprintf (line
, "%s*menubar*background: grey75", myclass
);
555 XrmPutLineResource (&rdb
, line
);
556 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
557 XrmPutLineResource (&rdb
, line
);
558 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
559 XrmPutLineResource (&rdb
, line
);
560 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
561 XrmPutLineResource (&rdb
, line
);
562 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
563 XrmPutLineResource (&rdb
, line
);
564 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
565 XrmPutLineResource (&rdb
, line
);
566 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
567 XrmPutLineResource (&rdb
, line
);
568 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
569 XrmPutLineResource (&rdb
, line
);
570 sprintf (line
, "%s*fsb*background: grey75", myclass
);
571 XrmPutLineResource (&rdb
, line
);
572 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
573 XrmPutLineResource (&rdb
, line
);
574 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
575 XrmPutLineResource (&rdb
, line
);
576 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
577 XrmPutLineResource (&rdb
, line
);
578 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
579 XrmPutLineResource (&rdb
, line
);
581 /* Set double click time of list boxes in the file selection
582 dialog from `double-click-time'. */
583 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
585 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
586 myclass
, XFASTINT (Vdouble_click_time
));
587 XrmPutLineResource (&rdb
, line
);
588 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
589 myclass
, XFASTINT (Vdouble_click_time
));
590 XrmPutLineResource (&rdb
, line
);
593 #else /* not USE_MOTIF */
595 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
596 XrmPutLineResource (&rdb
, line
);
597 sprintf (line
, "Emacs.dialog*.background: grey75");
598 XrmPutLineResource (&rdb
, line
);
599 sprintf (line
, "*XlwMenu*font: %s", helv
);
600 XrmPutLineResource (&rdb
, line
);
601 sprintf (line
, "*XlwMenu*background: grey75");
602 XrmPutLineResource (&rdb
, line
);
603 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
604 XrmPutLineResource (&rdb
, line
);
606 #endif /* not USE_MOTIF */
608 user_database
= get_user_db (display
);
610 /* Figure out what the "customization string" is, so we can use it
612 if (x_customization_string
)
613 free (x_customization_string
);
614 x_customization_string
615 = x_get_customization_string (user_database
, myname
, myclass
);
617 /* Get application system defaults */
618 db
= get_system_app (myclass
);
620 XrmMergeDatabases (db
, &rdb
);
622 /* Get Fallback resources */
623 db
= get_fallback (display
);
625 XrmMergeDatabases (db
, &rdb
);
627 /* Get application user defaults */
628 db
= get_user_app (myclass
);
630 XrmMergeDatabases (db
, &rdb
);
632 /* get User defaults */
633 if (user_database
!= NULL
)
634 XrmMergeDatabases (user_database
, &rdb
);
636 /* Get Environment defaults. */
637 db
= get_environ_db ();
639 XrmMergeDatabases (db
, &rdb
);
641 /* Last, merge in any specification from the command line. */
642 if (xrm_string
!= NULL
)
644 db
= XrmGetStringDatabase (xrm_string
);
646 XrmMergeDatabases (db
, &rdb
);
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. */
657 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
660 XrmRepresentation expected_type
;
664 XrmName namelist
[100];
665 XrmClass classlist
[100];
666 XrmRepresentation type
;
668 XrmStringToNameList(name
, namelist
);
669 XrmStringToClassList(class, classlist
);
671 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
672 && (type
== expected_type
))
674 if (type
== x_rm_string
)
675 ret_value
->addr
= (char *) value
.addr
;
677 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
685 /* Retrieve the string resource specified by NAME with CLASS from
689 x_get_string_resource (rdb
, name
, class)
695 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
696 return (char *) value
.addr
;
701 /* Stand-alone test facilities. */
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)
719 for (p
= list
; ! NIL (p
); p
= cdr (p
))
720 if (! strcmp (elt
, car (p
)))
727 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
729 int x1
, x2
, x3
, x4
, x5
;
736 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
745 char *displayname
, *resource_string
, *class, *name
;
749 arg_list
= arg_listify (argc
, argv
);
751 lp
= member ("-d", arg_list
);
753 displayname
= car (cdr (lp
));
755 displayname
= "localhost:0.0";
757 lp
= member ("-xrm", arg_list
);
759 resource_string
= car (cdr (lp
));
761 resource_string
= (char *) 0;
763 lp
= member ("-c", arg_list
);
765 class = car (cdr (lp
));
769 lp
= member ("-n", arg_list
);
771 name
= car (cdr (lp
));
775 free_arglist (arg_list
);
777 if (!(display
= XOpenDisplay (displayname
)))
778 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
780 xdb
= x_load_resources (display
, resource_string
, name
, class);
782 /* In a real program, you'd want to also do this: */
788 char query_class
[90];
793 if (strlen (query_name
))
800 value
= x_get_string_resource (xdb
, query_name
, query_class
);
803 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
805 printf ("\tNo Value.\n\n");
810 printf ("\tExit.\n\n");
812 XCloseDisplay (display
);
816 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
817 (do not change this comment) */