Fix calendar-cursor-to-date to handle starred days correctly.
[emacs.git] / src / xrdb.c
blob9ab9f87e9151a5e012e3d896c61715a73af70863
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)
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 #define SYSV
30 #include <unistd.h>
31 #endif /* USG5 */
33 #endif /* 1 */
35 /* This should be included before the X include files; otherwise, we get
36 warnings about redefining NULL under BSD 4.3. */
37 #include <sys/param.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xatom.h>
41 #if 0
42 #include <X11/Xos.h>
43 #endif
44 #include <X11/X.h>
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
47 #ifdef VMS
48 #include "vms-pwd.h"
49 #else
50 #include <pwd.h>
51 #endif
52 #include <sys/stat.h>
54 #ifndef MAXPATHLEN
55 #define MAXPATHLEN 256
56 #endif
58 #if !defined(S_ISDIR) && defined(S_IFDIR)
59 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
60 #endif
62 extern char *getenv ();
64 /* This does cause trouble on AIX. I'm going to take the comment at
65 face value. */
66 #if 0
67 extern short getuid (); /* If this causes portability problems,
68 I think we should just delete it; it'll
69 default to `int' anyway. */
70 #endif
72 #if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
73 extern struct passwd *getpwuid (uid_t);
74 extern struct passwd *getpwnam (const char *);
75 #else
76 extern struct passwd *getpwuid ();
77 extern struct passwd *getpwnam ();
78 #endif
80 extern char *get_system_name ();
82 /* Make sure not to #include anything after these definitions. Let's
83 not step on anyone's prototypes. */
84 #ifdef emacs
85 #define malloc xmalloc
86 #define realloc xrealloc
87 #define free xfree
88 #endif
90 char *x_get_string_resource ();
91 static int file_p ();
94 /* X file search path processing. */
97 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
98 and friends, or zero if none was specified. */
99 char *x_customization_string;
102 /* Return the value of the emacs.customization (Emacs.Customization)
103 resource, for later use in search path decoding. If we find no
104 such resource, return zero. */
105 char *
106 x_get_customization_string (db, name, class)
107 XrmDatabase db;
108 char *name, *class;
110 char *full_name
111 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
112 char *full_class
113 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
114 char *result;
116 sprintf (full_name, "%s.%s", name, "customization");
117 sprintf (full_class, "%s.%s", class, "Customization");
119 result = x_get_string_resource (db, full_name, full_class);
121 if (result)
123 char *copy = (char *) malloc (strlen (result) + 1);
124 strcpy (copy, result);
125 return copy;
127 else
128 return 0;
132 /* Expand all the Xt-style %-escapes in STRING, whose length is given
133 by STRING_LEN. Here are the escapes we're supposed to recognize:
135 %N The value of the application's class name
136 %T The value of the type parameter ("app-defaults" in this
137 context)
138 %S The value of the suffix parameter ("" in this context)
139 %L The language string associated with the specified display
140 (We use the "LANG" environment variable here, if it's set.)
141 %l The language part of the display's language string
142 (We treat this just like %L. If someone can tell us what
143 we're really supposed to do, dandy.)
144 %t The territory part of the display's language string
145 (This never gets used.)
146 %c The codeset part of the display's language string
147 (This never gets used either.)
148 %C The customization string retrieved from the resource
149 database associated with display.
150 (This is x_customization_string.)
152 Return the expanded file name if it exists and is readable, and
153 refers to %L only when the LANG environment variable is set, or
154 otherwise provided by X.
156 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
157 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
158 alone.
160 Return NULL otherwise. */
162 static char *
163 magic_file_p (string, string_len, class, escaped_suffix, suffix)
164 char *string;
165 int string_len;
166 char *class, *escaped_suffix, *suffix;
168 char *lang = getenv ("LANG");
170 int path_size = 100;
171 char *path = (char *) malloc (path_size);
172 int path_len = 0;
174 char *p = string;
176 while (p < string + string_len)
178 /* The chunk we're about to stick on the end of result. */
179 char *next;
180 int next_len;
182 if (*p == '%')
184 p++;
186 if (p >= string + string_len)
187 next_len = 0;
188 else
189 switch (*p)
191 case '%':
192 next = "%";
193 next_len = 1;
194 break;
196 case 'C':
197 next = (x_customization_string
198 ? x_customization_string
199 : "");
200 next_len = strlen (next);
201 break;
203 case 'N':
204 next = class;
205 next_len = strlen (class);
206 break;
208 case 'T':
209 next = "app-defaults";
210 next_len = strlen (next);
211 break;
213 default:
214 case 'S':
215 next_len = 0;
216 break;
218 case 'L':
219 case 'l':
220 if (! lang)
222 free (path);
223 return NULL;
226 next = lang;
227 next_len = strlen (next);
228 break;
230 case 't':
231 case 'c':
232 free (path);
233 return NULL;
236 else
237 next = p, next_len = 1;
239 /* Do we have room for this component followed by a '\0' ? */
240 if (path_len + next_len + 1 > path_size)
242 path_size = (path_len + next_len + 1) * 2;
243 path = (char *) realloc (path, path_size);
246 bcopy (next, path + path_len, next_len);
247 path_len += next_len;
249 p++;
251 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
252 if (p >= string + string_len && escaped_suffix)
254 string = escaped_suffix;
255 string_len = strlen (string);
256 p = string;
257 escaped_suffix = NULL;
261 /* Perhaps we should add the SUFFIX now. */
262 if (suffix)
264 int suffix_len = strlen (suffix);
266 if (path_len + suffix_len + 1 > path_size)
268 path_size = (path_len + suffix_len + 1);
269 path = (char *) realloc (path, path_size);
272 bcopy (suffix, path + path_len, suffix_len);
273 path_len += suffix_len;
276 path[path_len] = '\0';
278 if (! file_p (path))
280 free (path);
281 return NULL;
284 return path;
288 static char *
289 gethomedir ()
291 struct passwd *pw;
292 char *ptr;
293 char *copy;
295 if ((ptr = getenv ("HOME")) == NULL)
297 if ((ptr = getenv ("LOGNAME")) != NULL
298 || (ptr = getenv ("USER")) != NULL)
299 pw = getpwnam (ptr);
300 else
301 pw = getpwuid (getuid ());
303 if (pw)
304 ptr = pw->pw_dir;
307 if (ptr == NULL)
308 return "/";
310 copy = (char *) malloc (strlen (ptr) + 2);
311 strcpy (copy, ptr);
312 strcat (copy, "/");
314 return copy;
318 static int
319 file_p (path)
320 char *path;
322 struct stat status;
324 return (access (path, 4) == 0 /* exists and is readable */
325 && stat (path, &status) == 0 /* get the status */
326 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
330 /* Find the first element of SEARCH_PATH which exists and is readable,
331 after expanding the %-escapes. Return 0 if we didn't find any, and
332 the path name of the one we found otherwise. */
334 static char *
335 search_magic_path (search_path, class, escaped_suffix, suffix)
336 char *search_path, *class, *escaped_suffix, *suffix;
338 register char *s, *p;
340 for (s = search_path; *s; s = p)
342 for (p = s; *p && *p != ':'; p++)
345 if (p > s)
347 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
348 if (path)
349 return path;
351 else if (*p == ':')
353 char *path;
355 s = "%N%S";
356 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
357 if (path)
358 return path;
361 if (*p == ':')
362 p++;
365 return 0;
368 /* Producing databases for individual sources. */
370 #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"
372 static XrmDatabase
373 get_system_app (class)
374 char *class;
376 XrmDatabase db = NULL;
377 char *path;
379 path = getenv ("XFILESEARCHPATH");
380 if (! path) path = X_DEFAULT_SEARCH_PATH;
382 path = search_magic_path (path, class, 0, 0);
383 if (path)
385 db = XrmGetFileDatabase (path);
386 free (path);
389 return db;
393 static XrmDatabase
394 get_fallback (display)
395 Display *display;
397 XrmDatabase db;
399 return NULL;
403 static XrmDatabase
404 get_user_app (class)
405 char *class;
407 char *path;
408 char *file = 0;
410 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
411 names, not directories. */
412 if (((path = getenv ("XUSERFILESEARCHPATH"))
413 && (file = search_magic_path (path, class, 0, 0)))
415 /* Check for APPLRESDIR; it is a path of directories. In each,
416 we have to search for LANG/CLASS and then CLASS. */
417 || ((path = getenv ("XAPPLRESDIR"))
418 && ((file = search_magic_path (path, class, "/%L/%N", 0))
419 || (file = search_magic_path (path, class, "/%N", 0))))
421 /* Check in the home directory. This is a bit of a hack; let's
422 hope one's home directory doesn't contain any %-escapes. */
423 || (path = gethomedir (),
424 ((file = search_magic_path (path, class, "%L/%N", 0))
425 || (file = search_magic_path (path, class, "%N", 0)))))
427 XrmDatabase db = XrmGetFileDatabase (file);
428 free (file);
429 return db;
431 else
432 return NULL;
436 static XrmDatabase
437 get_user_db (display)
438 Display *display;
440 XrmDatabase db;
441 char *xdefs;
443 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
444 xdefs = XResourceManagerString (display);
445 #else
446 xdefs = display->xdefaults;
447 #endif
449 if (xdefs != NULL)
450 db = XrmGetStringDatabase (xdefs);
451 else
453 char *home;
454 char *xdefault;
456 home = gethomedir ();
457 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
458 strcpy (xdefault, home);
459 strcat (xdefault, ".Xdefaults");
460 db = XrmGetFileDatabase (xdefault);
461 free (home);
462 free (xdefault);
465 #ifdef HAVE_XSCREENRESOURCESTRING
466 /* Get the screen-specific resources too. */
467 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
468 if (xdefs != NULL)
470 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
471 XFree (xdefs);
473 #endif
475 return db;
478 static XrmDatabase
479 get_environ_db ()
481 XrmDatabase db;
482 char *p;
483 char *path = 0, *home = 0, *host;
485 if ((p = getenv ("XENVIRONMENT")) == NULL)
487 home = gethomedir ();
488 host = get_system_name ();
489 path = (char *) malloc (strlen (home)
490 + sizeof (".Xdefaults-")
491 + strlen (host));
492 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
493 p = path;
496 db = XrmGetFileDatabase (p);
498 if (path) free (path);
499 if (home) free (home);
501 return db;
504 /* External interface. */
506 /* Types of values that we can find in a database */
508 #define XrmStringType "String" /* String representation */
509 XrmRepresentation x_rm_string; /* Quark representation */
511 /* Load X resources based on the display and a possible -xrm option. */
513 XrmDatabase
514 x_load_resources (display, xrm_string, myname, myclass)
515 Display *display;
516 char *xrm_string, *myname, *myclass;
518 char *xdefs;
519 XrmDatabase user_database;
520 XrmDatabase rdb;
521 XrmDatabase db;
523 x_rm_string = XrmStringToQuark (XrmStringType);
524 XrmInitialize ();
525 rdb = XrmGetStringDatabase ("");
527 user_database = get_user_db (display);
529 /* Figure out what the "customization string" is, so we can use it
530 to decode paths. */
531 if (x_customization_string)
532 free (x_customization_string);
533 x_customization_string
534 = x_get_customization_string (user_database, myname, myclass);
536 /* Get application system defaults */
537 db = get_system_app (myclass);
538 if (db != NULL)
539 XrmMergeDatabases (db, &rdb);
541 /* Get Fallback resources */
542 db = get_fallback (display);
543 if (db != NULL)
544 XrmMergeDatabases (db, &rdb);
546 /* Get application user defaults */
547 db = get_user_app (myclass);
548 if (db != NULL)
549 XrmMergeDatabases (db, &rdb);
551 /* get User defaults */
552 if (user_database != NULL)
553 XrmMergeDatabases (user_database, &rdb);
555 /* Get Environment defaults. */
556 db = get_environ_db ();
557 if (db != NULL)
558 XrmMergeDatabases (db, &rdb);
560 /* Last, merge in any specification from the command line. */
561 if (xrm_string != NULL)
563 db = XrmGetStringDatabase (xrm_string);
564 if (db != NULL)
565 XrmMergeDatabases (db, &rdb);
568 return rdb;
572 /* Retrieve the value of the resource specified by NAME with class CLASS
573 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
576 x_get_resource (rdb, name, class, expected_type, ret_value)
577 XrmDatabase rdb;
578 char *name, *class;
579 XrmRepresentation expected_type;
580 XrmValue *ret_value;
582 XrmValue value;
583 XrmName namelist[100];
584 XrmClass classlist[100];
585 XrmRepresentation type;
587 XrmStringToNameList(name, namelist);
588 XrmStringToClassList(class, classlist);
590 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
591 && (type == expected_type))
593 if (type == x_rm_string)
594 ret_value->addr = (char *) value.addr;
595 else
596 bcopy (value.addr, ret_value->addr, ret_value->size);
598 return value.size;
601 return 0;
604 /* Retrieve the string resource specified by NAME with CLASS from
605 database RDB. */
607 char *
608 x_get_string_resource (rdb, name, class)
609 XrmDatabase rdb;
610 char *name, *class;
612 XrmValue value;
614 if (x_get_resource (rdb, name, class, x_rm_string, &value))
615 return (char *) value.addr;
617 return (char *) 0;
620 /* Stand-alone test facilities. */
622 #ifdef TESTRM
624 typedef char **List;
625 #define arg_listify(len, list) (list)
626 #define car(list) (*(list))
627 #define cdr(list) (list + 1)
628 #define NIL(list) (! *(list))
629 #define free_arglist(list)
631 static List
632 member (elt, list)
633 char *elt;
634 List list;
636 List p;
638 for (p = list; ! NIL (p); p = cdr (p))
639 if (! strcmp (elt, car (p)))
640 return p;
642 return p;
645 static void
646 fatal (msg, prog, x1, x2, x3, x4, x5)
647 char *msg, *prog;
648 int x1, x2, x3, x4, x5;
650 extern int errno;
652 if (errno)
653 perror (prog);
655 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
656 exit (1);
659 main (argc, argv)
660 int argc;
661 char **argv;
663 Display *display;
664 char *displayname, *resource_string, *class, *name;
665 XrmDatabase xdb;
666 List arg_list, lp;
668 arg_list = arg_listify (argc, argv);
670 lp = member ("-d", arg_list);
671 if (!NIL (lp))
672 displayname = car (cdr (lp));
673 else
674 displayname = "localhost:0.0";
676 lp = member ("-xrm", arg_list);
677 if (! NIL (lp))
678 resource_string = car (cdr (lp));
679 else
680 resource_string = (char *) 0;
682 lp = member ("-c", arg_list);
683 if (! NIL (lp))
684 class = car (cdr (lp));
685 else
686 class = "Emacs";
688 lp = member ("-n", arg_list);
689 if (! NIL (lp))
690 name = car (cdr (lp));
691 else
692 name = "emacs";
694 free_arglist (arg_list);
696 if (!(display = XOpenDisplay (displayname)))
697 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
699 xdb = x_load_resources (display, resource_string, name, class);
701 /* In a real program, you'd want to also do this: */
702 display->db = xdb;
704 while (1)
706 char query_name[90];
707 char query_class[90];
709 printf ("Name: ");
710 gets (query_name);
712 if (strlen (query_name))
714 char *value;
716 printf ("Class: ");
717 gets (query_class);
719 value = x_get_string_resource (xdb, query_name, query_class);
721 if (value != NULL)
722 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
723 else
724 printf ("\tNo Value.\n\n");
726 else
727 break;
729 printf ("\tExit.\n\n");
731 XCloseDisplay (display);
733 #endif /* TESTRM */