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