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