1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993 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 */
35 /* This should be included before the X include files; otherwise, we get
36 warnings about redefining NULL under BSD 4.3. */
37 #include <sys/param.h>
40 #include <X11/Xatom.h>
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
55 #define MAXPATHLEN 256
58 extern char *getenv ();
60 /* This does cause trouble on AIX. I'm going to take the comment at
63 extern short getuid (); /* If this causes portability problems,
64 I think we should just delete it; it'll
65 default to `int' anyway. */
68 #if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
69 extern struct passwd
*getpwuid (uid_t
);
70 extern struct passwd
*getpwnam (const char *);
72 extern struct passwd
*getpwuid ();
73 extern struct passwd
*getpwnam ();
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
);
116 return strcpy ((char *) malloc (strlen (result
) + 1), result
);
122 /* Expand all the Xt-style %-escapes in STRING, whose length is given
123 by STRING_LEN. Here are the escapes we're supposed to recognize:
125 %N The value of the application's class name
126 %T The value of the type parameter ("app-defaults" in this
128 %S The value of the suffix parameter ("" in this context)
129 %L The language string associated with the specified display
130 (We use the "LANG" environment variable here, if it's set.)
131 %l The language part of the display's language string
132 (We treat this just like %L. If someone can tell us what
133 we're really supposed to do, dandy.)
134 %t The territory part of the display's language string
135 (This never gets used.)
136 %c The codeset part of the display's language string
137 (This never gets used either.)
138 %C The customization string retrieved from the resource
139 database associated with display.
140 (This is x_customization_string.)
142 Return the expanded file name if it exists and is readable, and
143 refers to %L only when the LANG environment variable is set, or
144 otherwise provided by X.
146 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
147 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
150 Return NULL otherwise. */
153 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
156 char *class, *escaped_suffix
, *suffix
;
158 char *lang
= getenv ("LANG");
161 char *path
= (char *) malloc (path_size
);
166 while (p
< string
+ string_len
)
168 /* The chunk we're about to stick on the end of result. */
176 if (p
>= string
+ string_len
)
187 next
= (x_customization_string
188 ? x_customization_string
190 next_len
= strlen (next
);
195 next_len
= strlen (class);
199 next
= "app-defaults";
200 next_len
= strlen (next
);
217 next_len
= strlen (next
);
227 next
= p
, next_len
= 1;
229 /* Do we have room for this component followed by a '\0' ? */
230 if (path_len
+ next_len
+ 1 > path_size
)
232 path_size
= (path_len
+ next_len
+ 1) * 2;
233 path
= (char *) realloc (path
, path_size
);
236 bcopy (next
, path
+ path_len
, next_len
);
237 path_len
+= next_len
;
241 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
242 if (p
>= string
+ string_len
&& escaped_suffix
)
244 string
= escaped_suffix
;
245 string_len
= strlen (string
);
247 escaped_suffix
= NULL
;
251 /* Perhaps we should add the SUFFIX now. */
254 int suffix_len
= strlen (suffix
);
256 if (path_len
+ suffix_len
+ 1 > path_size
)
258 path_size
= (path_len
+ suffix_len
+ 1);
259 path
= (char *) realloc (path
, path_size
);
262 bcopy (suffix
, path
+ path_len
, suffix_len
);
263 path_len
+= suffix_len
;
266 path
[path_len
] = '\0';
286 if ((ptr
= getenv ("HOME")) == NULL
)
288 if ((ptr
= getenv ("USER")) != NULL
)
303 copy
= (char *) malloc (strlen (ptr
) + 2);
317 return (access (path
, 4) == 0 /* exists and is readable */
318 && stat (path
, &status
) == 0 /* get the status */
319 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
323 /* Find the first element of SEARCH_PATH which exists and is readable,
324 after expanding the %-escapes. Return 0 if we didn't find any, and
325 the path name of the one we found otherwise. */
328 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
329 char *search_path
, *class, *escaped_suffix
, *suffix
;
331 register char *s
, *p
;
333 for (s
= search_path
; *s
; s
= p
)
335 for (p
= s
; *p
&& *p
!= ':'; p
++)
338 if (*p
== ':' && *(p
+ 1) == ':')
343 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
353 char *path
= magic_file_p (s
, p
- 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
)
407 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
408 names, not directories. */
409 if (((path
= getenv ("XUSERFILESEARCHPATH"))
410 && (file
= search_magic_path (path
, class, 0, 0)))
412 /* Check for APPLRESDIR; it is a path of directories. In each,
413 we have to search for LANG/CLASS and then CLASS. */
414 || ((path
= getenv ("XAPPLRESDIR"))
415 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
416 || (file
= search_magic_path (path
, class, "/%N", 0))))
418 /* Check in the home directory. This is a bit of a hack; let's
419 hope one's home directory doesn't contain any %-escapes. */
420 || (path
= gethomedir (),
421 ((file
= search_magic_path (path
, class, "%L/%N", 0))
422 || (file
= search_magic_path (path
, class, "%N", 0)))))
424 XrmDatabase db
= XrmGetFileDatabase (file
);
434 get_user_db (display
)
440 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
441 xdefs
= XResourceManagerString (display
);
443 xdefs
= display
->xdefaults
;
447 db
= XrmGetStringDatabase (xdefs
);
453 home
= gethomedir ();
454 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
455 strcpy (xdefault
, home
);
456 strcat (xdefault
, ".Xdefaults");
457 db
= XrmGetFileDatabase (xdefault
);
462 #ifdef XlibSpecificationRelease
463 #if XlibSpecificationRelease >= 5
464 /* Get the screen-specific resources too. */
465 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
468 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
482 char *path
= 0, *home
= 0, *host
= 0;
484 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
486 home
= gethomedir ();
490 host
= (char *) malloc (host_size
);
494 host
[host_size
- 1] = '\0';
495 gethostname (host
, host_size
- 1);
496 if (strlen (host
) < host_size
- 1)
498 host
= (char *) realloc (host
, host_size
*= 2);
502 path
= (char *) malloc (strlen (home
)
503 + sizeof (".Xdefaults-")
505 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
509 db
= XrmGetFileDatabase (p
);
511 if (path
) free (path
);
512 if (home
) free (home
);
513 if (host
) free (host
);
518 /* External interface. */
520 /* Types of values that we can find in a database */
522 #define XrmStringType "String" /* String representation */
523 XrmRepresentation x_rm_string
; /* Quark representation */
525 /* Load X resources based on the display and a possible -xrm option. */
528 x_load_resources (display
, xrm_string
, myname
, myclass
)
530 char *xrm_string
, *myname
, *myclass
;
533 XrmDatabase user_database
;
537 x_rm_string
= XrmStringToQuark (XrmStringType
);
539 rdb
= XrmGetStringDatabase ("");
541 user_database
= get_user_db (display
);
543 /* Figure out what the "customization string" is, so we can use it
545 if (x_customization_string
)
546 free (x_customization_string
);
547 x_customization_string
548 = x_get_customization_string (user_database
, myname
, myclass
);
550 /* Get application system defaults */
551 db
= get_system_app (myclass
);
553 XrmMergeDatabases (db
, &rdb
);
555 /* Get Fallback resources */
556 db
= get_fallback (display
);
558 XrmMergeDatabases (db
, &rdb
);
560 /* Get application user defaults */
561 db
= get_user_app (myclass
);
563 XrmMergeDatabases (db
, &rdb
);
565 /* get User defaults */
566 if (user_database
!= NULL
)
567 XrmMergeDatabases (user_database
, &rdb
);
569 /* Get Environment defaults. */
570 db
= get_environ_db ();
572 XrmMergeDatabases (db
, &rdb
);
574 /* Last, merge in any specification from the command line. */
575 if (xrm_string
!= NULL
)
577 db
= XrmGetStringDatabase (xrm_string
);
579 XrmMergeDatabases (db
, &rdb
);
586 /* Retrieve the value of the resource specified by NAME with class CLASS
587 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
590 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
593 XrmRepresentation expected_type
;
597 XrmName namelist
[100];
598 XrmClass classlist
[100];
599 XrmRepresentation type
;
601 XrmStringToNameList(name
, namelist
);
602 XrmStringToClassList(class, classlist
);
604 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
605 && (type
== expected_type
))
607 if (type
== x_rm_string
)
608 ret_value
->addr
= (char *) value
.addr
;
610 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
618 /* Retrieve the string resource specified by NAME with CLASS from
622 x_get_string_resource (rdb
, name
, class)
628 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
629 return (char *) value
.addr
;
634 /* Stand-alone test facilities. */
639 #define arg_listify(len, list) (list)
640 #define car(list) (*(list))
641 #define cdr(list) (list + 1)
642 #define NIL(list) (! *(list))
643 #define free_arglist(list)
652 for (p
= list
; ! NIL (p
); p
= cdr (p
))
653 if (! strcmp (elt
, car (p
)))
660 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
662 int x1
, x2
, x3
, x4
, x5
;
669 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
678 char *displayname
, *resource_string
, *class, *name
;
682 arg_list
= arg_listify (argc
, argv
);
684 lp
= member ("-d", arg_list
);
686 displayname
= car (cdr (lp
));
688 displayname
= "localhost:0.0";
690 lp
= member ("-xrm", arg_list
);
692 resource_string
= car (cdr (lp
));
694 resource_string
= (char *) 0;
696 lp
= member ("-c", arg_list
);
698 class = car (cdr (lp
));
702 lp
= member ("-n", arg_list
);
704 name
= car (cdr (lp
));
708 free_arglist (arg_list
);
710 if (!(display
= XOpenDisplay (displayname
)))
711 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
713 xdb
= x_load_resources (display
, resource_string
, name
, class);
715 /* In a real program, you'd want to also do this: */
721 char query_class
[90];
726 if (strlen (query_name
))
733 value
= x_get_string_resource (xdb
, query_name
, query_class
);
736 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
738 printf ("\tNo Value.\n\n");
743 printf ("\tExit.\n\n");
745 XCloseDisplay (display
);