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>
34 extern char *getenv ();
36 extern struct passwd
*getpwuid ();
37 extern struct passwd
*getpwnam ();
47 if ((ptr
= getenv ("HOME")) == NULL
)
49 if ((ptr
= getenv ("USER")) != NULL
)
66 strcpy (dirname
, ptr
);
68 dirname
+= strlen (dirname
);
82 return (access (path
, R_OK
) == 0 /* exists and is readable */
83 && stat (path
, &status
) == 0 /* get the status */
84 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
88 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
91 /* Isn't this just disgusting? */
93 #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"
96 decode_magic (string
, file
, return_path
)
97 char *string
, *file
, *return_path
;
100 char *t
= return_path
;
121 if (*t
== '/' && *p
== '/')
129 strcat (return_path
, file
);
131 if (file_p (return_path
))
134 return_path
[0] = '\0';
139 magic_searchpath_decoder (incantation_string
, file
, return_path
)
140 char *incantation_string
, *return_path
, *file
;
142 register char *s
= incantation_string
;
144 register char string
[MAXPATHLEN
];
150 while (*p
&& *p
!= ':')
153 if (*p
== ':' && *(p
+ 1) == ':')
155 bcopy ("%N%S", string
, 5);
156 if (decode_magic (string
, file
, return_path
))
167 bcopy (s
, string
, len
);
168 string
[len
+ 1] = '\0';
169 if (decode_magic (string
, file
, return_path
))
183 get_system_app (class)
187 char path
[MAXPATHLEN
];
190 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
191 p
= X_DEFAULT_SEARCH_PATH
;
193 if (! magic_searchpath_decoder (p
, class, path
))
196 db
= XrmGetFileDatabase (path
);
201 get_fallback (display
)
215 char path
[MAXPATHLEN
];
217 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
219 char homedir
[MAXPATHLEN
];
223 gethomedir (homedir
);
225 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
227 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
228 magic_path
= (char *) alloca ((3 * strlen (homedir
))
229 + strlen (default_magic
));
230 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
234 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
235 magic_path
= (char *) alloca ((3 * strlen (p
))
236 + strlen (default_magic
)
238 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
242 if (! magic_searchpath_decoder (magic_path
, class, path
))
245 db
= XrmGetFileDatabase (path
);
250 get_user_db (display
)
256 xdefs
= XResourceManagerString (display
);
258 db
= XrmGetStringDatabase (xdefs
);
261 char xdefault
[MAXPATHLEN
];
263 gethomedir (xdefault
);
264 strcat (xdefault
, ".Xdefaults");
265 db
= XrmGetFileDatabase (xdefault
);
276 char path
[MAXPATHLEN
];
278 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
281 strcat (path
, ".Xdefaults-");
282 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
286 db
= XrmGetFileDatabase (p
);
290 /* Types of values that we can find in a database */
292 #define XrmStringType "String" /* String representation */
293 XrmRepresentation x_rm_string
; /* Quark representation */
295 /* Load X resources based on the display and a possible -xrm option. */
298 x_load_resources (display
, xrm_string
, myclass
)
300 char *xrm_string
, *myclass
;
306 x_rm_string
= XrmStringToQuark (XrmStringType
);
308 rdb
= XrmGetStringDatabase ("");
310 /* Get application system defaults */
311 db
= get_system_app (myclass
);
313 XrmMergeDatabases (db
, &rdb
);
315 /* Get Fallback resources */
316 db
= get_fallback (display
);
318 XrmMergeDatabases (db
, &rdb
);
320 /* Get application user defaults */
321 db
= get_user_app (myclass
);
323 XrmMergeDatabases (db
, &rdb
);
325 /* get User defaults */
326 db
= get_user_db (display
);
328 XrmMergeDatabases (db
, &rdb
);
330 /* Get Environment defaults. */
331 db
= get_environ_db ();
333 XrmMergeDatabases (db
, &rdb
);
335 /* Last, merge in any specification from the command line. */
336 if (xrm_string
!= NULL
)
338 db
= XrmGetStringDatabase (xrm_string
);
340 XrmMergeDatabases (db
, &rdb
);
346 /* Retrieve the value of the resource specified by NAME with class CLASS
347 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
350 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
353 XrmRepresentation expected_type
;
357 XrmName namelist
[100];
358 XrmClass classlist
[100];
359 XrmRepresentation type
;
361 XrmStringToNameList(name
, namelist
);
362 XrmStringToClassList(class, classlist
);
364 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
365 && (type
== expected_type
))
367 if (type
== x_rm_string
)
368 (char *) ret_value
->addr
= value
.addr
;
370 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
378 /* Retrieve the string resource specified by NAME with CLASS from
382 x_get_string_resource (rdb
, name
, class)
388 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
389 return (char *) value
.addr
;
396 #include "arg-list.h"
399 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
401 int x1
, x2
, x3
, x4
, x5
;
408 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
417 char *displayname
, *resource_string
, *class;
421 arg_list
= arg_listify (argc
, argv
);
423 lp
= member ("-d", arg_list
);
425 displayname
= car (cdr (lp
));
427 displayname
= "localhost:0.0";
429 lp
= member ("-xrm", arg_list
);
431 resource_string
= car (cdr (lp
));
433 resource_string
= (char *) 0;
435 lp
= member ("-c", arg_list
);
437 class = car (cdr (lp
));
441 free_arglist (arg_list
);
445 if (!(display
= XOpenDisplay (displayname
)))
446 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
448 xdb
= x_load_resources (display
, resource_string
, class);
451 /* In a real program, you'd want to also do this: */
463 char *value
= x_get_string_resource (xdb
, line
, class);
466 printf ("\t%s: %s\n\n", line
, value
);
468 printf ("\tNo Value.\n\n");
473 printf ("\tExit.\n\n");
475 XCloseDisplay (display
);