commands.texi small fix for bug#13393
[emacs.git] / src / xrdb.c
blob9d3ce1670b14d57282789b7f099d6db509af9179
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
45 #include <sys/stat.h>
47 #ifdef USE_MOTIF
48 /* For Vdouble_click_time. */
49 #include "keyboard.h"
50 #endif
52 char *x_get_string_resource (XrmDatabase rdb, const char *name,
53 const char *class);
54 static int file_p (const char *filename);
57 /* X file search path processing. */
60 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
61 and friends, or zero if none was specified. */
62 static char *x_customization_string;
65 /* Return the value of the emacs.customization (Emacs.Customization)
66 resource, for later use in search path decoding. If we find no
67 such resource, return zero. */
68 static char *
69 x_get_customization_string (XrmDatabase db, const char *name,
70 const char *class)
72 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
73 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
74 char *result;
76 sprintf (full_name, "%s.%s", name, "customization");
77 sprintf (full_class, "%s.%s", class, "Customization");
79 result = x_get_string_resource (db, full_name, full_class);
81 if (result)
83 char *copy = xmalloc (strlen (result) + 1);
84 strcpy (copy, result);
85 return copy;
87 else
88 return 0;
92 /* Expand all the Xt-style %-escapes in STRING, whose length is given
93 by STRING_LEN. Here are the escapes we're supposed to recognize:
95 %N The value of the application's class name
96 %T The value of the type parameter ("app-defaults" in this
97 context)
98 %S The value of the suffix parameter ("" in this context)
99 %L The language string associated with the specified display
100 (We use the "LANG" environment variable here, if it's set.)
101 %l The language part of the display's language string
102 (We treat this just like %L. If someone can tell us what
103 we're really supposed to do, dandy.)
104 %t The territory part of the display's language string
105 (This never gets used.)
106 %c The codeset part of the display's language string
107 (This never gets used either.)
108 %C The customization string retrieved from the resource
109 database associated with display.
110 (This is x_customization_string.)
112 Return the expanded file name if it exists and is readable, and
113 refers to %L only when the LANG environment variable is set, or
114 otherwise provided by X.
116 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
117 %-escapes in ESCAPED_SUFFIX are expanded.
119 Return NULL otherwise. */
121 static char *
122 magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
123 const char *escaped_suffix)
125 char *lang = getenv ("LANG");
127 ptrdiff_t path_size = 100;
128 char *path = xmalloc (path_size);
129 ptrdiff_t path_len = 0;
131 const char *p = string;
133 while (p < string + string_len)
135 /* The chunk we're about to stick on the end of result. */
136 const char *next = NULL;
137 ptrdiff_t next_len;
139 if (*p == '%')
141 p++;
143 if (p >= string + string_len)
144 next_len = 0;
145 else
146 switch (*p)
148 case '%':
149 next = "%";
150 next_len = 1;
151 break;
153 case 'C':
154 next = (x_customization_string
155 ? x_customization_string
156 : "");
157 next_len = strlen (next);
158 break;
160 case 'N':
161 next = class;
162 next_len = strlen (class);
163 break;
165 case 'T':
166 next = "app-defaults";
167 next_len = strlen (next);
168 break;
170 default:
171 case 'S':
172 next_len = 0;
173 break;
175 case 'L':
176 case 'l':
177 if (! lang)
179 xfree (path);
180 return NULL;
183 next = lang;
184 next_len = strlen (next);
185 break;
187 case 't':
188 case 'c':
189 xfree (path);
190 return NULL;
193 else
194 next = p, next_len = 1;
196 /* Do we have room for this component followed by a '\0' ? */
197 if (path_size - path_len <= next_len)
199 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
200 memory_full (SIZE_MAX);
201 path_size = (path_len + next_len + 1) * 2;
202 path = xrealloc (path, path_size);
205 memcpy (path + path_len, next, next_len);
206 path_len += next_len;
208 p++;
210 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
211 if (p >= string + string_len && escaped_suffix)
213 string = escaped_suffix;
214 string_len = strlen (string);
215 p = string;
216 escaped_suffix = NULL;
220 path[path_len] = '\0';
222 if (! file_p (path))
224 xfree (path);
225 return NULL;
228 return path;
232 static char *
233 gethomedir (void)
235 struct passwd *pw;
236 char *ptr;
237 char *copy;
239 if ((ptr = getenv ("HOME")) == NULL)
241 if ((ptr = getenv ("LOGNAME")) != NULL
242 || (ptr = getenv ("USER")) != NULL)
243 pw = getpwnam (ptr);
244 else
245 pw = getpwuid (getuid ());
247 if (pw)
248 ptr = pw->pw_dir;
251 if (ptr == NULL)
252 return xstrdup ("/");
254 copy = xmalloc (strlen (ptr) + 2);
255 strcpy (copy, ptr);
256 strcat (copy, "/");
258 return copy;
262 static int
263 file_p (const char *filename)
265 struct stat status;
267 return (access (filename, 4) == 0 /* exists and is readable */
268 && stat (filename, &status) == 0 /* get the status */
269 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
273 /* Find the first element of SEARCH_PATH which exists and is readable,
274 after expanding the %-escapes. Return 0 if we didn't find any, and
275 the path name of the one we found otherwise. */
277 static char *
278 search_magic_path (const char *search_path, const char *class,
279 const char *escaped_suffix)
281 const char *s, *p;
283 for (s = search_path; *s; s = p)
285 for (p = s; *p && *p != ':'; p++)
288 if (p > s)
290 char *path = magic_file_p (s, p - s, class, escaped_suffix);
291 if (path)
292 return path;
294 else if (*p == ':')
296 char *path;
298 s = "%N%S";
299 path = magic_file_p (s, strlen (s), class, escaped_suffix);
300 if (path)
301 return path;
304 if (*p == ':')
305 p++;
308 return 0;
311 /* Producing databases for individual sources. */
313 static XrmDatabase
314 get_system_app (const char *class)
316 XrmDatabase db = NULL;
317 const char *path;
318 char *p;
320 path = getenv ("XFILESEARCHPATH");
321 if (! path) path = PATH_X_DEFAULTS;
323 p = search_magic_path (path, class, 0);
324 if (p)
326 db = XrmGetFileDatabase (p);
327 xfree (p);
330 return db;
334 static XrmDatabase
335 get_fallback (Display *display)
337 return NULL;
341 static XrmDatabase
342 get_user_app (const char *class)
344 const char *path;
345 char *file = 0;
346 char *free_it = 0;
348 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
349 names, not directories. */
350 if (((path = getenv ("XUSERFILESEARCHPATH"))
351 && (file = search_magic_path (path, class, 0)))
353 /* Check for APPLRESDIR; it is a path of directories. In each,
354 we have to search for LANG/CLASS and then CLASS. */
355 || ((path = getenv ("XAPPLRESDIR"))
356 && ((file = search_magic_path (path, class, "/%L/%N"))
357 || (file = search_magic_path (path, class, "/%N"))))
359 /* Check in the home directory. This is a bit of a hack; let's
360 hope one's home directory doesn't contain any %-escapes. */
361 || (free_it = gethomedir (),
362 ((file = search_magic_path (free_it, class, "%L/%N"))
363 || (file = search_magic_path (free_it, class, "%N")))))
365 XrmDatabase db = XrmGetFileDatabase (file);
366 xfree (file);
367 xfree (free_it);
368 return db;
371 xfree (free_it);
372 return NULL;
376 static XrmDatabase
377 get_user_db (Display *display)
379 XrmDatabase db;
380 char *xdefs;
382 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
383 xdefs = XResourceManagerString (display);
384 #else
385 xdefs = display->xdefaults;
386 #endif
388 if (xdefs != NULL)
389 db = XrmGetStringDatabase (xdefs);
390 else
392 char *home;
393 char *xdefault;
395 home = gethomedir ();
396 xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
397 strcpy (xdefault, home);
398 strcat (xdefault, ".Xdefaults");
399 db = XrmGetFileDatabase (xdefault);
400 xfree (home);
401 xfree (xdefault);
404 #ifdef HAVE_XSCREENRESOURCESTRING
405 /* Get the screen-specific resources too. */
406 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
407 if (xdefs != NULL)
409 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
410 XFree (xdefs);
412 #endif
414 return db;
417 static XrmDatabase
418 get_environ_db (void)
420 XrmDatabase db;
421 char *p;
422 char *path = 0;
424 if ((p = getenv ("XENVIRONMENT")) == NULL)
426 static char const xdefaults[] = ".Xdefaults-";
427 char *home = gethomedir ();
428 char const *host = SSDATA (Vsystem_name);
429 ptrdiff_t pathsize = (strlen (home) + sizeof xdefaults
430 + SBYTES (Vsystem_name));
431 path = xrealloc (home, pathsize);
432 strcat (strcat (path, xdefaults), host);
433 p = path;
436 db = XrmGetFileDatabase (p);
438 xfree (path);
440 return db;
443 /* External interface. */
445 /* Types of values that we can find in a database */
447 #define XrmStringType "String" /* String representation */
448 static XrmRepresentation x_rm_string; /* Quark representation */
450 /* Load X resources based on the display and a possible -xrm option. */
452 XrmDatabase
453 x_load_resources (Display *display, const char *xrm_string,
454 const char *myname, const char *myclass)
456 XrmDatabase user_database;
457 XrmDatabase rdb;
458 XrmDatabase db;
459 char line[256];
461 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
462 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
463 #endif
465 #ifdef USE_MOTIF
466 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
467 #endif
469 x_rm_string = XrmStringToQuark (XrmStringType);
470 #ifndef USE_X_TOOLKIT
471 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
472 I suspect it's because the toolkit version does this elsewhere. */
473 XrmInitialize ();
474 #endif
475 rdb = XrmGetStringDatabase ("");
477 /* Add some font defaults. If the font `helv' doesn't exist, widgets
478 will use some other default font. */
479 #ifdef USE_MOTIF
481 sprintf (line, "%s.pane.background: grey75", myclass);
482 XrmPutLineResource (&rdb, line);
483 sprintf (line, "%s*fontList: %s", myclass, helv);
484 XrmPutLineResource (&rdb, line);
485 sprintf (line, "%s*menu*background: grey75", myclass);
486 XrmPutLineResource (&rdb, line);
487 sprintf (line, "%s*menubar*background: grey75", myclass);
488 XrmPutLineResource (&rdb, line);
489 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
490 XrmPutLineResource (&rdb, line);
491 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
492 XrmPutLineResource (&rdb, line);
493 sprintf (line, "%s.dialog*.background: grey75", myclass);
494 XrmPutLineResource (&rdb, line);
495 sprintf (line, "%s*fsb.Text.background: white", myclass);
496 XrmPutLineResource (&rdb, line);
497 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
498 XrmPutLineResource (&rdb, line);
499 sprintf (line, "%s*fsb*DirList.background: white", myclass);
500 XrmPutLineResource (&rdb, line);
501 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
502 XrmPutLineResource (&rdb, line);
503 sprintf (line, "%s*fsb*background: grey75", myclass);
504 XrmPutLineResource (&rdb, line);
505 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
506 XrmPutLineResource (&rdb, line);
507 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
508 XrmPutLineResource (&rdb, line);
509 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
510 XrmPutLineResource (&rdb, line);
511 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
512 XrmPutLineResource (&rdb, line);
514 /* Set double click time of list boxes in the file selection
515 dialog from `double-click-time'. */
516 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
518 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
519 myclass, XFASTINT (Vdouble_click_time));
520 XrmPutLineResource (&rdb, line);
521 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
522 myclass, XFASTINT (Vdouble_click_time));
523 XrmPutLineResource (&rdb, line);
526 #else /* not USE_MOTIF */
528 sprintf (line, "Emacs.dialog*.background: grey75");
529 XrmPutLineResource (&rdb, line);
530 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
531 sprintf (line, "Emacs.dialog*.font: %s", helv);
532 XrmPutLineResource (&rdb, line);
533 sprintf (line, "*XlwMenu*font: %s", helv);
534 XrmPutLineResource (&rdb, line);
535 #endif
536 sprintf (line, "*XlwMenu*background: grey75");
537 XrmPutLineResource (&rdb, line);
538 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
539 XrmPutLineResource (&rdb, line);
541 #endif /* not USE_MOTIF */
543 user_database = get_user_db (display);
545 /* Figure out what the "customization string" is, so we can use it
546 to decode paths. */
547 xfree (x_customization_string);
548 x_customization_string
549 = x_get_customization_string (user_database, myname, myclass);
551 /* Get application system defaults */
552 db = get_system_app (myclass);
553 if (db != NULL)
554 XrmMergeDatabases (db, &rdb);
556 /* Get Fallback resources */
557 db = get_fallback (display);
558 if (db != NULL)
559 XrmMergeDatabases (db, &rdb);
561 /* Get application user defaults */
562 db = get_user_app (myclass);
563 if (db != NULL)
564 XrmMergeDatabases (db, &rdb);
566 /* get User defaults */
567 if (user_database != NULL)
568 XrmMergeDatabases (user_database, &rdb);
570 /* Get Environment defaults. */
571 db = get_environ_db ();
572 if (db != NULL)
573 XrmMergeDatabases (db, &rdb);
575 /* Last, merge in any specification from the command line. */
576 if (xrm_string != NULL)
578 db = XrmGetStringDatabase (xrm_string);
579 if (db != NULL)
580 XrmMergeDatabases (db, &rdb);
583 return rdb;
587 /* Retrieve the value of the resource specified by NAME with class CLASS
588 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
590 static int
591 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
592 XrmRepresentation expected_type, XrmValue *ret_value)
594 XrmValue value;
595 XrmName namelist[100];
596 XrmClass classlist[100];
597 XrmRepresentation type;
599 XrmStringToNameList (name, namelist);
600 XrmStringToClassList (class, classlist);
602 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
603 && (type == expected_type))
605 if (type == x_rm_string)
606 ret_value->addr = (char *) value.addr;
607 else
608 memcpy (ret_value->addr, value.addr, ret_value->size);
610 return value.size;
613 return 0;
616 /* Retrieve the string resource specified by NAME with CLASS from
617 database RDB. */
619 char *
620 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
622 XrmValue value;
624 if (inhibit_x_resources)
625 /* --quick was passed, so this is a no-op. */
626 return NULL;
628 if (x_get_resource (rdb, name, class, x_rm_string, &value))
629 return (char *) value.addr;
631 return (char *) 0;
634 /* Stand-alone test facilities. */
636 #ifdef TESTRM
638 typedef char **List;
639 #define arg_listify(len, list) (list)
640 #define car(list) (*(list))
641 #define cdr(list) (list + 1)
642 #define NIL(list) (! *(list))
643 #define free_arglist(list)
645 static List
646 member (char *elt, List list)
648 List p;
650 for (p = list; ! NIL (p); p = cdr (p))
651 if (! strcmp (elt, car (p)))
652 return p;
654 return p;
657 static void
658 fatal (char *msg, char *prog)
660 if (errno)
661 perror (prog);
663 (void) fprintf (stderr, msg, prog);
664 exit (1);
668 main (int argc, char **argv)
670 Display *display;
671 char *displayname, *resource_string, *class, *name;
672 XrmDatabase xdb;
673 List arg_list, lp;
675 arg_list = arg_listify (argc, argv);
677 lp = member ("-d", arg_list);
678 if (!NIL (lp))
679 displayname = car (cdr (lp));
680 else
681 displayname = "localhost:0.0";
683 lp = member ("-xrm", arg_list);
684 if (! NIL (lp))
685 resource_string = car (cdr (lp));
686 else
687 resource_string = (char *) 0;
689 lp = member ("-c", arg_list);
690 if (! NIL (lp))
691 class = car (cdr (lp));
692 else
693 class = "Emacs";
695 lp = member ("-n", arg_list);
696 if (! NIL (lp))
697 name = car (cdr (lp));
698 else
699 name = "emacs";
701 free_arglist (arg_list);
703 if (!(display = XOpenDisplay (displayname)))
704 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
706 xdb = x_load_resources (display, resource_string, name, class);
708 /* In a real program, you'd want to also do this: */
709 display->db = xdb;
711 while (1)
713 char query_name[90];
714 char query_class[90];
716 printf ("Name: ");
717 gets (query_name);
719 if (strlen (query_name))
721 char *value;
723 printf ("Class: ");
724 gets (query_class);
726 value = x_get_string_resource (xdb, query_name, query_class);
728 if (value != NULL)
729 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
730 else
731 printf ("\tNo Value.\n\n");
733 else
734 break;
736 printf ("\tExit.\n\n");
738 XCloseDisplay (display);
740 return 0;
742 #endif /* TESTRM */