More changes for itcl.
[emacs.git] / src / xrdb.c
blobea2bdd13c007679af7684990e7e6f8ff3297b04d
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994 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)
7 any later version.
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 */
20 #ifdef emacs
21 #include <config.h>
22 #endif
24 #include <stdio.h>
26 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
27 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
28 #ifdef USG5
29 #ifndef SYSV
30 #define SYSV
31 #endif
32 #include <unistd.h>
33 #endif /* USG5 */
35 #endif /* 1 */
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #if 0
40 #include <X11/Xos.h>
41 #endif
42 #include <X11/X.h>
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
45 #ifdef VMS
46 #include "vms-pwd.h"
47 #else
48 #include <pwd.h>
49 #endif
50 #include <sys/stat.h>
52 #if !defined(S_ISDIR) && defined(S_IFDIR)
53 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
54 #endif
56 extern char *getenv ();
58 /* This does cause trouble on AIX. I'm going to take the comment at
59 face value. */
60 #if 0
61 extern short getuid (); /* If this causes portability problems,
62 I think we should just delete it; it'll
63 default to `int' anyway. */
64 #endif
66 #ifdef DECLARE_GETPWUID_WITH_UID_T
67 extern struct passwd *getpwuid (uid_t);
68 extern struct passwd *getpwnam (const char *);
69 #else
70 extern struct passwd *getpwuid ();
71 extern struct passwd *getpwnam ();
72 #endif
74 extern char *get_system_name ();
76 /* Make sure not to #include anything after these definitions. Let's
77 not step on anyone's prototypes. */
78 #ifdef emacs
79 #define malloc xmalloc
80 #define realloc xrealloc
81 #define free xfree
82 #endif
84 char *x_get_string_resource ();
85 static int file_p ();
88 /* X file search path processing. */
91 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
92 and friends, or zero if none was specified. */
93 char *x_customization_string;
96 /* Return the value of the emacs.customization (Emacs.Customization)
97 resource, for later use in search path decoding. If we find no
98 such resource, return zero. */
99 char *
100 x_get_customization_string (db, name, class)
101 XrmDatabase db;
102 char *name, *class;
104 char *full_name
105 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
106 char *full_class
107 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
108 char *result;
110 sprintf (full_name, "%s.%s", name, "customization");
111 sprintf (full_class, "%s.%s", class, "Customization");
113 result = x_get_string_resource (db, full_name, full_class);
115 if (result)
117 char *copy = (char *) malloc (strlen (result) + 1);
118 strcpy (copy, result);
119 return copy;
121 else
122 return 0;
126 /* Expand all the Xt-style %-escapes in STRING, whose length is given
127 by STRING_LEN. Here are the escapes we're supposed to recognize:
129 %N The value of the application's class name
130 %T The value of the type parameter ("app-defaults" in this
131 context)
132 %S The value of the suffix parameter ("" in this context)
133 %L The language string associated with the specified display
134 (We use the "LANG" environment variable here, if it's set.)
135 %l The language part of the display's language string
136 (We treat this just like %L. If someone can tell us what
137 we're really supposed to do, dandy.)
138 %t The territory part of the display's language string
139 (This never gets used.)
140 %c The codeset part of the display's language string
141 (This never gets used either.)
142 %C The customization string retrieved from the resource
143 database associated with display.
144 (This is x_customization_string.)
146 Return the expanded file name if it exists and is readable, and
147 refers to %L only when the LANG environment variable is set, or
148 otherwise provided by X.
150 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
151 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
152 alone.
154 Return NULL otherwise. */
156 static char *
157 magic_file_p (string, string_len, class, escaped_suffix, suffix)
158 char *string;
159 int string_len;
160 char *class, *escaped_suffix, *suffix;
162 char *lang = getenv ("LANG");
164 int path_size = 100;
165 char *path = (char *) malloc (path_size);
166 int path_len = 0;
168 char *p = string;
170 while (p < string + string_len)
172 /* The chunk we're about to stick on the end of result. */
173 char *next;
174 int next_len;
176 if (*p == '%')
178 p++;
180 if (p >= string + string_len)
181 next_len = 0;
182 else
183 switch (*p)
185 case '%':
186 next = "%";
187 next_len = 1;
188 break;
190 case 'C':
191 next = (x_customization_string
192 ? x_customization_string
193 : "");
194 next_len = strlen (next);
195 break;
197 case 'N':
198 next = class;
199 next_len = strlen (class);
200 break;
202 case 'T':
203 next = "app-defaults";
204 next_len = strlen (next);
205 break;
207 default:
208 case 'S':
209 next_len = 0;
210 break;
212 case 'L':
213 case 'l':
214 if (! lang)
216 free (path);
217 return NULL;
220 next = lang;
221 next_len = strlen (next);
222 break;
224 case 't':
225 case 'c':
226 free (path);
227 return NULL;
230 else
231 next = p, next_len = 1;
233 /* Do we have room for this component followed by a '\0' ? */
234 if (path_len + next_len + 1 > path_size)
236 path_size = (path_len + next_len + 1) * 2;
237 path = (char *) realloc (path, path_size);
240 bcopy (next, path + path_len, next_len);
241 path_len += next_len;
243 p++;
245 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
246 if (p >= string + string_len && escaped_suffix)
248 string = escaped_suffix;
249 string_len = strlen (string);
250 p = string;
251 escaped_suffix = NULL;
255 /* Perhaps we should add the SUFFIX now. */
256 if (suffix)
258 int suffix_len = strlen (suffix);
260 if (path_len + suffix_len + 1 > path_size)
262 path_size = (path_len + suffix_len + 1);
263 path = (char *) realloc (path, path_size);
266 bcopy (suffix, path + path_len, suffix_len);
267 path_len += suffix_len;
270 path[path_len] = '\0';
272 if (! file_p (path))
274 free (path);
275 return NULL;
278 return path;
282 static char *
283 gethomedir ()
285 struct passwd *pw;
286 char *ptr;
287 char *copy;
289 if ((ptr = getenv ("HOME")) == NULL)
291 if ((ptr = getenv ("LOGNAME")) != NULL
292 || (ptr = getenv ("USER")) != NULL)
293 pw = getpwnam (ptr);
294 else
295 pw = getpwuid (getuid ());
297 if (pw)
298 ptr = pw->pw_dir;
301 if (ptr == NULL)
302 return "/";
304 copy = (char *) malloc (strlen (ptr) + 2);
305 strcpy (copy, ptr);
306 strcat (copy, "/");
308 return copy;
312 static int
313 file_p (path)
314 char *path;
316 struct stat status;
318 return (access (path, 4) == 0 /* exists and is readable */
319 && stat (path, &status) == 0 /* get the status */
320 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
324 /* Find the first element of SEARCH_PATH which exists and is readable,
325 after expanding the %-escapes. Return 0 if we didn't find any, and
326 the path name of the one we found otherwise. */
328 static char *
329 search_magic_path (search_path, class, escaped_suffix, suffix)
330 char *search_path, *class, *escaped_suffix, *suffix;
332 register char *s, *p;
334 for (s = search_path; *s; s = p)
336 for (p = s; *p && *p != ':'; p++)
339 if (p > s)
341 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
342 if (path)
343 return path;
345 else if (*p == ':')
347 char *path;
349 s = "%N%S";
350 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
351 if (path)
352 return path;
355 if (*p == ':')
356 p++;
359 return 0;
362 /* Producing databases for individual sources. */
364 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/%L/%T/%N%C%S:/usr/lib/X11/%l/%T/%N%C%S:/usr/lib/X11/%T/%N%C%S:/usr/lib/X11/%L/%T/%N%S:/usr/lib/X11/%l/%T/%N%S:/usr/lib/X11/%T/%N%S"
366 static XrmDatabase
367 get_system_app (class)
368 char *class;
370 XrmDatabase db = NULL;
371 char *path;
373 path = getenv ("XFILESEARCHPATH");
374 if (! path) path = X_DEFAULT_SEARCH_PATH;
376 path = search_magic_path (path, class, 0, 0);
377 if (path)
379 db = XrmGetFileDatabase (path);
380 free (path);
383 return db;
387 static XrmDatabase
388 get_fallback (display)
389 Display *display;
391 XrmDatabase db;
393 return NULL;
397 static XrmDatabase
398 get_user_app (class)
399 char *class;
401 char *path;
402 char *file = 0;
404 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
405 names, not directories. */
406 if (((path = getenv ("XUSERFILESEARCHPATH"))
407 && (file = search_magic_path (path, class, 0, 0)))
409 /* Check for APPLRESDIR; it is a path of directories. In each,
410 we have to search for LANG/CLASS and then CLASS. */
411 || ((path = getenv ("XAPPLRESDIR"))
412 && ((file = search_magic_path (path, class, "/%L/%N", 0))
413 || (file = search_magic_path (path, class, "/%N", 0))))
415 /* Check in the home directory. This is a bit of a hack; let's
416 hope one's home directory doesn't contain any %-escapes. */
417 || (path = gethomedir (),
418 ((file = search_magic_path (path, class, "%L/%N", 0))
419 || (file = search_magic_path (path, class, "%N", 0)))))
421 XrmDatabase db = XrmGetFileDatabase (file);
422 free (file);
423 return db;
425 else
426 return NULL;
430 static XrmDatabase
431 get_user_db (display)
432 Display *display;
434 XrmDatabase db;
435 char *xdefs;
437 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
438 xdefs = XResourceManagerString (display);
439 #else
440 xdefs = display->xdefaults;
441 #endif
443 if (xdefs != NULL)
444 db = XrmGetStringDatabase (xdefs);
445 else
447 char *home;
448 char *xdefault;
450 home = gethomedir ();
451 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
452 strcpy (xdefault, home);
453 strcat (xdefault, ".Xdefaults");
454 db = XrmGetFileDatabase (xdefault);
455 free (home);
456 free (xdefault);
459 #ifdef HAVE_XSCREENRESOURCESTRING
460 /* Get the screen-specific resources too. */
461 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
462 if (xdefs != NULL)
464 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
465 XFree (xdefs);
467 #endif
469 return db;
472 static XrmDatabase
473 get_environ_db ()
475 XrmDatabase db;
476 char *p;
477 char *path = 0, *home = 0, *host;
479 if ((p = getenv ("XENVIRONMENT")) == NULL)
481 home = gethomedir ();
482 host = get_system_name ();
483 path = (char *) malloc (strlen (home)
484 + sizeof (".Xdefaults-")
485 + strlen (host));
486 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
487 p = path;
490 db = XrmGetFileDatabase (p);
492 if (path) free (path);
493 if (home) free (home);
495 return db;
498 /* External interface. */
500 /* Types of values that we can find in a database */
502 #define XrmStringType "String" /* String representation */
503 XrmRepresentation x_rm_string; /* Quark representation */
505 /* Load X resources based on the display and a possible -xrm option. */
507 XrmDatabase
508 x_load_resources (display, xrm_string, myname, myclass)
509 Display *display;
510 char *xrm_string, *myname, *myclass;
512 char *xdefs;
513 XrmDatabase user_database;
514 XrmDatabase rdb;
515 XrmDatabase db;
517 x_rm_string = XrmStringToQuark (XrmStringType);
518 #ifndef USE_X_TOOLKIT
519 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
520 I suspect it's because the toolkit version does this elsewhere. */
521 XrmInitialize ();
522 #endif
523 rdb = XrmGetStringDatabase ("");
525 user_database = get_user_db (display);
527 /* Figure out what the "customization string" is, so we can use it
528 to decode paths. */
529 if (x_customization_string)
530 free (x_customization_string);
531 x_customization_string
532 = x_get_customization_string (user_database, myname, myclass);
534 /* Get application system defaults */
535 db = get_system_app (myclass);
536 if (db != NULL)
537 XrmMergeDatabases (db, &rdb);
539 /* Get Fallback resources */
540 db = get_fallback (display);
541 if (db != NULL)
542 XrmMergeDatabases (db, &rdb);
544 /* Get application user defaults */
545 db = get_user_app (myclass);
546 if (db != NULL)
547 XrmMergeDatabases (db, &rdb);
549 /* get User defaults */
550 if (user_database != NULL)
551 XrmMergeDatabases (user_database, &rdb);
553 /* Get Environment defaults. */
554 db = get_environ_db ();
555 if (db != NULL)
556 XrmMergeDatabases (db, &rdb);
558 /* Last, merge in any specification from the command line. */
559 if (xrm_string != NULL)
561 db = XrmGetStringDatabase (xrm_string);
562 if (db != NULL)
563 XrmMergeDatabases (db, &rdb);
566 return rdb;
570 /* Retrieve the value of the resource specified by NAME with class CLASS
571 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
574 x_get_resource (rdb, name, class, expected_type, ret_value)
575 XrmDatabase rdb;
576 char *name, *class;
577 XrmRepresentation expected_type;
578 XrmValue *ret_value;
580 XrmValue value;
581 XrmName namelist[100];
582 XrmClass classlist[100];
583 XrmRepresentation type;
585 XrmStringToNameList(name, namelist);
586 XrmStringToClassList(class, classlist);
588 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
589 && (type == expected_type))
591 if (type == x_rm_string)
592 ret_value->addr = (char *) value.addr;
593 else
594 bcopy (value.addr, ret_value->addr, ret_value->size);
596 return value.size;
599 return 0;
602 /* Retrieve the string resource specified by NAME with CLASS from
603 database RDB. */
605 char *
606 x_get_string_resource (rdb, name, class)
607 XrmDatabase rdb;
608 char *name, *class;
610 XrmValue value;
612 if (x_get_resource (rdb, name, class, x_rm_string, &value))
613 return (char *) value.addr;
615 return (char *) 0;
618 /* Stand-alone test facilities. */
620 #ifdef TESTRM
622 typedef char **List;
623 #define arg_listify(len, list) (list)
624 #define car(list) (*(list))
625 #define cdr(list) (list + 1)
626 #define NIL(list) (! *(list))
627 #define free_arglist(list)
629 static List
630 member (elt, list)
631 char *elt;
632 List list;
634 List p;
636 for (p = list; ! NIL (p); p = cdr (p))
637 if (! strcmp (elt, car (p)))
638 return p;
640 return p;
643 static void
644 fatal (msg, prog, x1, x2, x3, x4, x5)
645 char *msg, *prog;
646 int x1, x2, x3, x4, x5;
648 extern int errno;
650 if (errno)
651 perror (prog);
653 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
654 exit (1);
657 main (argc, argv)
658 int argc;
659 char **argv;
661 Display *display;
662 char *displayname, *resource_string, *class, *name;
663 XrmDatabase xdb;
664 List arg_list, lp;
666 arg_list = arg_listify (argc, argv);
668 lp = member ("-d", arg_list);
669 if (!NIL (lp))
670 displayname = car (cdr (lp));
671 else
672 displayname = "localhost:0.0";
674 lp = member ("-xrm", arg_list);
675 if (! NIL (lp))
676 resource_string = car (cdr (lp));
677 else
678 resource_string = (char *) 0;
680 lp = member ("-c", arg_list);
681 if (! NIL (lp))
682 class = car (cdr (lp));
683 else
684 class = "Emacs";
686 lp = member ("-n", arg_list);
687 if (! NIL (lp))
688 name = car (cdr (lp));
689 else
690 name = "emacs";
692 free_arglist (arg_list);
694 if (!(display = XOpenDisplay (displayname)))
695 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
697 xdb = x_load_resources (display, resource_string, name, class);
699 /* In a real program, you'd want to also do this: */
700 display->db = xdb;
702 while (1)
704 char query_name[90];
705 char query_class[90];
707 printf ("Name: ");
708 gets (query_name);
710 if (strlen (query_name))
712 char *value;
714 printf ("Class: ");
715 gets (query_class);
717 value = x_get_string_resource (xdb, query_name, query_class);
719 if (value != NULL)
720 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
721 else
722 printf ("\tNo Value.\n\n");
724 else
725 break;
727 printf ("\tExit.\n\n");
729 XCloseDisplay (display);
731 #endif /* TESTRM */