Alter last change to be compatible with Emacs 23.
[emacs.git] / src / xrdb.c
blob73672c9617c78aad31ddb57210ca7052bb11d6a9
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2012 Free Software Foundation, Inc.
4 Author: Joseph Arceneaux
5 Created: 4/90
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <epaths.h>
28 #include <stdio.h>
30 #include "lisp.h"
32 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
34 #include "xterm.h"
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/X.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
41 #ifdef HAVE_PWD_H
42 #include <pwd.h>
43 #endif
44 #include <sys/stat.h>
46 #ifdef USE_MOTIF
47 /* For Vdouble_click_time. */
48 #include "keyboard.h"
49 #endif
51 extern char *getenv (const char *);
53 extern struct passwd *getpwuid (uid_t);
54 extern struct passwd *getpwnam (const char *);
56 char *x_get_string_resource (XrmDatabase rdb, const char *name,
57 const char *class);
58 static int file_p (const char *filename);
61 /* X file search path processing. */
64 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
65 and friends, or zero if none was specified. */
66 static char *x_customization_string;
69 /* Return the value of the emacs.customization (Emacs.Customization)
70 resource, for later use in search path decoding. If we find no
71 such resource, return zero. */
72 static char *
73 x_get_customization_string (XrmDatabase db, const char *name,
74 const char *class)
76 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
77 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
78 char *result;
80 sprintf (full_name, "%s.%s", name, "customization");
81 sprintf (full_class, "%s.%s", class, "Customization");
83 result = x_get_string_resource (db, full_name, full_class);
85 if (result)
87 char *copy = xmalloc (strlen (result) + 1);
88 strcpy (copy, result);
89 return copy;
91 else
92 return 0;
96 /* Expand all the Xt-style %-escapes in STRING, whose length is given
97 by STRING_LEN. Here are the escapes we're supposed to recognize:
99 %N The value of the application's class name
100 %T The value of the type parameter ("app-defaults" in this
101 context)
102 %S The value of the suffix parameter ("" in this context)
103 %L The language string associated with the specified display
104 (We use the "LANG" environment variable here, if it's set.)
105 %l The language part of the display's language string
106 (We treat this just like %L. If someone can tell us what
107 we're really supposed to do, dandy.)
108 %t The territory part of the display's language string
109 (This never gets used.)
110 %c The codeset part of the display's language string
111 (This never gets used either.)
112 %C The customization string retrieved from the resource
113 database associated with display.
114 (This is x_customization_string.)
116 Return the expanded file name if it exists and is readable, and
117 refers to %L only when the LANG environment variable is set, or
118 otherwise provided by X.
120 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
121 %-escapes in ESCAPED_SUFFIX are expanded.
123 Return NULL otherwise. */
125 static char *
126 magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
127 const char *escaped_suffix)
129 char *lang = getenv ("LANG");
131 ptrdiff_t path_size = 100;
132 char *path = xmalloc (path_size);
133 ptrdiff_t path_len = 0;
135 const char *p = string;
137 while (p < string + string_len)
139 /* The chunk we're about to stick on the end of result. */
140 const char *next = NULL;
141 ptrdiff_t next_len;
143 if (*p == '%')
145 p++;
147 if (p >= string + string_len)
148 next_len = 0;
149 else
150 switch (*p)
152 case '%':
153 next = "%";
154 next_len = 1;
155 break;
157 case 'C':
158 next = (x_customization_string
159 ? x_customization_string
160 : "");
161 next_len = strlen (next);
162 break;
164 case 'N':
165 next = class;
166 next_len = strlen (class);
167 break;
169 case 'T':
170 next = "app-defaults";
171 next_len = strlen (next);
172 break;
174 default:
175 case 'S':
176 next_len = 0;
177 break;
179 case 'L':
180 case 'l':
181 if (! lang)
183 xfree (path);
184 return NULL;
187 next = lang;
188 next_len = strlen (next);
189 break;
191 case 't':
192 case 'c':
193 xfree (path);
194 return NULL;
197 else
198 next = p, next_len = 1;
200 /* Do we have room for this component followed by a '\0' ? */
201 if (path_size - path_len <= next_len)
203 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
204 memory_full (SIZE_MAX);
205 path_size = (path_len + next_len + 1) * 2;
206 path = xrealloc (path, path_size);
209 memcpy (path + path_len, next, next_len);
210 path_len += next_len;
212 p++;
214 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
215 if (p >= string + string_len && escaped_suffix)
217 string = escaped_suffix;
218 string_len = strlen (string);
219 p = string;
220 escaped_suffix = NULL;
224 path[path_len] = '\0';
226 if (! file_p (path))
228 xfree (path);
229 return NULL;
232 return path;
236 static char *
237 gethomedir (void)
239 struct passwd *pw;
240 char *ptr;
241 char *copy;
243 if ((ptr = getenv ("HOME")) == NULL)
245 if ((ptr = getenv ("LOGNAME")) != NULL
246 || (ptr = getenv ("USER")) != NULL)
247 pw = getpwnam (ptr);
248 else
249 pw = getpwuid (getuid ());
251 if (pw)
252 ptr = pw->pw_dir;
255 if (ptr == NULL)
256 return xstrdup ("/");
258 copy = xmalloc (strlen (ptr) + 2);
259 strcpy (copy, ptr);
260 strcat (copy, "/");
262 return copy;
266 static int
267 file_p (const char *filename)
269 struct stat status;
271 return (access (filename, 4) == 0 /* exists and is readable */
272 && stat (filename, &status) == 0 /* get the status */
273 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
277 /* Find the first element of SEARCH_PATH which exists and is readable,
278 after expanding the %-escapes. Return 0 if we didn't find any, and
279 the path name of the one we found otherwise. */
281 static char *
282 search_magic_path (const char *search_path, const char *class,
283 const char *escaped_suffix)
285 const char *s, *p;
287 for (s = search_path; *s; s = p)
289 for (p = s; *p && *p != ':'; p++)
292 if (p > s)
294 char *path = magic_file_p (s, p - s, class, escaped_suffix);
295 if (path)
296 return path;
298 else if (*p == ':')
300 char *path;
302 s = "%N%S";
303 path = magic_file_p (s, strlen (s), class, escaped_suffix);
304 if (path)
305 return path;
308 if (*p == ':')
309 p++;
312 return 0;
315 /* Producing databases for individual sources. */
317 static XrmDatabase
318 get_system_app (const char *class)
320 XrmDatabase db = NULL;
321 const char *path;
322 char *p;
324 path = getenv ("XFILESEARCHPATH");
325 if (! path) path = PATH_X_DEFAULTS;
327 p = search_magic_path (path, class, 0);
328 if (p)
330 db = XrmGetFileDatabase (p);
331 xfree (p);
334 return db;
338 static XrmDatabase
339 get_fallback (Display *display)
341 return NULL;
345 static XrmDatabase
346 get_user_app (const char *class)
348 const char *path;
349 char *file = 0;
350 char *free_it = 0;
352 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
353 names, not directories. */
354 if (((path = getenv ("XUSERFILESEARCHPATH"))
355 && (file = search_magic_path (path, class, 0)))
357 /* Check for APPLRESDIR; it is a path of directories. In each,
358 we have to search for LANG/CLASS and then CLASS. */
359 || ((path = getenv ("XAPPLRESDIR"))
360 && ((file = search_magic_path (path, class, "/%L/%N"))
361 || (file = search_magic_path (path, class, "/%N"))))
363 /* Check in the home directory. This is a bit of a hack; let's
364 hope one's home directory doesn't contain any %-escapes. */
365 || (free_it = gethomedir (),
366 ((file = search_magic_path (free_it, class, "%L/%N"))
367 || (file = search_magic_path (free_it, class, "%N")))))
369 XrmDatabase db = XrmGetFileDatabase (file);
370 xfree (file);
371 xfree (free_it);
372 return db;
375 xfree (free_it);
376 return NULL;
380 static XrmDatabase
381 get_user_db (Display *display)
383 XrmDatabase db;
384 char *xdefs;
386 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
387 xdefs = XResourceManagerString (display);
388 #else
389 xdefs = display->xdefaults;
390 #endif
392 if (xdefs != NULL)
393 db = XrmGetStringDatabase (xdefs);
394 else
396 char *home;
397 char *xdefault;
399 home = gethomedir ();
400 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
401 strcpy (xdefault, home);
402 strcat (xdefault, ".Xdefaults");
403 db = XrmGetFileDatabase (xdefault);
404 xfree (home);
405 xfree (xdefault);
408 #ifdef HAVE_XSCREENRESOURCESTRING
409 /* Get the screen-specific resources too. */
410 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
411 if (xdefs != NULL)
413 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
414 XFree (xdefs);
416 #endif
418 return db;
421 static XrmDatabase
422 get_environ_db (void)
424 XrmDatabase db;
425 char *p;
426 char *path = 0;
428 if ((p = getenv ("XENVIRONMENT")) == NULL)
430 static char const xdefaults[] = ".Xdefaults-";
431 char *home = gethomedir ();
432 char const *host = get_system_name ();
433 ptrdiff_t pathsize = strlen (home) + sizeof xdefaults + strlen (host);
434 path = xrealloc (home, pathsize);
435 strcat (strcat (path, xdefaults), host);
436 p = path;
439 db = XrmGetFileDatabase (p);
441 xfree (path);
443 return db;
446 /* External interface. */
448 /* Types of values that we can find in a database */
450 #define XrmStringType "String" /* String representation */
451 static XrmRepresentation x_rm_string; /* Quark representation */
453 /* Load X resources based on the display and a possible -xrm option. */
455 XrmDatabase
456 x_load_resources (Display *display, const char *xrm_string,
457 const char *myname, const char *myclass)
459 XrmDatabase user_database;
460 XrmDatabase rdb;
461 XrmDatabase db;
462 char line[256];
464 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
465 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
466 #endif
468 #ifdef USE_MOTIF
469 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
470 #endif
472 x_rm_string = XrmStringToQuark (XrmStringType);
473 #ifndef USE_X_TOOLKIT
474 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
475 I suspect it's because the toolkit version does this elsewhere. */
476 XrmInitialize ();
477 #endif
478 rdb = XrmGetStringDatabase ("");
480 /* Add some font defaults. If the font `helv' doesn't exist, widgets
481 will use some other default font. */
482 #ifdef USE_MOTIF
484 sprintf (line, "%s.pane.background: grey75", myclass);
485 XrmPutLineResource (&rdb, line);
486 sprintf (line, "%s*fontList: %s", myclass, helv);
487 XrmPutLineResource (&rdb, line);
488 sprintf (line, "%s*menu*background: grey75", myclass);
489 XrmPutLineResource (&rdb, line);
490 sprintf (line, "%s*menubar*background: grey75", myclass);
491 XrmPutLineResource (&rdb, line);
492 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
493 XrmPutLineResource (&rdb, line);
494 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
495 XrmPutLineResource (&rdb, line);
496 sprintf (line, "%s.dialog*.background: grey75", myclass);
497 XrmPutLineResource (&rdb, line);
498 sprintf (line, "%s*fsb.Text.background: white", myclass);
499 XrmPutLineResource (&rdb, line);
500 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
501 XrmPutLineResource (&rdb, line);
502 sprintf (line, "%s*fsb*DirList.background: white", myclass);
503 XrmPutLineResource (&rdb, line);
504 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
505 XrmPutLineResource (&rdb, line);
506 sprintf (line, "%s*fsb*background: grey75", myclass);
507 XrmPutLineResource (&rdb, line);
508 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
509 XrmPutLineResource (&rdb, line);
510 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
511 XrmPutLineResource (&rdb, line);
512 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
513 XrmPutLineResource (&rdb, line);
514 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
515 XrmPutLineResource (&rdb, line);
517 /* Set double click time of list boxes in the file selection
518 dialog from `double-click-time'. */
519 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
521 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
522 myclass, XFASTINT (Vdouble_click_time));
523 XrmPutLineResource (&rdb, line);
524 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
525 myclass, XFASTINT (Vdouble_click_time));
526 XrmPutLineResource (&rdb, line);
529 #else /* not USE_MOTIF */
531 sprintf (line, "Emacs.dialog*.background: grey75");
532 XrmPutLineResource (&rdb, line);
533 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
534 sprintf (line, "Emacs.dialog*.font: %s", helv);
535 XrmPutLineResource (&rdb, line);
536 sprintf (line, "*XlwMenu*font: %s", helv);
537 XrmPutLineResource (&rdb, line);
538 #endif
539 sprintf (line, "*XlwMenu*background: grey75");
540 XrmPutLineResource (&rdb, line);
541 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
542 XrmPutLineResource (&rdb, line);
544 #endif /* not USE_MOTIF */
546 user_database = get_user_db (display);
548 /* Figure out what the "customization string" is, so we can use it
549 to decode paths. */
550 xfree (x_customization_string);
551 x_customization_string
552 = x_get_customization_string (user_database, myname, myclass);
554 /* Get application system defaults */
555 db = get_system_app (myclass);
556 if (db != NULL)
557 XrmMergeDatabases (db, &rdb);
559 /* Get Fallback resources */
560 db = get_fallback (display);
561 if (db != NULL)
562 XrmMergeDatabases (db, &rdb);
564 /* Get application user defaults */
565 db = get_user_app (myclass);
566 if (db != NULL)
567 XrmMergeDatabases (db, &rdb);
569 /* get User defaults */
570 if (user_database != NULL)
571 XrmMergeDatabases (user_database, &rdb);
573 /* Get Environment defaults. */
574 db = get_environ_db ();
575 if (db != NULL)
576 XrmMergeDatabases (db, &rdb);
578 /* Last, merge in any specification from the command line. */
579 if (xrm_string != NULL)
581 db = XrmGetStringDatabase (xrm_string);
582 if (db != NULL)
583 XrmMergeDatabases (db, &rdb);
586 return rdb;
590 /* Retrieve the value of the resource specified by NAME with class CLASS
591 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
593 static int
594 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
595 XrmRepresentation expected_type, XrmValue *ret_value)
597 XrmValue value;
598 XrmName namelist[100];
599 XrmClass classlist[100];
600 XrmRepresentation type;
602 XrmStringToNameList (name, namelist);
603 XrmStringToClassList (class, classlist);
605 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
606 && (type == expected_type))
608 if (type == x_rm_string)
609 ret_value->addr = (char *) value.addr;
610 else
611 memcpy (ret_value->addr, value.addr, ret_value->size);
613 return value.size;
616 return 0;
619 /* Retrieve the string resource specified by NAME with CLASS from
620 database RDB. */
622 char *
623 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
625 XrmValue value;
627 if (inhibit_x_resources)
628 /* --quick was passed, so this is a no-op. */
629 return NULL;
631 if (x_get_resource (rdb, name, class, x_rm_string, &value))
632 return (char *) value.addr;
634 return (char *) 0;
637 /* Stand-alone test facilities. */
639 #ifdef TESTRM
641 typedef char **List;
642 #define arg_listify(len, list) (list)
643 #define car(list) (*(list))
644 #define cdr(list) (list + 1)
645 #define NIL(list) (! *(list))
646 #define free_arglist(list)
648 static List
649 member (char *elt, List list)
651 List p;
653 for (p = list; ! NIL (p); p = cdr (p))
654 if (! strcmp (elt, car (p)))
655 return p;
657 return p;
660 static void
661 fatal (char *msg, char *prog)
663 if (errno)
664 perror (prog);
666 (void) fprintf (stderr, msg, prog);
667 exit (1);
671 main (int argc, char **argv)
673 Display *display;
674 char *displayname, *resource_string, *class, *name;
675 XrmDatabase xdb;
676 List arg_list, lp;
678 arg_list = arg_listify (argc, argv);
680 lp = member ("-d", arg_list);
681 if (!NIL (lp))
682 displayname = car (cdr (lp));
683 else
684 displayname = "localhost:0.0";
686 lp = member ("-xrm", arg_list);
687 if (! NIL (lp))
688 resource_string = car (cdr (lp));
689 else
690 resource_string = (char *) 0;
692 lp = member ("-c", arg_list);
693 if (! NIL (lp))
694 class = car (cdr (lp));
695 else
696 class = "Emacs";
698 lp = member ("-n", arg_list);
699 if (! NIL (lp))
700 name = car (cdr (lp));
701 else
702 name = "emacs";
704 free_arglist (arg_list);
706 if (!(display = XOpenDisplay (displayname)))
707 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
709 xdb = x_load_resources (display, resource_string, name, class);
711 /* In a real program, you'd want to also do this: */
712 display->db = xdb;
714 while (1)
716 char query_name[90];
717 char query_class[90];
719 printf ("Name: ");
720 gets (query_name);
722 if (strlen (query_name))
724 char *value;
726 printf ("Class: ");
727 gets (query_class);
729 value = x_get_string_resource (xdb, query_name, query_class);
731 if (value != NULL)
732 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
733 else
734 printf ("\tNo Value.\n\n");
736 else
737 break;
739 printf ("\tExit.\n\n");
741 XCloseDisplay (display);
743 return 0;
745 #endif /* TESTRM */