* files.el (directory-files-no-dot-files-regexp): Doc fix (bug#6298).
[emacs.git] / src / xrdb.c
blobbd3474c0466f0cb894473c16bded35d2d11cdf35
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Author: Joseph Arceneaux
6 Created: 4/90
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #ifdef emacs
24 #include <config.h>
25 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 #include <errno.h>
32 #include <epaths.h>
34 #include <stdio.h>
35 #include <setjmp.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/X.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
42 #ifdef HAVE_PWD_H
43 #include <pwd.h>
44 #endif
45 #include <sys/stat.h>
47 #if !defined(S_ISDIR) && defined(S_IFDIR)
48 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 #endif
51 #include "lisp.h"
53 extern char *getenv ();
55 /* This does cause trouble on AIX. I'm going to take the comment at
56 face value. */
57 #if 0
58 extern short getuid (); /* If this causes portability problems,
59 I think we should just delete it; it'll
60 default to `int' anyway. */
61 #endif
63 #ifdef DECLARE_GETPWUID_WITH_UID_T
64 extern struct passwd *getpwuid (uid_t);
65 extern struct passwd *getpwnam (const char *);
66 #else
67 extern struct passwd *getpwuid ();
68 extern struct passwd *getpwnam ();
69 #endif
71 extern char *get_system_name ();
73 /* Make sure not to #include anything after these definitions. Let's
74 not step on anyone's prototypes. */
75 #ifdef emacs
76 /* darwin.h may have already defined these. */
77 #undef malloc
78 #undef realloc
79 #undef free
80 #define malloc xmalloc
81 #define realloc xrealloc
82 #define free xfree
83 #endif
85 char *x_get_string_resource ();
86 static int file_p ();
89 /* X file search path processing. */
92 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
93 and friends, or zero if none was specified. */
94 char *x_customization_string;
97 /* Return the value of the emacs.customization (Emacs.Customization)
98 resource, for later use in search path decoding. If we find no
99 such resource, return zero. */
100 char *
101 x_get_customization_string (db, name, class)
102 XrmDatabase db;
103 char *name, *class;
105 char *full_name
106 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
107 char *full_class
108 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
109 char *result;
111 sprintf (full_name, "%s.%s", name, "customization");
112 sprintf (full_class, "%s.%s", class, "Customization");
114 result = x_get_string_resource (db, full_name, full_class);
116 if (result)
118 char *copy = (char *) malloc (strlen (result) + 1);
119 strcpy (copy, result);
120 return copy;
122 else
123 return 0;
127 /* Expand all the Xt-style %-escapes in STRING, whose length is given
128 by STRING_LEN. Here are the escapes we're supposed to recognize:
130 %N The value of the application's class name
131 %T The value of the type parameter ("app-defaults" in this
132 context)
133 %S The value of the suffix parameter ("" in this context)
134 %L The language string associated with the specified display
135 (We use the "LANG" environment variable here, if it's set.)
136 %l The language part of the display's language string
137 (We treat this just like %L. If someone can tell us what
138 we're really supposed to do, dandy.)
139 %t The territory part of the display's language string
140 (This never gets used.)
141 %c The codeset part of the display's language string
142 (This never gets used either.)
143 %C The customization string retrieved from the resource
144 database associated with display.
145 (This is x_customization_string.)
147 Return the expanded file name if it exists and is readable, and
148 refers to %L only when the LANG environment variable is set, or
149 otherwise provided by X.
151 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
152 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
153 alone.
155 Return NULL otherwise. */
157 static char *
158 magic_file_p (string, string_len, class, escaped_suffix, suffix)
159 char *string;
160 int string_len;
161 char *class, *escaped_suffix, *suffix;
163 char *lang = getenv ("LANG");
165 int path_size = 100;
166 char *path = (char *) malloc (path_size);
167 int path_len = 0;
169 char *p = string;
171 while (p < string + string_len)
173 /* The chunk we're about to stick on the end of result. */
174 char *next = NULL;
175 int next_len;
177 if (*p == '%')
179 p++;
181 if (p >= string + string_len)
182 next_len = 0;
183 else
184 switch (*p)
186 case '%':
187 next = "%";
188 next_len = 1;
189 break;
191 case 'C':
192 next = (x_customization_string
193 ? x_customization_string
194 : "");
195 next_len = strlen (next);
196 break;
198 case 'N':
199 next = class;
200 next_len = strlen (class);
201 break;
203 case 'T':
204 next = "app-defaults";
205 next_len = strlen (next);
206 break;
208 default:
209 case 'S':
210 next_len = 0;
211 break;
213 case 'L':
214 case 'l':
215 if (! lang)
217 free (path);
218 return NULL;
221 next = lang;
222 next_len = strlen (next);
223 break;
225 case 't':
226 case 'c':
227 free (path);
228 return NULL;
231 else
232 next = p, next_len = 1;
234 /* Do we have room for this component followed by a '\0' ? */
235 if (path_len + next_len + 1 > path_size)
237 path_size = (path_len + next_len + 1) * 2;
238 path = (char *) realloc (path, path_size);
241 bcopy (next, path + path_len, next_len);
242 path_len += next_len;
244 p++;
246 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
247 if (p >= string + string_len && escaped_suffix)
249 string = escaped_suffix;
250 string_len = strlen (string);
251 p = string;
252 escaped_suffix = NULL;
256 /* Perhaps we should add the SUFFIX now. */
257 if (suffix)
259 int suffix_len = strlen (suffix);
261 if (path_len + suffix_len + 1 > path_size)
263 path_size = (path_len + suffix_len + 1);
264 path = (char *) realloc (path, path_size);
267 bcopy (suffix, path + path_len, suffix_len);
268 path_len += suffix_len;
271 path[path_len] = '\0';
273 if (! file_p (path))
275 free (path);
276 return NULL;
279 return path;
283 static char *
284 gethomedir ()
286 struct passwd *pw;
287 char *ptr;
288 char *copy;
290 if ((ptr = getenv ("HOME")) == NULL)
292 if ((ptr = getenv ("LOGNAME")) != NULL
293 || (ptr = getenv ("USER")) != NULL)
294 pw = getpwnam (ptr);
295 else
296 pw = getpwuid (getuid ());
298 if (pw)
299 ptr = pw->pw_dir;
302 if (ptr == NULL)
303 return xstrdup ("/");
305 copy = (char *) malloc (strlen (ptr) + 2);
306 strcpy (copy, ptr);
307 strcat (copy, "/");
309 return copy;
313 static int
314 file_p (filename)
315 char *filename;
317 struct stat status;
319 return (access (filename, 4) == 0 /* exists and is readable */
320 && stat (filename, &status) == 0 /* get the status */
321 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
325 /* Find the first element of SEARCH_PATH which exists and is readable,
326 after expanding the %-escapes. Return 0 if we didn't find any, and
327 the path name of the one we found otherwise. */
329 static char *
330 search_magic_path (search_path, class, escaped_suffix, suffix)
331 char *search_path, *class, *escaped_suffix, *suffix;
333 register char *s, *p;
335 for (s = search_path; *s; s = p)
337 for (p = s; *p && *p != ':'; p++)
340 if (p > s)
342 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
343 if (path)
344 return path;
346 else if (*p == ':')
348 char *path;
350 s = "%N%S";
351 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
352 if (path)
353 return path;
356 if (*p == ':')
357 p++;
360 return 0;
363 /* Producing databases for individual sources. */
365 static XrmDatabase
366 get_system_app (class)
367 char *class;
369 XrmDatabase db = NULL;
370 char *path;
372 path = getenv ("XFILESEARCHPATH");
373 if (! path) path = PATH_X_DEFAULTS;
375 path = search_magic_path (path, class, 0, 0);
376 if (path)
378 db = XrmGetFileDatabase (path);
379 free (path);
382 return db;
386 static XrmDatabase
387 get_fallback (display)
388 Display *display;
390 return NULL;
394 static XrmDatabase
395 get_user_app (class)
396 char *class;
398 char *path;
399 char *file = 0;
400 char *free_it = 0;
402 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
403 names, not directories. */
404 if (((path = getenv ("XUSERFILESEARCHPATH"))
405 && (file = search_magic_path (path, class, 0, 0)))
407 /* Check for APPLRESDIR; it is a path of directories. In each,
408 we have to search for LANG/CLASS and then CLASS. */
409 || ((path = getenv ("XAPPLRESDIR"))
410 && ((file = search_magic_path (path, class, "/%L/%N", 0))
411 || (file = search_magic_path (path, class, "/%N", 0))))
413 /* Check in the home directory. This is a bit of a hack; let's
414 hope one's home directory doesn't contain any %-escapes. */
415 || (free_it = gethomedir (),
416 ((file = search_magic_path (free_it, class, "%L/%N", 0))
417 || (file = search_magic_path (free_it, class, "%N", 0)))))
419 XrmDatabase db = XrmGetFileDatabase (file);
420 free (file);
421 free (free_it);
422 return db;
425 free (free_it);
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 free (path);
493 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 XrmDatabase user_database;
513 XrmDatabase rdb;
514 XrmDatabase db;
515 char line[256];
517 char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
519 #ifdef USE_MOTIF
520 char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
521 extern Lisp_Object Vdouble_click_time;
522 #endif
524 x_rm_string = XrmStringToQuark (XrmStringType);
525 #ifndef USE_X_TOOLKIT
526 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
527 I suspect it's because the toolkit version does this elsewhere. */
528 XrmInitialize ();
529 #endif
530 rdb = XrmGetStringDatabase ("");
532 /* Add some font defaults. If the font `helv' doesn't exist, widgets
533 will use some other default font. */
534 #ifdef USE_MOTIF
536 sprintf (line, "%s.pane.background: grey75", myclass);
537 XrmPutLineResource (&rdb, line);
538 sprintf (line, "%s*fontList: %s", myclass, helv);
539 XrmPutLineResource (&rdb, line);
540 sprintf (line, "%s*menu*background: grey75", myclass);
541 XrmPutLineResource (&rdb, line);
542 sprintf (line, "%s*menubar*background: grey75", myclass);
543 XrmPutLineResource (&rdb, line);
544 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
545 XrmPutLineResource (&rdb, line);
546 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
547 XrmPutLineResource (&rdb, line);
548 sprintf (line, "%s.dialog*.background: grey75", myclass);
549 XrmPutLineResource (&rdb, line);
550 sprintf (line, "%s*fsb.Text.background: white", myclass);
551 XrmPutLineResource (&rdb, line);
552 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
553 XrmPutLineResource (&rdb, line);
554 sprintf (line, "%s*fsb*DirList.background: white", myclass);
555 XrmPutLineResource (&rdb, line);
556 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
557 XrmPutLineResource (&rdb, line);
558 sprintf (line, "%s*fsb*background: grey75", myclass);
559 XrmPutLineResource (&rdb, line);
560 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
561 XrmPutLineResource (&rdb, line);
562 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
563 XrmPutLineResource (&rdb, line);
564 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
565 XrmPutLineResource (&rdb, line);
566 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
567 XrmPutLineResource (&rdb, line);
569 /* Set double click time of list boxes in the file selection
570 dialog from `double-click-time'. */
571 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
573 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %d",
574 myclass, XFASTINT (Vdouble_click_time));
575 XrmPutLineResource (&rdb, line);
576 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %d",
577 myclass, XFASTINT (Vdouble_click_time));
578 XrmPutLineResource (&rdb, line);
581 #else /* not USE_MOTIF */
583 sprintf (line, "Emacs.dialog*.font: %s", helv);
584 XrmPutLineResource (&rdb, line);
585 sprintf (line, "Emacs.dialog*.background: grey75");
586 XrmPutLineResource (&rdb, line);
587 sprintf (line, "*XlwMenu*font: %s", helv);
588 XrmPutLineResource (&rdb, line);
589 sprintf (line, "*XlwMenu*background: grey75");
590 XrmPutLineResource (&rdb, line);
591 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
592 XrmPutLineResource (&rdb, line);
594 #endif /* not USE_MOTIF */
596 user_database = get_user_db (display);
598 /* Figure out what the "customization string" is, so we can use it
599 to decode paths. */
600 free (x_customization_string);
601 x_customization_string
602 = x_get_customization_string (user_database, myname, myclass);
604 /* Get application system defaults */
605 db = get_system_app (myclass);
606 if (db != NULL)
607 XrmMergeDatabases (db, &rdb);
609 /* Get Fallback resources */
610 db = get_fallback (display);
611 if (db != NULL)
612 XrmMergeDatabases (db, &rdb);
614 /* Get application user defaults */
615 db = get_user_app (myclass);
616 if (db != NULL)
617 XrmMergeDatabases (db, &rdb);
619 /* get User defaults */
620 if (user_database != NULL)
621 XrmMergeDatabases (user_database, &rdb);
623 /* Get Environment defaults. */
624 db = get_environ_db ();
625 if (db != NULL)
626 XrmMergeDatabases (db, &rdb);
628 /* Last, merge in any specification from the command line. */
629 if (xrm_string != NULL)
631 db = XrmGetStringDatabase (xrm_string);
632 if (db != NULL)
633 XrmMergeDatabases (db, &rdb);
636 return rdb;
640 /* Retrieve the value of the resource specified by NAME with class CLASS
641 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
644 x_get_resource (rdb, name, class, expected_type, ret_value)
645 XrmDatabase rdb;
646 char *name, *class;
647 XrmRepresentation expected_type;
648 XrmValue *ret_value;
650 XrmValue value;
651 XrmName namelist[100];
652 XrmClass classlist[100];
653 XrmRepresentation type;
655 XrmStringToNameList(name, namelist);
656 XrmStringToClassList(class, classlist);
658 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
659 && (type == expected_type))
661 if (type == x_rm_string)
662 ret_value->addr = (char *) value.addr;
663 else
664 bcopy (value.addr, ret_value->addr, ret_value->size);
666 return value.size;
669 return 0;
672 /* Retrieve the string resource specified by NAME with CLASS from
673 database RDB. */
675 char *
676 x_get_string_resource (rdb, name, class)
677 XrmDatabase rdb;
678 char *name, *class;
680 XrmValue value;
682 if (inhibit_x_resources)
683 /* --quick was passed, so this is a no-op. */
684 return NULL;
686 if (x_get_resource (rdb, name, class, x_rm_string, &value))
687 return (char *) value.addr;
689 return (char *) 0;
692 /* Stand-alone test facilities. */
694 #ifdef TESTRM
696 typedef char **List;
697 #define arg_listify(len, list) (list)
698 #define car(list) (*(list))
699 #define cdr(list) (list + 1)
700 #define NIL(list) (! *(list))
701 #define free_arglist(list)
703 static List
704 member (elt, list)
705 char *elt;
706 List list;
708 List p;
710 for (p = list; ! NIL (p); p = cdr (p))
711 if (! strcmp (elt, car (p)))
712 return p;
714 return p;
717 static void
718 fatal (msg, prog, x1, x2, x3, x4, x5)
719 char *msg, *prog;
720 int x1, x2, x3, x4, x5;
722 if (errno)
723 perror (prog);
725 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
726 exit (1);
729 main (argc, argv)
730 int argc;
731 char **argv;
733 Display *display;
734 char *displayname, *resource_string, *class, *name;
735 XrmDatabase xdb;
736 List arg_list, lp;
738 arg_list = arg_listify (argc, argv);
740 lp = member ("-d", arg_list);
741 if (!NIL (lp))
742 displayname = car (cdr (lp));
743 else
744 displayname = "localhost:0.0";
746 lp = member ("-xrm", arg_list);
747 if (! NIL (lp))
748 resource_string = car (cdr (lp));
749 else
750 resource_string = (char *) 0;
752 lp = member ("-c", arg_list);
753 if (! NIL (lp))
754 class = car (cdr (lp));
755 else
756 class = "Emacs";
758 lp = member ("-n", arg_list);
759 if (! NIL (lp))
760 name = car (cdr (lp));
761 else
762 name = "emacs";
764 free_arglist (arg_list);
766 if (!(display = XOpenDisplay (displayname)))
767 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
769 xdb = x_load_resources (display, resource_string, name, class);
771 /* In a real program, you'd want to also do this: */
772 display->db = xdb;
774 while (1)
776 char query_name[90];
777 char query_class[90];
779 printf ("Name: ");
780 gets (query_name);
782 if (strlen (query_name))
784 char *value;
786 printf ("Class: ");
787 gets (query_class);
789 value = x_get_string_resource (xdb, query_name, query_class);
791 if (value != NULL)
792 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
793 else
794 printf ("\tNo Value.\n\n");
796 else
797 break;
799 printf ("\tExit.\n\n");
801 XCloseDisplay (display);
803 #endif /* TESTRM */
805 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
806 (do not change this comment) */