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