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