(vendor-key-syms): Set this variable.
[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 extern char *getenv ();
59
60 /* This does cause trouble on AIX. I'm going to take the comment at
61 face value. */
62 #if 0
63 extern short getuid (); /* If this causes portability problems,
64 I think we should just delete it; it'll
65 default to `int' anyway. */
66 #endif
67
68 #if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
69 extern struct passwd *getpwuid (uid_t);
70 extern struct passwd *getpwnam (const char *);
71 #else
72 extern struct passwd *getpwuid ();
73 extern struct passwd *getpwnam ();
74 #endif
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 int uid;
286 struct passwd *pw;
287 char *ptr;
288 char *copy;
289
290 if ((ptr = getenv ("HOME")) == NULL)
291 {
292 if ((ptr = getenv ("USER")) != NULL)
293 pw = getpwnam (ptr);
294 else
295 {
296 uid = getuid ();
297 pw = getpwuid (uid);
298 }
299
300 if (pw)
301 ptr = pw->pw_dir;
302 }
303
304 if (ptr == NULL)
305 return "/";
306
307 copy = (char *) malloc (strlen (ptr) + 2);
308 strcpy (copy, ptr);
309 strcat (copy, "/");
310
311 return copy;
312 }
313
314
315 static int
316 file_p (path)
317 char *path;
318 {
319 struct stat status;
320
321 return (access (path, 4) == 0 /* exists and is readable */
322 && stat (path, &status) == 0 /* get the status */
323 && (status.st_mode & S_IFDIR) == 0); /* not a directory */
324 }
325
326
327 /* Find the first element of SEARCH_PATH which exists and is readable,
328 after expanding the %-escapes. Return 0 if we didn't find any, and
329 the path name of the one we found otherwise. */
330
331 static char *
332 search_magic_path (search_path, class, escaped_suffix, suffix)
333 char *search_path, *class, *escaped_suffix, *suffix;
334 {
335 register char *s, *p;
336
337 for (s = search_path; *s; s = p)
338 {
339 for (p = s; *p && *p != ':'; p++)
340 ;
341
342 if (*p == ':' && *(p + 1) == ':')
343 {
344 char *path;
345
346 s = "%N%S";
347 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
348 if (path)
349 return path;
350
351 /* Skip the first colon. */
352 p++;
353 continue;
354 }
355
356 if (p > s)
357 {
358 char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
359 if (path)
360 return path;
361 }
362
363 if (*p == ':')
364 p++;
365 }
366
367 return 0;
368 }
369 \f
370 /* Producing databases for individual sources. */
371
372 #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"
373
374 static XrmDatabase
375 get_system_app (class)
376 char *class;
377 {
378 XrmDatabase db = NULL;
379 char *path;
380
381 path = getenv ("XFILESEARCHPATH");
382 if (! path) path = X_DEFAULT_SEARCH_PATH;
383
384 path = search_magic_path (path, class, 0, 0);
385 if (path)
386 {
387 db = XrmGetFileDatabase (path);
388 free (path);
389 }
390
391 return db;
392 }
393
394
395 static XrmDatabase
396 get_fallback (display)
397 Display *display;
398 {
399 XrmDatabase db;
400
401 return NULL;
402 }
403
404
405 static XrmDatabase
406 get_user_app (class)
407 char *class;
408 {
409 char *path;
410 char *file = 0;
411
412 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
413 names, not directories. */
414 if (((path = getenv ("XUSERFILESEARCHPATH"))
415 && (file = search_magic_path (path, class, 0, 0)))
416
417 /* Check for APPLRESDIR; it is a path of directories. In each,
418 we have to search for LANG/CLASS and then CLASS. */
419 || ((path = getenv ("XAPPLRESDIR"))
420 && ((file = search_magic_path (path, class, "/%L/%N", 0))
421 || (file = search_magic_path (path, class, "/%N", 0))))
422
423 /* Check in the home directory. This is a bit of a hack; let's
424 hope one's home directory doesn't contain any %-escapes. */
425 || (path = gethomedir (),
426 ((file = search_magic_path (path, class, "%L/%N", 0))
427 || (file = search_magic_path (path, class, "%N", 0)))))
428 {
429 XrmDatabase db = XrmGetFileDatabase (file);
430 free (file);
431 return db;
432 }
433 else
434 return NULL;
435 }
436
437
438 static XrmDatabase
439 get_user_db (display)
440 Display *display;
441 {
442 XrmDatabase db;
443 char *xdefs;
444
445 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
446 xdefs = XResourceManagerString (display);
447 #else
448 xdefs = display->xdefaults;
449 #endif
450
451 if (xdefs != NULL)
452 db = XrmGetStringDatabase (xdefs);
453 else
454 {
455 char *home;
456 char *xdefault;
457
458 home = gethomedir ();
459 xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
460 strcpy (xdefault, home);
461 strcat (xdefault, ".Xdefaults");
462 db = XrmGetFileDatabase (xdefault);
463 free (home);
464 free (xdefault);
465 }
466
467 #ifdef HAVE_XSCREENRESOURCESTRING
468 /* Get the screen-specific resources too. */
469 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
470 if (xdefs != NULL)
471 {
472 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
473 XFree (xdefs);
474 }
475 #endif
476
477 return db;
478 }
479
480 static XrmDatabase
481 get_environ_db ()
482 {
483 XrmDatabase db;
484 char *p;
485 char *path = 0, *home = 0, *host = 0;
486
487 if ((p = getenv ("XENVIRONMENT")) == NULL)
488 {
489 home = gethomedir ();
490
491 {
492 int host_size = 100;
493 host = (char *) malloc (host_size);
494
495 for (;;)
496 {
497 host[host_size - 1] = '\0';
498 gethostname (host, host_size - 1);
499 if (strlen (host) < host_size - 1)
500 break;
501 host = (char *) realloc (host, host_size *= 2);
502 }
503 }
504
505 path = (char *) malloc (strlen (home)
506 + sizeof (".Xdefaults-")
507 + strlen (host));
508 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
509 p = path;
510 }
511
512 db = XrmGetFileDatabase (p);
513
514 if (path) free (path);
515 if (home) free (home);
516 if (host) free (host);
517
518 return db;
519 }
520 \f
521 /* External interface. */
522
523 /* Types of values that we can find in a database */
524
525 #define XrmStringType "String" /* String representation */
526 XrmRepresentation x_rm_string; /* Quark representation */
527
528 /* Load X resources based on the display and a possible -xrm option. */
529
530 XrmDatabase
531 x_load_resources (display, xrm_string, myname, myclass)
532 Display *display;
533 char *xrm_string, *myname, *myclass;
534 {
535 char *xdefs;
536 XrmDatabase user_database;
537 XrmDatabase rdb;
538 XrmDatabase db;
539
540 x_rm_string = XrmStringToQuark (XrmStringType);
541 XrmInitialize ();
542 rdb = XrmGetStringDatabase ("");
543
544 user_database = get_user_db (display);
545
546 /* Figure out what the "customization string" is, so we can use it
547 to decode paths. */
548 if (x_customization_string)
549 free (x_customization_string);
550 x_customization_string
551 = x_get_customization_string (user_database, myname, myclass);
552
553 /* Get application system defaults */
554 db = get_system_app (myclass);
555 if (db != NULL)
556 XrmMergeDatabases (db, &rdb);
557
558 /* Get Fallback resources */
559 db = get_fallback (display);
560 if (db != NULL)
561 XrmMergeDatabases (db, &rdb);
562
563 /* Get application user defaults */
564 db = get_user_app (myclass);
565 if (db != NULL)
566 XrmMergeDatabases (db, &rdb);
567
568 /* get User defaults */
569 if (user_database != NULL)
570 XrmMergeDatabases (user_database, &rdb);
571
572 /* Get Environment defaults. */
573 db = get_environ_db ();
574 if (db != NULL)
575 XrmMergeDatabases (db, &rdb);
576
577 /* Last, merge in any specification from the command line. */
578 if (xrm_string != NULL)
579 {
580 db = XrmGetStringDatabase (xrm_string);
581 if (db != NULL)
582 XrmMergeDatabases (db, &rdb);
583 }
584
585 return rdb;
586 }
587
588
589 /* Retrieve the value of the resource specified by NAME with class CLASS
590 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
591
592 int
593 x_get_resource (rdb, name, class, expected_type, ret_value)
594 XrmDatabase rdb;
595 char *name, *class;
596 XrmRepresentation expected_type;
597 XrmValue *ret_value;
598 {
599 XrmValue value;
600 XrmName namelist[100];
601 XrmClass classlist[100];
602 XrmRepresentation type;
603
604 XrmStringToNameList(name, namelist);
605 XrmStringToClassList(class, classlist);
606
607 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
608 && (type == expected_type))
609 {
610 if (type == x_rm_string)
611 ret_value->addr = (char *) value.addr;
612 else
613 bcopy (value.addr, ret_value->addr, ret_value->size);
614
615 return value.size;
616 }
617
618 return 0;
619 }
620
621 /* Retrieve the string resource specified by NAME with CLASS from
622 database RDB. */
623
624 char *
625 x_get_string_resource (rdb, name, class)
626 XrmDatabase rdb;
627 char *name, *class;
628 {
629 XrmValue value;
630
631 if (x_get_resource (rdb, name, class, x_rm_string, &value))
632 return (char *) value.addr;
633
634 return (char *) 0;
635 }
636 \f
637 /* Stand-alone test facilities. */
638
639 #ifdef TESTRM
640
641 typedef char **List;
642 #define arg_listify(len, list) (list)
643 #define car(list) (*(list))
644 #define cdr(list) (list + 1)
645 #define NIL(list) (! *(list))
646 #define free_arglist(list)
647
648 static List
649 member (elt, list)
650 char *elt;
651 List list;
652 {
653 List p;
654
655 for (p = list; ! NIL (p); p = cdr (p))
656 if (! strcmp (elt, car (p)))
657 return p;
658
659 return p;
660 }
661
662 static void
663 fatal (msg, prog, x1, x2, x3, x4, x5)
664 char *msg, *prog;
665 int x1, x2, x3, x4, x5;
666 {
667 extern int errno;
668
669 if (errno)
670 perror (prog);
671
672 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
673 exit (1);
674 }
675
676 main (argc, argv)
677 int argc;
678 char **argv;
679 {
680 Display *display;
681 char *displayname, *resource_string, *class, *name;
682 XrmDatabase xdb;
683 List arg_list, lp;
684
685 arg_list = arg_listify (argc, argv);
686
687 lp = member ("-d", arg_list);
688 if (!NIL (lp))
689 displayname = car (cdr (lp));
690 else
691 displayname = "localhost:0.0";
692
693 lp = member ("-xrm", arg_list);
694 if (! NIL (lp))
695 resource_string = car (cdr (lp));
696 else
697 resource_string = (char *) 0;
698
699 lp = member ("-c", arg_list);
700 if (! NIL (lp))
701 class = car (cdr (lp));
702 else
703 class = "Emacs";
704
705 lp = member ("-n", arg_list);
706 if (! NIL (lp))
707 name = car (cdr (lp));
708 else
709 name = "emacs";
710
711 free_arglist (arg_list);
712
713 if (!(display = XOpenDisplay (displayname)))
714 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
715
716 xdb = x_load_resources (display, resource_string, name, class);
717
718 /* In a real program, you'd want to also do this: */
719 display->db = xdb;
720
721 while (1)
722 {
723 char query_name[90];
724 char query_class[90];
725
726 printf ("Name: ");
727 gets (query_name);
728
729 if (strlen (query_name))
730 {
731 char *value;
732
733 printf ("Class: ");
734 gets (query_class);
735
736 value = x_get_string_resource (xdb, query_name, query_class);
737
738 if (value != NULL)
739 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
740 else
741 printf ("\tNo Value.\n\n");
742 }
743 else
744 break;
745 }
746 printf ("\tExit.\n\n");
747
748 XCloseDisplay (display);
749 }
750 #endif /* TESTRM */