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 #if !defined(S_ISDIR) && defined(S_IFDIR)
59 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
62 extern char *getenv ();
64 /* This does cause trouble on AIX. I'm going to take the comment at
67 extern short getuid (); /* If this causes portability problems,
68 I think we should just delete it; it'll
69 default to `int' anyway. */
72 #if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
73 extern struct passwd
*getpwuid (uid_t
);
74 extern struct passwd
*getpwnam (const char *);
76 extern struct passwd
*getpwuid ();
77 extern struct passwd
*getpwnam ();
80 extern char *get_system_name ();
82 /* Make sure not to #include anything after these definitions. Let's
83 not step on anyone's prototypes. */
85 #define malloc xmalloc
86 #define realloc xrealloc
90 char *x_get_string_resource ();
94 /* X file search path processing. */
97 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
98 and friends, or zero if none was specified. */
99 char *x_customization_string
;
102 /* Return the value of the emacs.customization (Emacs.Customization)
103 resource, for later use in search path decoding. If we find no
104 such resource, return zero. */
106 x_get_customization_string (db
, name
, class)
111 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
113 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
116 sprintf (full_name
, "%s.%s", name
, "customization");
117 sprintf (full_class
, "%s.%s", class, "Customization");
119 result
= x_get_string_resource (db
, full_name
, full_class
);
123 char *copy
= (char *) malloc (strlen (result
) + 1);
124 strcpy (copy
, result
);
132 /* Expand all the Xt-style %-escapes in STRING, whose length is given
133 by STRING_LEN. Here are the escapes we're supposed to recognize:
135 %N The value of the application's class name
136 %T The value of the type parameter ("app-defaults" in this
138 %S The value of the suffix parameter ("" in this context)
139 %L The language string associated with the specified display
140 (We use the "LANG" environment variable here, if it's set.)
141 %l The language part of the display's language string
142 (We treat this just like %L. If someone can tell us what
143 we're really supposed to do, dandy.)
144 %t The territory part of the display's language string
145 (This never gets used.)
146 %c The codeset part of the display's language string
147 (This never gets used either.)
148 %C The customization string retrieved from the resource
149 database associated with display.
150 (This is x_customization_string.)
152 Return the expanded file name if it exists and is readable, and
153 refers to %L only when the LANG environment variable is set, or
154 otherwise provided by X.
156 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
157 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
160 Return NULL otherwise. */
163 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
166 char *class, *escaped_suffix
, *suffix
;
168 char *lang
= getenv ("LANG");
171 char *path
= (char *) malloc (path_size
);
176 while (p
< string
+ string_len
)
178 /* The chunk we're about to stick on the end of result. */
186 if (p
>= string
+ string_len
)
197 next
= (x_customization_string
198 ? x_customization_string
200 next_len
= strlen (next
);
205 next_len
= strlen (class);
209 next
= "app-defaults";
210 next_len
= strlen (next
);
227 next_len
= strlen (next
);
237 next
= p
, next_len
= 1;
239 /* Do we have room for this component followed by a '\0' ? */
240 if (path_len
+ next_len
+ 1 > path_size
)
242 path_size
= (path_len
+ next_len
+ 1) * 2;
243 path
= (char *) realloc (path
, path_size
);
246 bcopy (next
, path
+ path_len
, next_len
);
247 path_len
+= next_len
;
251 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
252 if (p
>= string
+ string_len
&& escaped_suffix
)
254 string
= escaped_suffix
;
255 string_len
= strlen (string
);
257 escaped_suffix
= NULL
;
261 /* Perhaps we should add the SUFFIX now. */
264 int suffix_len
= strlen (suffix
);
266 if (path_len
+ suffix_len
+ 1 > path_size
)
268 path_size
= (path_len
+ suffix_len
+ 1);
269 path
= (char *) realloc (path
, path_size
);
272 bcopy (suffix
, path
+ path_len
, suffix_len
);
273 path_len
+= suffix_len
;
276 path
[path_len
] = '\0';
295 if ((ptr
= getenv ("HOME")) == NULL
)
297 if ((ptr
= getenv ("LOGNAME")) != NULL
298 || (ptr
= getenv ("USER")) != NULL
)
301 pw
= getpwuid (getuid ());
310 copy
= (char *) malloc (strlen (ptr
) + 2);
324 return (access (path
, 4) == 0 /* exists and is readable */
325 && stat (path
, &status
) == 0 /* get the status */
326 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
330 /* Find the first element of SEARCH_PATH which exists and is readable,
331 after expanding the %-escapes. Return 0 if we didn't find any, and
332 the path name of the one we found otherwise. */
335 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
336 char *search_path
, *class, *escaped_suffix
, *suffix
;
338 register char *s
, *p
;
340 for (s
= search_path
; *s
; s
= p
)
342 for (p
= s
; *p
&& *p
!= ':'; p
++)
347 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
356 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
368 /* Producing databases for individual sources. */
370 #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"
373 get_system_app (class)
376 XrmDatabase db
= NULL
;
379 path
= getenv ("XFILESEARCHPATH");
380 if (! path
) path
= X_DEFAULT_SEARCH_PATH
;
382 path
= search_magic_path (path
, class, 0, 0);
385 db
= XrmGetFileDatabase (path
);
394 get_fallback (display
)
410 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
411 names, not directories. */
412 if (((path
= getenv ("XUSERFILESEARCHPATH"))
413 && (file
= search_magic_path (path
, class, 0, 0)))
415 /* Check for APPLRESDIR; it is a path of directories. In each,
416 we have to search for LANG/CLASS and then CLASS. */
417 || ((path
= getenv ("XAPPLRESDIR"))
418 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
419 || (file
= search_magic_path (path
, class, "/%N", 0))))
421 /* Check in the home directory. This is a bit of a hack; let's
422 hope one's home directory doesn't contain any %-escapes. */
423 || (path
= gethomedir (),
424 ((file
= search_magic_path (path
, class, "%L/%N", 0))
425 || (file
= search_magic_path (path
, class, "%N", 0)))))
427 XrmDatabase db
= XrmGetFileDatabase (file
);
437 get_user_db (display
)
443 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
444 xdefs
= XResourceManagerString (display
);
446 xdefs
= display
->xdefaults
;
450 db
= XrmGetStringDatabase (xdefs
);
456 home
= gethomedir ();
457 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
458 strcpy (xdefault
, home
);
459 strcat (xdefault
, ".Xdefaults");
460 db
= XrmGetFileDatabase (xdefault
);
465 #ifdef HAVE_XSCREENRESOURCESTRING
466 /* Get the screen-specific resources too. */
467 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
470 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
483 char *path
= 0, *home
= 0, *host
;
485 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
487 home
= gethomedir ();
488 host
= get_system_name ();
489 path
= (char *) malloc (strlen (home
)
490 + sizeof (".Xdefaults-")
492 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
496 db
= XrmGetFileDatabase (p
);
498 if (path
) free (path
);
499 if (home
) free (home
);
504 /* External interface. */
506 /* Types of values that we can find in a database */
508 #define XrmStringType "String" /* String representation */
509 XrmRepresentation x_rm_string
; /* Quark representation */
511 /* Load X resources based on the display and a possible -xrm option. */
514 x_load_resources (display
, xrm_string
, myname
, myclass
)
516 char *xrm_string
, *myname
, *myclass
;
519 XrmDatabase user_database
;
523 x_rm_string
= XrmStringToQuark (XrmStringType
);
525 rdb
= XrmGetStringDatabase ("");
527 user_database
= get_user_db (display
);
529 /* Figure out what the "customization string" is, so we can use it
531 if (x_customization_string
)
532 free (x_customization_string
);
533 x_customization_string
534 = x_get_customization_string (user_database
, myname
, myclass
);
536 /* Get application system defaults */
537 db
= get_system_app (myclass
);
539 XrmMergeDatabases (db
, &rdb
);
541 /* Get Fallback resources */
542 db
= get_fallback (display
);
544 XrmMergeDatabases (db
, &rdb
);
546 /* Get application user defaults */
547 db
= get_user_app (myclass
);
549 XrmMergeDatabases (db
, &rdb
);
551 /* get User defaults */
552 if (user_database
!= NULL
)
553 XrmMergeDatabases (user_database
, &rdb
);
555 /* Get Environment defaults. */
556 db
= get_environ_db ();
558 XrmMergeDatabases (db
, &rdb
);
560 /* Last, merge in any specification from the command line. */
561 if (xrm_string
!= NULL
)
563 db
= XrmGetStringDatabase (xrm_string
);
565 XrmMergeDatabases (db
, &rdb
);
572 /* Retrieve the value of the resource specified by NAME with class CLASS
573 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
576 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
579 XrmRepresentation expected_type
;
583 XrmName namelist
[100];
584 XrmClass classlist
[100];
585 XrmRepresentation type
;
587 XrmStringToNameList(name
, namelist
);
588 XrmStringToClassList(class, classlist
);
590 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
591 && (type
== expected_type
))
593 if (type
== x_rm_string
)
594 ret_value
->addr
= (char *) value
.addr
;
596 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
604 /* Retrieve the string resource specified by NAME with CLASS from
608 x_get_string_resource (rdb
, name
, class)
614 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
615 return (char *) value
.addr
;
620 /* Stand-alone test facilities. */
625 #define arg_listify(len, list) (list)
626 #define car(list) (*(list))
627 #define cdr(list) (list + 1)
628 #define NIL(list) (! *(list))
629 #define free_arglist(list)
638 for (p
= list
; ! NIL (p
); p
= cdr (p
))
639 if (! strcmp (elt
, car (p
)))
646 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
648 int x1
, x2
, x3
, x4
, x5
;
655 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
664 char *displayname
, *resource_string
, *class, *name
;
668 arg_list
= arg_listify (argc
, argv
);
670 lp
= member ("-d", arg_list
);
672 displayname
= car (cdr (lp
));
674 displayname
= "localhost:0.0";
676 lp
= member ("-xrm", arg_list
);
678 resource_string
= car (cdr (lp
));
680 resource_string
= (char *) 0;
682 lp
= member ("-c", arg_list
);
684 class = car (cdr (lp
));
688 lp
= member ("-n", arg_list
);
690 name
= car (cdr (lp
));
694 free_arglist (arg_list
);
696 if (!(display
= XOpenDisplay (displayname
)))
697 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
699 xdb
= x_load_resources (display
, resource_string
, name
, class);
701 /* In a real program, you'd want to also do this: */
707 char query_class
[90];
712 if (strlen (query_name
))
719 value
= x_get_string_resource (xdb
, query_name
, query_class
);
722 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
724 printf ("\tNo Value.\n\n");
729 printf ("\tExit.\n\n");
731 XCloseDisplay (display
);