File: xrdb.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (519 lines) | stat: -rw-r--r-- 10,709 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Deal with the X Resource Manager.
   Copyright (C) 1990, 1992, 1993 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Coopted for oleo -t 12/92 */
/* Written by jla, 4/90 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef X_DISPLAY_MISSING
#ifndef alloca
#ifdef __GNUC__
#define alloca __builtin_alloca
#else /* __GNUC__ not defined.  */
#if HAVE_ALLOCA_H
#include <alloca.h>
#else /* not HAVE_ALLOCA_H */
#if defined (MSDOS) && !defined (__TURBOC__)
#include <malloc.h>
#else /* not MSDOS, or __TURBOC__ */
#if defined(_AIX)
#include <malloc.h>
 #pragma alloca
#endif /* not _AIX */
#endif /* not MSDOS, or __TURBOC__ */
#endif /* not HAVE_ALLOCA_H */
#endif /* __GNUC__ not defined.  */
#endif /* alloca not defined.  */

#include <stdio.h>
#include "sysdef.h"
#if HAVE_SYS_SYSTEMINFO_H
#include <sys/systeminfo.h>
#endif

/* This should be included before the X include files; otherwise, we get
   warnings about redefining NULL under BSD 4.3.  */
#include <sys/param.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#ifdef VMS
#include "vms-pwd.h"
#else
#include <pwd.h>
#endif

/*
 * Already included <sys/stat.h> via sysdef.h
 */

#if !defined(S_ISDIR) && defined(S_IFDIR)
#define       S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif


#ifndef MAXPATHLEN
#define MAXPATHLEN	256
#endif

extern char *getenv (const char *);
#ifndef __sgi
extern struct passwd *getpwuid (uid_t);
#endif /* !__sgi */

static char *
gethomedir (char *dirname)
{
  int uid;
  struct passwd *pw;
  char *ptr;

  if ((ptr = getenv ("HOME")) == NULL)
    {
      if ((ptr = getenv ("USER")) != NULL)
	pw = getpwnam (ptr);
      else
	{
	  uid = getuid ();
	  pw = getpwuid (uid);
	}
      if (pw)
	ptr = pw->pw_dir;
      else
	{
	  ptr = NULL;
	  *dirname = '\0';
	}
    }

  if (ptr != NULL) 
    strcpy (dirname, ptr);

  dirname += strlen (dirname);
  *dirname = '/';
  dirname++;
  *dirname = '\0';

  return dirname;
}

static int
file_p (char *path)
{
  struct stat status;

  return (access (path, 4) == 0			/* exists and is readable */
	  && stat (path, &status) == 0		/* get the status */
	  && !S_ISDIR(status.st_mode));
}


/* Isn't this just disgusting? */

#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"

static int
decode_magic (char *string, char *file, char *return_path)
{
  char *p = string;
  char *t = return_path;

  while (*p)
    {
      if (*p == '%')
	switch (*++p)
	  {
	  case '%':
	    *t++ = '%';
	    p++;
	    break;

	  case 'N':
	  case 'T':
	  case 'S':
	  case 'L':
	  case 'l':
	  case 't':
	  case 'c':
	  default:
	    p++;
	    if (*t == '/' && *p == '/')
	      p++;
	    break;
	  }
      else
	*t++ = *p++;
    }
  *t = '\0';
  strcat (return_path, file);

  if (file_p (return_path))
    return 1;

  return_path[0] = '\0';
  return 0;
}

