Merge from emacs-24; up to 2012-12-23T02:41:17Z!rgm@gnu.org
[emacs.git] / src / xrdb.c
blobc25c25d6f33dde6f3a72cad82c3e87a4361d0ece
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2013 Free Software Foundation,
3 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 #include <config.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <epaths.h>
28 #include <stdlib.h>
29 #include <stdio.h>
31 #include "lisp.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.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
46 #ifdef USE_MOTIF
47 /* For Vdouble_click_time. */
48 #include "keyboard.h"
49 #endif
51 char *x_get_string_resource (XrmDatabase rdb, const char *name,
52 const char *class);
55 /* X file search path processing. */
58 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
59 and friends, or zero if none was specified. */
60 static char *x_customization_string;
63 /* Return the value of the emacs.customization (Emacs.Customization)
64 resource, for later use in search path decoding. If we find no
65 such resource, return zero. */
66 static char *
67 x_get_customization_string (XrmDatabase db, const char *name,
68 const char *class)
70 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
71 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
72 char *result;
74 sprintf (full_name, "%s.%s", name, "customization");
75 sprintf (full_class, "%s.%s", class, "Customization");
77 result = x_get_string_resource (db, full_name, full_class);
79 if (result)
81 char *copy = xmalloc (strlen (result) + 1);
82 strcpy (copy, result);
83 return copy;
85 else
86 return 0;
90 /* Expand all the Xt-style %-escapes in STRING, whose length is given
91 by STRING_LEN. Here are the escapes we're supposed to recognize:
93 %N The value of the application's class name
94 %T The value of the type parameter ("app-defaults" in this
95 context)
96 %S The value of the suffix parameter ("" in this context)
97 %L The language string associated with the specified display
98 (We use the "LANG" environment variable here, if it's set.)
99 %l The language part of the display's language string
100 (We treat this just like %L. If someone can tell us what
101 we're really supposed to do, dandy.)
102 %t The territory part of the display's language string
103 (This never gets used.)
104 %c The codeset part of the display's language string
105 (This never gets used either.)
106 %C The customization string retrieved from the resource
107 database associated with display.
108 (This is x_customization_string.)
110 Return the resource database if its file was read successfully, and
111 refers to %L only when the LANG environment variable is set, or
112 otherwise provided by X.
114 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
115 %-escapes in ESCAPED_SUFFIX are expanded.
117 Return NULL otherwise. */
119 static XrmDatabase
120 magic_db (const char *string, ptrdiff_t string_len, const char *class,
121 const char *escaped_suffix)
123 XrmDatabase db;
124 char *lang = getenv ("LANG");
126 ptrdiff_t path_size = 100;
127 char *path = xmalloc (path_size);
128 ptrdiff_t path_len = 0;
130 const char *p = string;
132 while (p < string + string_len)
134 /* The chunk we're about to stick on the end of result. */
135 const char *next = NULL;
136 ptrdiff_t next_len;
138 if (*p == '%')
140 p++;
142 if (p >= string + string_len)
143 next_len = 0;
144 else
145 switch (*p)
147 case '%':
148 next = "%";
149 next_len = 1;
150 break;
152 case 'C':
153 next = (x_customization_string
154 ? x_customization_string
155 : "");
156 next_len = strlen (next);
157 break;
159 case 'N':
160 next = class;
161 next_len = strlen (class);
162 break;
164 case 'T':
165 next = "app-defaults";
166 next_len = strlen (next);
167 break;
169 default:
170 case 'S':
171 next_len = 0;
172 break;
174 case 'L':
175 case 'l':
176 if (! lang)
178 xfree (path);
179 return NULL;
182 next = lang;
183 next_len = strlen (next);
184 break;
186 case 't':
187 case 'c':
188 xfree (path);
189 return NULL;
192 else
193 next = p, next_len = 1;
195 /* Do we have room for this component followed by a '\0' ? */
196 if (path_size - path_len <= next_len)
198 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
199 memory_full (SIZE_MAX);
200 path_size = (path_len + next_len + 1) * 2;
201 path = xrealloc (path, path_size);
204 memcpy (path + path_len, next, next_len);
205 path_len += next_len;
207 p++;
209 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
210 if (p >= string + string_len && escaped_suffix)
212 string = escaped_suffix;
213 string_len = strlen (string);
214 p = string;
215 escaped_suffix = NULL;
219 path[path_len] = '\0';
220 db = XrmGetFileDatabase (path);
221 xfree (path);
222 return db;
226 static char *
227 gethomedir (void)
229 struct passwd *pw;
230 char *ptr;
231 char *copy;
233 if ((ptr = getenv ("HOME")) == NULL)
235 if ((ptr = getenv ("LOGNAME")) != NULL
236 || (ptr = getenv ("USER")) != NULL)
237 pw = getpwnam (ptr);
238 else
239 pw = getpwuid (getuid ());
241 if (pw)
242 ptr = pw->pw_dir;
245 if (ptr == NULL)
246 return xstrdup ("/");
248 copy = xmalloc (strlen (ptr) + 2);
249 strcpy (copy, ptr);
250 strcat (copy, "/");
252 return copy;
256 /* Find the first element of SEARCH_PATH which exists and is readable,
257 after expanding the %-escapes. Return 0 if we didn't find any, and
258 the path name of the one we found otherwise. */
260 static XrmDatabase
261 search_magic_path (const char *search_path, const char *class,
262 const char *escaped_suffix)
264 const char *s, *p;
266 for (s = search_path; *s; s = p)
268 for (p = s; *p && *p != ':'; p++)
271 if (p > s)
273 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
274 if (db)
275 return db;
277 else if (*p == ':')
279 static char const ns[] = "%N%S";
280 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
281 if (db)
282 return db;
285 if (*p == ':')
286 p++;
289 return 0;
292 /* Producing databases for individual sources. */
294 static XrmDatabase
295 get_system_app (const char *class)
297 const char *path;
299 path = getenv ("XFILESEARCHPATH");
300 if (! path) path = PATH_X_DEFAULTS;
302 return search_magic_path (path, class, 0);
306 static XrmDatabase
307 get_fallback (Display *display)
309 return NULL;
313 static XrmDatabase
314 get_user_app (const char *class)
316 XrmDatabase db = 0;
317 const char *path;
319 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
320 names, not directories. */
321 path = getenv ("XUSERFILESEARCHPATH");
322 if (path)
323 db = search_magic_path (path, class, 0);
325 if (! db)
327 /* Check for APPLRESDIR; it is a path of directories. In each,
328 we have to search for LANG/CLASS and then CLASS. */
329 path = getenv ("XAPPLRESDIR");
330 if (path)
332 db = search_magic_path (path, class, "/%L/%N");
333 if (!db)
334 db = search_magic_path (path, class, "/%N");
338 if (! db)
340 /* Check in the home directory. This is a bit of a hack; let's
341 hope one's home directory doesn't contain any %-escapes. */
342 char *home = gethomedir ();
343 db = search_magic_path (home, class, "%L/%N");
344 if (! db)
345 db = search_magic_path (home, class, "%N");
346 xfree (home);
349 return db;
353 static XrmDatabase
354 get_user_db (Display *display)
356 XrmDatabase db;
357 char *xdefs;
359 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
360 xdefs = XResourceManagerString (display);
361 #else
362 xdefs = display->xdefaults;
363 #endif
365 if (xdefs != NULL)
366 db = XrmGetStringDatabase (xdefs);
367 else
369 char *home;
370 char *xdefault;
372 home = gethomedir ();
373 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
374 strcpy (xdefault, home);
375 strcat (xdefault, ".Xdefaults");
376 db = XrmGetFileDatabase (xdefault);
377 xfree (home);
378 xfree (xdefault);
381 #ifdef HAVE_XSCREENRESOURCESTRING
382 /* Get the screen-specific resources too. */
383 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
384 if (xdefs != NULL)
386 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
387 XFree (xdefs);
389 #endif
391 return db;
394 static XrmDatabase
395 get_environ_db (void)
397 XrmDatabase db;
398 char *p;
399 char *path = 0;
401 if ((p = getenv ("XENVIRONMENT")) == NULL)
403 static char const xdefaults[] = ".Xdefaults-";
404 char *home = gethomedir ();
405 char const *host = SSDATA (Vsystem_name);
406 ptrdiff_t pathsize = (strlen (home) + sizeof xdefaults
407 + SBYTES (Vsystem_name));
408 path = xrealloc (home, pathsize);
409 strcat (strcat (path, xdefaults), host);
410 p = path;
413 db = XrmGetFileDatabase (p);
415 xfree (path);
417 return db;
420 /* External interface. */
422 /* Types of values that we can find in a database */
424 #define XrmStringType "String" /* String representation */
425 static XrmRepresentation x_rm_string; /* Quark representation */
427 /* Load X resources based on the display and a possible -xrm option. */
429 XrmDatabase
430 x_load_resources (Display *display, const char *xrm_string,
431 const char *myname, const char *myclass)
433 XrmDatabase user_database;
434 XrmDatabase rdb;
435 XrmDatabase db;
436 char line[256];
438 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
439 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
440 #endif
442 #ifdef USE_MOTIF
443 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
444 #endif
446 x_rm_string = XrmStringToQuark (XrmStringType);
447 #ifndef USE_X_TOOLKIT
448 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
449 I suspect it's because the toolkit version does this elsewhere. */
450 XrmInitialize ();
451 #endif
452 rdb = XrmGetStringDatabase ("");
454 /* Add some font defaults. If the font `helv' doesn't exist, widgets
455 will use some other default font. */
456 #ifdef USE_MOTIF
458 sprintf (line, "%s.pane.background: grey75", myclass);
459 XrmPutLineResource (&rdb, line);
460 sprintf (line, "%s*fontList: %s", myclass, helv);
461 XrmPutLineResource (&rdb, line);
462 sprintf (line, "%s*menu*background: grey75", myclass);
463 XrmPutLineResource (&rdb, line);
464 sprintf (line, "%s*menubar*background: grey75", myclass);
465 XrmPutLineResource (&rdb, line);
466 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
467 XrmPutLineResource (&rdb, line);
468 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
469 XrmPutLineResource (&rdb, line);
470 sprintf (line, "%s.dialog*.background: grey75", myclass);
471 XrmPutLineResource (&rdb, line);
472 sprintf (line, "%s*fsb.Text.background: white", myclass);
473 XrmPutLineResource (&rdb, line);
474 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
475 XrmPutLineResource (&rdb, line);
476 sprintf (line, "%s*fsb*DirList.background: white", myclass);
477 XrmPutLineResource (&rdb, line);
478 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
479 XrmPutLineResource (&rdb, line);
480 sprintf (line, "%s*fsb*background: grey75", myclass);
481 XrmPutLineResource (&rdb, line);
482 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
483 XrmPutLineResource (&rdb, line);
484 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
485 XrmPutLineResource (&rdb, line);
486 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
487 XrmPutLineResource (&rdb, line);
488 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
489 XrmPutLineResource (&rdb, line);
491 /* Set double click time of list boxes in the file selection
492 dialog from `double-click-time'. */
493 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
495 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
496 myclass, XFASTINT (Vdouble_click_time));
497 XrmPutLineResource (&rdb, line);
498 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
499 myclass, XFASTINT (Vdouble_click_time));
500 XrmPutLineResource (&rdb, line);
503 #else /* not USE_MOTIF */
505 sprintf (line, "Emacs.dialog*.background: grey75");
506 XrmPutLineResource (&rdb, line);
507 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
508 sprintf (line, "Emacs.dialog*.font: %s", helv);
509 XrmPutLineResource (&rdb, line);
510 sprintf (line, "*XlwMenu*font: %s", helv);
511 XrmPutLineResource (&rdb, line);
512 #endif
513 sprintf (line, "*XlwMenu*background: grey75");
514 XrmPutLineResource (&rdb, line);
515 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
516 XrmPutLineResource (&rdb, line);
518 #endif /* not USE_MOTIF */
520 user_database = get_user_db (display);
522 /* Figure out what the "customization string" is, so we can use it
523 to decode paths. */
524 xfree (x_customization_string);
525 x_customization_string
526 = x_get_customization_string (user_database, myname, myclass);
528 /* Get application system defaults */
529 db = get_system_app (myclass);
530 if (db != NULL)
531 XrmMergeDatabases (db, &rdb);
533 /* Get Fallback resources */
534 db = get_fallback (display);
535 if (db != NULL)
536 XrmMergeDatabases (db, &rdb);
538 /* Get application user defaults */
539 db = get_user_app (myclass);
540 if (db != NULL)
541 XrmMergeDatabases (db, &rdb);
543 /* get User defaults */
544 if (user_database != NULL)
545 XrmMergeDatabases (user_database, &rdb);
547 /* Get Environment defaults. */
548 db = get_environ_db ();
549 if (db != NULL)
550 XrmMergeDatabases (db, &rdb);
552 /* Last, merge in any specification from the command line. */
553 if (xrm_string != NULL)
555 db = XrmGetStringDatabase (xrm_string);
556 if (db != NULL)
557 XrmMergeDatabases (db, &rdb);
560 return rdb;
564 /* Retrieve the value of the resource specified by NAME with class CLASS
565 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
567 static int
568 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
569 XrmRepresentation expected_type, XrmValue *ret_value)
571 XrmValue value;
572 XrmName namelist[100];
573 XrmClass classlist[100];
574 XrmRepresentation type;
576 XrmStringToNameList (name, namelist);
577 XrmStringToClassList (class, classlist);
579 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
580 && (type == expected_type))
582 if (type == x_rm_string)
583 ret_value->addr = (char *) value.addr;
584 else
585 memcpy (ret_value->addr, value.addr, ret_value->size);
587 return value.size;
590 return 0;
593 /* Retrieve the string resource specified by NAME with CLASS from
594 database RDB. */
596 char *
597 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
599 XrmValue value;
601 if (inhibit_x_resources)
602 /* --quick was passed, so this is a no-op. */
603 return NULL;
605 if (x_get_resource (rdb, name, class, x_rm_string, &value))
606 return (char *) value.addr;
608 return (char *) 0;
611 /* Stand-alone test facilities. */
613 #ifdef TESTRM
615 typedef char **List;
616 #define arg_listify(len, list) (list)
617 #define car(list) (*(list))
618 #define cdr(list) (list + 1)
619 #define NIL(list) (! *(list))
620 #define free_arglist(list)
622 static List
623 member (char *elt, List list)
625 List p;
627 for (p = list; ! NIL (p); p = cdr (p))
628 if (! strcmp (elt, car (p)))
629 return p;
631 return p;
634 static void
635 fatal (char *msg, char *prog)
637 if (errno)
638 perror (prog);
640 (void) fprintf (stderr, msg, prog);
641 exit (1);
645 main (int argc, char **argv)
647 Display *display;
648 char *displayname, *resource_string, *class, *name;
649 XrmDatabase xdb;
650 List arg_list, lp;
652 arg_list = arg_listify (argc, argv);
654 lp = member ("-d", arg_list);
655 if (!NIL (lp))
656 displayname = car (cdr (lp));
657 else
658 displayname = "localhost:0.0";
660 lp = member ("-xrm", arg_list);
661 if (! NIL (lp))
662 resource_string = car (cdr (lp));
663 else
664 resource_string = (char *) 0;
666 lp = member ("-c", arg_list);
667 if (! NIL (lp))
668 class = car (cdr (lp));
669 else
670 class = "Emacs";
672 lp = member ("-n", arg_list);
673 if (! NIL (lp))
674 name = car (cdr (lp));
675 else
676 name = "emacs";
678 free_arglist (arg_list);
680 if (!(display = XOpenDisplay (displayname)))
681 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
683 xdb = x_load_resources (display, resource_string, name, class);
685 /* In a real program, you'd want to also do this: */
686 display->db = xdb;
688 while (1)
690 char query_name[90];
691 char query_class[90];
693 printf ("Name: ");
694 gets (query_name);
696 if (strlen (query_name))
698 char *value;
700 printf ("Class: ");
701 gets (query_class);
703 value = x_get_string_resource (xdb, query_name, query_class);
705 if (value != NULL)
706 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
707 else
708 printf ("\tNo Value.\n\n");
710 else
711 break;
713 printf ("\tExit.\n\n");
715 XCloseDisplay (display);
717 return 0;
719 #endif /* TESTRM */