Use gnulib's alloca-opt module.
[emacs.git] / src / xrdb.c
blobb490afdabaa8eb7a421c49852c9b410eb53c5d8b
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2011 Free Software Foundation, Inc.
4 Author: Joseph Arceneaux
5 Created: 4/90
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <epaths.h>
28 #include <stdio.h>
29 #include <setjmp.h>
31 #include "lisp.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.h"
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>
47 #ifdef USE_MOTIF
48 /* For Vdouble_click_time. */
49 #include "keyboard.h"
50 #endif
52 extern char *getenv (const char *);
54 extern struct passwd *getpwuid (uid_t);
55 extern struct passwd *getpwnam (const char *);
57 char *x_get_string_resource (XrmDatabase rdb, const char *name,
58 const char *class);
59 static int file_p (const char *filename);
62 /* X file search path processing. */
65 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
66 and friends, or zero if none was specified. */
67 static char *x_customization_string;
70 /* Return the value of the emacs.customization (Emacs.Customization)
71 resource, for later use in search path decoding. If we find no
72 such resource, return zero. */
73 static char *
74 x_get_customization_string (XrmDatabase db, const char *name,
75 const char *class)
77 char *full_name
78 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
79 char *full_class
80 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
81 char *result;
83 sprintf (full_name, "%s.%s", name, "customization");
84 sprintf (full_class, "%s.%s", class, "Customization");
86 result = x_get_string_resource (db, full_name, full_class);
88 if (result)
90 char *copy = (char *) xmalloc (strlen (result) + 1);
91 strcpy (copy, result);
92 return copy;
94 else
95 return 0;
99 /* Expand all the Xt-style %-escapes in STRING, whose length is given
100 by STRING_LEN. Here are the escapes we're supposed to recognize:
102 %N The value of the application's class name
103 %T The value of the type parameter ("app-defaults" in this
104 context)
105 %S The value of the suffix parameter ("" in this context)
106 %L The language string associated with the specified display
107 (We use the "LANG" environment variable here, if it's set.)
108 %l The language part of the display's language string
109 (We treat this just like %L. If someone can tell us what
110 we're really supposed to do, dandy.)
111 %t The territory part of the display's language string
112 (This never gets used.)
113 %c The codeset part of the display's language string
114 (This never gets used either.)
115 %C The customization string retrieved from the resource
116 database associated with display.
117 (This is x_customization_string.)
119 Return the expanded file name if it exists and is readable, and
120 refers to %L only when the LANG environment variable is set, or
121 otherwise provided by X.
123 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
124 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
125 alone.
127 Return NULL otherwise. */
129 static char *
130 magic_file_p (const char *string, EMACS_INT string_len, const char *class, const char *escaped_suffix, const char *suffix)
132 char *lang = getenv ("LANG");
134 int path_size = 100;
135 char *path = (char *) xmalloc (path_size);
136 int path_len = 0;
138 const char *p = string;
140 while (p < string + string_len)
142 /* The chunk we're about to stick on the end of result. */
143 const char *next = NULL;
144 int next_len;
146 if (*p == '%')
148 p++;
150 if (p >= string + string_len)
151 next_len = 0;
152 else
153 switch (*p)
155 case '%':
156 next = "%";
157 next_len = 1;
158 break;
160 case 'C':
161 next = (x_customization_string
162 ? x_customization_string
163 : "");
164 next_len = strlen (next);
165 break;
167 case 'N':
168 next = class;
169 next_len = strlen (class);
170 break;
172 case 'T':
173 next = "app-defaults";
174 next_len = strlen (next);
175 break;
177 default:
178 case 'S':
179 next_len = 0;
180 break;
182 case 'L':
183 case 'l':
184 if (! lang)
186 xfree (path);
187 return NULL;
190 next = lang;
191 next_len = strlen (next);
192 break;
194 case 't':
195 case 'c':
196 xfree (path);
197 return NULL;
200 else
201 next = p, next_len = 1;
203 /* Do we have room for this component followed by a '\0' ? */
204 if (path_len + next_len + 1 > path_size)
206 path_size = (path_len + next_len + 1) * 2;
207 path = (char *) xrealloc (path, path_size);
210 memcpy (path + path_len, next, next_len);
211 path_len += next_len;
213 p++;
215 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
216 if (p >= string + string_len && escaped_suffix)
218 string = escaped_suffix;
219 string_len = strlen (string);
220 p = string;
221 escaped_suffix = NULL;
225 /* Perhaps we should add the SUFFIX now. */
226 if (suffix)
228 int suffix_len = strlen (suffix);
230 if (path_len + suffix_len + 1 > path_size)
232 path_size = (path_len + suffix_len + 1);
233 path = (char *) xrealloc (path, path_size);
236 memcpy (path + path_len, suffix, suffix_len);
237 path_len += suffix_len;
240 path[path_len] = '\0';
242 if (! file_p (path))
244 xfree (path);
245 return NULL;
248 return path;
252 static char *
253 gethomedir (void)
255 struct passwd *pw;
256 char *ptr;
257 char *copy;
259 if ((ptr = getenv ("HOME")) == NULL)
261 if ((ptr = getenv ("LOGNAME")) != NULL
262 || (ptr = getenv ("USER")) != NULL)
263 pw = getpwnam (ptr);
264 else
265 pw = getpwuid (getuid ());
267 if (pw)
268 ptr = pw->pw_dir;
271 if (ptr == NULL)
272 return xstrdup ("/");
274 copy = (char *) xmalloc (strlen (ptr) + 2);
275 strcpy (copy, ptr);
276 strcat (copy, "/");
278 return copy;
282 static int
283 file_p (const char *filename)
285 struct stat status;
287 return (access (filename, 4) == 0 /* exists and is readable */
288 && stat (filename, &status) == 0 /* get the status */
289 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
293 /* Find the first element of SEARCH_PATH which exists and is readable,
294 after expanding the %-escapes. Return 0 if we didn't find any, and
295 the path name of the one we found otherwise. */
297 static char *
298 search_magic_path (const char *search_path, const char *class, const char *escaped_suffix, const char *suffix)
300 const char *s, *p;
302 for (s = search_path; *s; s = p)
304 for (p = s; *p && *p != ':'; p++)
307 if (p > s)
309 char *path = magic_file_p (s, p - s, class, escaped_suffix,
310 suffix);
311 if (path)
312 return path;
314 else if (*p == ':')
316 char *path;
318 s = "%N%S";
319 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
320 if (path)
321 return path;
324 if (*p == ':')
325 p++;
328 return 0;
331 /* Producing databases for individual sources. */
333 static XrmDatabase
334 get_system_app (const char *class)
336 XrmDatabase db = NULL;
337 const char *path;
338 char *p;
340 path = getenv ("XFILESEARCHPATH");
341 if (! path) path = PATH_X_DEFAULTS;
343 p = search_magic_path (path, class, 0, 0);
344 if (p)
346 db = XrmGetFileDatabase (p);
347 xfree (p);
350 return db;
354 static XrmDatabase
355 get_fallback (Display *display)
357 return NULL;
361 static XrmDatabase
362 get_user_app (const char *class)
364 const char *path;
365 char *file = 0;
366 char *free_it = 0;
368 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
369 names, not directories. */
370 if (((path = getenv ("XUSERFILESEARCHPATH"))
371 && (file = search_magic_path (path, class, 0, 0)))
373 /* Check for APPLRESDIR; it is a path of directories. In each,
374 we have to search for LANG/CLASS and then CLASS. */
375 || ((path = getenv ("XAPPLRESDIR"))
376 && ((file = search_magic_path (path, class, "/%L/%N", 0))
377 || (file = search_magic_path (path, class, "/%N", 0))))
379 /* Check in the home directory. This is a bit of a hack; let's
380 hope one's home directory doesn't contain any %-escapes. */
381 || (free_it = gethomedir (),
382 ((file = search_magic_path (free_it, class, "%L/%N", 0))
383 || (file = search_magic_path (free_it, class, "%N", 0)))))
385 XrmDatabase db = XrmGetFileDatabase (file);
386 xfree (file);
387 xfree (free_it);
388 return db;
391 xfree (free_it);
392 return NULL;
396 static XrmDatabase
397 get_user_db (Display *display)
399 XrmDatabase db;
400 char *xdefs;
402 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
403 xdefs = XResourceManagerString (display);
404 #else
405 xdefs = display->xdefaults;
406 #endif
408 if (xdefs != NULL)
409 db = XrmGetStringDatabase (xdefs);
410 else
412 char *home;
413 char *xdefault;
415 home = gethomedir ();
416 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
417 strcpy (xdefault, home);
418 strcat (xdefault, ".Xdefaults");
419 db = XrmGetFileDatabase (xdefault);
420 xfree (home);
421 xfree (xdefault);
424 #ifdef HAVE_XSCREENRESOURCESTRING
425 /* Get the screen-specific resources too. */
426 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
427 if (xdefs != NULL)
429 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
430 XFree (xdefs);
432 #endif
434 return db;
437 static XrmDatabase
438 get_environ_db (void)
440 XrmDatabase db;
441 char *p;
442 char *path = 0, *home = 0;
443 const char *host;
445 if ((p = getenv ("XENVIRONMENT")) == NULL)
447 home = gethomedir ();
448 host = get_system_name ();
449 path = (char *) xmalloc (strlen (home)
450 + sizeof (".Xdefaults-")
451 + strlen (host));
452 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
453 p = path;
456 db = XrmGetFileDatabase (p);
458 xfree (path);
459 xfree (home);
461 return db;
464 /* External interface. */
466 /* Types of values that we can find in a database */
468 #define XrmStringType "String" /* String representation */
469 static XrmRepresentation x_rm_string; /* Quark representation */
471 /* Load X resources based on the display and a possible -xrm option. */
473 XrmDatabase
474 x_load_resources (Display *display, const char *xrm_string,
475 const char *myname, const char *myclass)
477 XrmDatabase user_database;
478 XrmDatabase rdb;
479 XrmDatabase db;
480 char line[256];
482 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
483 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
484 #endif
486 #ifdef USE_MOTIF
487 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
488 #endif
490 x_rm_string = XrmStringToQuark (XrmStringType);
491 #ifndef USE_X_TOOLKIT
492 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
493 I suspect it's because the toolkit version does this elsewhere. */
494 XrmInitialize ();
495 #endif
496 rdb = XrmGetStringDatabase ("");
498 /* Add some font defaults. If the font `helv' doesn't exist, widgets
499 will use some other default font. */
500 #ifdef USE_MOTIF
502 sprintf (line, "%s.pane.background: grey75", myclass);
503 XrmPutLineResource (&rdb, line);
504 sprintf (line, "%s*fontList: %s", myclass, helv);
505 XrmPutLineResource (&rdb, line);
506 sprintf (line, "%s*menu*background: grey75", myclass);
507 XrmPutLineResource (&rdb, line);
508 sprintf (line, "%s*menubar*background: grey75", myclass);
509 XrmPutLineResource (&rdb, line);
510 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
511 XrmPutLineResource (&rdb, line);
512 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
513 XrmPutLineResource (&rdb, line);
514 sprintf (line, "%s.dialog*.background: grey75", myclass);
515 XrmPutLineResource (&rdb, line);
516 sprintf (line, "%s*fsb.Text.background: white", myclass);
517 XrmPutLineResource (&rdb, line);
518 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
519 XrmPutLineResource (&rdb, line);
520 sprintf (line, "%s*fsb*DirList.background: white", myclass);
521 XrmPutLineResource (&rdb, line);
522 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
523 XrmPutLineResource (&rdb, line);
524 sprintf (line, "%s*fsb*background: grey75", myclass);
525 XrmPutLineResource (&rdb, line);
526 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
527 XrmPutLineResource (&rdb, line);
528 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
529 XrmPutLineResource (&rdb, line);
530 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
531 XrmPutLineResource (&rdb, line);
532 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
533 XrmPutLineResource (&rdb, line);
535 /* Set double click time of list boxes in the file selection
536 dialog from `double-click-time'. */
537 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
539 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
540 myclass, XFASTINT (Vdouble_click_time));
541 XrmPutLineResource (&rdb, line);
542 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
543 myclass, XFASTINT (Vdouble_click_time));
544 XrmPutLineResource (&rdb, line);
547 #else /* not USE_MOTIF */
549 sprintf (line, "Emacs.dialog*.background: grey75");
550 XrmPutLineResource (&rdb, line);
551 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
552 sprintf (line, "Emacs.dialog*.font: %s", helv);
553 XrmPutLineResource (&rdb, line);
554 sprintf (line, "*XlwMenu*font: %s", helv);
555 XrmPutLineResource (&rdb, line);
556 #endif
557 sprintf (line, "*XlwMenu*background: grey75");
558 XrmPutLineResource (&rdb, line);
559 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
560 XrmPutLineResource (&rdb, line);
562 #endif /* not USE_MOTIF */
564 user_database = get_user_db (display);
566 /* Figure out what the "customization string" is, so we can use it
567 to decode paths. */
568 xfree (x_customization_string);
569 x_customization_string
570 = x_get_customization_string (user_database, myname, myclass);
572 /* Get application system defaults */
573 db = get_system_app (myclass);
574 if (db != NULL)
575 XrmMergeDatabases (db, &rdb);
577 /* Get Fallback resources */
578 db = get_fallback (display);
579 if (db != NULL)
580 XrmMergeDatabases (db, &rdb);
582 /* Get application user defaults */
583 db = get_user_app (myclass);
584 if (db != NULL)
585 XrmMergeDatabases (db, &rdb);
587 /* get User defaults */
588 if (user_database != NULL)
589 XrmMergeDatabases (user_database, &rdb);
591 /* Get Environment defaults. */
592 db = get_environ_db ();
593 if (db != NULL)
594 XrmMergeDatabases (db, &rdb);
596 /* Last, merge in any specification from the command line. */
597 if (xrm_string != NULL)
599 db = XrmGetStringDatabase (xrm_string);
600 if (db != NULL)
601 XrmMergeDatabases (db, &rdb);
604 return rdb;
608 /* Retrieve the value of the resource specified by NAME with class CLASS
609 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
611 static int
612 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
613 XrmRepresentation expected_type, XrmValue *ret_value)
615 XrmValue value;
616 XrmName namelist[100];
617 XrmClass classlist[100];
618 XrmRepresentation type;
620 XrmStringToNameList(name, namelist);
621 XrmStringToClassList(class, classlist);
623 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
624 && (type == expected_type))
626 if (type == x_rm_string)
627 ret_value->addr = (char *) value.addr;
628 else
629 memcpy (ret_value->addr, value.addr, ret_value->size);
631 return value.size;
634 return 0;
637 /* Retrieve the string resource specified by NAME with CLASS from
638 database RDB. */
640 char *
641 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
643 XrmValue value;
645 if (inhibit_x_resources)
646 /* --quick was passed, so this is a no-op. */
647 return NULL;
649 if (x_get_resource (rdb, name, class, x_rm_string, &value))
650 return (char *) value.addr;
652 return (char *) 0;
655 /* Stand-alone test facilities. */
657 #ifdef TESTRM
659 typedef char **List;
660 #define arg_listify(len, list) (list)
661 #define car(list) (*(list))
662 #define cdr(list) (list + 1)
663 #define NIL(list) (! *(list))
664 #define free_arglist(list)
666 static List
667 member (elt, list)
668 char *elt;
669 List list;
671 List p;
673 for (p = list; ! NIL (p); p = cdr (p))
674 if (! strcmp (elt, car (p)))
675 return p;
677 return p;
680 static void
681 fatal (msg, prog, x1, x2, x3, x4, x5)
682 char *msg, *prog;
683 int x1, x2, x3, x4, x5;
685 if (errno)
686 perror (prog);
688 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
689 exit (1);
692 main (argc, argv)
693 int argc;
694 char **argv;
696 Display *display;
697 char *displayname, *resource_string, *class, *name;
698 XrmDatabase xdb;
699 List arg_list, lp;
701 arg_list = arg_listify (argc, argv);
703 lp = member ("-d", arg_list);
704 if (!NIL (lp))
705 displayname = car (cdr (lp));
706 else
707 displayname = "localhost:0.0";
709 lp = member ("-xrm", arg_list);
710 if (! NIL (lp))
711 resource_string = car (cdr (lp));
712 else
713 resource_string = (char *) 0;
715 lp = member ("-c", arg_list);
716 if (! NIL (lp))
717 class = car (cdr (lp));
718 else
719 class = "Emacs";
721 lp = member ("-n", arg_list);
722 if (! NIL (lp))
723 name = car (cdr (lp));
724 else
725 name = "emacs";
727 free_arglist (arg_list);
729 if (!(display = XOpenDisplay (displayname)))
730 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
732 xdb = x_load_resources (display, resource_string, name, class);
734 /* In a real program, you'd want to also do this: */
735 display->db = xdb;
737 while (1)
739 char query_name[90];
740 char query_class[90];
742 printf ("Name: ");
743 gets (query_name);
745 if (strlen (query_name))
747 char *value;
749 printf ("Class: ");
750 gets (query_class);
752 value = x_get_string_resource (xdb, query_name, query_class);
754 if (value != NULL)
755 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
756 else
757 printf ("\tNo Value.\n\n");
759 else
760 break;
762 printf ("\tExit.\n\n");
764 XCloseDisplay (display);
766 #endif /* TESTRM */