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