Try to use "gpg2" if "gpg" isn't available.
[emacs.git] / src / xrdb.c
blob90a85e287bbe11a89225a4c28043853640bcdd94
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Author: Joseph Arceneaux
6 Created: 4/90
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
29 #include <errno.h>
30 #include <epaths.h>
32 #include <stdio.h>
33 #include <setjmp.h>
35 #include <X11/Xlib.h>
36 #include <X11/Xatom.h>
37 #include <X11/X.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
40 #ifdef HAVE_PWD_H
41 #include <pwd.h>
42 #endif
43 #include <sys/stat.h>
45 #if !defined(S_ISDIR) && defined(S_IFDIR)
46 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 #endif
49 #include "lisp.h"
51 #ifdef USE_MOTIF
52 /* For Vdouble_click_time. */
53 #include "keyboard.h"
54 #endif
56 extern char *getenv (const char *);
58 extern struct passwd *getpwuid (uid_t);
59 extern struct passwd *getpwnam (const char *);
61 extern const char *get_system_name (void);
63 char *x_get_string_resource (XrmDatabase rdb, const char *name,
64 const char *class);
65 static int file_p (const char *filename);
68 /* X file search path processing. */
71 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
72 and friends, or zero if none was specified. */
73 char *x_customization_string;
76 /* Return the value of the emacs.customization (Emacs.Customization)
77 resource, for later use in search path decoding. If we find no
78 such resource, return zero. */
79 char *
80 x_get_customization_string (XrmDatabase db, const char *name, const char *class)
82 char *full_name
83 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
84 char *full_class
85 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
86 char *result;
88 sprintf (full_name, "%s.%s", name, "customization");
89 sprintf (full_class, "%s.%s", class, "Customization");
91 result = x_get_string_resource (db, full_name, full_class);
93 if (result)
95 char *copy = (char *) xmalloc (strlen (result) + 1);
96 strcpy (copy, result);
97 return copy;
99 else
100 return 0;
104 /* Expand all the Xt-style %-escapes in STRING, whose length is given
105 by STRING_LEN. Here are the escapes we're supposed to recognize:
107 %N The value of the application's class name
108 %T The value of the type parameter ("app-defaults" in this
109 context)
110 %S The value of the suffix parameter ("" in this context)
111 %L The language string associated with the specified display
112 (We use the "LANG" environment variable here, if it's set.)
113 %l The language part of the display's language string
114 (We treat this just like %L. If someone can tell us what
115 we're really supposed to do, dandy.)
116 %t The territory part of the display's language string
117 (This never gets used.)
118 %c The codeset part of the display's language string
119 (This never gets used either.)
120 %C The customization string retrieved from the resource
121 database associated with display.
122 (This is x_customization_string.)
124 Return the expanded file name if it exists and is readable, and
125 refers to %L only when the LANG environment variable is set, or
126 otherwise provided by X.
128 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
129 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
130 alone.
132 Return NULL otherwise. */
134 static char *
135 magic_file_p (const char *string, EMACS_INT string_len, const char *class, const char *escaped_suffix, const char *suffix)
137 char *lang = getenv ("LANG");
139 int path_size = 100;
140 char *path = (char *) xmalloc (path_size);
141 int path_len = 0;
143 const char *p = string;
145 while (p < string + string_len)
147 /* The chunk we're about to stick on the end of result. */
148 const char *next = NULL;
149 int next_len;
151 if (*p == '%')
153 p++;
155 if (p >= string + string_len)
156 next_len = 0;
157 else
158 switch (*p)
160 case '%':
161 next = "%";
162 next_len = 1;
163 break;
165 case 'C':
166 next = (x_customization_string
167 ? x_customization_string
168 : "");
169 next_len = strlen (next);
170 break;
172 case 'N':
173 next = class;
174 next_len = strlen (class);
175 break;
177 case 'T':
178 next = "app-defaults";
179 next_len = strlen (next);
180 break;
182 default:
183 case 'S':
184 next_len = 0;
185 break;
187 case 'L':
188 case 'l':
189 if (! lang)
191 xfree (path);
192 return NULL;
195 next = lang;
196 next_len = strlen (next);
197 break;
199 case 't':
200 case 'c':
201 xfree (path);
202 return NULL;
205 else
206 next = p, next_len = 1;
208 /* Do we have room for this component followed by a '\0' ? */
209 if (path_len + next_len + 1 > path_size)
211 path_size = (path_len + next_len + 1) * 2;
212 path = (char *) xrealloc (path, path_size);
215 memcpy (path + path_len, next, next_len);
216 path_len += next_len;
218 p++;
220 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
221 if (p >= string + string_len && escaped_suffix)
223 string = escaped_suffix;
224 string_len = strlen (string);
225 p = string;
226 escaped_suffix = NULL;
230 /* Perhaps we should add the SUFFIX now. */
231 if (suffix)
233 int suffix_len = strlen (suffix);
235 if (path_len + suffix_len + 1 > path_size)
237 path_size = (path_len + suffix_len + 1);
238 path = (char *) xrealloc (path, path_size);
241 memcpy (path + path_len, suffix, suffix_len);
242 path_len += suffix_len;
245 path[path_len] = '\0';
247 if (! file_p (path))
249 xfree (path);
250 return NULL;
253 return path;
257 static char *
258 gethomedir (void)
260 struct passwd *pw;
261 char *ptr;
262 char *copy;
264 if ((ptr = getenv ("HOME")) == NULL)
266 if ((ptr = getenv ("LOGNAME")) != NULL
267 || (ptr = getenv ("USER")) != NULL)
268 pw = getpwnam (ptr);
269 else
270 pw = getpwuid (getuid ());
272 if (pw)
273 ptr = pw->pw_dir;
276 if (ptr == NULL)
277 return xstrdup ("/");
279 copy = (char *) xmalloc (strlen (ptr) + 2);
280 strcpy (copy, ptr);
281 strcat (copy, "/");
283 return copy;
287 static int
288 file_p (const char *filename)
290 struct stat status;
292 return (access (filename, 4) == 0 /* exists and is readable */
293 && stat (filename, &status) == 0 /* get the status */
294 && (S_ISDIR (status.st_mode)) == 0); /* not a directory */
298 /* Find the first element of SEARCH_PATH which exists and is readable,
299 after expanding the %-escapes. Return 0 if we didn't find any, and
300 the path name of the one we found otherwise. */
302 static char *
303 search_magic_path (const char *search_path, const char *class, const char *escaped_suffix, const char *suffix)
305 const char *s, *p;
307 for (s = search_path; *s; s = p)
309 for (p = s; *p && *p != ':'; p++)
312 if (p > s)
314 char *path = magic_file_p (s, p - s, class, escaped_suffix,
315 suffix);
316 if (path)
317 return path;
319 else if (*p == ':')
321 char *path;
323 s = "%N%S";
324 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
325 if (path)
326 return path;
329 if (*p == ':')
330 p++;
333 return 0;
336 /* Producing databases for individual sources. */
338 static XrmDatabase
339 get_system_app (const char *class)
341 XrmDatabase db = NULL;
342 const char *path;
343 char *p;
345 path = getenv ("XFILESEARCHPATH");
346 if (! path) path = PATH_X_DEFAULTS;
348 p = search_magic_path (path, class, 0, 0);
349 if (p)
351 db = XrmGetFileDatabase (p);
352 xfree (p);
355 return db;
359 static XrmDatabase
360 get_fallback (Display *display)
362 return NULL;
366 static XrmDatabase
367 get_user_app (const char *class)
369 const char *path;
370 char *file = 0;
371 char *free_it = 0;
373 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
374 names, not directories. */
375 if (((path = getenv ("XUSERFILESEARCHPATH"))
376 && (file = search_magic_path (path, class, 0, 0)))
378 /* Check for APPLRESDIR; it is a path of directories. In each,
379 we have to search for LANG/CLASS and then CLASS. */
380 || ((path = getenv ("XAPPLRESDIR"))
381 && ((file = search_magic_path (path, class, "/%L/%N", 0))
382 || (file = search_magic_path (path, class, "/%N", 0))))
384 /* Check in the home directory. This is a bit of a hack; let's
385 hope one's home directory doesn't contain any %-escapes. */
386 || (free_it = gethomedir (),
387 ((file = search_magic_path (free_it, class, "%L/%N", 0))
388 || (file = search_magic_path (free_it, class, "%N", 0)))))
390 XrmDatabase db = XrmGetFileDatabase (file);
391 xfree (file);
392 xfree (free_it);
393 return db;
396 xfree (free_it);
397 return NULL;
401 static XrmDatabase
402 get_user_db (Display *display)
404 XrmDatabase db;
405 char *xdefs;
407 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
408 xdefs = XResourceManagerString (display);
409 #else
410 xdefs = display->xdefaults;
411 #endif
413 if (xdefs != NULL)
414 db = XrmGetStringDatabase (xdefs);
415 else
417 char *home;
418 char *xdefault;
420 home = gethomedir ();
421 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
422 strcpy (xdefault, home);
423 strcat (xdefault, ".Xdefaults");
424 db = XrmGetFileDatabase (xdefault);
425 xfree (home);
426 xfree (xdefault);
429 #ifdef HAVE_XSCREENRESOURCESTRING
430 /* Get the screen-specific resources too. */
431 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
432 if (xdefs != NULL)
434 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
435 XFree (xdefs);
437 #endif
439 return db;
442 static XrmDatabase
443 get_environ_db (void)
445 XrmDatabase db;
446 char *p;
447 char *path = 0, *home = 0;
448 const char *host;
450 if ((p = getenv ("XENVIRONMENT")) == NULL)
452 home = gethomedir ();
453 host = get_system_name ();
454 path = (char *) xmalloc (strlen (home)
455 + sizeof (".Xdefaults-")
456 + strlen (host));
457 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
458 p = path;
461 db = XrmGetFileDatabase (p);
463 xfree (path);
464 xfree (home);
466 return db;
469 /* External interface. */
471 /* Types of values that we can find in a database */
473 #define XrmStringType "String" /* String representation */
474 XrmRepresentation x_rm_string; /* Quark representation */
476 /* Load X resources based on the display and a possible -xrm option. */
478 XrmDatabase
479 x_load_resources (Display *display, const char *xrm_string,
480 const char *myname, const char *myclass)
482 XrmDatabase user_database;
483 XrmDatabase rdb;
484 XrmDatabase db;
485 char line[256];
487 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
489 #ifdef USE_MOTIF
490 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
491 #endif
493 x_rm_string = XrmStringToQuark (XrmStringType);
494 #ifndef USE_X_TOOLKIT
495 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
496 I suspect it's because the toolkit version does this elsewhere. */
497 XrmInitialize ();
498 #endif
499 rdb = XrmGetStringDatabase ("");
501 /* Add some font defaults. If the font `helv' doesn't exist, widgets
502 will use some other default font. */
503 #ifdef USE_MOTIF
505 sprintf (line, "%s.pane.background: grey75", myclass);
506 XrmPutLineResource (&rdb, line);
507 sprintf (line, "%s*fontList: %s", myclass, helv);
508 XrmPutLineResource (&rdb, line);
509 sprintf (line, "%s*menu*background: grey75", myclass);
510 XrmPutLineResource (&rdb, line);
511 sprintf (line, "%s*menubar*background: grey75", myclass);
512 XrmPutLineResource (&rdb, line);
513 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
514 XrmPutLineResource (&rdb, line);
515 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
516 XrmPutLineResource (&rdb, line);
517 sprintf (line, "%s.dialog*.background: grey75", myclass);
518 XrmPutLineResource (&rdb, line);
519 sprintf (line, "%s*fsb.Text.background: white", myclass);
520 XrmPutLineResource (&rdb, line);
521 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
522 XrmPutLineResource (&rdb, line);
523 sprintf (line, "%s*fsb*DirList.background: white", myclass);
524 XrmPutLineResource (&rdb, line);
525 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
526 XrmPutLineResource (&rdb, line);
527 sprintf (line, "%s*fsb*background: grey75", myclass);
528 XrmPutLineResource (&rdb, line);
529 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
530 XrmPutLineResource (&rdb, line);
531 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
532 XrmPutLineResource (&rdb, line);
533 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
534 XrmPutLineResource (&rdb, line);
535 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
536 XrmPutLineResource (&rdb, line);
538 /* Set double click time of list boxes in the file selection
539 dialog from `double-click-time'. */
540 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
542 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %d",
543 myclass, XFASTINT (Vdouble_click_time));
544 XrmPutLineResource (&rdb, line);
545 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %d",
546 myclass, XFASTINT (Vdouble_click_time));
547 XrmPutLineResource (&rdb, line);
550 #else /* not USE_MOTIF */
552 sprintf (line, "Emacs.dialog*.font: %s", helv);
553 XrmPutLineResource (&rdb, line);
554 sprintf (line, "Emacs.dialog*.background: grey75");
555 XrmPutLineResource (&rdb, line);
556 sprintf (line, "*XlwMenu*font: %s", helv);
557 XrmPutLineResource (&rdb, line);
558 sprintf (line, "*XlwMenu*background: grey75");
559 XrmPutLineResource (&rdb, line);
560 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
561 XrmPutLineResource (&rdb, line);
563 #endif /* not USE_MOTIF */
565 user_database = get_user_db (display);
567 /* Figure out what the "customization string" is, so we can use it
568 to decode paths. */
569 xfree (x_customization_string);
570 x_customization_string
571 = x_get_customization_string (user_database, myname, myclass);
573 /* Get application system defaults */
574 db = get_system_app (myclass);
575 if (db != NULL)
576 XrmMergeDatabases (db, &rdb);
578 /* Get Fallback resources */
579 db = get_fallback (display);
580 if (db != NULL)
581 XrmMergeDatabases (db, &rdb);
583 /* Get application user defaults */
584 db = get_user_app (myclass);
585 if (db != NULL)
586 XrmMergeDatabases (db, &rdb);
588 /* get User defaults */
589 if (user_database != NULL)
590 XrmMergeDatabases (user_database, &rdb);
592 /* Get Environment defaults. */
593 db = get_environ_db ();
594 if (db != NULL)
595 XrmMergeDatabases (db, &rdb);
597 /* Last, merge in any specification from the command line. */
598 if (xrm_string != NULL)
600 db = XrmGetStringDatabase (xrm_string);
601 if (db != NULL)
602 XrmMergeDatabases (db, &rdb);
605 return rdb;
609 /* Retrieve the value of the resource specified by NAME with class CLASS
610 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
613 x_get_resource (XrmDatabase rdb, const char *name, const char *class, 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 */
768 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
769 (do not change this comment) */