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 */
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. */
66 extern struct passwd
*getpwuid ();
67 extern struct passwd
*getpwnam ();
77 if ((ptr
= getenv ("HOME")) == NULL
)
79 if ((ptr
= getenv ("USER")) != NULL
)
96 strcpy (dirname
, ptr
);
98 dirname
+= strlen (dirname
);
112 return (access (path
, 4) == 0 /* exists and is readable */
113 && stat (path
, &status
) == 0 /* get the status */
114 && (status
.st_mode
& S_IFDIR
) == 0); /* not a directory */
118 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
121 /* Isn't this just disgusting? */
123 #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"
126 decode_magic (string
, file
, return_path
)
127 char *string
, *file
, *return_path
;
130 char *t
= return_path
;
151 if (*t
== '/' && *p
== '/')
159 strcat (return_path
, file
);
161 if (file_p (return_path
))
164 return_path
[0] = '\0';
169 magic_searchpath_decoder (incantation_string
, file
, return_path
)
170 char *incantation_string
, *return_path
, *file
;
172 register char *s
= incantation_string
;
175 /* Must be big enough for "%N%S". */
176 register int string_size
= MAXPATHLEN
;
177 register char *string
= (char *) alloca (string_size
* sizeof (*string
));
183 while (*p
&& *p
!= ':')
186 if (*p
== ':' && *(p
+ 1) == ':')
188 /* We know string is big enough for this. */
189 bcopy ("%N%S", string
, 5);
190 if (decode_magic (string
, file
, return_path
))
201 if (string_size
< len
+1)
203 string_size
= 2 * len
;
204 string
= (char *) alloca (string_size
* sizeof (*string
));
206 bcopy (s
, string
, len
);
207 string
[len
+ 1] = '\0';
208 if (decode_magic (string
, file
, return_path
))
222 get_system_app (class)
226 char path
[MAXPATHLEN
];
229 if ((p
= getenv ("XFILESEARCHPATH")) == NULL
)
230 p
= X_DEFAULT_SEARCH_PATH
;
232 if (! magic_searchpath_decoder (p
, class, path
))
235 db
= XrmGetFileDatabase (path
);
240 get_fallback (display
)
254 char path
[MAXPATHLEN
];
256 if ((magic_path
= getenv ("XUSERFILESEARCHPATH")) == NULL
)
258 char homedir
[MAXPATHLEN
];
262 gethomedir (homedir
);
264 if ((p
= getenv ("XAPPLRESDIR")) == NULL
)
266 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
267 magic_path
= (char *) alloca ((3 * strlen (homedir
))
268 + strlen (default_magic
));
269 sprintf (magic_path
, default_magic
, homedir
, homedir
, homedir
);
273 default_magic
= "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
274 magic_path
= (char *) alloca ((3 * strlen (p
))
275 + strlen (default_magic
)
277 sprintf (magic_path
, default_magic
, p
, p
, p
, homedir
);
281 if (! magic_searchpath_decoder (magic_path
, class, path
))
284 db
= XrmGetFileDatabase (path
);
289 get_user_db (display
)
295 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
296 xdefs
= XResourceManagerString (display
);
298 xdefs
= display
->xdefaults
;
302 db
= XrmGetStringDatabase (xdefs
);
305 char xdefault
[MAXPATHLEN
];
307 gethomedir (xdefault
);
308 strcat (xdefault
, ".Xdefaults");
309 db
= XrmGetFileDatabase (xdefault
);
320 char path
[MAXPATHLEN
];
322 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
325 strcat (path
, ".Xdefaults-");
326 gethostname (path
+ strlen (path
), MAXPATHLEN
- strlen (path
));
330 db
= XrmGetFileDatabase (p
);
334 /* Types of values that we can find in a database */
336 #define XrmStringType "String" /* String representation */
337 XrmRepresentation x_rm_string
; /* Quark representation */
339 /* Load X resources based on the display and a possible -xrm option. */
342 x_load_resources (display
, xrm_string
, myclass
)
344 char *xrm_string
, *myclass
;
350 x_rm_string
= XrmStringToQuark (XrmStringType
);
352 rdb
= XrmGetStringDatabase ("");
354 /* Get application system defaults */
355 db
= get_system_app (myclass
);
357 XrmMergeDatabases (db
, &rdb
);
359 /* Get Fallback resources */
360 db
= get_fallback (display
);
362 XrmMergeDatabases (db
, &rdb
);
364 /* Get application user defaults */
365 db
= get_user_app (myclass
);
367 XrmMergeDatabases (db
, &rdb
);
369 /* get User defaults */
370 db
= get_user_db (display
);
372 XrmMergeDatabases (db
, &rdb
);
374 /* Get Environment defaults. */
375 db
= get_environ_db ();
377 XrmMergeDatabases (db
, &rdb
);
379 /* Last, merge in any specification from the command line. */
380 if (xrm_string
!= NULL
)
382 db
= XrmGetStringDatabase (xrm_string
);
384 XrmMergeDatabases (db
, &rdb
);
390 /* Retrieve the value of the resource specified by NAME with class CLASS
391 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
394 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
397 XrmRepresentation expected_type
;
401 XrmName namelist
[100];
402 XrmClass classlist
[100];
403 XrmRepresentation type
;
405 XrmStringToNameList(name
, namelist
);
406 XrmStringToClassList(class, classlist
);
408 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
409 && (type
== expected_type
))
411 if (type
== x_rm_string
)
412 ret_value
->addr
= (char *) value
.addr
;
414 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
422 /* Retrieve the string resource specified by NAME with CLASS from
426 x_get_string_resource (rdb
, name
, class)
432 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
433 return (char *) value
.addr
;
440 #include "arg-list.h"
443 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
445 int x1
, x2
, x3
, x4
, x5
;
452 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
461 char *displayname
, *resource_string
, *class;
465 arg_list
= arg_listify (argc
, argv
);
467 lp
= member ("-d", arg_list
);
469 displayname
= car (cdr (lp
));
471 displayname
= "localhost:0.0";
473 lp
= member ("-xrm", arg_list
);
475 resource_string
= car (cdr (lp
));
477 resource_string
= (char *) 0;
479 lp
= member ("-c", arg_list
);
481 class = car (cdr (lp
));
485 free_arglist (arg_list
);
489 if (!(display
= XOpenDisplay (displayname
)))
490 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
492 xdb
= x_load_resources (display
, resource_string
, class);
495 /* In a real program, you'd want to also do this: */
507 char *value
= x_get_string_resource (xdb
, line
, class);
510 printf ("\t%s: %s\n\n", line
, value
);
512 printf ("\tNo Value.\n\n");
517 printf ("\tExit.\n\n");
519 XCloseDisplay (display
);