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