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