1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1992 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 */
21 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xresource.h>
26 #include <sys/param.h>
33 #include <sys/types.h>
39 extern char *getenv ();
40 extern uid_t
getuid ();
41 extern struct passwd
*getpwuid ();
42 extern struct passwd
*getpwnam ();
52 if ((ptr
= getenv ("HOME")) == NULL
)
54 if ((ptr
= getenv ("USER")) != NULL
)
71 strcpy (dirname
, ptr
);
73 dirname
+= strlen (dirname
);
87 return (access (path
, R_OK
) == 0 /* exists and is readable */
88 && stat (path
, &status
) == 0 /* get the status */
89 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
93 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
96 /* Isn't this just disgusting? */
98 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/%L/%T/%N%S:/usr/lib/X11/%l/%T/%N%S:/usr/lib/X11/%T/%N%S"
101 decode_magic (string
, file
, return_path
)
102 char *string
, *file
, *return_path
;
105 char *t
= return_path
;
126 if (*t
== '/' && *p
== '/')
134 strcat (return_path
, file
);
136 if (file_p (return_path
))
139 return_path
[0] = '\0';
144 magic_searchpath_decoder (incantation_string
, file
, return_path
)
145 char *incantation_string
, *return_path
, *file
;
147 register char *s
= incantation_string
;
150 /* Must be big enough for "%N%S". */
151 register int string_size
= MAXPATHLEN
;
152 register char *string
= (char *) alloca (string_size
* sizeof (*string
));
158 while (*p
&& *p
!= ':')
161 if (*p
== ':' && *(p
+ 1) == ':')
163 /* We know string is big enough for this. */
164 bcopy ("%N%S", string
, 5);
165 if (decode_magic (string
, file
, return_path
))
176 if (string_size
< len
+1)
178 string_size
= 2 * len
;
179 string
= (char *) alloca (string_size
* sizeof (*string
));
181 bcopy (s
, string
, len
);
182 string
[len
+ 1] = '\0';
183 if (decode_magic (string
, file
, return_path
))
197 get_system_app (class)
201 char path
[MAXPATHLEN
];
204 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
205 p
= X_DEFAULT_SEARCH_PATH
;
207 if (! magic_searchpath_decoder (p
, class, path
))
210 db
= XrmGetFileDatabase (path
);
215 get_fallback (display
)
229 char path
[MAXPATHLEN
];
231 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
233 char homedir
[MAXPATHLEN
];
237 gethomedir (homedir
);
239 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
241 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
242 magic_path
= (char *) alloca ((3 * strlen (homedir
))
243 + strlen (default_magic
));
244 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
248 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
249 magic_path
= (char *) alloca ((3 * strlen (p
))
250 + strlen (default_magic
)
252 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
256 if (! magic_searchpath_decoder (magic_path
, class, path
))
259 db
= XrmGetFileDatabase (path
);
264 get_user_db (display
)
270 xdefs
= XResourceManagerString (display
);
272 db
= XrmGetStringDatabase (xdefs
);
275 char xdefault
[MAXPATHLEN
];
277 gethomedir (xdefault
);
278 strcat (xdefault
, ".Xdefaults");
279 db
= XrmGetFileDatabase (xdefault
);
290 char path
[MAXPATHLEN
];
292 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
295 strcat (path
, ".Xdefaults-");
296 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
300 db
= XrmGetFileDatabase (p
);
304 /* Types of values that we can find in a database */
306 #define XrmStringType "String" /* String representation */
307 XrmRepresentation x_rm_string
; /* Quark representation */
309 /* Load X resources based on the display and a possible -xrm option. */
312 x_load_resources (display
, xrm_string
, myclass
)
314 char *xrm_string
, *myclass
;
320 x_rm_string
= XrmStringToQuark (XrmStringType
);
322 rdb
= XrmGetStringDatabase ("");
324 /* Get application system defaults */
325 db
= get_system_app (myclass
);
327 XrmMergeDatabases (db
, &rdb
);
329 /* Get Fallback resources */
330 db
= get_fallback (display
);
332 XrmMergeDatabases (db
, &rdb
);
334 /* Get application user defaults */
335 db
= get_user_app (myclass
);
337 XrmMergeDatabases (db
, &rdb
);
339 /* get User defaults */
340 db
= get_user_db (display
);
342 XrmMergeDatabases (db
, &rdb
);
344 /* Get Environment defaults. */
345 db
= get_environ_db ();
347 XrmMergeDatabases (db
, &rdb
);
349 /* Last, merge in any specification from the command line. */
350 if (xrm_string
!= NULL
)
352 db
= XrmGetStringDatabase (xrm_string
);
354 XrmMergeDatabases (db
, &rdb
);
360 /* Retrieve the value of the resource specified by NAME with class CLASS
361 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
364 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
367 XrmRepresentation expected_type
;
371 XrmName namelist
[100];
372 XrmClass classlist
[100];
373 XrmRepresentation type
;
375 XrmStringToNameList(name
, namelist
);
376 XrmStringToClassList(class, classlist
);
378 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
379 && (type
== expected_type
))
381 if (type
== x_rm_string
)
382 (char *) ret_value
->addr
= value
.addr
;
384 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
392 /* Retrieve the string resource specified by NAME with CLASS from
396 x_get_string_resource (rdb
, name
, class)
402 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
403 return (char *) value
.addr
;
410 #include "arg-list.h"
413 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
415 int x1
, x2
, x3
, x4
, x5
;
422 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
431 char *displayname
, *resource_string
, *class;
435 arg_list
= arg_listify (argc
, argv
);
437 lp
= member ("-d", arg_list
);
439 displayname
= car (cdr (lp
));
441 displayname
= "localhost:0.0";
443 lp
= member ("-xrm", arg_list
);
445 resource_string
= car (cdr (lp
));
447 resource_string
= (char *) 0;
449 lp
= member ("-c", arg_list
);
451 class = car (cdr (lp
));
455 free_arglist (arg_list
);
459 if (!(display
= XOpenDisplay (displayname
)))
460 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
462 xdb
= x_load_resources (display
, resource_string
, class);
465 /* In a real program, you'd want to also do this: */
477 char *value
= x_get_string_resource (xdb
, line
, class);
480 printf ("\t%s: %s\n\n", line
, value
);
482 printf ("\tNo Value.\n\n");
487 printf ("\tExit.\n\n");
489 XCloseDisplay (display
);