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 */
29 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
30 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
41 #include <X11/Xatom.h>
46 #include <X11/Xutil.h>
47 #include <X11/Xresource.h>
55 #if !defined(S_ISDIR) && defined(S_IFDIR)
56 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
59 extern char *getenv ();
61 /* This does cause trouble on AIX. I'm going to take the comment at
64 extern short getuid (); /* If this causes portability problems,
65 I think we should just delete it; it'll
66 default to `int' anyway. */
69 #ifdef DECLARE_GETPWUID_WITH_UID_T
70 extern struct passwd
*getpwuid (uid_t
);
71 extern struct passwd
*getpwnam (const char *);
73 extern struct passwd
*getpwuid ();
74 extern struct passwd
*getpwnam ();
77 extern char *get_system_name ();
79 /* Make sure not to #include anything after these definitions. Let's
80 not step on anyone's prototypes. */
82 #define malloc xmalloc
83 #define realloc xrealloc
87 char *x_get_string_resource ();
91 /* X file search path processing. */
94 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
95 and friends, or zero if none was specified. */
96 char *x_customization_string
;
99 /* Return the value of the emacs.customization (Emacs.Customization)
100 resource, for later use in search path decoding. If we find no
101 such resource, return zero. */
103 x_get_customization_string (db
, name
, class)
108 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
110 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
113 sprintf (full_name
, "%s.%s", name
, "customization");
114 sprintf (full_class
, "%s.%s", class, "Customization");
116 result
= x_get_string_resource (db
, full_name
, full_class
);
120 char *copy
= (char *) malloc (strlen (result
) + 1);
121 strcpy (copy
, result
);
129 /* Expand all the Xt-style %-escapes in STRING, whose length is given
130 by STRING_LEN. Here are the escapes we're supposed to recognize:
132 %N The value of the application's class name
133 %T The value of the type parameter ("app-defaults" in this
135 %S The value of the suffix parameter ("" in this context)
136 %L The language string associated with the specified display
137 (We use the "LANG" environment variable here, if it's set.)
138 %l The language part of the display's language string
139 (We treat this just like %L. If someone can tell us what
140 we're really supposed to do, dandy.)
141 %t The territory part of the display's language string
142 (This never gets used.)
143 %c The codeset part of the display's language string
144 (This never gets used either.)
145 %C The customization string retrieved from the resource
146 database associated with display.
147 (This is x_customization_string.)
149 Return the expanded file name if it exists and is readable, and
150 refers to %L only when the LANG environment variable is set, or
151 otherwise provided by X.
153 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
154 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
157 Return NULL otherwise. */
160 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
163 char *class, *escaped_suffix
, *suffix
;
165 char *lang
= getenv ("LANG");
168 char *path
= (char *) malloc (path_size
);
173 while (p
< string
+ string_len
)
175 /* The chunk we're about to stick on the end of result. */
183 if (p
>= string
+ string_len
)
194 next
= (x_customization_string
195 ? x_customization_string
197 next_len
= strlen (next
);
202 next_len
= strlen (class);
206 next
= "app-defaults";
207 next_len
= strlen (next
);
224 next_len
= strlen (next
);
234 next
= p
, next_len
= 1;
236 /* Do we have room for this component followed by a '\0' ? */
237 if (path_len
+ next_len
+ 1 > path_size
)
239 path_size
= (path_len
+ next_len
+ 1) * 2;
240 path
= (char *) realloc (path
, path_size
);
243 bcopy (next
, path
+ path_len
, next_len
);
244 path_len
+= next_len
;
248 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
249 if (p
>= string
+ string_len
&& escaped_suffix
)
251 string
= escaped_suffix
;
252 string_len
= strlen (string
);
254 escaped_suffix
= NULL
;
258 /* Perhaps we should add the SUFFIX now. */
261 int suffix_len
= strlen (suffix
);
263 if (path_len
+ suffix_len
+ 1 > path_size
)
265 path_size
= (path_len
+ suffix_len
+ 1);
266 path
= (char *) realloc (path
, path_size
);
269 bcopy (suffix
, path
+ path_len
, suffix_len
);
270 path_len
+= suffix_len
;
273 path
[path_len
] = '\0';
292 if ((ptr
= getenv ("HOME")) == NULL
)
294 if ((ptr
= getenv ("LOGNAME")) != NULL
295 || (ptr
= getenv ("USER")) != NULL
)
298 pw
= getpwuid (getuid ());
307 copy
= (char *) malloc (strlen (ptr
) + 2);
321 return (access (path
, 4) == 0 /* exists and is readable */
322 && stat (path
, &status
) == 0 /* get the status */
323 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
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. */
332 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
333 char *search_path
, *class, *escaped_suffix
, *suffix
;
335 register char *s
, *p
;
337 for (s
= search_path
; *s
; s
= p
)
339 for (p
= s
; *p
&& *p
!= ':'; p
++)
344 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
353 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
365 /* Producing databases for individual sources. */
367 #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"
370 get_system_app (class)
373 XrmDatabase db
= NULL
;
376 path
= getenv ("XFILESEARCHPATH");
377 if (! path
) path
= X_DEFAULT_SEARCH_PATH
;
379 path
= search_magic_path (path
, class, 0, 0);
382 db
= XrmGetFileDatabase (path
);
391 get_fallback (display
)
408 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
409 names, not directories. */
410 if (((path
= getenv ("XUSERFILESEARCHPATH"))
411 && (file
= search_magic_path (path
, class, 0, 0)))
413 /* Check for APPLRESDIR; it is a path of directories. In each,
414 we have to search for LANG/CLASS and then CLASS. */
415 || ((path
= getenv ("XAPPLRESDIR"))
416 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
417 || (file
= search_magic_path (path
, class, "/%N", 0))))
419 /* Check in the home directory. This is a bit of a hack; let's
420 hope one's home directory doesn't contain any %-escapes. */
421 || (free_it
= gethomedir (),
422 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
423 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
425 XrmDatabase db
= XrmGetFileDatabase (file
);
439 get_user_db (display
)
445 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
446 xdefs
= XResourceManagerString (display
);
448 xdefs
= display
->xdefaults
;
452 db
= XrmGetStringDatabase (xdefs
);
458 home
= gethomedir ();
459 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
460 strcpy (xdefault
, home
);
461 strcat (xdefault
, ".Xdefaults");
462 db
= XrmGetFileDatabase (xdefault
);
467 #ifdef HAVE_XSCREENRESOURCESTRING
468 /* Get the screen-specific resources too. */
469 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
472 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
485 char *path
= 0, *home
= 0, *host
;
487 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
489 home
= gethomedir ();
490 host
= get_system_name ();
491 path
= (char *) malloc (strlen (home
)
492 + sizeof (".Xdefaults-")
494 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
498 db
= XrmGetFileDatabase (p
);
500 if (path
) free (path
);
501 if (home
) free (home
);
506 /* External interface. */
508 /* Types of values that we can find in a database */
510 #define XrmStringType "String" /* String representation */
511 XrmRepresentation x_rm_string
; /* Quark representation */
513 /* Load X resources based on the display and a possible -xrm option. */
516 x_load_resources (display
, xrm_string
, myname
, myclass
)
518 char *xrm_string
, *myname
, *myclass
;
521 XrmDatabase user_database
;
525 x_rm_string
= XrmStringToQuark (XrmStringType
);
526 #ifndef USE_X_TOOLKIT
527 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
528 I suspect it's because the toolkit version does this elsewhere. */
531 rdb
= XrmGetStringDatabase ("");
533 user_database
= get_user_db (display
);
535 /* Figure out what the "customization string" is, so we can use it
537 if (x_customization_string
)
538 free (x_customization_string
);
539 x_customization_string
540 = x_get_customization_string (user_database
, myname
, myclass
);
542 /* Get application system defaults */
543 db
= get_system_app (myclass
);
545 XrmMergeDatabases (db
, &rdb
);
547 /* Get Fallback resources */
548 db
= get_fallback (display
);
550 XrmMergeDatabases (db
, &rdb
);
552 /* Get application user defaults */
553 db
= get_user_app (myclass
);
555 XrmMergeDatabases (db
, &rdb
);
557 /* get User defaults */
558 if (user_database
!= NULL
)
559 XrmMergeDatabases (user_database
, &rdb
);
561 /* Get Environment defaults. */
562 db
= get_environ_db ();
564 XrmMergeDatabases (db
, &rdb
);
566 /* Last, merge in any specification from the command line. */
567 if (xrm_string
!= NULL
)
569 db
= XrmGetStringDatabase (xrm_string
);
571 XrmMergeDatabases (db
, &rdb
);
578 /* Retrieve the value of the resource specified by NAME with class CLASS
579 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
582 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
585 XrmRepresentation expected_type
;
589 XrmName namelist
[100];
590 XrmClass classlist
[100];
591 XrmRepresentation type
;
593 XrmStringToNameList(name
, namelist
);
594 XrmStringToClassList(class, classlist
);
596 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
597 && (type
== expected_type
))
599 if (type
== x_rm_string
)
600 ret_value
->addr
= (char *) value
.addr
;
602 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
610 /* Retrieve the string resource specified by NAME with CLASS from
614 x_get_string_resource (rdb
, name
, class)
620 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
621 return (char *) value
.addr
;
626 /* Stand-alone test facilities. */
631 #define arg_listify(len, list) (list)
632 #define car(list) (*(list))
633 #define cdr(list) (list + 1)
634 #define NIL(list) (! *(list))
635 #define free_arglist(list)
644 for (p
= list
; ! NIL (p
); p
= cdr (p
))
645 if (! strcmp (elt
, car (p
)))
652 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
654 int x1
, x2
, x3
, x4
, x5
;
661 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
670 char *displayname
, *resource_string
, *class, *name
;
674 arg_list
= arg_listify (argc
, argv
);
676 lp
= member ("-d", arg_list
);
678 displayname
= car (cdr (lp
));
680 displayname
= "localhost:0.0";
682 lp
= member ("-xrm", arg_list
);
684 resource_string
= car (cdr (lp
));
686 resource_string
= (char *) 0;
688 lp
= member ("-c", arg_list
);
690 class = car (cdr (lp
));
694 lp
= member ("-n", arg_list
);
696 name
= car (cdr (lp
));
700 free_arglist (arg_list
);
702 if (!(display
= XOpenDisplay (displayname
)))
703 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
705 xdb
= x_load_resources (display
, resource_string
, name
, class);
707 /* In a real program, you'd want to also do this: */
713 char query_class
[90];
718 if (strlen (query_name
))
725 value
= x_get_string_resource (xdb
, query_name
, query_class
);
728 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
730 printf ("\tNo Value.\n\n");
735 printf ("\tExit.\n\n");
737 XCloseDisplay (display
);