1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990 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 1, 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
;
149 register char string
[MAXPATHLEN
];
155 while (*p
&& *p
!= ':')
158 if (*p
== ':' && *(p
+ 1) == ':')
160 bcopy ("%N%S", string
, 5);
161 if (decode_magic (string
, file
, return_path
))
172 bcopy (s
, string
, len
);
173 string
[len
+ 1] = '\0';
174 if (decode_magic (string
, file
, return_path
))
188 get_system_app (class)
192 char path
[MAXPATHLEN
];
195 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
196 p
= X_DEFAULT_SEARCH_PATH
;
198 if (! magic_searchpath_decoder (p
, class, path
))
201 db
= XrmGetFileDatabase (path
);
206 get_fallback (display
)
220 char path
[MAXPATHLEN
];
222 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
224 char homedir
[MAXPATHLEN
];
228 gethomedir (homedir
);
230 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
232 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
233 magic_path
= (char *) alloca ((3 * strlen (homedir
))
234 + strlen (default_magic
));
235 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
239 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
240 magic_path
= (char *) alloca ((3 * strlen (p
))
241 + strlen (default_magic
)
243 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
247 if (! magic_searchpath_decoder (magic_path
, class, path
))
250 db
= XrmGetFileDatabase (path
);
255 get_user_db (display
)
261 xdefs
= XResourceManagerString (display
);
263 db
= XrmGetStringDatabase (xdefs
);
266 char xdefault
[MAXPATHLEN
];
268 gethomedir (xdefault
);
269 strcat (xdefault
, ".Xdefaults");
270 db
= XrmGetFileDatabase (xdefault
);
281 char path
[MAXPATHLEN
];
283 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
286 strcat (path
, ".Xdefaults-");
287 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
291 db
= XrmGetFileDatabase (p
);
295 /* Types of values that we can find in a database */
297 #define XrmStringType "String" /* String representation */
298 XrmRepresentation x_rm_string
; /* Quark representation */
300 /* Load X resources based on the display and a possible -xrm option. */
303 x_load_resources (display
, xrm_string
, myclass
)
305 char *xrm_string
, *myclass
;
311 x_rm_string
= XrmStringToQuark (XrmStringType
);
313 rdb
= XrmGetStringDatabase ("");
315 /* Get application system defaults */
316 db
= get_system_app (myclass
);
318 XrmMergeDatabases (db
, &rdb
);
320 /* Get Fallback resources */
321 db
= get_fallback (display
);
323 XrmMergeDatabases (db
, &rdb
);
325 /* Get application user defaults */
326 db
= get_user_app (myclass
);
328 XrmMergeDatabases (db
, &rdb
);
330 /* get User defaults */
331 db
= get_user_db (display
);
333 XrmMergeDatabases (db
, &rdb
);
335 /* Get Environment defaults. */
336 db
= get_environ_db ();
338 XrmMergeDatabases (db
, &rdb
);
340 /* Last, merge in any specification from the command line. */
341 if (xrm_string
!= NULL
)
343 db
= XrmGetStringDatabase (xrm_string
);
345 XrmMergeDatabases (db
, &rdb
);
351 /* Retrieve the value of the resource specified by NAME with class CLASS
352 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
355 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
358 XrmRepresentation expected_type
;
362 XrmName namelist
[100];
363 XrmClass classlist
[100];
364 XrmRepresentation type
;
366 XrmStringToNameList(name
, namelist
);
367 XrmStringToClassList(class, classlist
);
369 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
370 && (type
== expected_type
))
372 if (type
== x_rm_string
)
373 (char *) ret_value
->addr
= value
.addr
;
375 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
383 /* Retrieve the string resource specified by NAME with CLASS from
387 x_get_string_resource (rdb
, name
, class)
393 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
394 return (char *) value
.addr
;
401 #include "arg-list.h"
404 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
406 int x1
, x2
, x3
, x4
, x5
;
413 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
422 char *displayname
, *resource_string
, *class;
426 arg_list
= arg_listify (argc
, argv
);
428 lp
= member ("-d", arg_list
);
430 displayname
= car (cdr (lp
));
432 displayname
= "localhost:0.0";
434 lp
= member ("-xrm", arg_list
);
436 resource_string
= car (cdr (lp
));
438 resource_string
= (char *) 0;
440 lp
= member ("-c", arg_list
);
442 class = car (cdr (lp
));
446 free_arglist (arg_list
);
450 if (!(display
= XOpenDisplay (displayname
)))
451 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
453 xdb
= x_load_resources (display
, resource_string
, class);
456 /* In a real program, you'd want to also do this: */
468 char *value
= x_get_string_resource (xdb
, line
, class);
471 printf ("\t%s: %s\n\n", line
, value
);
473 printf ("\tNo Value.\n\n");
478 printf ("\tExit.\n\n");
480 XCloseDisplay (display
);