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