static int
magic_searchpath_decoder (char *incantation_string, char *file, char *return_path)
{
  register char *s = incantation_string;
  register char *p;

  /* Must be big enough for "%N%S".  */
  register int string_size = MAXPATHLEN;
  register char *string = (char *) alloca (string_size * sizeof (*string));

  while (*s)
    {
      p = s;

      while (*p && *p != ':')
	p++;

      if (p == s)
	{
	  /* We know string is big enough for this.  */
	  bcopy ("%N%S", string, 5);
	  if (decode_magic (string, file, return_path))
	    return 1;
	}
      else
	{
	  int len = p - s;

	  if (string_size < len+1)
	    {
	      string_size = 2 * len;
	      string = (char *) alloca (string_size * sizeof (*string));
	    }
	  bcopy (s, string, len);
	  string[len + 1] = '\0';
	  if (decode_magic (string, file, return_path))
	    return 1;
	}

      if (*p)
	s = p + 1;
      else
	return 0;
    }

  return 0;
}

static XrmDatabase
get_system_app (char *class)
{
  XrmDatabase db;
  char path[MAXPATHLEN];
  char *p;

  if ((p = getenv ("XFILESEARCHPATH")) == NULL)
    p = X_DEFAULT_SEARCH_PATH;

  if (! magic_searchpath_decoder (p, class, path))
    return NULL;

  db = XrmGetFileDatabase (path);
  return db;
}

static XrmDatabase
get_fallback (Display *display)
{
  return NULL;
}

static XrmDatabase
get_user_app (char *class)
{
  XrmDatabase db;
  char *magic_path;
  char path[MAXPATHLEN];

  if ((magic_path = getenv ("XUSERFILESEARCHPATH")) == NULL)
    {
      char homedir[MAXPATHLEN];
      char *default_magic;
      char *p;

      gethomedir (homedir);

      if ((p = getenv ("XAPPLRESDIR")) == NULL)
	{
	  default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N";
	  magic_path = (char *) alloca ((3 * strlen (homedir))
					+ strlen (default_magic));
	  sprintf (magic_path, default_magic, homedir, homedir, homedir);
	}
      else
	{
	  default_magic = "%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N";
	  magic_path = (char *) alloca ((3 * strlen (p))
					+ strlen (default_magic)
					+ strlen (homedir));
	  sprintf (magic_path, default_magic, p, p, p, homedir);
	}
    }

  if (! magic_searchpath_decoder (magic_path, class, path))
    return NULL;
  
  db = XrmGetFileDatabase (path);
  return db;
}

static XrmDatabase
get_user_db (Display *display)
{
  XrmDatabase db;
  char *xdefs;

#ifdef HAVE_XRESOURCEMANAGERSTRING
  xdefs = XResourceManagerString (display);
#else
  xdefs = display->xdefaults;
#endif

  if (xdefs != NULL)
    db = XrmGetStringDatabase (xdefs);
  else
    {
      char xdefault[MAXPATHLEN];

      gethomedir (xdefault);
      strcat (xdefault, ".Xdefaults");
      db = XrmGetFileDatabase (xdefault);
    }

  return db;
}

static XrmDatabase
get_environ_db (void)
{
  XrmDatabase db;
  char *p;
  char path[MAXPATHLEN];

  if ((p = getenv ("XENVIRONMENT")) == NULL)
    {
      gethomedir (path);
      strcat (path, ".Xdefaults-");
#if defined(HAVE_SYSINFO) && defined(HAVE_SYS_SYSTEMINFO_H)
      sysinfo (SI_HOSTNAME, path + strlen (path), MAXPATHLEN - strlen (path));
#else
      gethostname (path + strlen (path), MAXPATHLEN - strlen (path));
#endif
      p = path;
    }

  db = XrmGetFileDatabase (p);
  return db;
}

/* Types of values that we can find in a database */

#define XrmStringType "String"	/* String representation */
XrmRepresentation x_rm_string = 0;	/* Quark representation */

/* Load X resources based on the display and a possible -xrm option. */

