[ChangeLog]
[bpt/emacs.git] / src / xrdb.c
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2011 Free Software Foundation, Inc.
3
4 Author: Joseph Arceneaux
5 Created: 4/90
6
7 This file is part of GNU Emacs.
8
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.
13
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.
18
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/>. */
21
22 #include <config.h>
23
24 #include <unistd.h>
25 #include <errno.h>
26 #include <epaths.h>
27
28 #include <stdio.h>
29 #include <setjmp.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xatom.h>
33 #include <X11/X.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
36 #ifdef HAVE_PWD_H
37 #include <pwd.h>
38 #endif
39 #include <sys/stat.h>
40
41 #include "lisp.h"
42
43 #ifdef USE_MOTIF
44 /* For Vdouble_click_time. */
45 #include "keyboard.h"
46 #endif
47
48 extern char *getenv (const char *);
49
50 extern struct passwd *getpwuid (uid_t);
51 extern struct passwd *getpwnam (const char *);
52
53 extern const char *get_system_name (void);
54
55 char *x_get_string_resource (XrmDatabase rdb, const char *name,
56 const char *class);
57 static int file_p (const char *filename);
58
59 \f
60 /* X file search path processing. */
61
62
63 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
64 and friends, or zero if none was specified. */
65 char *x_customization_string;
66
67
68 /* Return the value of the emacs.customization (Emacs.Customization)
69 resource, for later use in search path decoding. If we find no
70 such resource, return zero. */
71 static char *
72 x_get_customization_string (XrmDatabase db, const char *name,
73 const char *class)
74 {
75 char *full_name
76 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
77 char *full_class
78 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
79 char *result;
80
81 sprintf (full_name, "%s.%s", name, "customization");
82 sprintf (full_class, "%s.%s", class, "Customization");
83
84 result = x_get_string_resource (db, full_name, full_class);
85
86 if (result)
87 {
88 char *copy = (char *) xmalloc (strlen (result) + 1);
89 strcpy (copy, result);
90 return copy;
91 }
92 else
93 return 0;
94 }
95
96
97 /* Expand all the Xt-style %-escapes in STRING, whose length is given
98 by STRING_LEN. Here are the escapes we're supposed to recognize:
99
100 %N The value of the application's class name
101 %T The value of the type parameter ("app-defaults" in this
102 context)
103 %S The value of the suffix parameter ("" in this context)
104 %L The language string associated with the specified display
105 (We use the "LANG" environment variable here, if it's set.)
106 %l The language part of the display's language string
107 (We treat this just like %L. If someone can tell us what
108 we're really supposed to do, dandy.)
109 %t The territory part of the display's language string
110 (This never gets used.)
111 %c The codeset part of the display's language string
112 (This never gets used either.)
113 %C The customization string retrieved from the resource
114 database associated with display.
115 (This is x_customization_string.)
116
117 Return the expanded file name if it exists and is readable, and
118 refers to %L only when the LANG environment variable is set, or
119 otherwise provided by X.
120
121 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
122 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
123 alone.
124
125 Return NULL otherwise. */
126
127 static char *
128 magic_file_p (const char *string, EMACS_INT string_len, const char *class, const char *escaped_suffix, const char *suffix)
129 {
130 char *lang = getenv ("LANG");
131
132 int path_size = 100;
133 char *path = (char *) xmalloc (path_size);
134 int path_len = 0;
135
136 const char *p = string;
137
138 while (p < string + string_len)
139 {
140 /* The chunk we're about to stick on the end of result. */
141 const char *next = NULL;
142 int next_len;
143
144 if (*p == '%')
145 {
146 p++;
147
148 if (p >= string + string_len)
149 next_len = 0;
150 else
151 switch (*p)
152 {
153 case '%':
154 next = "%";
155 next_len = 1;
156 break;
157
158 case 'C':
159 next = (x_customization_string
160 ? x_customization_string
161 : "");
162 next_len = strlen (next);
163 break;
164
165 case 'N':
166 next = class;
167 next_len = strlen (class);
168 break;
169
170 case 'T':
171 next = "app-defaults";
172 next_len = strlen (next);
173 break;
174
175 default:
176 case 'S':
177 next_len = 0;
178 break;
179
180 case 'L':
181 case 'l':
182 if (! lang)
183 {
184 xfree (path);
185 return NULL;
186 }
187
188 next = lang;
189 next_len = strlen (next);
190 break;
191
192 case 't':
193 case 'c':
194 xfree (path);
195 return NULL;
196 }
197 }
198 else
199 next = p, next_len = 1;
200
201 /* Do we have room for this component followed by a '\0' ? */
202 if (path_len + next_len + 1 > path_size)
203 {
204 path_size = (path_len + next_len + 1) * 2;
205 path = (char *) xrealloc (path, path_size);
206 }
207
208 memcpy (path + path_len, next, next_len);
209 path_len += next_len;
210
211 p++;
212
213 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
214 if (p >= string + string_len && escaped_suffix)
215 {
216 string = escaped_suffix;
217 string_len = strlen (string);
218 p = string;
219 escaped_suffix = NULL;
220 }
221 }
222
223 /* Perhaps we should add the SUFFIX now. */
224 if (suffix)
225 {
226 int suffix_len = strlen (suffix);
227
228 if (path_len + suffix_len + 1 > path_size)
229 {
230 path_size = (path_len + suffix_len + 1);
231 path = (char *) xrealloc (path, path_size);
232 }
233
234 memcpy (path + path_len, suffix, suffix_len);
235 path_len += suffix_len;
236 }
237
238 path[path_len] = '\0';
239
240 if (! file_p (path))
241 {
242 xfree (path);
243 return NULL;
244 }
245
246 return path;
247 }
248
249
250 static char *
251 gethomedir (void)
252 {
253 struct passwd *pw;
254 char *ptr;
255 char *copy;
256
257 if ((ptr = getenv ("HOME")) == NULL)
258 {
259 if ((ptr = getenv ("LOGNAME")) != NULL
260 || (ptr = getenv ("USER")) != NULL)
261 pw = getpwnam (ptr);
262 else
263 pw = getpwuid (getuid ());
264
265 if (pw)
266 ptr = pw->pw_dir;
267 }
268
269 if (ptr == NULL)
270 return xstrdup ("/");
271
272 copy = (char *) xmalloc (strlen (ptr) + 2);
273 strcpy (copy, ptr);
274 strcat (copy, "/");
275
276 return copy;
277 }
278
279
280 static int
281 file_p (const char *filename)
282 {
283 struct stat status;
284
285 return (access (filename, 4) == 0 /* exists and is readable */
286 && stat (filename, &status) == 0 /* get the status */
287 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
288 }
289
290
291 /* Find the first element of SEARCH_PATH which exists and is readable,
292 after expanding the %-escapes. Return 0 if we didn't find any, and
293 the path name of the one we found otherwise. */
294
295 static char *
296 search_magic_path (const char *search_path, const char *class, const char *escaped_suffix, const char *suffix)
297 {
298 const char *s, *p;
299
300 for (s = search_path; *s; s = p)
301 {
302 for (p = s; *p && *p != ':'; p++)
303 ;
304
305 if (p > s)
306 {
307 char *path = magic_file_p (s, p - s, class, escaped_suffix,
308 suffix);
309 if (path)
310 return path;
311 }
312 else if (*p == ':')
313 {
314 char *path;
315
316 s = "%N%S";
317 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
318 if (path)
319 return path;
320 }
321
322 if (*p == ':')
323 p++;
324 }
325
326 return 0;
327 }
328 \f
329 /* Producing databases for individual sources. */
330
331 static XrmDatabase
332 get_system_app (const char *class)
333 {
334 XrmDatabase db = NULL;
335 const char *path;
336 char *p;
337
338 path = getenv ("XFILESEARCHPATH");
339 if (! path) path = PATH_X_DEFAULTS;
340
341 p = search_magic_path (path, class, 0, 0);
342 if (p)
343 {
344 db = XrmGetFileDatabase (p);
345 xfree (p);
346 }
347
348 return db;
349 }
350
351
352 static XrmDatabase
353 get_fallback (Display *display)
354 {
355 return NULL;
356 }
357
358
359 static XrmDatabase
360 get_user_app (const char *class)
361 {
362 const char *path;
363 char *file = 0;
364 char *free_it = 0;
365
366 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
367 names, not directories. */
368 if (((path = getenv ("XUSERFILESEARCHPATH"))
369 && (file = search_magic_path (path, class, 0, 0)))
370
371 /* Check for APPLRESDIR; it is a path of directories. In each,
372 we have to search for LANG/CLASS and then CLASS. */
373 || ((path = getenv ("XAPPLRESDIR"))
374 && ((file = search_magic_path (path, class, "/%L/%N", 0))
375 || (file = search_magic_path (path, class, "/%N", 0))))
376
377 /* Check in the home directory. This is a bit of a hack; let's
378 hope one's home directory doesn't contain any %-escapes. */
379 || (free_it = gethomedir (),
380 ((file = search_magic_path (free_it, class, "%L/%N", 0))
381 || (file = search_magic_path (free_it, class, "%N", 0)))))
382 {
383 XrmDatabase db = XrmGetFileDatabase (file);
384 xfree (file);
385 xfree (free_it);
386 return db;
387 }
388
389 xfree (free_it);
390 return NULL;
391 }
392
393
394 static XrmDatabase
395 get_user_db (Display *display)
396 {
397 XrmDatabase db;
398 char *xdefs;
399
400 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
401 xdefs = XResourceManagerString (display);
402 #else
403 xdefs = display->xdefaults;
404 #endif
405
406 if (xdefs != NULL)
407 db = XrmGetStringDatabase (xdefs);
408 else
409 {
410 char *home;
411 char *xdefault;
412
413 home = gethomedir ();
414 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
415 strcpy (xdefault, home);
416 strcat (xdefault, ".Xdefaults");
417 db = XrmGetFileDatabase (xdefault);
418 xfree (home);
419 xfree (xdefault);
420 }
421
422 #ifdef HAVE_XSCREENRESOURCESTRING
423 /* Get the screen-specific resources too. */
424 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
425 if (xdefs != NULL)
426 {
427 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
428 XFree (xdefs);
429 }
430 #endif
431
432 return db;
433 }
434
435 static XrmDatabase
436 get_environ_db (void)
437 {
438 XrmDatabase db;
439 char *p;
440 char *path = 0, *home = 0;
441 const char *host;
442
443 if ((p = getenv ("XENVIRONMENT")) == NULL)
444 {
445 home = gethomedir ();
446 host = get_system_name ();
447 path = (char *) xmalloc (strlen (home)
448 + sizeof (".Xdefaults-")
449 + strlen (host));
450 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
451 p = path;
452 }
453
454 db = XrmGetFileDatabase (p);
455
456 xfree (path);
457 xfree (home);
458
459 return db;
460 }
461 \f
462 /* External interface. */
463
464 /* Types of values that we can find in a database */
465
466 #define XrmStringType "String" /* String representation */
467 XrmRepresentation x_rm_string; /* Quark representation */
468
469 /* Load X resources based on the display and a possible -xrm option. */
470
471 XrmDatabase
472 x_load_resources (Display *display, const char *xrm_string,
473 const char *myname, const char *myclass)
474 {
475 XrmDatabase user_database;
476 XrmDatabase rdb;
477 XrmDatabase db;
478 char line[256];
479
480 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
481
482 #ifdef USE_MOTIF
483 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
484 #endif
485
486 x_rm_string = XrmStringToQuark (XrmStringType);
487 #ifndef USE_X_TOOLKIT
488 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
489 I suspect it's because the toolkit version does this elsewhere. */
490 XrmInitialize ();
491 #endif
492 rdb = XrmGetStringDatabase ("");
493
494 /* Add some font defaults. If the font `helv' doesn't exist, widgets
495 will use some other default font. */
496 #ifdef USE_MOTIF
497
498 sprintf (line, "%s.pane.background: grey75", myclass);
499 XrmPutLineResource (&rdb, line);
500 sprintf (line, "%s*fontList: %s", myclass, helv);
501 XrmPutLineResource (&rdb, line);
502 sprintf (line, "%s*menu*background: grey75", myclass);
503 XrmPutLineResource (&rdb, line);
504 sprintf (line, "%s*menubar*background: grey75", myclass);
505 XrmPutLineResource (&rdb, line);
506 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
507 XrmPutLineResource (&rdb, line);
508 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
509 XrmPutLineResource (&rdb, line);
510 sprintf (line, "%s.dialog*.background: grey75", myclass);
511 XrmPutLineResource (&rdb, line);
512 sprintf (line, "%s*fsb.Text.background: white", myclass);
513 XrmPutLineResource (&rdb, line);
514 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
515 XrmPutLineResource (&rdb, line);
516 sprintf (line, "%s*fsb*DirList.background: white", myclass);
517 XrmPutLineResource (&rdb, line);
518 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
519 XrmPutLineResource (&rdb, line);
520 sprintf (line, "%s*fsb*background: grey75", myclass);
521 XrmPutLineResource (&rdb, line);
522 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
523 XrmPutLineResource (&rdb, line);
524 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
525 XrmPutLineResource (&rdb, line);
526 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
527 XrmPutLineResource (&rdb, line);
528 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
529 XrmPutLineResource (&rdb, line);
530
531 /* Set double click time of list boxes in the file selection
532 dialog from `double-click-time'. */
533 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
534 {
535 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %d",
536 myclass, XFASTINT (Vdouble_click_time));
537 XrmPutLineResource (&rdb, line);
538 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %d",
539 myclass, XFASTINT (Vdouble_click_time));
540 XrmPutLineResource (&rdb, line);
541 }
542
543 #else /* not USE_MOTIF */
544
545 sprintf (line, "Emacs.dialog*.background: grey75");
546 XrmPutLineResource (&rdb, line);
547 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
548 sprintf (line, "Emacs.dialog*.font: %s", helv);
549 XrmPutLineResource (&rdb, line);
550 sprintf (line, "*XlwMenu*font: %s", helv);
551 XrmPutLineResource (&rdb, line);
552 #endif
553 sprintf (line, "*XlwMenu*background: grey75");
554 XrmPutLineResource (&rdb, line);
555 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
556 XrmPutLineResource (&rdb, line);
557
558 #endif /* not USE_MOTIF */
559
560 user_database = get_user_db (display);
561
562 /* Figure out what the "customization string" is, so we can use it
563 to decode paths. */
564 xfree (x_customization_string);
565 x_customization_string
566 = x_get_customization_string (user_database, myname, myclass);
567
568 /* Get application system defaults */
569 db = get_system_app (myclass);
570 if (db != NULL)
571 XrmMergeDatabases (db, &rdb);
572
573 /* Get Fallback resources */
574 db = get_fallback (display);
575 if (db != NULL)
576 XrmMergeDatabases (db, &rdb);
577
578 /* Get application user defaults */
579 db = get_user_app (myclass);
580 if (db != NULL)
581 XrmMergeDatabases (db, &rdb);
582
583 /* get User defaults */
584 if (user_database != NULL)
585 XrmMergeDatabases (user_database, &rdb);
586
587 /* Get Environment defaults. */
588 db = get_environ_db ();
589 if (db != NULL)
590 XrmMergeDatabases (db, &rdb);
591
592 /* Last, merge in any specification from the command line. */
593 if (xrm_string != NULL)
594 {
595 db = XrmGetStringDatabase (xrm_string);
596 if (db != NULL)
597 XrmMergeDatabases (db, &rdb);
598 }
599
600 return rdb;
601 }
602
603
604 /* Retrieve the value of the resource specified by NAME with class CLASS
605 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
606
607 static int
608 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
609 XrmRepresentation expected_type, XrmValue *ret_value)
610 {
611 XrmValue value;
612 XrmName namelist[100];
613 XrmClass classlist[100];
614 XrmRepresentation type;
615
616 XrmStringToNameList(name, namelist);
617 XrmStringToClassList(class, classlist);
618
619 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
620 && (type == expected_type))
621 {
622 if (type == x_rm_string)
623 ret_value->addr = (char *) value.addr;
624 else
625 memcpy (ret_value->addr, value.addr, ret_value->size);
626
627 return value.size;
628 }
629
630 return 0;
631 }
632
633 /* Retrieve the string resource specified by NAME with CLASS from
634 database RDB. */
635
636 char *
637 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
638 {
639 XrmValue value;
640
641 if (inhibit_x_resources)
642 /* --quick was passed, so this is a no-op. */
643 return NULL;
644
645 if (x_get_resource (rdb, name, class, x_rm_string, &value))
646 return (char *) value.addr;
647
648 return (char *) 0;
649 }
650 \f
651 /* Stand-alone test facilities. */
652
653 #ifdef TESTRM
654
655 typedef char **List;
656 #define arg_listify(len, list) (list)
657 #define car(list) (*(list))
658 #define cdr(list) (list + 1)
659 #define NIL(list) (! *(list))
660 #define free_arglist(list)
661
662 static List
663 member (elt, list)
664 char *elt;
665 List list;
666 {
667 List p;
668
669 for (p = list; ! NIL (p); p = cdr (p))
670 if (! strcmp (elt, car (p)))
671 return p;
672
673 return p;
674 }
675
676 static void
677 fatal (msg, prog, x1, x2, x3, x4, x5)
678 char *msg, *prog;
679 int x1, x2, x3, x4, x5;
680 {
681 if (errno)
682 perror (prog);
683
684 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
685 exit (1);
686 }
687
688 main (argc, argv)
689 int argc;
690 char **argv;
691 {
692 Display *display;
693 char *displayname, *resource_string, *class, *name;
694 XrmDatabase xdb;
695 List arg_list, lp;
696
697 arg_list = arg_listify (argc, argv);
698
699 lp = member ("-d", arg_list);
700 if (!NIL (lp))
701 displayname = car (cdr (lp));
702 else
703 displayname = "localhost:0.0";
704
705 lp = member ("-xrm", arg_list);
706 if (! NIL (lp))
707 resource_string = car (cdr (lp));
708 else
709 resource_string = (char *) 0;
710
711 lp = member ("-c", arg_list);
712 if (! NIL (lp))
713 class = car (cdr (lp));
714 else
715 class = "Emacs";
716
717 lp = member ("-n", arg_list);
718 if (! NIL (lp))
719 name = car (cdr (lp));
720 else
721 name = "emacs";
722
723 free_arglist (arg_list);
724
725 if (!(display = XOpenDisplay (displayname)))
726 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
727
728 xdb = x_load_resources (display, resource_string, name, class);
729
730 /* In a real program, you'd want to also do this: */
731 display->db = xdb;
732
733 while (1)
734 {
735 char query_name[90];
736 char query_class[90];
737
738 printf ("Name: ");
739 gets (query_name);
740
741 if (strlen (query_name))
742 {
743 char *value;
744
745 printf ("Class: ");
746 gets (query_class);
747
748 value = x_get_string_resource (xdb, query_name, query_class);
749
750 if (value != NULL)
751 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
752 else
753 printf ("\tNo Value.\n\n");
754 }
755 else
756 break;
757 }
758 printf ("\tExit.\n\n");
759
760 XCloseDisplay (display);
761 }
762 #endif /* TESTRM */