]> code.delx.au - gnu-emacs/blob - src/xrdb.c
Use the term `scroll bar', instead of `scrollbar'.
[gnu-emacs] / src / xrdb.c
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1992 Free Software Foundation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 /* Written by jla, 4/90 */
19
20 #ifdef emacs
21 #include "config.h"
22 #endif
23
24 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
25 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
26 #ifdef USG5
27 #define SYSV
28 #include <unistd.h>
29 #endif /* USG5 */
30
31 #endif /* 1 */
32
33 /* This should be included before the X include files; otherwise, we get
34 warnings about redefining NULL under BSD 4.3. */
35 #include <sys/param.h>
36
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #if 0
40 #include <X11/Xos.h>
41 #endif
42 #include <X11/X.h>
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
45 #ifdef VMS
46 #include "vms-pwd.h"
47 #else
48 #include <pwd.h>
49 #endif
50 #include <sys/stat.h>
51
52 #ifndef MAXPATHLEN
53 #define MAXPATHLEN 256
54 #endif
55
56 extern char *getenv ();
57
58 /* This does cause trouble on AIX. I'm going to take the comment at
59 face value. */
60 #if 0
61 extern short getuid (); /* If this causes portability problems,
62 I think we should just delete it; it'll
63 default to `int' anyway. */
64 #endif
65
66 extern struct passwd *getpwuid ();
67 extern struct passwd *getpwnam ();
68
69 static char *
70 gethomedir (dirname)
71 char *dirname;
72 {
73 int uid;
74 struct passwd *pw;
75 char *ptr;
76
77 if ((ptr = getenv ("HOME")) == NULL)
78 {
79 if ((ptr = getenv ("USER")) != NULL)
80 pw = getpwnam (ptr);
81 else
82 {
83 uid = getuid ();
84 pw = getpwuid (uid);
85 }
86 if (pw)
87 ptr = pw->pw_dir;
88 else
89 {
90 ptr = NULL;
91 *dirname = '\0';
92 }
93 }
94
95 if (ptr != NULL)
96 strcpy (dirname, ptr);
97
98 dirname += strlen (dirname);
99 *dirname = '/';
100 dirname++;
101 *dirname = '\0';
102
103 return dirname;
104 }
105
106 static int
107 file_p (path)
108 char *path;
109 {
110 struct stat status;
111
112 return (access (path, 4) == 0 /* exists and is readable */
113 && stat (path, &status) == 0 /* get the status */
114 && (status.st_mode & S_IFDIR) == 0); /* not a directory */
115 }
116
117 #if 0
118 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/"
119 #endif
120
121 /* Isn't this just disgusting? */
122
123 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/%L/%T/%N%S:/usr/lib/X11/%l/%T/%N%S:/usr/lib/X11/%T/%N%S"
124
125 static int
126 decode_magic (string, file, return_path)
127 char *string, *file, *return_path;
128 {
129 char *p = string;
130 char *t = return_path;
131
132 while (*p)
133 {
134 if (*p == '%')
135 switch (*++p)
136 {
137 case '%':
138 *t++ = '%';
139 p++;
140 break;
141
142 case 'N':
143 case 'T':
144 case 'S':
145 case 'L':
146 case 'l':
147 case 't':
148 case 'c':
149 default:
150 p++;
151 if (*t == '/' && *p == '/')
152 p++;
153 break;
154 }
155 else
156 *t++ = *p++;
157 }
158 *t = '\0';
159 strcat (return_path, file);
160
161 if (file_p (return_path))
162 return 1;
163
164 return_path[0] = '\0';
165 return 0;
166 }
167
168 static int
169 magic_searchpath_decoder (incantation_string, file, return_path)
170 char *incantation_string, *return_path, *file;
171 {
172 register char *s = incantation_string;
173 register char *p;
174
175 /* Must be big enough for "%N%S". */
176 register int string_size = MAXPATHLEN;
177 register char *string = (char *) alloca (string_size * sizeof (*string));
178
179 while (*s)
180 {
181 p = s;
182
183 while (*p && *p != ':')
184 p++;
185
186 if (*p == ':' && *(p + 1) == ':')
187 {
188 /* We know string is big enough for this. */
189 bcopy ("%N%S", string, 5);
190 if (decode_magic (string, file, return_path))
191 return 1;
192
193 s = p + 1;
194 continue;
195 }
196
197 if (p > s)
198 {
199 int len = p - s;
200
201 if (string_size < len+1)
202 {
203 string_size = 2 * len;
204 string = (char *) alloca (string_size * sizeof (*string));
205 }
206 bcopy (s, string, len);
207 string[len + 1] = '\0';
208 if (decode_magic (string, file, return_path))
209 return 1;
210 }
211
212 if (p)
213 s = p + 1;
214 else
215 return 0;
216 }
217
218 return 0;
219 }
220 \f
221 static XrmDatabase
222 get_system_app (class)
223 char *class;
224 {
225 XrmDatabase db;
226 char path[MAXPATHLEN];
227 char *p;
228
229 if ((p = getenv ("XFILESEARCHPATH")) == NULL)
230 p = X_DEFAULT_SEARCH_PATH;
231
232 if (! magic_searchpath_decoder (p, class, path))
233 return NULL;
234
235 db = XrmGetFileDatabase (path);
236 return db;
237 }
238
239 static XrmDatabase
240 get_fallback (display)
241 Display *display;
242 {
243 XrmDatabase db;
244
245 return NULL;
246 }
247
248 static XrmDatabase
249 get_user_app (class)
250 char *class;
251 {
252 XrmDatabase db;
253 char *magic_path;
254 char path[MAXPATHLEN];
255
256 if ((magic_path = getenv ("XUSERFILESEARCHPATH")) == NULL)
257 {
258 char homedir[MAXPATHLEN];
259 char *default_magic;
260 char *p;
261
262 gethomedir (homedir);
263
264 if ((p = getenv ("XAPPLRESDIR")) == NULL)
265 {
266 default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
267 magic_path = (char *) alloca ((3 * strlen (homedir))
268 + strlen (default_magic));
269 sprintf (magic_path, default_magic, homedir, homedir, homedir);
270 }
271 else
272 {
273 default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
274 magic_path = (char *) alloca ((3 * strlen (p))
275 + strlen (default_magic)
276 + strlen (homedir));
277 sprintf (magic_path, default_magic, p, p, p, homedir);
278 }
279 }
280
281 if (! magic_searchpath_decoder (magic_path, class, path))
282 return NULL;
283
284 db = XrmGetFileDatabase (path);
285 return db;
286 }
287
288 static XrmDatabase
289 get_user_db (display)
290 Display *display;
291 {
292 XrmDatabase db;
293 char *xdefs;
294
295 #ifdef HAVE_X11R4
296 xdefs = XResourceManagerString (display);
297 #else
298 xdefs = display->xdefaults;
299 #endif
300
301 if (xdefs != NULL)
302 db = XrmGetStringDatabase (xdefs);
303 else
304 {
305 char xdefault[MAXPATHLEN];
306
307 gethomedir (xdefault);
308 strcat (xdefault, ".Xdefaults");
309 db = XrmGetFileDatabase (xdefault);
310 }
311
312 return db;
313 }
314
315 static XrmDatabase
316 get_environ_db ()
317 {
318 XrmDatabase db;
319 char *p;
320 char path[MAXPATHLEN];
321
322 if ((p = getenv ("XENVIRONMENT")) == NULL)
323 {
324 gethomedir (path);
325 strcat (path, ".Xdefaults-");
326 gethostname (path + strlen (path), MAXPATHLEN - strlen (path));
327 p = path;
328 }
329
330 db = XrmGetFileDatabase (p);
331 return db;
332 }
333 \f
334 /* Types of values that we can find in a database */
335
336 #define XrmStringType "String" /* String representation */
337 XrmRepresentation x_rm_string; /* Quark representation */
338
339 /* Load X resources based on the display and a possible -xrm option. */
340
341 XrmDatabase
342 x_load_resources (display, xrm_string, myclass)
343 Display *display;
344 char *xrm_string, *myclass;
345 {
346 char *xdefs;
347 XrmDatabase rdb;
348 XrmDatabase db;
349
350 x_rm_string = XrmStringToQuark (XrmStringType);
351 XrmInitialize ();
352 rdb = XrmGetStringDatabase ("");
353
354 /* Get application system defaults */
355 db = get_system_app (myclass);
356 if (db != NULL)
357 XrmMergeDatabases (db, &rdb);
358
359 /* Get Fallback resources */
360 db = get_fallback (display);
361 if (db != NULL)
362 XrmMergeDatabases (db, &rdb);
363
364 /* Get application user defaults */
365 db = get_user_app (myclass);
366 if (db != NULL)
367 XrmMergeDatabases (db, &rdb);
368
369 /* get User defaults */
370 db = get_user_db (display);
371 if (db != NULL)
372 XrmMergeDatabases (db, &rdb);
373
374 /* Get Environment defaults. */
375 db = get_environ_db ();
376 if (db != NULL)
377 XrmMergeDatabases (db, &rdb);
378
379 /* Last, merge in any specification from the command line. */
380 if (xrm_string != NULL)
381 {
382 db = XrmGetStringDatabase (xrm_string);
383 if (db != NULL)
384 XrmMergeDatabases (db, &rdb);
385 }
386
387 return rdb;
388 }
389
390 /* Retrieve the value of the resource specified by NAME with class CLASS
391 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
392
393 int
394 x_get_resource (rdb, name, class, expected_type, ret_value)
395 XrmDatabase rdb;
396 char *name, *class;
397 XrmRepresentation expected_type;
398 XrmValue *ret_value;
399 {
400 XrmValue value;
401 XrmName namelist[100];
402 XrmClass classlist[100];
403 XrmRepresentation type;
404
405 XrmStringToNameList(name, namelist);
406 XrmStringToClassList(class, classlist);
407
408 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
409 && (type == expected_type))
410 {
411 if (type == x_rm_string)
412 ret_value->addr = (char *) value.addr;
413 else
414 bcopy (value.addr, ret_value->addr, ret_value->size);
415
416 return value.size;
417 }
418
419 return 0;
420 }
421
422 /* Retrieve the string resource specified by NAME with CLASS from
423 database RDB. */
424
425 char *
426 x_get_string_resource (rdb, name, class)
427 XrmDatabase rdb;
428 char *name, *class;
429 {
430 XrmValue value;
431
432 if (x_get_resource (rdb, name, class, x_rm_string, &value))
433 return (char *) value.addr;
434
435 return (char *) 0;
436 }
437 \f
438 #ifdef TESTRM
439 #include <stdio.h>
440 #include "arg-list.h"
441
442 static void
443 fatal (msg, prog, x1, x2, x3, x4, x5)
444 char *msg, *prog;
445 int x1, x2, x3, x4, x5;
446 {
447 extern int errno;
448
449 if (errno)
450 perror (prog);
451
452 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
453 exit (1);
454 }
455
456 main (argc, argv)
457 int argc;
458 char **argv;
459 {
460 Display *display;
461 char *displayname, *resource_string, *class;
462 XrmDatabase xdb;
463 List *arg_list, *lp;
464
465 arg_list = arg_listify (argc, argv);
466
467 lp = member ("-d", arg_list);
468 if (!NIL (lp))
469 displayname = car (cdr (lp));
470 else
471 displayname = "localhost:0.0";
472
473 lp = member ("-xrm", arg_list);
474 if (! NIL (lp))
475 resource_string = car (cdr (lp));
476 else
477 resource_string = (char *) 0;
478
479 lp = member ("-c", arg_list);
480 if (! NIL (lp))
481 class = car (cdr (lp));
482 else
483 class = "Emacs";
484
485 free_arglist (arg_list);
486
487
488
489 if (!(display = XOpenDisplay (displayname)))
490 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
491
492 xdb = x_load_resources (display, resource_string, class);
493
494 #if 0
495 /* In a real program, you'd want to also do this: */
496 display->db = xdb;
497 #endif
498
499 while (1)
500 {
501 char line[90];
502
503 printf ("String: ");
504 gets (line);
505 if (strlen (line))
506 {
507 char *value = x_get_string_resource (xdb, line, class);
508
509 if (value != NULL)
510 printf ("\t%s: %s\n\n", line, value);
511 else
512 printf ("\tNo Value.\n\n");
513 }
514 else
515 break;
516 }
517 printf ("\tExit.\n\n");
518
519 XCloseDisplay (display);
520 }
521 #endif /* TESTRM */