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