Mercurial > emacs
annotate src/xrdb.c @ 1200:d2360c870787
* xrdb.c (x_get_resource): Cast the value being assigned to
ret_value->addr, rather than ret_value->addr itself; only GCC
allows you to cast lvalues.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 22 Sep 1992 06:43:04 +0000 |
parents | 94d76dbb5ce6 |
children | 517c3893ec5b |
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 */ |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
32 |
159 | 33 #include <X11/Xlib.h> |
34 #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
|
35 #if 0 |
159 | 36 #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
|
37 #endif |
159 | 38 #include <X11/X.h> |
39 #include <X11/Xutil.h> | |
40 #include <X11/Xresource.h> | |
41 #include <sys/param.h> | |
556 | 42 #ifdef VMS |
43 #include "vms-pwd.h" | |
44 #else | |
159 | 45 #include <pwd.h> |
556 | 46 #endif |
159 | 47 #include <sys/stat.h> |
48 | |
1019
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
49 #ifndef MAXPATHLEN |
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
50 #define MAXPATHLEN 256 |
159 | 51 #endif |
52 | |
53 extern char *getenv (); | |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
54 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
|
55 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
|
56 default to `int' anyway. */ |
159 | 57 extern struct passwd *getpwuid (); |
58 extern struct passwd *getpwnam (); | |
59 | |
60 static char * | |
61 gethomedir (dirname) | |
62 char *dirname; | |
63 { | |
64 int uid; | |
65 struct passwd *pw; | |
66 char *ptr; | |
67 | |
68 if ((ptr = getenv ("HOME")) == NULL) | |
69 { | |
70 if ((ptr = getenv ("USER")) != NULL) | |
71 pw = getpwnam (ptr); | |
72 else | |
73 { | |
74 uid = getuid (); | |
75 pw = getpwuid (uid); | |
76 } | |
77 if (pw) | |
78 ptr = pw->pw_dir; | |
79 else | |
80 { | |
81 ptr = NULL; | |
82 *dirname = '\0'; | |
83 } | |
84 } | |
85 | |
86 if (ptr != NULL) | |
87 strcpy (dirname, ptr); | |
88 | |
89 dirname += strlen (dirname); | |
90 *dirname = '/'; | |
91 dirname++; | |
92 *dirname = '\0'; | |
93 | |
94 return dirname; | |
95 } | |
96 | |
97 static int | |
98 file_p (path) | |
99 char *path; | |
100 { | |
101 struct stat status; | |
102 | |
1126
94d76dbb5ce6
* xrdb.c: Don't include <X11/Xos.h>. Under R4, it stupidly
Jim Blandy <jimb@redhat.com>
parents:
1051
diff
changeset
|
103 return (access (path, 4) == 0 /* exists and is readable */ |
159 | 104 && stat (path, &status) == 0 /* get the status */ |
105 && (status.st_mode & S_IFDIR) == 0); /* not a directory */ | |
106 } | |
107 | |
108 #if 0 | |
109 #define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/" | |
110 #endif | |
111 | |
112 /* Isn't this just disgusting? */ | |
113 | |
114 #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" | |
115 | |
116 static int | |
117 decode_magic (string, file, return_path) | |
118 char *string, *file, *return_path; | |
119 { | |
120 char *p = string; | |
121 char *t = return_path; | |
122 | |
123 while (*p) | |
124 { | |
125 if (*p == '%') | |
126 switch (*++p) | |
127 { | |
128 case '%': | |
129 *t++ = '%'; | |
130 p++; | |
131 break; | |
132 | |
133 case 'N': | |
134 case 'T': | |
135 case 'S': | |
136 case 'L': | |
137 case 'l': | |
138 case 't': | |
139 case 'c': | |
140 default: | |
141 p++; | |
142 if (*t == '/' && *p == '/') | |
143 p++; | |
144 break; | |
145 } | |
146 else | |
147 *t++ = *p++; | |
148 } | |
149 *t = '\0'; | |
150 strcat (return_path, file); | |
151 | |
152 if (file_p (return_path)) | |
153 return 1; | |
154 | |
155 return_path[0] = '\0'; | |
156 return 0; | |
157 } | |
158 | |
159 static int | |
160 magic_searchpath_decoder (incantation_string, file, return_path) | |
161 char *incantation_string, *return_path, *file; | |
162 { | |
163 register char *s = incantation_string; | |
164 register char *p; | |
620 | 165 |
166 /* Must be big enough for "%N%S". */ | |
167 register int string_size = MAXPATHLEN; | |
168 register char *string = (char *) alloca (string_size * sizeof (*string)); | |
159 | 169 |
170 while (*s) | |
171 { | |
172 p = s; | |
173 | |
174 while (*p && *p != ':') | |
175 p++; | |
176 | |
177 if (*p == ':' && *(p + 1) == ':') | |
178 { | |
620 | 179 /* We know string is big enough for this. */ |
159 | 180 bcopy ("%N%S", string, 5); |
181 if (decode_magic (string, file, return_path)) | |
182 return 1; | |
183 | |
184 s = p + 1; | |
185 continue; | |
186 } | |
187 | |
188 if (p > s) | |
189 { | |
190 int len = p - s; | |
191 | |
620 | 192 if (string_size < len+1) |
193 { | |
194 string_size = 2 * len; | |
195 string = (char *) alloca (string_size * sizeof (*string)); | |
196 } | |
159 | 197 bcopy (s, string, len); |
198 string[len + 1] = '\0'; | |
199 if (decode_magic (string, file, return_path)) | |
200 return 1; | |
201 } | |
202 | |
203 if (p) | |
204 s = p + 1; | |
205 else | |
206 return 0; | |
207 } | |
208 | |
209 return 0; | |
210 } | |
211 | |
212 static XrmDatabase | |
213 get_system_app (class) | |
214 char *class; | |
215 { | |
216 XrmDatabase db; | |
217 char path[MAXPATHLEN]; | |
218 char *p; | |
219 | |
220 if ((p = getenv ("XFILESEARCHPATH")) == NULL) | |
221 p = X_DEFAULT_SEARCH_PATH; | |
222 | |
223 if (! magic_searchpath_decoder (p, class, path)) | |
224 return NULL; | |
225 | |
226 db = XrmGetFileDatabase (path); | |
227 return db; | |
228 } | |
229 | |
230 static XrmDatabase | |
231 get_fallback (display) | |
232 Display *display; | |
233 { | |
234 XrmDatabase db; | |
235 | |
236 return NULL; | |
237 } | |
238 | |
239 static XrmDatabase | |
240 get_user_app (class) | |
241 char *class; | |
242 { | |
243 XrmDatabase db; | |
244 char *magic_path; | |
245 char path[MAXPATHLEN]; | |
246 | |
247 if ((magic_path = getenv ("XUSERFILESEARCHPATH")) == NULL) | |
248 { | |
249 char homedir[MAXPATHLEN]; | |
250 char *default_magic; | |
251 char *p; | |
252 | |
253 gethomedir (homedir); | |
254 | |
255 if ((p = getenv ("XAPPLRESDIR")) == NULL) | |
256 { | |
257 default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N"; | |
258 magic_path = (char *) alloca ((3 * strlen (homedir)) | |
259 + strlen (default_magic)); | |
260 sprintf (magic_path, default_magic, homedir, homedir, homedir); | |
261 } | |
262 else | |
263 { | |
264 default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N"; | |
265 magic_path = (char *) alloca ((3 * strlen (p)) | |
266 + strlen (default_magic) | |
267 + strlen (homedir)); | |
268 sprintf (magic_path, default_magic, p, p, p, homedir); | |
269 } | |
270 } | |
271 | |
272 if (! magic_searchpath_decoder (magic_path, class, path)) | |
273 return NULL; | |
274 | |
275 db = XrmGetFileDatabase (path); | |
276 return db; | |
277 } | |
278 | |
279 static XrmDatabase | |
280 get_user_db (display) | |
281 Display *display; | |
282 { | |
283 XrmDatabase db; | |
284 char *xdefs; | |
285 | |
1051
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
286 #ifdef HAVE_X11R4 |
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
287 xdefs = XResourceManagerString (display); |
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
288 #else |
1019
aaa628aaf808
* xrdb.c [USG5]: Define SYSV, and then include <unistd.h>. I
Jim Blandy <jimb@redhat.com>
parents:
620
diff
changeset
|
289 xdefs = display->xdefaults; |
1051
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
290 #endif |
777ed2b92359
* xrdb.c (get_user_db): Use XResourceManagerString when available
Jim Blandy <jimb@redhat.com>
parents:
1019
diff
changeset
|
291 |
159 | 292 if (xdefs != NULL) |
293 db = XrmGetStringDatabase (xdefs); | |
294 else | |
295 { | |
296 char xdefault[MAXPATHLEN]; | |
297 | |
298 gethomedir (xdefault); | |
299 strcat (xdefault, ".Xdefaults"); | |
300 db = XrmGetFileDatabase (xdefault); | |
301 } | |
302 | |
303 return db; | |
304 } | |
305 | |
306 static XrmDatabase | |
307 get_environ_db () | |
308 { | |
309 XrmDatabase db; | |
310 char *p; | |
311 char path[MAXPATHLEN]; | |
312 | |
313 if ((p = getenv ("XENVIRONMENT")) == NULL) | |
314 { | |
315 gethomedir (path); | |
316 strcat (path, ".Xdefaults-"); | |
317 gethostname (path + strlen (path), MAXPATHLEN - strlen (path)); | |
318 p = path; | |
319 } | |
320 | |
321 db = XrmGetFileDatabase (p); | |
322 return db; | |
323 } | |
324 | |
325 /* Types of values that we can find in a database */ | |
326 | |
327 #define XrmStringType "String" /* String representation */ | |
328 XrmRepresentation x_rm_string; /* Quark representation */ | |
329 | |
330 /* Load X resources based on the display and a possible -xrm option. */ | |
331 | |
332 XrmDatabase | |
333 x_load_resources (display, xrm_string, myclass) | |
334 Display *display; | |
335 char *xrm_string, *myclass; | |
336 { | |
337 char *xdefs; | |
338 XrmDatabase rdb; | |
339 XrmDatabase db; | |
340 | |
341 x_rm_string = XrmStringToQuark (XrmStringType); | |
342 XrmInitialize (); | |
343 rdb = XrmGetStringDatabase (""); | |
344 | |
345 /* Get application system defaults */ | |
346 db = get_system_app (myclass); | |
347 if (db != NULL) | |
348 XrmMergeDatabases (db, &rdb); | |
349 | |
350 /* Get Fallback resources */ | |
351 db = get_fallback (display); | |
352 if (db != NULL) | |
353 XrmMergeDatabases (db, &rdb); | |
354 | |
355 /* Get application user defaults */ | |
356 db = get_user_app (myclass); | |
357 if (db != NULL) | |
358 XrmMergeDatabases (db, &rdb); | |
359 | |
360 /* get User defaults */ | |
361 db = get_user_db (display); | |
362 if (db != NULL) | |
363 XrmMergeDatabases (db, &rdb); | |
364 | |
365 /* Get Environment defaults. */ | |
366 db = get_environ_db (); | |
367 if (db != NULL) | |
368 XrmMergeDatabases (db, &rdb); | |
369 | |
370 /* Last, merge in any specification from the command line. */ | |
371 if (xrm_string != NULL) | |
372 { | |
373 db = XrmGetStringDatabase (xrm_string); | |
374 if (db != NULL) | |
375 XrmMergeDatabases (db, &rdb); | |
376 } | |
377 | |
378 return rdb; | |
379 } | |
380 | |
381 /* Retrieve the value of the resource specified by NAME with class CLASS | |
382 and of type TYPE from database RDB. The value is returned in RET_VALUE. */ | |
383 | |
384 int | |
385 x_get_resource (rdb, name, class, expected_type, ret_value) | |
386 XrmDatabase rdb; | |
387 char *name, *class; | |
388 XrmRepresentation expected_type; | |
389 XrmValue *ret_value; | |
390 { | |
391 XrmValue value; | |
392 XrmName namelist[100]; | |
393 XrmClass classlist[100]; | |
394 XrmRepresentation type; | |
395 | |
396 XrmStringToNameList(name, namelist); | |
397 XrmStringToClassList(class, classlist); | |
398 | |
399 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True | |
400 && (type == expected_type)) | |
401 { | |
402 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
|
403 ret_value->addr = (char *) value.addr; |
159 | 404 else |
405 bcopy (value.addr, ret_value->addr, ret_value->size); | |
406 | |
407 return value.size; | |
408 } | |
409 | |
410 return 0; | |
411 } | |
412 | |
413 /* Retrieve the string resource specified by NAME with CLASS from | |
414 database RDB. */ | |
415 | |
416 char * | |
417 x_get_string_resource (rdb, name, class) | |
418 XrmDatabase rdb; | |
419 char *name, *class; | |
420 { | |
421 XrmValue value; | |
422 | |
423 if (x_get_resource (rdb, name, class, x_rm_string, &value)) | |
424 return (char *) value.addr; | |
425 | |
426 return (char *) 0; | |
427 } | |
428 | |
429 #ifdef TESTRM | |
430 #include <stdio.h> | |
431 #include "arg-list.h" | |
432 | |
433 static void | |
434 fatal (msg, prog, x1, x2, x3, x4, x5) | |
435 char *msg, *prog; | |
436 int x1, x2, x3, x4, x5; | |
437 { | |
438 extern int errno; | |
439 | |
440 if (errno) | |
441 perror (prog); | |
442 | |
443 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5); | |
444 exit (1); | |
445 } | |
446 | |
447 main (argc, argv) | |
448 int argc; | |
449 char **argv; | |
450 { | |
451 Display *display; | |
452 char *displayname, *resource_string, *class; | |
453 XrmDatabase xdb; | |
454 List *arg_list, *lp; | |
455 | |
456 arg_list = arg_listify (argc, argv); | |
457 | |
458 lp = member ("-d", arg_list); | |
459 if (!NIL (lp)) | |
460 displayname = car (cdr (lp)); | |
461 else | |
462 displayname = "localhost:0.0"; | |
463 | |
464 lp = member ("-xrm", arg_list); | |
465 if (! NIL (lp)) | |
466 resource_string = car (cdr (lp)); | |
467 else | |
468 resource_string = (char *) 0; | |
469 | |
470 lp = member ("-c", arg_list); | |
471 if (! NIL (lp)) | |
472 class = car (cdr (lp)); | |
473 else | |
474 class = "Emacs"; | |
475 | |
476 free_arglist (arg_list); | |
477 | |
478 | |
479 | |
480 if (!(display = XOpenDisplay (displayname))) | |
481 fatal ("Can't open display '%s'\n", XDisplayName (displayname)); | |
482 | |
483 xdb = x_load_resources (display, resource_string, class); | |
484 | |
485 #if 0 | |
486 /* In a real program, you'd want to also do this: */ | |
487 display->db = xdb; | |
488 #endif | |
489 | |
490 while (1) | |
491 { | |
492 char line[90]; | |
493 | |
494 printf ("String: "); | |
495 gets (line); | |
496 if (strlen (line)) | |
497 { | |
498 char *value = x_get_string_resource (xdb, line, class); | |
499 | |
500 if (value != NULL) | |
501 printf ("\t%s: %s\n\n", line, value); | |
502 else | |
503 printf ("\tNo Value.\n\n"); | |
504 } | |
505 else | |
506 break; | |
507 } | |
508 printf ("\tExit.\n\n"); | |
509 | |
510 XCloseDisplay (display); | |
511 } | |
512 #endif /* TESTRM */ |