1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 Author: Joseph Arceneaux
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
36 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
37 /* This avoids lossage in the `dual-universe' headers on AT&T SysV
38 X11. Don't do it on Solaris, because it breaks compilation with
39 XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
41 #if defined(USG5) && !defined(SOLARIS2)
45 #endif /* USG5 && !SOLARIS2 */
50 #include <X11/Xatom.h>
55 #include <X11/Xutil.h>
56 #include <X11/Xresource.h>
62 #if !defined(S_ISDIR) && defined(S_IFDIR)
63 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
68 extern char *getenv ();
70 /* This does cause trouble on AIX. I'm going to take the comment at
73 extern short getuid (); /* If this causes portability problems,
74 I think we should just delete it; it'll
75 default to `int' anyway. */
78 #ifdef DECLARE_GETPWUID_WITH_UID_T
79 extern struct passwd
*getpwuid (uid_t
);
80 extern struct passwd
*getpwnam (const char *);
82 extern struct passwd
*getpwuid ();
83 extern struct passwd
*getpwnam ();
86 extern char *get_system_name ();
88 /* Make sure not to #include anything after these definitions. Let's
89 not step on anyone's prototypes. */
91 /* darwin.h may have already defined these. */
95 #define malloc xmalloc
96 #define realloc xrealloc
100 char *x_get_string_resource ();
101 static int file_p ();
104 /* X file search path processing. */
107 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
108 and friends, or zero if none was specified. */
109 char *x_customization_string
;
112 /* Return the value of the emacs.customization (Emacs.Customization)
113 resource, for later use in search path decoding. If we find no
114 such resource, return zero. */
116 x_get_customization_string (db
, name
, class)
121 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
123 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
126 sprintf (full_name
, "%s.%s", name
, "customization");
127 sprintf (full_class
, "%s.%s", class, "Customization");
129 result
= x_get_string_resource (db
, full_name
, full_class
);
133 char *copy
= (char *) malloc (strlen (result
) + 1);
134 strcpy (copy
, result
);
142 /* Expand all the Xt-style %-escapes in STRING, whose length is given
143 by STRING_LEN. Here are the escapes we're supposed to recognize:
145 %N The value of the application's class name
146 %T The value of the type parameter ("app-defaults" in this
148 %S The value of the suffix parameter ("" in this context)
149 %L The language string associated with the specified display
150 (We use the "LANG" environment variable here, if it's set.)
151 %l The language part of the display's language string
152 (We treat this just like %L. If someone can tell us what
153 we're really supposed to do, dandy.)
154 %t The territory part of the display's language string
155 (This never gets used.)
156 %c The codeset part of the display's language string
157 (This never gets used either.)
158 %C The customization string retrieved from the resource
159 database associated with display.
160 (This is x_customization_string.)
162 Return the expanded file name if it exists and is readable, and
163 refers to %L only when the LANG environment variable is set, or
164 otherwise provided by X.
166 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
167 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
170 Return NULL otherwise. */
173 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
176 char *class, *escaped_suffix
, *suffix
;
178 char *lang
= getenv ("LANG");
181 char *path
= (char *) malloc (path_size
);
186 while (p
< string
+ string_len
)
188 /* The chunk we're about to stick on the end of result. */
196 if (p
>= string
+ string_len
)
207 next
= (x_customization_string
208 ? x_customization_string
210 next_len
= strlen (next
);
215 next_len
= strlen (class);
219 next
= "app-defaults";
220 next_len
= strlen (next
);
237 next_len
= strlen (next
);
247 next
= p
, next_len
= 1;
249 /* Do we have room for this component followed by a '\0' ? */
250 if (path_len
+ next_len
+ 1 > path_size
)
252 path_size
= (path_len
+ next_len
+ 1) * 2;
253 path
= (char *) realloc (path
, path_size
);
256 bcopy (next
, path
+ path_len
, next_len
);
257 path_len
+= next_len
;
261 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
262 if (p
>= string
+ string_len
&& escaped_suffix
)
264 string
= escaped_suffix
;
265 string_len
= strlen (string
);
267 escaped_suffix
= NULL
;
271 /* Perhaps we should add the SUFFIX now. */
274 int suffix_len
= strlen (suffix
);
276 if (path_len
+ suffix_len
+ 1 > path_size
)
278 path_size
= (path_len
+ suffix_len
+ 1);
279 path
= (char *) realloc (path
, path_size
);
282 bcopy (suffix
, path
+ path_len
, suffix_len
);
283 path_len
+= suffix_len
;
286 path
[path_len
] = '\0';
305 if ((ptr
= getenv ("HOME")) == NULL
)
307 if ((ptr
= getenv ("LOGNAME")) != NULL
308 || (ptr
= getenv ("USER")) != NULL
)
311 pw
= getpwuid (getuid ());
318 return xstrdup ("/");
320 copy
= (char *) malloc (strlen (ptr
) + 2);
334 return (access (filename
, 4) == 0 /* exists and is readable */
335 && stat (filename
, &status
) == 0 /* get the status */
336 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
340 /* Find the first element of SEARCH_PATH which exists and is readable,
341 after expanding the %-escapes. Return 0 if we didn't find any, and
342 the path name of the one we found otherwise. */
345 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
346 char *search_path
, *class, *escaped_suffix
, *suffix
;
348 register char *s
, *p
;
350 for (s
= search_path
; *s
; s
= p
)
352 for (p
= s
; *p
&& *p
!= ':'; p
++)
357 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
366 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
378 /* Producing databases for individual sources. */
381 get_system_app (class)
384 XrmDatabase db
= NULL
;
387 path
= getenv ("XFILESEARCHPATH");
388 if (! path
) path
= PATH_X_DEFAULTS
;
390 path
= search_magic_path (path
, class, 0, 0);
393 db
= XrmGetFileDatabase (path
);
402 get_fallback (display
)
417 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
418 names, not directories. */
419 if (((path
= getenv ("XUSERFILESEARCHPATH"))
420 && (file
= search_magic_path (path
, class, 0, 0)))
422 /* Check for APPLRESDIR; it is a path of directories. In each,
423 we have to search for LANG/CLASS and then CLASS. */
424 || ((path
= getenv ("XAPPLRESDIR"))
425 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
426 || (file
= search_magic_path (path
, class, "/%N", 0))))
428 /* Check in the home directory. This is a bit of a hack; let's
429 hope one's home directory doesn't contain any %-escapes. */
430 || (free_it
= gethomedir (),
431 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
432 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
434 XrmDatabase db
= XrmGetFileDatabase (file
);
446 get_user_db (display
)
452 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
453 xdefs
= XResourceManagerString (display
);
455 xdefs
= display
->xdefaults
;
459 db
= XrmGetStringDatabase (xdefs
);
465 home
= gethomedir ();
466 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
467 strcpy (xdefault
, home
);
468 strcat (xdefault
, ".Xdefaults");
469 db
= XrmGetFileDatabase (xdefault
);
474 #ifdef HAVE_XSCREENRESOURCESTRING
475 /* Get the screen-specific resources too. */
476 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
479 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
492 char *path
= 0, *home
= 0, *host
;
494 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
496 home
= gethomedir ();
497 host
= get_system_name ();
498 path
= (char *) malloc (strlen (home
)
499 + sizeof (".Xdefaults-")
501 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
505 db
= XrmGetFileDatabase (p
);
513 /* External interface. */
515 /* Types of values that we can find in a database */
517 #define XrmStringType "String" /* String representation */
518 XrmRepresentation x_rm_string
; /* Quark representation */
520 /* Load X resources based on the display and a possible -xrm option. */
523 x_load_resources (display
, xrm_string
, myname
, myclass
)
525 char *xrm_string
, *myname
, *myclass
;
527 XrmDatabase user_database
;
532 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
535 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
536 extern Lisp_Object Vdouble_click_time
;
539 x_rm_string
= XrmStringToQuark (XrmStringType
);
540 #ifndef USE_X_TOOLKIT
541 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
542 I suspect it's because the toolkit version does this elsewhere. */
545 rdb
= XrmGetStringDatabase ("");
547 /* Add some font defaults. If the font `helv' doesn't exist, widgets
548 will use some other default font. */
551 sprintf (line
, "%s.pane.background: grey75", myclass
);
552 XrmPutLineResource (&rdb
, line
);
553 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
554 XrmPutLineResource (&rdb
, line
);
555 sprintf (line
, "%s*menu*background: grey75", myclass
);
556 XrmPutLineResource (&rdb
, line
);
557 sprintf (line
, "%s*menubar*background: grey75", myclass
);
558 XrmPutLineResource (&rdb
, line
);
559 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
560 XrmPutLineResource (&rdb
, line
);
561 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
562 XrmPutLineResource (&rdb
, line
);
563 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
564 XrmPutLineResource (&rdb
, line
);
565 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
566 XrmPutLineResource (&rdb
, line
);
567 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
568 XrmPutLineResource (&rdb
, line
);
569 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
570 XrmPutLineResource (&rdb
, line
);
571 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
572 XrmPutLineResource (&rdb
, line
);
573 sprintf (line
, "%s*fsb*background: grey75", myclass
);
574 XrmPutLineResource (&rdb
, line
);
575 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
576 XrmPutLineResource (&rdb
, line
);
577 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
578 XrmPutLineResource (&rdb
, line
);
579 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
580 XrmPutLineResource (&rdb
, line
);
581 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
582 XrmPutLineResource (&rdb
, line
);
584 /* Set double click time of list boxes in the file selection
585 dialog from `double-click-time'. */
586 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
588 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
589 myclass
, XFASTINT (Vdouble_click_time
));
590 XrmPutLineResource (&rdb
, line
);
591 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
592 myclass
, XFASTINT (Vdouble_click_time
));
593 XrmPutLineResource (&rdb
, line
);
596 #else /* not USE_MOTIF */
598 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
599 XrmPutLineResource (&rdb
, line
);
600 sprintf (line
, "Emacs.dialog*.background: grey75");
601 XrmPutLineResource (&rdb
, line
);
602 sprintf (line
, "*XlwMenu*font: %s", helv
);
603 XrmPutLineResource (&rdb
, line
);
604 sprintf (line
, "*XlwMenu*background: grey75");
605 XrmPutLineResource (&rdb
, line
);
606 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
607 XrmPutLineResource (&rdb
, line
);
609 #endif /* not USE_MOTIF */
611 user_database
= get_user_db (display
);
613 /* Figure out what the "customization string" is, so we can use it
615 free (x_customization_string
);
616 x_customization_string
617 = x_get_customization_string (user_database
, myname
, myclass
);
619 /* Get application system defaults */
620 db
= get_system_app (myclass
);
622 XrmMergeDatabases (db
, &rdb
);
624 /* Get Fallback resources */
625 db
= get_fallback (display
);
627 XrmMergeDatabases (db
, &rdb
);
629 /* Get application user defaults */
630 db
= get_user_app (myclass
);
632 XrmMergeDatabases (db
, &rdb
);
634 /* get User defaults */
635 if (user_database
!= NULL
)
636 XrmMergeDatabases (user_database
, &rdb
);
638 /* Get Environment defaults. */
639 db
= get_environ_db ();
641 XrmMergeDatabases (db
, &rdb
);
643 /* Last, merge in any specification from the command line. */
644 if (xrm_string
!= NULL
)
646 db
= XrmGetStringDatabase (xrm_string
);
648 XrmMergeDatabases (db
, &rdb
);
655 /* Retrieve the value of the resource specified by NAME with class CLASS
656 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
659 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
662 XrmRepresentation expected_type
;
666 XrmName namelist
[100];
667 XrmClass classlist
[100];
668 XrmRepresentation type
;
670 XrmStringToNameList(name
, namelist
);
671 XrmStringToClassList(class, classlist
);
673 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
674 && (type
== expected_type
))
676 if (type
== x_rm_string
)
677 ret_value
->addr
= (char *) value
.addr
;
679 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
687 /* Retrieve the string resource specified by NAME with CLASS from
691 x_get_string_resource (rdb
, name
, class)
697 if (inhibit_x_resources
)
698 /* --quick was passed, so this is a no-op. */
701 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
702 return (char *) value
.addr
;
707 /* Stand-alone test facilities. */
712 #define arg_listify(len, list) (list)
713 #define car(list) (*(list))
714 #define cdr(list) (list + 1)
715 #define NIL(list) (! *(list))
716 #define free_arglist(list)
725 for (p
= list
; ! NIL (p
); p
= cdr (p
))
726 if (! strcmp (elt
, car (p
)))
733 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
735 int x1
, x2
, x3
, x4
, x5
;
742 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
751 char *displayname
, *resource_string
, *class, *name
;
755 arg_list
= arg_listify (argc
, argv
);
757 lp
= member ("-d", arg_list
);
759 displayname
= car (cdr (lp
));
761 displayname
= "localhost:0.0";
763 lp
= member ("-xrm", arg_list
);
765 resource_string
= car (cdr (lp
));
767 resource_string
= (char *) 0;
769 lp
= member ("-c", arg_list
);
771 class = car (cdr (lp
));
775 lp
= member ("-n", arg_list
);
777 name
= car (cdr (lp
));
781 free_arglist (arg_list
);
783 if (!(display
= XOpenDisplay (displayname
)))
784 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
786 xdb
= x_load_resources (display
, resource_string
, name
, class);
788 /* In a real program, you'd want to also do this: */
794 char query_class
[90];
799 if (strlen (query_name
))
806 value
= x_get_string_resource (xdb
, query_name
, query_class
);
809 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
811 printf ("\tNo Value.\n\n");
816 printf ("\tExit.\n\n");
818 XCloseDisplay (display
);
822 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
823 (do not change this comment) */