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