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>
29 #include <sys/types.h>
35 extern char *getenv ();
36 extern uid_t
getuid ();
37 extern struct passwd
*getpwuid ();
38 extern struct passwd
*getpwnam ();
48 if ((ptr
= getenv ("HOME")) == NULL
)
50 if ((ptr
= getenv ("USER")) != NULL
)
67 strcpy (dirname
, ptr
);
69 dirname
+= strlen (dirname
);
83 return (access (path
, R_OK
) == 0 /* exists and is readable */
84 && stat (path
, &status
) == 0 /* get the status */
85 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
89 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
92 /* Isn't this just disgusting? */
94 #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"
97 decode_magic (string
, file
, return_path
)
98 char *string
, *file
, *return_path
;
101 char *t
= return_path
;
122 if (*t
== '/' && *p
== '/')
130 strcat (return_path
, file
);
132 if (file_p (return_path
))
135 return_path
[0] = '\0';
140 magic_searchpath_decoder (incantation_string
, file
, return_path
)
141 char *incantation_string
, *return_path
, *file
;
143 register char *s
= incantation_string
;
145 register char string
[MAXPATHLEN
];
151 while (*p
&& *p
!= ':')
154 if (*p
== ':' && *(p
+ 1) == ':')
156 bcopy ("%N%S", string
, 5);
157 if (decode_magic (string
, file
, return_path
))
168 bcopy (s
, string
, len
);
169 string
[len
+ 1] = '\0';
170 if (decode_magic (string
, file
, return_path
))
184 get_system_app (class)
188 char path
[MAXPATHLEN
];
191 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
192 p
= X_DEFAULT_SEARCH_PATH
;
194 if (! magic_searchpath_decoder (p
, class, path
))
197 db
= XrmGetFileDatabase (path
);
202 get_fallback (display
)
216 char path
[MAXPATHLEN
];
218 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
220 char homedir
[MAXPATHLEN
];
224 gethomedir (homedir
);
226 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
228 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
229 magic_path
= (char *) alloca ((3 * strlen (homedir
))
230 + strlen (default_magic
));
231 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
235 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
236 magic_path
= (char *) alloca ((3 * strlen (p
))
237 + strlen (default_magic
)
239 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
243 if (! magic_searchpath_decoder (magic_path
, class, path
))
246 db
= XrmGetFileDatabase (path
);
251 get_user_db (display
)
257 xdefs
= XResourceManagerString (display
);
259 db
= XrmGetStringDatabase (xdefs
);
262 char xdefault
[MAXPATHLEN
];
264 gethomedir (xdefault
);
265 strcat (xdefault
, ".Xdefaults");
266 db
= XrmGetFileDatabase (xdefault
);
277 char path
[MAXPATHLEN
];
279 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
282 strcat (path
, ".Xdefaults-");
283 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
287 db
= XrmGetFileDatabase (p
);
291 /* Types of values that we can find in a database */
293 #define XrmStringType "String" /* String representation */
294 XrmRepresentation x_rm_string
; /* Quark representation */
296 /* Load X resources based on the display and a possible -xrm option. */
299 x_load_resources (display
, xrm_string
, myclass
)
301 char *xrm_string
, *myclass
;
307 x_rm_string
= XrmStringToQuark (XrmStringType
);
309 rdb
= XrmGetStringDatabase ("");
311 /* Get application system defaults */
312 db
= get_system_app (myclass
);
314 XrmMergeDatabases (db
, &rdb
);
316 /* Get Fallback resources */
317 db
= get_fallback (display
);
319 XrmMergeDatabases (db
, &rdb
);
321 /* Get application user defaults */
322 db
= get_user_app (myclass
);
324 XrmMergeDatabases (db
, &rdb
);
326 /* get User defaults */
327 db
= get_user_db (display
);
329 XrmMergeDatabases (db
, &rdb
);
331 /* Get Environment defaults. */
332 db
= get_environ_db ();
334 XrmMergeDatabases (db
, &rdb
);
336 /* Last, merge in any specification from the command line. */
337 if (xrm_string
!= NULL
)
339 db
= XrmGetStringDatabase (xrm_string
);
341 XrmMergeDatabases (db
, &rdb
);
347 /* Retrieve the value of the resource specified by NAME with class CLASS
348 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
351 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
354 XrmRepresentation expected_type
;
358 XrmName namelist
[100];
359 XrmClass classlist
[100];
360 XrmRepresentation type
;
362 XrmStringToNameList(name
, namelist
);
363 XrmStringToClassList(class, classlist
);
365 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
366 && (type
== expected_type
))
368 if (type
== x_rm_string
)
369 (char *) ret_value
->addr
= value
.addr
;
371 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
379 /* Retrieve the string resource specified by NAME with CLASS from
383 x_get_string_resource (rdb
, name
, class)
389 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
390 return (char *) value
.addr
;
397 #include "arg-list.h"
400 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
402 int x1
, x2
, x3
, x4
, x5
;
409 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
418 char *displayname
, *resource_string
, *class;
422 arg_list
= arg_listify (argc
, argv
);
424 lp
= member ("-d", arg_list
);
426 displayname
= car (cdr (lp
));
428 displayname
= "localhost:0.0";
430 lp
= member ("-xrm", arg_list
);
432 resource_string
= car (cdr (lp
));
434 resource_string
= (char *) 0;
436 lp
= member ("-c", arg_list
);
438 class = car (cdr (lp
));
442 free_arglist (arg_list
);
446 if (!(display
= XOpenDisplay (displayname
)))
447 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
449 xdb
= x_load_resources (display
, resource_string
, class);
452 /* In a real program, you'd want to also do this: */
464 char *value
= x_get_string_resource (xdb
, line
, class);
467 printf ("\t%s: %s\n\n", line
, value
);
469 printf ("\tNo Value.\n\n");
474 printf ("\tExit.\n\n");
476 XCloseDisplay (display
);