1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994 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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 X11 */
47 #include <X11/Xatom.h>
52 #include <X11/Xutil.h>
53 #include <X11/Xresource.h>
61 #if !defined(S_ISDIR) && defined(S_IFDIR)
62 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
65 extern char *getenv ();
67 /* This does cause trouble on AIX. I'm going to take the comment at
70 extern short getuid (); /* If this causes portability problems,
71 I think we should just delete it; it'll
72 default to `int' anyway. */
75 #ifdef DECLARE_GETPWUID_WITH_UID_T
76 extern struct passwd
*getpwuid (uid_t
);
77 extern struct passwd
*getpwnam (const char *);
79 extern struct passwd
*getpwuid ();
80 extern struct passwd
*getpwnam ();
83 extern char *get_system_name ();
85 /* Make sure not to #include anything after these definitions. Let's
86 not step on anyone's prototypes. */
88 #define malloc xmalloc
89 #define realloc xrealloc
91 extern long *xmalloc (), *xrealloc ();
94 char *x_get_string_resource ();
98 /* X file search path processing. */
101 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
102 and friends, or zero if none was specified. */
103 char *x_customization_string
;
106 /* Return the value of the emacs.customization (Emacs.Customization)
107 resource, for later use in search path decoding. If we find no
108 such resource, return zero. */
110 x_get_customization_string (db
, name
, class)
115 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
117 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
120 sprintf (full_name
, "%s.%s", name
, "customization");
121 sprintf (full_class
, "%s.%s", class, "Customization");
123 result
= x_get_string_resource (db
, full_name
, full_class
);
127 char *copy
= (char *) malloc (strlen (result
) + 1);
128 strcpy (copy
, result
);
136 /* Expand all the Xt-style %-escapes in STRING, whose length is given
137 by STRING_LEN. Here are the escapes we're supposed to recognize:
139 %N The value of the application's class name
140 %T The value of the type parameter ("app-defaults" in this
142 %S The value of the suffix parameter ("" in this context)
143 %L The language string associated with the specified display
144 (We use the "LANG" environment variable here, if it's set.)
145 %l The language part of the display's language string
146 (We treat this just like %L. If someone can tell us what
147 we're really supposed to do, dandy.)
148 %t The territory part of the display's language string
149 (This never gets used.)
150 %c The codeset part of the display's language string
151 (This never gets used either.)
152 %C The customization string retrieved from the resource
153 database associated with display.
154 (This is x_customization_string.)
156 Return the expanded file name if it exists and is readable, and
157 refers to %L only when the LANG environment variable is set, or
158 otherwise provided by X.
160 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
161 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
164 Return NULL otherwise. */
167 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
170 char *class, *escaped_suffix
, *suffix
;
172 char *lang
= getenv ("LANG");
175 char *path
= (char *) malloc (path_size
);
180 while (p
< string
+ string_len
)
182 /* The chunk we're about to stick on the end of result. */
190 if (p
>= string
+ string_len
)
201 next
= (x_customization_string
202 ? x_customization_string
204 next_len
= strlen (next
);
209 next_len
= strlen (class);
213 next
= "app-defaults";
214 next_len
= strlen (next
);
231 next_len
= strlen (next
);
241 next
= p
, next_len
= 1;
243 /* Do we have room for this component followed by a '\0' ? */
244 if (path_len
+ next_len
+ 1 > path_size
)
246 path_size
= (path_len
+ next_len
+ 1) * 2;
247 path
= (char *) realloc (path
, path_size
);
250 bcopy (next
, path
+ path_len
, next_len
);
251 path_len
+= next_len
;
255 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
256 if (p
>= string
+ string_len
&& escaped_suffix
)
258 string
= escaped_suffix
;
259 string_len
= strlen (string
);
261 escaped_suffix
= NULL
;
265 /* Perhaps we should add the SUFFIX now. */
268 int suffix_len
= strlen (suffix
);
270 if (path_len
+ suffix_len
+ 1 > path_size
)
272 path_size
= (path_len
+ suffix_len
+ 1);
273 path
= (char *) realloc (path
, path_size
);
276 bcopy (suffix
, path
+ path_len
, suffix_len
);
277 path_len
+= suffix_len
;
280 path
[path_len
] = '\0';
299 if ((ptr
= getenv ("HOME")) == NULL
)
301 if ((ptr
= getenv ("LOGNAME")) != NULL
302 || (ptr
= getenv ("USER")) != NULL
)
305 pw
= getpwuid (getuid ());
314 copy
= (char *) malloc (strlen (ptr
) + 2);
328 return (access (path
, 4) == 0 /* exists and is readable */
329 && stat (path
, &status
) == 0 /* get the status */
330 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
334 /* Find the first element of SEARCH_PATH which exists and is readable,
335 after expanding the %-escapes. Return 0 if we didn't find any, and
336 the path name of the one we found otherwise. */
339 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
340 char *search_path
, *class, *escaped_suffix
, *suffix
;
342 register char *s
, *p
;
344 for (s
= search_path
; *s
; s
= p
)
346 for (p
= s
; *p
&& *p
!= ':'; p
++)
351 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
360 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
372 /* Producing databases for individual sources. */
375 get_system_app (class)
378 XrmDatabase db
= NULL
;
381 path
= getenv ("XFILESEARCHPATH");
382 if (! path
) path
= PATH_X_DEFAULTS
;
384 path
= search_magic_path (path
, class, 0, 0);
387 db
= XrmGetFileDatabase (path
);
396 get_fallback (display
)
413 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
414 names, not directories. */
415 if (((path
= getenv ("XUSERFILESEARCHPATH"))
416 && (file
= search_magic_path (path
, class, 0, 0)))
418 /* Check for APPLRESDIR; it is a path of directories. In each,
419 we have to search for LANG/CLASS and then CLASS. */
420 || ((path
= getenv ("XAPPLRESDIR"))
421 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
422 || (file
= search_magic_path (path
, class, "/%N", 0))))
424 /* Check in the home directory. This is a bit of a hack; let's
425 hope one's home directory doesn't contain any %-escapes. */
426 || (free_it
= gethomedir (),
427 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
428 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
430 XrmDatabase db
= XrmGetFileDatabase (file
);
444 get_user_db (display
)
450 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
451 xdefs
= XResourceManagerString (display
);
453 xdefs
= display
->xdefaults
;
457 db
= XrmGetStringDatabase (xdefs
);
463 home
= gethomedir ();
464 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
465 strcpy (xdefault
, home
);
466 strcat (xdefault
, ".Xdefaults");
467 db
= XrmGetFileDatabase (xdefault
);
472 #ifdef HAVE_XSCREENRESOURCESTRING
473 /* Get the screen-specific resources too. */
474 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
477 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
490 char *path
= 0, *home
= 0, *host
;
492 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
494 home
= gethomedir ();
495 host
= get_system_name ();
496 path
= (char *) malloc (strlen (home
)
497 + sizeof (".Xdefaults-")
499 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
503 db
= XrmGetFileDatabase (p
);
505 if (path
) free (path
);
506 if (home
) free (home
);
511 /* External interface. */
513 /* Types of values that we can find in a database */
515 #define XrmStringType "String" /* String representation */
516 XrmRepresentation x_rm_string
; /* Quark representation */
518 /* Load X resources based on the display and a possible -xrm option. */
521 x_load_resources (display
, xrm_string
, myname
, myclass
)
523 char *xrm_string
, *myname
, *myclass
;
526 XrmDatabase user_database
;
530 x_rm_string
= XrmStringToQuark (XrmStringType
);
531 #ifndef USE_X_TOOLKIT
532 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
533 I suspect it's because the toolkit version does this elsewhere. */
536 rdb
= XrmGetStringDatabase ("");
538 user_database
= get_user_db (display
);
540 /* Figure out what the "customization string" is, so we can use it
542 if (x_customization_string
)
543 free (x_customization_string
);
544 x_customization_string
545 = x_get_customization_string (user_database
, myname
, myclass
);
547 /* Get application system defaults */
548 db
= get_system_app (myclass
);
550 XrmMergeDatabases (db
, &rdb
);
552 /* Get Fallback resources */
553 db
= get_fallback (display
);
555 XrmMergeDatabases (db
, &rdb
);
557 /* Get application user defaults */
558 db
= get_user_app (myclass
);
560 XrmMergeDatabases (db
, &rdb
);
562 /* get User defaults */
563 if (user_database
!= NULL
)
564 XrmMergeDatabases (user_database
, &rdb
);
566 /* Get Environment defaults. */
567 db
= get_environ_db ();
569 XrmMergeDatabases (db
, &rdb
);
571 /* Last, merge in any specification from the command line. */
572 if (xrm_string
!= NULL
)
574 db
= XrmGetStringDatabase (xrm_string
);
576 XrmMergeDatabases (db
, &rdb
);
583 /* Retrieve the value of the resource specified by NAME with class CLASS
584 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
587 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
590 XrmRepresentation expected_type
;
594 XrmName namelist
[100];
595 XrmClass classlist
[100];
596 XrmRepresentation type
;
598 XrmStringToNameList(name
, namelist
);
599 XrmStringToClassList(class, classlist
);
601 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
602 && (type
== expected_type
))
604 if (type
== x_rm_string
)
605 ret_value
->addr
= (char *) value
.addr
;
607 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
615 /* Retrieve the string resource specified by NAME with CLASS from
619 x_get_string_resource (rdb
, name
, class)
625 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
626 return (char *) value
.addr
;
631 /* Stand-alone test facilities. */
636 #define arg_listify(len, list) (list)
637 #define car(list) (*(list))
638 #define cdr(list) (list + 1)
639 #define NIL(list) (! *(list))
640 #define free_arglist(list)
649 for (p
= list
; ! NIL (p
); p
= cdr (p
))
650 if (! strcmp (elt
, car (p
)))
657 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
659 int x1
, x2
, x3
, x4
, x5
;
666 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
675 char *displayname
, *resource_string
, *class, *name
;
679 arg_list
= arg_listify (argc
, argv
);
681 lp
= member ("-d", arg_list
);
683 displayname
= car (cdr (lp
));
685 displayname
= "localhost:0.0";
687 lp
= member ("-xrm", arg_list
);
689 resource_string
= car (cdr (lp
));
691 resource_string
= (char *) 0;
693 lp
= member ("-c", arg_list
);
695 class = car (cdr (lp
));
699 lp
= member ("-n", arg_list
);
701 name
= car (cdr (lp
));
705 free_arglist (arg_list
);
707 if (!(display
= XOpenDisplay (displayname
)))
708 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
710 xdb
= x_load_resources (display
, resource_string
, name
, class);
712 /* In a real program, you'd want to also do this: */
718 char query_class
[90];
723 if (strlen (query_name
))
730 value
= x_get_string_resource (xdb
, query_name
, query_class
);
733 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
735 printf ("\tNo Value.\n\n");
740 printf ("\tExit.\n\n");
742 XCloseDisplay (display
);