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 */
24 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
25 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
33 /* This should be included before the X include files; otherwise, we get
34 warnings about redefining NULL under BSD 4.3. */
35 #include <sys/param.h>
38 #include <X11/Xatom.h>
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
53 #define MAXPATHLEN 256
56 extern char *getenv ();
58 /* This does cause trouble on AIX. I'm going to take the comment at
61 extern short getuid (); /* If this causes portability problems,
62 I think we should just delete it; it'll
63 default to `int' anyway. */
67 extern struct passwd
*getpwuid (uid_t
);
68 extern struct passwd
*getpwnam (const char *);
70 extern struct passwd
*getpwuid ();
71 extern struct passwd
*getpwnam ();
82 if ((ptr
= getenv ("HOME")) == NULL
)
84 if ((ptr
= getenv ("USER")) != NULL
)
101 strcpy (dirname
, ptr
);
103 dirname
+= strlen (dirname
);
117 return (access (path
, 4) == 0 /* exists and is readable */
118 && stat (path
, &status
) == 0 /* get the status */
119 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
123 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
126 /* Isn't this just disgusting? */
128 #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"
131 decode_magic (string
, file
, return_path
)
132 char *string
, *file
, *return_path
;
135 char *t
= return_path
;
156 if (*t
== '/' && *p
== '/')
164 strcat (return_path
, file
);
166 if (file_p (return_path
))
169 return_path
[0] = '\0';
174 magic_searchpath_decoder (incantation_string
, file
, return_path
)
175 char *incantation_string
, *return_path
, *file
;
177 register char *s
= incantation_string
;
180 /* Must be big enough for "%N%S". */
181 register int string_size
= MAXPATHLEN
;
182 register char *string
= (char *) alloca (string_size
* sizeof (*string
));
188 while (*p
&& *p
!= ':')
191 if (*p
== ':' && *(p
+ 1) == ':')
193 /* We know string is big enough for this. */
194 bcopy ("%N%S", string
, 5);
195 if (decode_magic (string
, file
, return_path
))
206 if (string_size
< len
+1)
208 string_size
= 2 * len
;
209 string
= (char *) alloca (string_size
* sizeof (*string
));
211 bcopy (s
, string
, len
);
213 if (decode_magic (string
, file
, return_path
))
227 get_system_app (class)
231 char path
[MAXPATHLEN
];
234 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
235 p
= X_DEFAULT_SEARCH_PATH
;
237 if (! magic_searchpath_decoder (p
, class, path
))
240 db
= XrmGetFileDatabase (path
);
245 get_fallback (display
)
259 char path
[MAXPATHLEN
];
261 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
263 char homedir
[MAXPATHLEN
];
267 gethomedir (homedir
);
269 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
271 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
272 magic_path
= (char *) alloca ((3 * strlen (homedir
))
273 + strlen (default_magic
));
274 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
278 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
279 magic_path
= (char *) alloca ((3 * strlen (p
))
280 + strlen (default_magic
)
282 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
286 if (! magic_searchpath_decoder (magic_path
, class, path
))
289 db
= XrmGetFileDatabase (path
);
294 get_user_db (display
)
300 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
301 xdefs
= XResourceManagerString (display
);
303 xdefs
= display
->xdefaults
;
307 db
= XrmGetStringDatabase (xdefs
);
310 char xdefault
[MAXPATHLEN
];
312 gethomedir (xdefault
);
313 strcat (xdefault
, ".Xdefaults");
314 db
= XrmGetFileDatabase (xdefault
);
325 char path
[MAXPATHLEN
];
327 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
330 strcat (path
, ".Xdefaults-");
331 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
335 db
= XrmGetFileDatabase (p
);
339 /* Types of values that we can find in a database */
341 #define XrmStringType "String" /* String representation */
342 XrmRepresentation x_rm_string
; /* Quark representation */
344 /* Load X resources based on the display and a possible -xrm option. */
347 x_load_resources (display
, xrm_string
, myclass
)
349 char *xrm_string
, *myclass
;
355 x_rm_string
= XrmStringToQuark (XrmStringType
);
357 rdb
= XrmGetStringDatabase ("");
359 /* Get application system defaults */
360 db
= get_system_app (myclass
);
362 XrmMergeDatabases (db
, &rdb
);
364 /* Get Fallback resources */
365 db
= get_fallback (display
);
367 XrmMergeDatabases (db
, &rdb
);
369 /* Get application user defaults */
370 db
= get_user_app (myclass
);
372 XrmMergeDatabases (db
, &rdb
);
374 /* get User defaults */
375 db
= get_user_db (display
);
377 XrmMergeDatabases (db
, &rdb
);
379 /* Get Environment defaults. */
380 db
= get_environ_db ();
382 XrmMergeDatabases (db
, &rdb
);
384 /* Last, merge in any specification from the command line. */
385 if (xrm_string
!= NULL
)
387 db
= XrmGetStringDatabase (xrm_string
);
389 XrmMergeDatabases (db
, &rdb
);
395 /* Retrieve the value of the resource specified by NAME with class CLASS
396 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
399 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
402 XrmRepresentation expected_type
;
406 XrmName namelist
[100];
407 XrmClass classlist
[100];
408 XrmRepresentation type
;
410 XrmStringToNameList(name
, namelist
);
411 XrmStringToClassList(class, classlist
);
413 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
414 && (type
== expected_type
))
416 if (type
== x_rm_string
)
417 ret_value
->addr
= (char *) value
.addr
;
419 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
427 /* Retrieve the string resource specified by NAME with CLASS from
431 x_get_string_resource (rdb
, name
, class)
437 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
438 return (char *) value
.addr
;
445 #include "arg-list.h"
448 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
450 int x1
, x2
, x3
, x4
, x5
;
457 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
466 char *displayname
, *resource_string
, *class;
470 arg_list
= arg_listify (argc
, argv
);
472 lp
= member ("-d", arg_list
);
474 displayname
= car (cdr (lp
));
476 displayname
= "localhost:0.0";
478 lp
= member ("-xrm", arg_list
);
480 resource_string
= car (cdr (lp
));
482 resource_string
= (char *) 0;
484 lp
= member ("-c", arg_list
);
486 class = car (cdr (lp
));
490 free_arglist (arg_list
);
494 if (!(display
= XOpenDisplay (displayname
)))
495 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
497 xdb
= x_load_resources (display
, resource_string
, class);
500 /* In a real program, you'd want to also do this: */
512 char *value
= x_get_string_resource (xdb
, line
, class);
515 printf ("\t%s: %s\n\n", line
, value
);
517 printf ("\tNo Value.\n\n");
522 printf ("\tExit.\n\n");
524 XCloseDisplay (display
);