* src/syntax.c (find_defun_start): Use syntax-ppss
[emacs.git] / src / xrdb.c
blob3c1bad1c7354a3f1340a2fcb158d3c890c103cc4
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2017 Free Software Foundation,
3 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 (at
13 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 <https://www.gnu.org/licenses/>. */
23 #include <config.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <epaths.h>
28 #include <stdlib.h>
29 #include <stdio.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
46 /* X file search path processing. */
49 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
50 and friends, or zero if none was specified. */
51 static char *x_customization_string;
54 /* Return the value of the emacs.customization (Emacs.Customization)
55 resource, for later use in search path decoding. If we find no
56 such resource, return zero. */
57 static char *
58 x_get_customization_string (XrmDatabase db, const char *name,
59 const char *class)
61 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
62 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
63 char *result;
65 sprintf (full_name, "%s.%s", name, "customization");
66 sprintf (full_class, "%s.%s", class, "Customization");
68 result = x_get_string_resource (db, full_name, full_class);
69 return result ? xstrdup (result) : NULL;
72 /* Expand all the Xt-style %-escapes in STRING, whose length is given
73 by STRING_LEN. Here are the escapes we're supposed to recognize:
75 %N The value of the application's class name
76 %T The value of the type parameter ("app-defaults" in this
77 context)
78 %S The value of the suffix parameter ("" in this context)
79 %L The language string associated with the specified display
80 (We use the "LANG" environment variable here, if it's set.)
81 %l The language part of the display's language string
82 (We treat this just like %L. If someone can tell us what
83 we're really supposed to do, dandy.)
84 %t The territory part of the display's language string
85 (This never gets used.)
86 %c The codeset part of the display's language string
87 (This never gets used either.)
88 %C The customization string retrieved from the resource
89 database associated with display.
90 (This is x_customization_string.)
92 Return the resource database if its file was read successfully, and
93 refers to %L only when the LANG environment variable is set, or
94 otherwise provided by X.
96 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
97 %-escapes in ESCAPED_SUFFIX are expanded.
99 Return NULL otherwise. */
101 static XrmDatabase
102 magic_db (const char *string, ptrdiff_t string_len, const char *class,
103 const char *escaped_suffix)
105 XrmDatabase db;
106 char *lang = getenv ("LANG");
108 ptrdiff_t path_size = 100;
109 char *path = xmalloc (path_size);
110 ptrdiff_t path_len = 0;
112 const char *p = string;
114 while (p < string + string_len)
116 /* The chunk we're about to stick on the end of result. */
117 const char *next = p;
118 ptrdiff_t next_len = 1;
120 if (*p == '%')
122 p++;
124 if (p >= string + string_len)
125 next_len = 0;
126 else
127 switch (*p)
129 case '%':
130 next = "%";
131 next_len = 1;
132 break;
134 case 'C':
135 if (x_customization_string)
137 next = x_customization_string;
138 next_len = strlen (next);
140 else
141 next_len = 0;
142 break;
144 case 'N':
145 next = class;
146 next_len = strlen (class);
147 break;
149 case 'T':
150 next = "app-defaults";
151 next_len = strlen (next);
152 break;
154 default:
155 case 'S':
156 next_len = 0;
157 break;
159 case 'L':
160 case 'l':
161 if (! lang)
163 xfree (path);
164 return NULL;
167 next = lang;
168 next_len = strlen (next);
169 break;
171 case 't':
172 case 'c':
173 xfree (path);
174 return NULL;
178 /* Do we have room for this component followed by a '\0'? */
179 if (path_size - path_len <= next_len)
180 path = xpalloc (path, &path_size, path_len - path_size + next_len + 1,
181 -1, sizeof *path);
183 memcpy (path + path_len, next, next_len);
184 path_len += next_len;
186 p++;
188 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
189 if (p >= string + string_len && escaped_suffix)
191 string = escaped_suffix;
192 string_len = strlen (string);
193 p = string;
194 escaped_suffix = NULL;
198 path[path_len] = '\0';
199 db = XrmGetFileDatabase (path);
200 xfree (path);
201 return db;
205 static char *
206 gethomedir (void)
208 struct passwd *pw;
209 char *ptr;
210 char *copy;
212 if ((ptr = getenv ("HOME")) == NULL)
214 if ((ptr = getenv ("LOGNAME")) != NULL
215 || (ptr = getenv ("USER")) != NULL)
216 pw = getpwnam (ptr);
217 else
218 pw = getpwuid (getuid ());
220 if (pw)
221 ptr = pw->pw_dir;
224 if (ptr == NULL)
225 return xstrdup ("/");
227 ptrdiff_t len = strlen (ptr);
228 copy = xmalloc (len + 2);
229 strcpy (copy + len, "/");
230 return memcpy (copy, ptr, len);
234 /* Find the first element of SEARCH_PATH which exists and is readable,
235 after expanding the %-escapes. Return 0 if we didn't find any, and
236 the path name of the one we found otherwise. */
238 static XrmDatabase
239 search_magic_path (const char *search_path, const char *class,
240 const char *escaped_suffix)
242 const char *s, *p;
244 for (s = search_path; *s; s = p)
246 for (p = s; *p && *p != ':'; p++)
249 if (p > s)
251 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
252 if (db)
253 return db;
255 else if (*p == ':')
257 static char const ns[] = "%N%S";
258 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
259 if (db)
260 return db;
263 if (*p == ':')
264 p++;
267 return 0;
270 /* Producing databases for individual sources. */
272 static XrmDatabase
273 get_system_app (const char *class)
275 const char *path;
277 path = getenv ("XFILESEARCHPATH");
278 if (! path) path = PATH_X_DEFAULTS;
280 return search_magic_path (path, class, 0);
284 static XrmDatabase
285 get_fallback (Display *display)
287 return NULL;
291 static XrmDatabase
292 get_user_app (const char *class)
294 XrmDatabase db = 0;
295 const char *path;
297 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
298 names, not directories. */
299 path = getenv ("XUSERFILESEARCHPATH");
300 if (path)
301 db = search_magic_path (path, class, 0);
303 if (! db)
305 /* Check for APPLRESDIR; it is a path of directories. In each,
306 we have to search for LANG/CLASS and then CLASS. */
307 path = getenv ("XAPPLRESDIR");
308 if (path)
310 db = search_magic_path (path, class, "/%L/%N");
311 if (!db)
312 db = search_magic_path (path, class, "/%N");
316 if (! db)
318 /* Check in the home directory. This is a bit of a hack; let's
319 hope one's home directory doesn't contain any %-escapes. */
320 char *home = gethomedir ();
321 db = search_magic_path (home, class, "%L/%N");
322 if (! db)
323 db = search_magic_path (home, class, "%N");
324 xfree (home);
327 return db;
330 static char const xdefaults[] = ".Xdefaults";
332 static XrmDatabase
333 get_user_db (Display *display)
335 XrmDatabase db;
336 char *xdefs;
338 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
339 xdefs = XResourceManagerString (display);
340 #else
341 xdefs = display->xdefaults;
342 #endif
344 if (xdefs != NULL)
345 db = XrmGetStringDatabase (xdefs);
346 else
348 /* Use ~/.Xdefaults. */
349 char *home = gethomedir ();
350 ptrdiff_t homelen = strlen (home);
351 char *filename = xrealloc (home, homelen + sizeof xdefaults);
352 strcpy (filename + homelen, xdefaults);
353 db = XrmGetFileDatabase (filename);
354 xfree (filename);
357 #ifdef HAVE_XSCREENRESOURCESTRING
358 /* Get the screen-specific resources too. */
359 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
360 if (xdefs != NULL)
362 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
363 XFree (xdefs);
365 #endif
367 return db;
370 static XrmDatabase
371 get_environ_db (void)
373 XrmDatabase db;
374 char *p = getenv ("XENVIRONMENT");
375 char *filename = 0;
377 if (!p)
379 /* Use ~/.Xdefaults-HOSTNAME. */
380 char *home = gethomedir ();
381 ptrdiff_t homelen = strlen (home);
382 Lisp_Object system_name = Fsystem_name ();
383 ptrdiff_t filenamesize = (homelen + sizeof xdefaults
384 + 1 + SBYTES (system_name));
385 p = filename = xrealloc (home, filenamesize);
386 lispstpcpy (stpcpy (stpcpy (filename + homelen, xdefaults), "-"),
387 system_name);
390 db = XrmGetFileDatabase (p);
392 xfree (filename);
394 return db;
397 /* External interface. */
399 /* Types of values that we can find in a database */
401 #define XrmStringType "String" /* String representation */
402 static XrmRepresentation x_rm_string; /* Quark representation */
404 /* Load X resources based on the display and a possible -xrm option. */
406 XrmDatabase
407 x_load_resources (Display *display, const char *xrm_string,
408 const char *myname, const char *myclass)
410 XrmDatabase user_database;
411 XrmDatabase rdb;
412 XrmDatabase db;
413 char line[256];
415 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
416 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
417 #endif
419 #ifdef USE_MOTIF
420 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
421 #endif
423 x_rm_string = XrmStringToQuark (XrmStringType);
424 #ifndef USE_X_TOOLKIT
425 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
426 I suspect it's because the toolkit version does this elsewhere. */
427 XrmInitialize ();
428 #endif
429 rdb = XrmGetStringDatabase ("");
431 /* Add some font defaults. If the font `helv' doesn't exist, widgets
432 will use some other default font. */
433 #ifdef USE_MOTIF
435 sprintf (line, "%s.pane.background: grey75", myclass);
436 XrmPutLineResource (&rdb, line);
437 sprintf (line, "%s*fontList: %s", myclass, helv);
438 XrmPutLineResource (&rdb, line);
439 sprintf (line, "%s*menu*background: grey75", myclass);
440 XrmPutLineResource (&rdb, line);
441 sprintf (line, "%s*menubar*background: grey75", myclass);
442 XrmPutLineResource (&rdb, line);
443 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
444 XrmPutLineResource (&rdb, line);
445 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
446 XrmPutLineResource (&rdb, line);
447 sprintf (line, "%s*horizontalScrollBar.background: grey75", myclass);
448 XrmPutLineResource (&rdb, line);
449 sprintf (line, "%s*horizontalScrollBar.troughColor: grey75", myclass);
450 XrmPutLineResource (&rdb, line);
451 sprintf (line, "%s.dialog*.background: grey75", myclass);
452 XrmPutLineResource (&rdb, line);
453 sprintf (line, "%s*fsb.Text.background: white", myclass);
454 XrmPutLineResource (&rdb, line);
455 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
456 XrmPutLineResource (&rdb, line);
457 sprintf (line, "%s*fsb*DirList.background: white", myclass);
458 XrmPutLineResource (&rdb, line);
459 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
460 XrmPutLineResource (&rdb, line);
461 sprintf (line, "%s*fsb*background: grey75", myclass);
462 XrmPutLineResource (&rdb, line);
463 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
464 XrmPutLineResource (&rdb, line);
465 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
466 XrmPutLineResource (&rdb, line);
467 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
468 XrmPutLineResource (&rdb, line);
469 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
470 XrmPutLineResource (&rdb, line);
472 /* Set double click time of list boxes in the file selection
473 dialog from `double-click-time'. */
474 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
476 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
477 myclass, XFASTINT (Vdouble_click_time));
478 XrmPutLineResource (&rdb, line);
479 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
480 myclass, XFASTINT (Vdouble_click_time));
481 XrmPutLineResource (&rdb, line);
484 #else /* not USE_MOTIF */
486 sprintf (line, "Emacs.dialog*.background: grey75");
487 XrmPutLineResource (&rdb, line);
488 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
489 sprintf (line, "Emacs.dialog*.font: %s", helv);
490 XrmPutLineResource (&rdb, line);
491 sprintf (line, "*XlwMenu*font: %s", helv);
492 XrmPutLineResource (&rdb, line);
493 #endif
494 sprintf (line, "*XlwMenu*background: grey75");
495 XrmPutLineResource (&rdb, line);
496 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
497 XrmPutLineResource (&rdb, line);
498 sprintf (line, "Emacs*horizontalScrollBar.background: grey75");
499 XrmPutLineResource (&rdb, line);
501 #endif /* not USE_MOTIF */
503 user_database = get_user_db (display);
505 /* Figure out what the "customization string" is, so we can use it
506 to decode paths. */
507 xfree (x_customization_string);
508 x_customization_string
509 = x_get_customization_string (user_database, myname, myclass);
511 /* Get application system defaults */
512 db = get_system_app (myclass);
513 if (db != NULL)
514 XrmMergeDatabases (db, &rdb);
516 /* Get Fallback resources */
517 db = get_fallback (display);
518 if (db != NULL)
519 XrmMergeDatabases (db, &rdb);
521 /* Get application user defaults */
522 db = get_user_app (myclass);
523 if (db != NULL)
524 XrmMergeDatabases (db, &rdb);
526 /* get User defaults */
527 if (user_database != NULL)
528 XrmMergeDatabases (user_database, &rdb);
530 /* Get Environment defaults. */
531 db = get_environ_db ();
532 if (db != NULL)
533 XrmMergeDatabases (db, &rdb);
535 /* Last, merge in any specification from the command line. */
536 if (xrm_string != NULL)
538 db = XrmGetStringDatabase (xrm_string);
539 if (db != NULL)
540 XrmMergeDatabases (db, &rdb);
543 return rdb;
547 /* Retrieve the value of the resource specified by NAME with class CLASS
548 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
550 static int
551 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
552 XrmRepresentation expected_type, XrmValue *ret_value)
554 XrmValue value;
555 XrmName namelist[100];
556 XrmClass classlist[100];
557 XrmRepresentation type;
559 XrmStringToNameList (name, namelist);
560 XrmStringToClassList (class, classlist);
562 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
563 && (type == expected_type))
565 if (type == x_rm_string)
566 ret_value->addr = (char *) value.addr;
567 else
568 memcpy (ret_value->addr, value.addr, ret_value->size);
570 return value.size;
573 return 0;
576 /* Retrieve the string resource specified by NAME with CLASS from
577 database RDB. */
579 char *
580 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
582 XrmValue value;
584 if (inhibit_x_resources)
585 /* --quick was passed, so this is a no-op. */
586 return NULL;
588 if (x_get_resource (rdb, name, class, x_rm_string, &value))
589 return (char *) value.addr;
591 return 0;
594 /* Stand-alone test facilities. */
596 #ifdef TESTRM
598 typedef char **List;
599 #define arg_listify(len, list) (list)
600 #define car(list) (*(list))
601 #define cdr(list) (list + 1)
602 #define NIL(list) (! *(list))
603 #define free_arglist(list)
605 static List
606 member (char *elt, List list)
608 List p;
610 for (p = list; ! NIL (p); p = cdr (p))
611 if (! strcmp (elt, car (p)))
612 return p;
614 return p;
617 static void
618 fatal (char *msg, char *prog)
620 fprintf (stderr, msg, prog);
621 exit (1);
625 main (int argc, char **argv)
627 Display *display;
628 char *displayname, *resource_string, *class, *name;
629 XrmDatabase xdb;
630 List arg_list, lp;
632 arg_list = arg_listify (argc, argv);
634 lp = member ("-d", arg_list);
635 if (!NIL (lp))
636 displayname = car (cdr (lp));
637 else
638 displayname = "localhost:0.0";
640 lp = member ("-xrm", arg_list);
641 resource_string = NIL (lp) ? 0 : car (cdr (lp));
643 lp = member ("-c", arg_list);
644 if (! NIL (lp))
645 class = car (cdr (lp));
646 else
647 class = "Emacs";
649 lp = member ("-n", arg_list);
650 if (! NIL (lp))
651 name = car (cdr (lp));
652 else
653 name = "emacs";
655 free_arglist (arg_list);
657 if (!(display = XOpenDisplay (displayname)))
658 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
660 xdb = x_load_resources (display, resource_string, name, class);
662 /* In a real program, you'd want to also do this: */
663 display->db = xdb;
665 while (true)
667 char query_name[90];
668 char query_class[90];
670 printf ("Name: ");
671 gets (query_name);
673 if (strlen (query_name))
675 char *value;
677 printf ("Class: ");
678 gets (query_class);
680 value = x_get_string_resource (xdb, query_name, query_class);
682 if (value != NULL)
683 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
684 else
685 printf ("\tNo Value.\n\n");
687 else
688 break;
690 printf ("\tExit.\n\n");
692 XCloseDisplay (display);
694 return 0;
696 #endif /* TESTRM */