Change 'needs-patch to 'needs-update.
[emacs.git] / src / xrdb.c
blob6472c8988a24394d706bdbec31bb7bde0684897f
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Written by jla, 4/90 */
24 #ifdef emacs
25 #include <config.h>
26 #endif
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <epaths.h>
34 #include <stdio.h>
36 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
37 /* This avoids lossage in the `dual-universe' headers on AT&T SysV
38 X11. Don't do it on Solaris, because it breaks compilation with
39 XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
40 2 */
41 #if defined(USG5) && !defined(SOLARIS2)
42 #ifndef SYSV
43 #define SYSV
44 #endif
45 #endif /* USG5 && !SOLARIS2 */
47 #endif /* 1 */
49 #include <X11/Xlib.h>
50 #include <X11/Xatom.h>
51 #if 0
52 #include <X11/Xos.h>
53 #endif
54 #include <X11/X.h>
55 #include <X11/Xutil.h>
56 #include <X11/Xresource.h>
57 #ifdef HAVE_PWD_H
58 #include <pwd.h>
59 #endif
60 #include <sys/stat.h>
62 #if !defined(S_ISDIR) && defined(S_IFDIR)
63 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
64 #endif
66 #include "lisp.h"
68 extern char *getenv ();
70 /* This does cause trouble on AIX. I'm going to take the comment at
71 face value. */
72 #if 0
73 extern short getuid (); /* If this causes portability problems,
74 I think we should just delete it; it'll
75 default to `int' anyway. */
76 #endif
78 #ifdef DECLARE_GETPWUID_WITH_UID_T
79 extern struct passwd *getpwuid (uid_t);
80 extern struct passwd *getpwnam (const char *);
81 #else
82 extern struct passwd *getpwuid ();
83 extern struct passwd *getpwnam ();
84 #endif
86 extern char *get_system_name ();
88 /* Make sure not to #include anything after these definitions. Let's
89 not step on anyone's prototypes. */
90 #ifdef emacs
91 /* darwin.h may have already defined these. */
92 #undef malloc
93 #undef realloc
94 #undef free
95 #define malloc xmalloc
96 #define realloc xrealloc
97 #define free xfree
98 #endif
100 char *x_get_string_resource ();
101 static int file_p ();
104 /* X file search path processing. */
107 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
108 and friends, or zero if none was specified. */
109 char *x_customization_string;
112 /* Return the value of the emacs.customization (Emacs.Customization)
113 resource, for later use in search path decoding. If we find no
114 such resource, return zero. */
115 char *
116 x_get_customization_string (db, name, class)
117 XrmDatabase db;
118 char *name, *class;
120 char *full_name
121 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
122 char *full_class
123 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
124 char *result;
126 sprintf (full_name, "%s.%s", name, "customization");
127 sprintf (full_class, "%s.%s", class, "Customization");
129 result = x_get_string_resource (db, full_name, full_class);
131 if (result)
133 char *copy = (char *) malloc (strlen (result) + 1);
134 strcpy (copy, result);
135 return copy;
137 else
138 return 0;
142 /* Expand all the Xt-style %-escapes in STRING, whose length is given
143 by STRING_LEN. Here are the escapes we're supposed to recognize:
145 %N The value of the application's class name
146 %T The value of the type parameter ("app-defaults" in this
147 context)
148 %S The value of the suffix parameter ("" in this context)
149 %L The language string associated with the specified display
150 (We use the "LANG" environment variable here, if it's set.)
151 %l The language part of the display's language string
152 (We treat this just like %L. If someone can tell us what
153 we're really supposed to do, dandy.)
154 %t The territory part of the display's language string
155 (This never gets used.)
156 %c The codeset part of the display's language string
157 (This never gets used either.)
158 %C The customization string retrieved from the resource
159 database associated with display.
160 (This is x_customization_string.)
162 Return the expanded file name if it exists and is readable, and
163 refers to %L only when the LANG environment variable is set, or
164 otherwise provided by X.
166 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
167 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
168 alone.
170 Return NULL otherwise. */
172 static char *
173 magic_file_p (string, string_len, class, escaped_suffix, suffix)
174 char *string;
175 int string_len;
176 char *class, *escaped_suffix, *suffix;
178 char *lang = getenv ("LANG");
180 int path_size = 100;
181 char *path = (char *) malloc (path_size);
182 int path_len = 0;
184 char *p = string;
186 while (p < string + string_len)
188 /* The chunk we're about to stick on the end of result. */
189 char *next = NULL;
190 int next_len;
192 if (*p == '%')
194 p++;
196 if (p >= string + string_len)
197 next_len = 0;
198 else
199 switch (*p)
201 case '%':
202 next = "%";
203 next_len = 1;
204 break;
206 case 'C':
207 next = (x_customization_string
208 ? x_customization_string
209 : "");
210 next_len = strlen (next);
211 break;
213 case 'N':
214 next = class;
215 next_len = strlen (class);
216 break;
218 case 'T':
219 next = "app-defaults";
220 next_len = strlen (next);
221 break;
223 default:
224 case 'S':
225 next_len = 0;
226 break;
228 case 'L':
229 case 'l':
230 if (! lang)
232 free (path);
233 return NULL;
236 next = lang;
237 next_len = strlen (next);
238 break;
240 case 't':
241 case 'c':
242 free (path);
243 return NULL;
246 else
247 next = p, next_len = 1;
249 /* Do we have room for this component followed by a '\0' ? */
250 if (path_len + next_len + 1 > path_size)
252 path_size = (path_len + next_len + 1) * 2;
253 path = (char *) realloc (path, path_size);
256 bcopy (next, path + path_len, next_len);
257 path_len += next_len;
259 p++;
261 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
262 if (p >= string + string_len && escaped_suffix)
264 string = escaped_suffix;
265 string_len = strlen (string);
266 p = string;
267 escaped_suffix = NULL;
271 /* Perhaps we should add the SUFFIX now. */
272 if (suffix)
274 int suffix_len = strlen (suffix);
276 if (path_len + suffix_len + 1 > path_size)
278 path_size = (path_len + suffix_len + 1);
279 path = (char *) realloc (path, path_size);
282 bcopy (suffix, path + path_len, suffix_len);
283 path_len += suffix_len;
286 path[path_len] = '\0';
288 if (! file_p (path))
290 free (path);
291 return NULL;
294 return path;
298 static char *
299 gethomedir ()
301 struct passwd *pw;
302 char *ptr;
303 char *copy;
305 if ((ptr = getenv ("HOME")) == NULL)
307 if ((ptr = getenv ("LOGNAME")) != NULL
308 || (ptr = getenv ("USER")) != NULL)
309 pw = getpwnam (ptr);
310 else
311 pw = getpwuid (getuid ());
313 if (pw)
314 ptr = pw->pw_dir;
317 if (ptr == NULL)
318 return xstrdup ("/");
320 copy = (char *) malloc (strlen (ptr) + 2);
321 strcpy (copy, ptr);
322 strcat (copy, "/");
324 return copy;
328 static int
329 file_p (filename)
330 char *filename;
332 struct stat status;
334 return (access (filename, 4) == 0 /* exists and is readable */
335 && stat (filename, &status) == 0 /* get the status */
336 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
340 /* Find the first element of SEARCH_PATH which exists and is readable,
341 after expanding the %-escapes. Return 0 if we didn't find any, and
342 the path name of the one we found otherwise. */
344 static char *
345 search_magic_path (search_path, class, escaped_suffix, suffix)
346 char *search_path, *class, *escaped_suffix, *suffix;
348 register char *s, *p;
350 for (s = search_path; *s; s = p)
352 for (p = s; *p && *p != ':'; p++)
355 if (p > s)
357 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
358 if (path)
359 return path;
361 else if (*p == ':')
363 char *path;
365 s = "%N%S";
366 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
367 if (path)
368 return path;
371 if (*p == ':')
372 p++;
375 return 0;
378 /* Producing databases for individual sources. */
380 static XrmDatabase
381 get_system_app (class)
382 char *class;
384 XrmDatabase db = NULL;
385 char *path;
387 path = getenv ("XFILESEARCHPATH");
388 if (! path) path = PATH_X_DEFAULTS;
390 path = search_magic_path (path, class, 0, 0);
391 if (path)
393 db = XrmGetFileDatabase (path);
394 free (path);
397 return db;
401 static XrmDatabase
402 get_fallback (display)
403 Display *display;
405 return NULL;
409 static XrmDatabase
410 get_user_app (class)
411 char *class;
413 char *path;
414 char *file = 0;
415 char *free_it = 0;
417 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
418 names, not directories. */
419 if (((path = getenv ("XUSERFILESEARCHPATH"))
420 && (file = search_magic_path (path, class, 0, 0)))
422 /* Check for APPLRESDIR; it is a path of directories. In each,
423 we have to search for LANG/CLASS and then CLASS. */
424 || ((path = getenv ("XAPPLRESDIR"))
425 && ((file = search_magic_path (path, class, "/%L/%N", 0))
426 || (file = search_magic_path (path, class, "/%N", 0))))
428 /* Check in the home directory. This is a bit of a hack; let's
429 hope one's home directory doesn't contain any %-escapes. */
430 || (free_it = gethomedir (),
431 ((file = search_magic_path (free_it, class, "%L/%N", 0))
432 || (file = search_magic_path (free_it, class, "%N", 0)))))
434 XrmDatabase db = XrmGetFileDatabase (file);
435 free (file);
436 if (free_it)
437 free (free_it);
438 return db;
441 if (free_it)
442 free (free_it);
443 return NULL;
447 static XrmDatabase
448 get_user_db (display)
449 Display *display;
451 XrmDatabase db;
452 char *xdefs;
454 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
455 xdefs = XResourceManagerString (display);
456 #else
457 xdefs = display->xdefaults;
458 #endif
460 if (xdefs != NULL)
461 db = XrmGetStringDatabase (xdefs);
462 else
464 char *home;
465 char *xdefault;
467 home = gethomedir ();
468 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
469 strcpy (xdefault, home);
470 strcat (xdefault, ".Xdefaults");
471 db = XrmGetFileDatabase (xdefault);
472 free (home);
473 free (xdefault);
476 #ifdef HAVE_XSCREENRESOURCESTRING
477 /* Get the screen-specific resources too. */
478 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
479 if (xdefs != NULL)
481 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
482 XFree (xdefs);
484 #endif
486 return db;
489 static XrmDatabase
490 get_environ_db ()
492 XrmDatabase db;
493 char *p;
494 char *path = 0, *home = 0, *host;
496 if ((p = getenv ("XENVIRONMENT")) == NULL)
498 home = gethomedir ();
499 host = get_system_name ();
500 path = (char *) malloc (strlen (home)
501 + sizeof (".Xdefaults-")
502 + strlen (host));
503 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
504 p = path;
507 db = XrmGetFileDatabase (p);
509 if (path) free (path);
510 if (home) free (home);
512 return db;
515 /* External interface. */
517 /* Types of values that we can find in a database */
519 #define XrmStringType "String" /* String representation */
520 XrmRepresentation x_rm_string; /* Quark representation */
522 /* Load X resources based on the display and a possible -xrm option. */
524 XrmDatabase
525 x_load_resources (display, xrm_string, myname, myclass)
526 Display *display;
527 char *xrm_string, *myname, *myclass;
529 XrmDatabase user_database;
530 XrmDatabase rdb;
531 XrmDatabase db;
532 char line[256];
534 char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
536 #ifdef USE_MOTIF
537 char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
538 extern Lisp_Object Vdouble_click_time;
539 #endif
541 x_rm_string = XrmStringToQuark (XrmStringType);
542 #ifndef USE_X_TOOLKIT
543 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
544 I suspect it's because the toolkit version does this elsewhere. */
545 XrmInitialize ();
546 #endif
547 rdb = XrmGetStringDatabase ("");
549 /* Add some font defaults. If the font `helv' doesn't exist, widgets
550 will use some other default font. */
551 #ifdef USE_MOTIF
553 sprintf (line, "%s.pane.background: grey75", myclass);
554 XrmPutLineResource (&rdb, line);
555 sprintf (line, "%s*fontList: %s", myclass, helv);
556 XrmPutLineResource (&rdb, line);
557 sprintf (line, "%s*menu*background: grey75", myclass);
558 XrmPutLineResource (&rdb, line);
559 sprintf (line, "%s*menubar*background: grey75", myclass);
560 XrmPutLineResource (&rdb, line);
561 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
562 XrmPutLineResource (&rdb, line);
563 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
564 XrmPutLineResource (&rdb, line);
565 sprintf (line, "%s.dialog*.background: grey75", myclass);
566 XrmPutLineResource (&rdb, line);
567 sprintf (line, "%s*fsb.Text.background: white", myclass);
568 XrmPutLineResource (&rdb, line);
569 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
570 XrmPutLineResource (&rdb, line);
571 sprintf (line, "%s*fsb*DirList.background: white", myclass);
572 XrmPutLineResource (&rdb, line);
573 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
574 XrmPutLineResource (&rdb, line);
575 sprintf (line, "%s*fsb*background: grey75", myclass);
576 XrmPutLineResource (&rdb, line);
577 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
578 XrmPutLineResource (&rdb, line);
579 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
580 XrmPutLineResource (&rdb, line);
581 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
582 XrmPutLineResource (&rdb, line);
583 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
584 XrmPutLineResource (&rdb, line);
586 /* Set double click time of list boxes in the file selection
587 dialog from `double-click-time'. */
588 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
590 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %d",
591 myclass, XFASTINT (Vdouble_click_time));
592 XrmPutLineResource (&rdb, line);
593 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %d",
594 myclass, XFASTINT (Vdouble_click_time));
595 XrmPutLineResource (&rdb, line);
598 #else /* not USE_MOTIF */
600 sprintf (line, "Emacs.dialog*.font: %s", helv);
601 XrmPutLineResource (&rdb, line);
602 sprintf (line, "Emacs.dialog*.background: grey75");
603 XrmPutLineResource (&rdb, line);
604 sprintf (line, "*XlwMenu*font: %s", helv);
605 XrmPutLineResource (&rdb, line);
606 sprintf (line, "*XlwMenu*background: grey75");
607 XrmPutLineResource (&rdb, line);
608 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
609 XrmPutLineResource (&rdb, line);
611 #endif /* not USE_MOTIF */
613 #ifdef HAVE_X_I18N
615 #ifdef USE_MOTIF
616 Bool motif = True;
617 #else /* not USE_MOTIF */
618 Bool motif = False;
619 #endif /* not USE_MOTIF */
620 /* Setup the default fontSet resource. */
621 extern char *xic_create_fontsetname P_ ((char *base_fontname, Bool motif));
622 char *fontsetname = xic_create_fontsetname (helv, motif);
623 int len = strlen (fontsetname);
624 char *buf = line;
626 /* fontsetname may be very long. */
627 if (len + 16 > 256)
628 buf = alloca (len + 16);
629 sprintf (buf, "Emacs*fontSet: %s", fontsetname);
630 XrmPutLineResource (&rdb, buf);
631 if (fontsetname != helv)
632 xfree (fontsetname);
634 #endif /* HAVE_X_I18N */
636 user_database = get_user_db (display);
638 /* Figure out what the "customization string" is, so we can use it
639 to decode paths. */
640 if (x_customization_string)
641 free (x_customization_string);
642 x_customization_string
643 = x_get_customization_string (user_database, myname, myclass);
645 /* Get application system defaults */
646 db = get_system_app (myclass);
647 if (db != NULL)
648 XrmMergeDatabases (db, &rdb);
650 /* Get Fallback resources */
651 db = get_fallback (display);
652 if (db != NULL)
653 XrmMergeDatabases (db, &rdb);
655 /* Get application user defaults */
656 db = get_user_app (myclass);
657 if (db != NULL)
658 XrmMergeDatabases (db, &rdb);
660 /* get User defaults */
661 if (user_database != NULL)
662 XrmMergeDatabases (user_database, &rdb);
664 /* Get Environment defaults. */
665 db = get_environ_db ();
666 if (db != NULL)
667 XrmMergeDatabases (db, &rdb);
669 /* Last, merge in any specification from the command line. */
670 if (xrm_string != NULL)
672 db = XrmGetStringDatabase (xrm_string);
673 if (db != NULL)
674 XrmMergeDatabases (db, &rdb);
677 return rdb;
681 /* Retrieve the value of the resource specified by NAME with class CLASS
682 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
685 x_get_resource (rdb, name, class, expected_type, ret_value)
686 XrmDatabase rdb;
687 char *name, *class;
688 XrmRepresentation expected_type;
689 XrmValue *ret_value;
691 XrmValue value;
692 XrmName namelist[100];
693 XrmClass classlist[100];
694 XrmRepresentation type;
696 XrmStringToNameList(name, namelist);
697 XrmStringToClassList(class, classlist);
699 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
700 && (type == expected_type))
702 if (type == x_rm_string)
703 ret_value->addr = (char *) value.addr;
704 else
705 bcopy (value.addr, ret_value->addr, ret_value->size);
707 return value.size;
710 return 0;
713 /* Retrieve the string resource specified by NAME with CLASS from
714 database RDB. */
716 char *
717 x_get_string_resource (rdb, name, class)
718 XrmDatabase rdb;
719 char *name, *class;
721 XrmValue value;
723 if (x_get_resource (rdb, name, class, x_rm_string, &value))
724 return (char *) value.addr;
726 return (char *) 0;
729 /* Stand-alone test facilities. */
731 #ifdef TESTRM
733 typedef char **List;
734 #define arg_listify(len, list) (list)
735 #define car(list) (*(list))
736 #define cdr(list) (list + 1)
737 #define NIL(list) (! *(list))
738 #define free_arglist(list)
740 static List
741 member (elt, list)
742 char *elt;
743 List list;
745 List p;
747 for (p = list; ! NIL (p); p = cdr (p))
748 if (! strcmp (elt, car (p)))
749 return p;
751 return p;
754 static void
755 fatal (msg, prog, x1, x2, x3, x4, x5)
756 char *msg, *prog;
757 int x1, x2, x3, x4, x5;
759 extern int errno;
761 if (errno)
762 perror (prog);
764 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
765 exit (1);
768 main (argc, argv)
769 int argc;
770 char **argv;
772 Display *display;
773 char *displayname, *resource_string, *class, *name;
774 XrmDatabase xdb;
775 List arg_list, lp;
777 arg_list = arg_listify (argc, argv);
779 lp = member ("-d", arg_list);
780 if (!NIL (lp))
781 displayname = car (cdr (lp));
782 else
783 displayname = "localhost:0.0";
785 lp = member ("-xrm", arg_list);
786 if (! NIL (lp))
787 resource_string = car (cdr (lp));
788 else
789 resource_string = (char *) 0;
791 lp = member ("-c", arg_list);
792 if (! NIL (lp))
793 class = car (cdr (lp));
794 else
795 class = "Emacs";
797 lp = member ("-n", arg_list);
798 if (! NIL (lp))
799 name = car (cdr (lp));
800 else
801 name = "emacs";
803 free_arglist (arg_list);
805 if (!(display = XOpenDisplay (displayname)))
806 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
808 xdb = x_load_resources (display, resource_string, name, class);
810 /* In a real program, you'd want to also do this: */
811 display->db = xdb;
813 while (1)
815 char query_name[90];
816 char query_class[90];
818 printf ("Name: ");
819 gets (query_name);
821 if (strlen (query_name))
823 char *value;
825 printf ("Class: ");
826 gets (query_class);
828 value = x_get_string_resource (xdb, query_name, query_class);
830 if (value != NULL)
831 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
832 else
833 printf ("\tNo Value.\n\n");
835 else
836 break;
838 printf ("\tExit.\n\n");
840 XCloseDisplay (display);
842 #endif /* TESTRM */
844 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
845 (do not change this comment) */