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