XrmDatabase
x_load_resources (Display *display, char *xrm_string, char *myclass)
{
  XrmDatabase rdb;
  XrmDatabase db;

  if (!x_rm_string)
    x_rm_string = XrmStringToQuark (XrmStringType);

  XrmInitialize ();
  rdb = XrmGetStringDatabase ("");

  /* Get application system defaults */
  db = get_system_app (myclass);
  if (db != NULL)
    XrmMergeDatabases (db, &rdb);

  /* Get Fallback resources */
  db = get_fallback (display);
  if (db != NULL)
    XrmMergeDatabases (db, &rdb);

  /* Get application user defaults */
  db = get_user_app (myclass);
  if (db != NULL)
    XrmMergeDatabases (db, &rdb);

  /* get User defaults */
  db = get_user_db (display);
  if (db != NULL)
    XrmMergeDatabases (db, &rdb);

  /* Get Environment defaults. */
  db = get_environ_db ();
  if (db != NULL)
    XrmMergeDatabases (db, &rdb);
  
  /* Last, merge in any specification from the command line. */
  if (xrm_string != NULL)
    {
      db = XrmGetStringDatabase (xrm_string);
      if (db != NULL)
	XrmMergeDatabases (db, &rdb);
    }

  return rdb;
}

/* Retrieve the value of the resource specified by NAME with class CLASS
   and of type TYPE from database RDB.  The value is returned in RET_VALUE. */

int
x_get_resource (XrmDatabase rdb, char *name, char *class, XrmRepresentation expected_type, XrmValue *ret_value)
{
  XrmValue value;
  XrmName namelist[100];
  XrmClass classlist[100];
  XrmRepresentation type;

  XrmStringToNameList(name, namelist);
  XrmStringToClassList(class, classlist);

  if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
      && (type == expected_type))
    {
      if (type == x_rm_string)
	ret_value->addr = (char *) value.addr;
      else
	bcopy (value.addr, ret_value->addr, ret_value->size);

      return value.size;
    }

  return 0;
}

/* Retrieve the string resource specified by NAME with CLASS from
   database RDB. */

char *
x_get_string_resource (XrmDatabase rdb, char *class, char *name)
{
  XrmValue value;

  if (!x_rm_string)
    x_rm_string = XrmStringToQuark (XrmStringType);

  if (x_get_resource (rdb, name, class, x_rm_string, &value))
    return (char *) value.addr;

  return (char *) 0;
}

#ifdef TESTRM
#include <stdio.h>
#include "arg-list.h"

static void
fatal (msg, prog, x1, x2, x3, x4, x5)
    char *msg, *prog;
    int x1, x2, x3, x4, x5;
{
    extern int errno;

    if (errno)
      perror (prog);

    (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
    exit (1);
}

main (argc, argv)
    int argc;
    char **argv;
{
  Display *display;
  char *displayname, *resource_string, *class;
  XrmDatabase xdb;
  List *arg_list, *lp;

  arg_list = arg_listify (argc, argv);

  lp = member ("-d", arg_list);
  if (!NIL (lp))
    displayname = car (cdr (lp));
  else
    displayname = "localhost:0.0";

  lp = member ("-xrm", arg_list);
  if (! NIL (lp))
    resource_string = car (cdr (lp));
  else
    resource_string = (char *) 0;

  lp = member ("-c", arg_list);
  if (! NIL (lp))
    class = car (cdr (lp));
  else
    class = "Emacs";

  free_arglist (arg_list);



  if (!(display = XOpenDisplay (displayname)))
    fatal ("Can't open display '%s'\n", XDisplayName (displayname));

  xdb = x_load_resources (display, resource_string, class);

#if 0
  /* In a real program, you'd want to also do this: */
  display->db = xdb;
#endif

  while (1)
    {
      char line[90];

      printf ("String: ");
      gets (line);
      if (strlen (line))
	{
	  char *value = x_get_string_resource (xdb, line, class);

	  if (value != NULL)
	    printf ("\t%s:  %s\n\n", line, value);
	  else
	    printf ("\tNo Value.\n\n");
	}
      else
	break;
    }
  printf ("\tExit.\n\n");

  XCloseDisplay (display);
}
#endif /* TESTRM */
#endif /* X_DISPLAY_MISSING */