Mercurial > emacs
annotate src/xrdb.c @ 2237:2670a8f6a42e
Teach this to use help-screen.el.
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Wed, 17 Mar 1993 22:20:57 +0000 |
parents | 616aeb5c1432 |
children | e94a593c3952 |
rev | line source |
---|---|
159 | 1 /* Deal with the X Resource Manager. |
620 | 2 Copyright (C) 1990, 1992 Free Software Foundation. |
159 | 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 | |
620 | 6 the Free Software Foundation; either version 2, or (at your option) |
159 | 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 | |
1019
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
20 #ifdef emacs |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
21 #include "config.h" |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
22 #endif |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
23 |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
24 #if 1 /* I'd really appreciate it if this code could go away... -JimB */ |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
25 /* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */ |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
26 #ifdef USG5 |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
27 #define SYSV |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
28 #include <unistd.h> |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
29 #endif /* USG5 */ |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
30 |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
31 #endif /* 1 */ |
1389
517c3893ec5b
* xterm.c, xrdb.c: #include <stdio.h> before "xterm.h", to avoid
Jim Blandy <jimb@redhat.com>
parents:
1200
diff
changeset
|
32 |
517c3893ec5b
* xterm.c, xrdb.c: #include <stdio.h> before "xterm.h", to avoid
Jim Blandy <jimb@redhat.com>
parents:
1200
diff
changeset
|
33 /* This should be included before the X include files; otherwise, we get |
517c3893ec5b
* xterm.c, xrdb.c: #include <stdio.h> before "xterm.h", to avoid
Jim Blandy <jimb@redhat.com>
parents:
1200
diff
changeset
|
34 warnings about redefining NULL under BSD 4.3. */ |
517c3893ec5b
* xterm.c, xrdb.c: #include <stdio.h> before "xterm.h", to avoid
Jim Blandy <jimb@redhat.com>
parents:
1200
diff
changeset
|
35 #include <sys/param.h> |
517c3893ec5b
* xterm.c, xrdb.c: #include <stdio.h> before "xterm.h", to avoid
Jim Blandy <jimb@redhat.com>
parents:
1200
diff
changeset
|
36 |
159 | 37 #include <X11/Xlib.h> |
38 #include <X11/Xatom.h> | |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
39 #if 0 |
159 | 40 #include <X11/Xos.h> |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
41 #endif |
159 | 42 #include <X11/X.h> |
43 #include <X11/Xutil.h> | |
44 #include <X11/Xresource.h> | |
556 | 45 #ifdef VMS |
46 #include "vms-pwd.h" | |
47 #else | |
159 | 48 #include <pwd.h> |
556 | 49 #endif |
159 | 50 #include <sys/stat.h> |
51 | |
1019
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
52 #ifndef MAXPATHLEN |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
53 #define MAXPATHLEN 256 |
159 | 54 #endif |
55 | |
56 extern char *getenv (); | |
1934
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
57 |
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
58 /* This does cause trouble on AIX. I'm going to take the comment at |
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
59 face value. */ |
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
60 #if 0 |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
61 extern short getuid (); /* If this causes portability problems, |
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
62 I think we should just delete it; it'll |
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
63 default to `int' anyway. */ |
1934
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
64 #endif |
458a1fceb462
* xrdb.c (getuid): Remove declaration.
Jim Blandy <jimb@redhat.com>
parents:
1389
diff
changeset
|
65 |
159 | 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 | |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
112 return (access (path, 4) == 0 /* exists and is readable */ |
159 | 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; | |
620 | 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)); | |
159 | 178 |
179 while (*s) | |
180 { | |
181 p = s; | |
182 | |
183 while (*p && *p != ':') | |
184 p++; | |
185 | |
186 if (*p == ':' && *(p + 1) == ':') | |
187 { | |
620 | 188 /* We know string is big enough for this. */ |
159 | 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 | |
620 | 201 if (string_size < len+1) |
202 { | |
203 string_size = 2 * len; | |
204 string = (char *) alloca (string_size * sizeof (*string)); | |
205 } | |
159 | 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 | |
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 | |
2126
616aeb5c1432
* xrdb.c (get_user_db): Since xrdb.c doesn't #include xterm.h,
Jim Blandy <jimb@redhat.com>
parents:
1934
diff
changeset
|
295 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */ |
1051
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
296 xdefs = XResourceManagerString (display); |
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
297 #else |
1019
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
298 xdefs = display->xdefaults; |
1051
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
299 #endif |
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
300 |
159 | 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 | |
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) | |
1200
d2360c870787
* xrdb.c (x_get_resource): Cast the value being assigned to
Jim Blandy <jimb@redhat.com>
parents:
1126
diff
changeset
|
412 ret_value->addr = (char *) value.addr; |
159 | 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 | |
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 */ |