]> code.delx.au - gnu-emacs/blob - src/xrdb.c
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[gnu-emacs] / src / xrdb.c
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2013 Free Software Foundation,
3 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 #include <stdlib.h>
29 #include <stdio.h>
30
31 #include "lisp.h"
32
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"
36
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
46 #ifdef USE_MOTIF
47 /* For Vdouble_click_time. */
48 #include "keyboard.h"
49 #endif
50
51 char *x_get_string_resource (XrmDatabase rdb, const char *name,
52 const char *class);
53
54 \f
55 /* X file search path processing. */
56
57
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;
61
62
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)
69 {
70 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
71 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
72 char *result;
73
74 sprintf (full_name, "%s.%s", name, "customization");
75 sprintf (full_class, "%s.%s", class, "Customization");
76
77 result = x_get_string_resource (db, full_name, full_class);
78
79 if (result)
80 {
81 char *copy = xmalloc (strlen (result) + 1);
82 strcpy (copy, result);
83 return copy;
84 }
85 else
86 return 0;
87 }
88
89
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:
92
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.)
109
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.
113
114 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
115 %-escapes in ESCAPED_SUFFIX are expanded.
116
117 Return NULL otherwise. */
118
119 static XrmDatabase
120 magic_db (const char *string, ptrdiff_t string_len, const char *class,
121 const char *escaped_suffix)
122 {
123 XrmDatabase db;
124 char *lang = getenv ("LANG");
125
126 ptrdiff_t path_size = 100;
127 char *path = xmalloc (path_size);
128 ptrdiff_t path_len = 0;
129
130 const char *p = string;
131
132 while (p < string + string_len)
133 {
134 /* The chunk we're about to stick on the end of result. */
135 const char *next = NULL;
136 ptrdiff_t next_len;
137
138 if (*p == '%')
139 {
140 p++;
141
142 if (p >= string + string_len)
143 next_len = 0;
144 else
145 switch (*p)
146 {
147 case '%':
148 next = "%";
149 next_len = 1;
150 break;
151
152 case 'C':
153 next = (x_customization_string
154 ? x_customization_string
155 : "");
156 next_len = strlen (next);
157 break;
158
159 case 'N':
160 next = class;
161 next_len = strlen (class);
162 break;
163
164 case 'T':
165 next = "app-defaults";
166 next_len = strlen (next);
167 break;
168
169 default:
170 case 'S':
171 next_len = 0;
172 break;
173
174 case 'L':
175 case 'l':
176 if (! lang)
177 {
178 xfree (path);
179 return NULL;
180 }
181
182 next = lang;
183 next_len = strlen (next);
184 break;
185
186 case 't':
187 case 'c':
188 xfree (path);
189 return NULL;
190 }
191 }
192 else
193 next = p, next_len = 1;
194
195 /* Do we have room for this component followed by a '\0' ? */
196 if (path_size - path_len <= next_len)
197 {
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);
202 }
203
204 memcpy (path + path_len, next, next_len);
205 path_len += next_len;
206
207 p++;
208
209 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
210 if (p >= string + string_len && escaped_suffix)
211 {
212 string = escaped_suffix;
213 string_len = strlen (string);
214 p = string;
215 escaped_suffix = NULL;
216 }
217 }
218
219 path[path_len] = '\0';
220 db = XrmGetFileDatabase (path);
221 xfree (path);
222 return db;
223 }
224
225
226 static char *
227 gethomedir (void)
228 {
229 struct passwd *pw;
230 char *ptr;
231 char *copy;
232
233 if ((ptr = getenv ("HOME")) == NULL)
234 {
235 if ((ptr = getenv ("LOGNAME")) != NULL
236 || (ptr = getenv ("USER")) != NULL)
237 pw = getpwnam (ptr);
238 else
239 pw = getpwuid (getuid ());
240
241 if (pw)
242 ptr = pw->pw_dir;
243 }
244
245 if (ptr == NULL)
246 return xstrdup ("/");
247
248 copy = xmalloc (strlen (ptr) + 2);
249 strcpy (copy, ptr);
250 strcat (copy, "/");
251
252 return copy;
253 }
254
255
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. */
259
260 static XrmDatabase
261 search_magic_path (const char *search_path, const char *class,
262 const char *escaped_suffix)
263 {
264 const char *s, *p;
265
266 for (s = search_path; *s; s = p)
267 {
268 for (p = s; *p && *p != ':'; p++)
269 ;
270
271 if (p > s)
272 {
273 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
274 if (db)
275 return db;
276 }
277 else if (*p == ':')
278 {
279 static char const ns[] = "%N%S";
280 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
281 if (db)
282 return db;
283 }
284
285 if (*p == ':')
286 p++;
287 }
288
289 return 0;
290 }
291 \f
292 /* Producing databases for individual sources. */
293
294 static XrmDatabase
295 get_system_app (const char *class)
296 {
297 const char *path;
298
299 path = getenv ("XFILESEARCHPATH");
300 if (! path) path = PATH_X_DEFAULTS;
301
302 return search_magic_path (path, class, 0);
303 }
304
305
306 static XrmDatabase
307 get_fallback (Display *display)
308 {
309 return NULL;
310 }
311
312
313 static XrmDatabase
314 get_user_app (const char *class)
315 {
316 XrmDatabase db = 0;
317 const char *path;
318
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);
324
325 if (! db)
326 {
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)
331 {
332 db = search_magic_path (path, class, "/%L/%N");
333 if (!db)
334 db = search_magic_path (path, class, "/%N");
335 }
336 }
337
338 if (! db)
339 {
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);
347 }
348
349 return db;
350 }
351
352
353 static XrmDatabase
354 get_user_db (Display *display)
355 {
356 XrmDatabase db;
357 char *xdefs;
358
359 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
360 xdefs = XResourceManagerString (display);
361 #else
362 xdefs = display->xdefaults;
363 #endif
364
365 if (xdefs != NULL)
366 db = XrmGetStringDatabase (xdefs);
367 else
368 {
369 char *home;
370 char *xdefault;
371
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);
379 }
380
381 #ifdef HAVE_XSCREENRESOURCESTRING
382 /* Get the screen-specific resources too. */
383 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
384 if (xdefs != NULL)
385 {
386 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
387 XFree (xdefs);
388 }
389 #endif
390
391 return db;
392 }
393
394 static XrmDatabase
395 get_environ_db (void)
396 {
397 XrmDatabase db;
398 char *p;
399 char *path = 0;
400
401 if ((p = getenv ("XENVIRONMENT")) == NULL)
402 {
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;
411 }
412
413 db = XrmGetFileDatabase (p);
414
415 xfree (path);
416
417 return db;
418 }
419 \f
420 /* External interface. */
421
422 /* Types of values that we can find in a database */
423
424 #define XrmStringType "String" /* String representation */
425 static XrmRepresentation x_rm_string; /* Quark representation */
426
427 /* Load X resources based on the display and a possible -xrm option. */
428
429 XrmDatabase
430 x_load_resources (Display *display, const char *xrm_string,
431 const char *myname, const char *myclass)
432 {
433 XrmDatabase user_database;
434 XrmDatabase rdb;
435 XrmDatabase db;
436 char line[256];
437
438 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
439 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
440 #endif
441
442 #ifdef USE_MOTIF
443 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
444 #endif
445
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 ("");
453
454 /* Add some font defaults. If the font `helv' doesn't exist, widgets
455 will use some other default font. */
456 #ifdef USE_MOTIF
457
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);
490
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)
494 {
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);
501 }
502
503 #else /* not USE_MOTIF */
504
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);
517
518 #endif /* not USE_MOTIF */
519
520 user_database = get_user_db (display);
521
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);
527
528 /* Get application system defaults */
529 db = get_system_app (myclass);
530 if (db != NULL)
531 XrmMergeDatabases (db, &rdb);
532
533 /* Get Fallback resources */
534 db = get_fallback (display);
535 if (db != NULL)
536 XrmMergeDatabases (db, &rdb);
537
538 /* Get application user defaults */
539 db = get_user_app (myclass);
540 if (db != NULL)
541 XrmMergeDatabases (db, &rdb);
542
543 /* get User defaults */
544 if (user_database != NULL)
545 XrmMergeDatabases (user_database, &rdb);
546
547 /* Get Environment defaults. */
548 db = get_environ_db ();
549 if (db != NULL)
550 XrmMergeDatabases (db, &rdb);
551
552 /* Last, merge in any specification from the command line. */
553 if (xrm_string != NULL)
554 {
555 db = XrmGetStringDatabase (xrm_string);
556 if (db != NULL)
557 XrmMergeDatabases (db, &rdb);
558 }
559
560 return rdb;
561 }
562
563
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. */
566
567 static int
568 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
569 XrmRepresentation expected_type, XrmValue *ret_value)
570 {
571 XrmValue value;
572 XrmName namelist[100];
573 XrmClass classlist[100];
574 XrmRepresentation type;
575
576 XrmStringToNameList (name, namelist);
577 XrmStringToClassList (class, classlist);
578
579 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
580 && (type == expected_type))
581 {
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);
586
587 return value.size;
588 }
589
590 return 0;
591 }
592
593 /* Retrieve the string resource specified by NAME with CLASS from
594 database RDB. */
595
596 char *
597 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
598 {
599 XrmValue value;
600
601 if (inhibit_x_resources)
602 /* --quick was passed, so this is a no-op. */
603 return NULL;
604
605 if (x_get_resource (rdb, name, class, x_rm_string, &value))
606 return (char *) value.addr;
607
608 return (char *) 0;
609 }
610 \f
611 /* Stand-alone test facilities. */
612
613 #ifdef TESTRM
614
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)
621
622 static List
623 member (char *elt, List list)
624 {
625 List p;
626
627 for (p = list; ! NIL (p); p = cdr (p))
628 if (! strcmp (elt, car (p)))
629 return p;
630
631 return p;
632 }
633
634 static void
635 fatal (char *msg, char *prog)
636 {
637 if (errno)
638 perror (prog);
639
640 (void) fprintf (stderr, msg, prog);
641 exit (1);
642 }
643
644 int
645 main (int argc, char **argv)
646 {
647 Display *display;
648 char *displayname, *resource_string, *class, *name;
649 XrmDatabase xdb;
650 List arg_list, lp;
651
652 arg_list = arg_listify (argc, argv);
653
654 lp = member ("-d", arg_list);
655 if (!NIL (lp))
656 displayname = car (cdr (lp));
657 else
658 displayname = "localhost:0.0";
659
660 lp = member ("-xrm", arg_list);
661 if (! NIL (lp))
662 resource_string = car (cdr (lp));
663 else
664 resource_string = (char *) 0;
665
666 lp = member ("-c", arg_list);
667 if (! NIL (lp))
668 class = car (cdr (lp));
669 else
670 class = "Emacs";
671
672 lp = member ("-n", arg_list);
673 if (! NIL (lp))
674 name = car (cdr (lp));
675 else
676 name = "emacs";
677
678 free_arglist (arg_list);
679
680 if (!(display = XOpenDisplay (displayname)))
681 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
682
683 xdb = x_load_resources (display, resource_string, name, class);
684
685 /* In a real program, you'd want to also do this: */
686 display->db = xdb;
687
688 while (1)
689 {
690 char query_name[90];
691 char query_class[90];
692
693 printf ("Name: ");
694 gets (query_name);
695
696 if (strlen (query_name))
697 {
698 char *value;
699
700 printf ("Class: ");
701 gets (query_class);
702
703 value = x_get_string_resource (xdb, query_name, query_class);
704
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");
709 }
710 else
711 break;
712 }
713 printf ("\tExit.\n\n");
714
715 XCloseDisplay (display);
716
717 return 0;
718 }
719 #endif /* TESTRM */