1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994 Free Software Foundation.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by jla, 4/90 */
26 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
27 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
38 #include <X11/Xatom.h>
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
52 #if !defined(S_ISDIR) && defined(S_IFDIR)
53 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
56 extern char *getenv ();
58 /* This does cause trouble on AIX. I'm going to take the comment at
61 extern short getuid (); /* If this causes portability problems,
62 I think we should just delete it; it'll
63 default to `int' anyway. */
66 #ifdef DECLARE_GETPWUID_WITH_UID_T
67 extern struct passwd
*getpwuid (uid_t
);
68 extern struct passwd
*getpwnam (const char *);
70 extern struct passwd
*getpwuid ();
71 extern struct passwd
*getpwnam ();
74 extern char *get_system_name ();
76 /* Make sure not to #include anything after these definitions. Let's
77 not step on anyone's prototypes. */
79 #define malloc xmalloc
80 #define realloc xrealloc
84 char *x_get_string_resource ();
88 /* X file search path processing. */
91 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
92 and friends, or zero if none was specified. */
93 char *x_customization_string
;
96 /* Return the value of the emacs.customization (Emacs.Customization)
97 resource, for later use in search path decoding. If we find no
98 such resource, return zero. */
100 x_get_customization_string (db
, name
, class)
105 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
107 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
110 sprintf (full_name
, "%s.%s", name
, "customization");
111 sprintf (full_class
, "%s.%s", class, "Customization");
113 result
= x_get_string_resource (db
, full_name
, full_class
);
117 char *copy
= (char *) malloc (strlen (result
) + 1);
118 strcpy (copy
, result
);
126 /* Expand all the Xt-style %-escapes in STRING, whose length is given
127 by STRING_LEN. Here are the escapes we're supposed to recognize:
129 %N The value of the application's class name
130 %T The value of the type parameter ("app-defaults" in this
132 %S The value of the suffix parameter ("" in this context)
133 %L The language string associated with the specified display
134 (We use the "LANG" environment variable here, if it's set.)
135 %l The language part of the display's language string
136 (We treat this just like %L. If someone can tell us what
137 we're really supposed to do, dandy.)
138 %t The territory part of the display's language string
139 (This never gets used.)
140 %c The codeset part of the display's language string
141 (This never gets used either.)
142 %C The customization string retrieved from the resource
143 database associated with display.
144 (This is x_customization_string.)
146 Return the expanded file name if it exists and is readable, and
147 refers to %L only when the LANG environment variable is set, or
148 otherwise provided by X.
150 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
151 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
154 Return NULL otherwise. */
157 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
160 char *class, *escaped_suffix
, *suffix
;
162 char *lang
= getenv ("LANG");
165 char *path
= (char *) malloc (path_size
);
170 while (p
< string
+ string_len
)
172 /* The chunk we're about to stick on the end of result. */
180 if (p
>= string
+ string_len
)
191 next
= (x_customization_string
192 ? x_customization_string
194 next_len
= strlen (next
);
199 next_len
= strlen (class);
203 next
= "app-defaults";
204 next_len
= strlen (next
);
221 next_len
= strlen (next
);
231 next
= p
, next_len
= 1;
233 /* Do we have room for this component followed by a '\0' ? */
234 if (path_len
+ next_len
+ 1 > path_size
)
236 path_size
= (path_len
+ next_len
+ 1) * 2;
237 path
= (char *) realloc (path
, path_size
);
240 bcopy (next
, path
+ path_len
, next_len
);
241 path_len
+= next_len
;
245 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
246 if (p
>= string
+ string_len
&& escaped_suffix
)
248 string
= escaped_suffix
;
249 string_len
= strlen (string
);
251 escaped_suffix
= NULL
;
255 /* Perhaps we should add the SUFFIX now. */
258 int suffix_len
= strlen (suffix
);
260 if (path_len
+ suffix_len
+ 1 > path_size
)
262 path_size
= (path_len
+ suffix_len
+ 1);
263 path
= (char *) realloc (path
, path_size
);
266 bcopy (suffix
, path
+ path_len
, suffix_len
);
267 path_len
+= suffix_len
;
270 path
[path_len
] = '\0';
289 if ((ptr
= getenv ("HOME")) == NULL
)
291 if ((ptr
= getenv ("LOGNAME")) != NULL
292 || (ptr
= getenv ("USER")) != NULL
)
295 pw
= getpwuid (getuid ());
304 copy
= (char *) malloc (strlen (ptr
) + 2);
318 return (access (path
, 4) == 0 /* exists and is readable */
319 && stat (path
, &status
) == 0 /* get the status */
320 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
324 /* Find the first element of SEARCH_PATH which exists and is readable,
325 after expanding the %-escapes. Return 0 if we didn't find any, and
326 the path name of the one we found otherwise. */
329 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
330 char *search_path
, *class, *escaped_suffix
, *suffix
;
332 register char *s
, *p
;
334 for (s
= search_path
; *s
; s
= p
)
336 for (p
= s
; *p
&& *p
!= ':'; p
++)
341 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
350 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
362 /* Producing databases for individual sources. */
364 #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"
367 get_system_app (class)
370 XrmDatabase db
= NULL
;
373 path
= getenv ("XFILESEARCHPATH");
374 if (! path
) path
= X_DEFAULT_SEARCH_PATH
;
376 path
= search_magic_path (path
, class, 0, 0);
379 db
= XrmGetFileDatabase (path
);
388 get_fallback (display
)
404 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
405 names, not directories. */
406 if (((path
= getenv ("XUSERFILESEARCHPATH"))
407 && (file
= search_magic_path (path
, class, 0, 0)))
409 /* Check for APPLRESDIR; it is a path of directories. In each,
410 we have to search for LANG/CLASS and then CLASS. */
411 || ((path
= getenv ("XAPPLRESDIR"))
412 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
413 || (file
= search_magic_path (path
, class, "/%N", 0))))
415 /* Check in the home directory. This is a bit of a hack; let's
416 hope one's home directory doesn't contain any %-escapes. */
417 || (path
= gethomedir (),
418 ((file
= search_magic_path (path
, class, "%L/%N", 0))
419 || (file
= search_magic_path (path
, class, "%N", 0)))))
421 XrmDatabase db
= XrmGetFileDatabase (file
);
431 get_user_db (display
)
437 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
438 xdefs
= XResourceManagerString (display
);
440 xdefs
= display
->xdefaults
;
444 db
= XrmGetStringDatabase (xdefs
);
450 home
= gethomedir ();
451 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
452 strcpy (xdefault
, home
);
453 strcat (xdefault
, ".Xdefaults");
454 db
= XrmGetFileDatabase (xdefault
);
459 #ifdef HAVE_XSCREENRESOURCESTRING
460 /* Get the screen-specific resources too. */
461 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
464 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
477 char *path
= 0, *home
= 0, *host
;
479 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
481 home
= gethomedir ();
482 host
= get_system_name ();
483 path
= (char *) malloc (strlen (home
)
484 + sizeof (".Xdefaults-")
486 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
490 db
= XrmGetFileDatabase (p
);
492 if (path
) free (path
);
493 if (home
) free (home
);
498 /* External interface. */
500 /* Types of values that we can find in a database */
502 #define XrmStringType "String" /* String representation */
503 XrmRepresentation x_rm_string
; /* Quark representation */
505 /* Load X resources based on the display and a possible -xrm option. */
508 x_load_resources (display
, xrm_string
, myname
, myclass
)
510 char *xrm_string
, *myname
, *myclass
;
513 XrmDatabase user_database
;
517 x_rm_string
= XrmStringToQuark (XrmStringType
);
518 #ifndef USE_X_TOOLKIT
519 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
520 I suspect it's because the toolkit version does this elsewhere. */
523 rdb
= XrmGetStringDatabase ("");
525 user_database
= get_user_db (display
);
527 /* Figure out what the "customization string" is, so we can use it
529 if (x_customization_string
)
530 free (x_customization_string
);
531 x_customization_string
532 = x_get_customization_string (user_database
, myname
, myclass
);
534 /* Get application system defaults */
535 db
= get_system_app (myclass
);
537 XrmMergeDatabases (db
, &rdb
);
539 /* Get Fallback resources */
540 db
= get_fallback (display
);
542 XrmMergeDatabases (db
, &rdb
);
544 /* Get application user defaults */
545 db
= get_user_app (myclass
);
547 XrmMergeDatabases (db
, &rdb
);
549 /* get User defaults */
550 if (user_database
!= NULL
)
551 XrmMergeDatabases (user_database
, &rdb
);
553 /* Get Environment defaults. */
554 db
= get_environ_db ();
556 XrmMergeDatabases (db
, &rdb
);
558 /* Last, merge in any specification from the command line. */
559 if (xrm_string
!= NULL
)
561 db
= XrmGetStringDatabase (xrm_string
);
563 XrmMergeDatabases (db
, &rdb
);
570 /* Retrieve the value of the resource specified by NAME with class CLASS
571 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
574 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
577 XrmRepresentation expected_type
;
581 XrmName namelist
[100];
582 XrmClass classlist
[100];
583 XrmRepresentation type
;
585 XrmStringToNameList(name
, namelist
);
586 XrmStringToClassList(class, classlist
);
588 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
589 && (type
== expected_type
))
591 if (type
== x_rm_string
)
592 ret_value
->addr
= (char *) value
.addr
;
594 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
602 /* Retrieve the string resource specified by NAME with CLASS from
606 x_get_string_resource (rdb
, name
, class)
612 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
613 return (char *) value
.addr
;
618 /* Stand-alone test facilities. */
623 #define arg_listify(len, list) (list)
624 #define car(list) (*(list))
625 #define cdr(list) (list + 1)
626 #define NIL(list) (! *(list))
627 #define free_arglist(list)
636 for (p
= list
; ! NIL (p
); p
= cdr (p
))
637 if (! strcmp (elt
, car (p
)))
644 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
646 int x1
, x2
, x3
, x4
, x5
;
653 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
662 char *displayname
, *resource_string
, *class, *name
;
666 arg_list
= arg_listify (argc
, argv
);
668 lp
= member ("-d", arg_list
);
670 displayname
= car (cdr (lp
));
672 displayname
= "localhost:0.0";
674 lp
= member ("-xrm", arg_list
);
676 resource_string
= car (cdr (lp
));
678 resource_string
= (char *) 0;
680 lp
= member ("-c", arg_list
);
682 class = car (cdr (lp
));
686 lp
= member ("-n", arg_list
);
688 name
= car (cdr (lp
));
692 free_arglist (arg_list
);
694 if (!(display
= XOpenDisplay (displayname
)))
695 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
697 xdb
= x_load_resources (display
, resource_string
, name
, class);
699 /* In a real program, you'd want to also do this: */
705 char query_class
[90];
710 if (strlen (query_name
))
717 value
= x_get_string_resource (xdb
, query_name
, query_class
);
720 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
722 printf ("\tNo Value.\n\n");
727 printf ("\tExit.\n\n");
729 XCloseDisplay (display
);