Source

emacs / src / xrdb.c

  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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* Deal with the X Resource Manager.
   Copyright (C) 1990, 1993, 1994 Free Software Foundation.

This file is part of GNU Emacs.

GNU Emacs 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.

GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Written by jla, 4/90 */

#ifdef emacs
#include <config.h>
#endif

#include <stdio.h>

#if 1 /* I'd really appreciate it if this code could go away...  -JimB */
/* this avoids lossage in the `dual-universe' headers on AT&T SysV X11 */
#ifdef USG5
#ifndef SYSV
#define SYSV
#endif
#include <unistd.h>
#endif /* USG5 */

#endif /* 1 */

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#if 0
#include <X11/Xos.h>
#endif
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#ifdef VMS
#include "vms-pwd.h"
#else
#include <pwd.h>
#endif
#include <sys/stat.h>

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

extern char *getenv ();

/* This does cause trouble on AIX.  I'm going to take the comment at
   face value.  */
#if 0
extern short getuid ();		/* If this causes portability problems,
				   I think we should just delete it; it'll
				   default to `int' anyway.  */
#endif

#ifdef DECLARE_GETPWUID_WITH_UID_T
extern struct passwd *getpwuid (uid_t);
extern struct passwd *getpwnam (const char *);
#else
extern struct passwd *getpwuid ();
extern struct passwd *getpwnam ();
#endif

extern char *get_system_name ();

/* Make sure not to #include anything after these definitions.  Let's
   not step on anyone's prototypes.  */
#ifdef emacs
#define malloc xmalloc
#define realloc xrealloc
#define free xfree
#endif

char *x_get_string_resource ();
static int file_p ();


/* X file search path processing.  */


/* The string which gets substituted for the %C escape in XFILESEARCHPATH
   and friends, or zero if none was specified.  */
char *x_customization_string;


/* Return the value of the emacs.customization (Emacs.Customization)
   resource, for later use in search path decoding.  If we find no
   such resource, return zero.  */
char *
x_get_customization_string (db, name, class)
     XrmDatabase db;
     char *name, *class;
{
  char *full_name
    = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
  char *full_class
    = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
  char *result;

  sprintf (full_name,  "%s.%s", name,  "customization");
  sprintf (full_class, "%s.%s", class, "Customization");

  result = x_get_string_resource (db, full_name, full_class);

  if (result)
    {
      char *copy = (char *) malloc (strlen (result) + 1);
      strcpy (copy, result);
      return copy;
    }
  else
    return 0;
}


/* Expand all the Xt-style %-escapes in STRING, whose length is given
   by STRING_LEN.  Here are the escapes we're supposed to recognize:

	%N	The value of the application's class name
	%T	The value of the type parameter ("app-defaults" in this
		context)
	%S	The value of the suffix parameter ("" in this context)
	%L	The language string associated with the specified display
		(We use the "LANG" environment variable here, if it's set.)
	%l	The language part of the display's language string
		(We treat this just like %L.  If someone can tell us what
		 we're really supposed to do, dandy.)
	%t	The territory part of the display's language string
	        (This never gets used.)
	%c	The codeset part of the display's language string
	        (This never gets used either.)
	%C	The customization string retrieved from the resource
		database associated with display.
		(This is x_customization_string.)

   Return the expanded file name if it exists and is readable, and
   refers to %L only when the LANG environment variable is set, or
   otherwise provided by X.

   ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
   non-zero.  %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
   alone.

   Return NULL otherwise.  */

static char *
magic_file_p (string, string_len, class, escaped_suffix, suffix)
     char *string;
     int string_len;
     char *class, *escaped_suffix, *suffix;
{
  char *lang = getenv ("LANG");

  int path_size = 100;
  char *path = (char *) malloc (path_size);
  int path_len = 0;

  char *p = string;

  while (p < string + string_len)
    {
      /* The chunk we're about to stick on the end of result.  */
      char *next;
      int next_len;

      if (*p == '%')
	{
	  p++;

	  if (p >= string + string_len)
	    next_len = 0;
	  else
	    switch (*p)
	      {
	      case '%':
		next = "%";
		next_len = 1;
		break;

	      case 'C':
		next = (x_customization_string
			? x_customization_string
			: "");
		next_len = strlen (next);
		break;

	      case 'N':
		next = class;
		next_len = strlen (class);
		break;

	      case 'T':
		next = "app-defaults";
		next_len = strlen (next);
		break;

	      default:
	      case 'S':
		next_len = 0;
		break;

	      case 'L':
	      case 'l':
		if (! lang)
		  {
		    free (path);
		    return NULL;
		  }
		
		next = lang;
		next_len = strlen (next);
		break;
	      
	      case 't':
	      case 'c':
		free (path);
		return NULL;
	      }
	}
      else
	next = p, next_len = 1;

      /* Do we have room for this component followed by a '\0' ?  */
      if (path_len + next_len + 1 > path_size)
	{
	  path_size = (path_len + next_len + 1) * 2;
	  path = (char *) realloc (path, path_size);
	}
      
      bcopy (next, path + path_len, next_len);
      path_len += next_len;

      p++;

      /* If we've reached the end of the string, append ESCAPED_SUFFIX.  */
      if (p >= string + string_len && escaped_suffix)
	{
	  string = escaped_suffix;
	  string_len = strlen (string);
	  p = string;
	  escaped_suffix = NULL;
	}
    }

  /* Perhaps we should add the SUFFIX now.  */
  if (suffix)
    {
      int suffix_len = strlen (suffix);

      if (path_len + suffix_len + 1 > path_size)
	{
	  path_size = (path_len + suffix_len + 1);
	  path = (char *) realloc (path, path_size);
	}

      bcopy (suffix, path + path_len, suffix_len);
      path_len += suffix_len;
    }

  path[path_len] = '\0';

  if (! file_p (path))
    {
      free (path);
      return NULL;
    }

  return path;
}


static char *
gethomedir ()
{
  struct passwd *pw;
  char *ptr;
  char *copy;

  if ((ptr = getenv ("HOME")) == NULL)
    {
      if ((ptr = getenv ("LOGNAME")) != NULL
	  || (ptr = getenv ("USER")) != NULL)
	pw = getpwnam (ptr);
      else
	pw = getpwuid (getuid ());

      if (pw)
	ptr = pw->pw_dir;
    }

  if (ptr == NULL) 
    return "/";

  copy = (char *) malloc (strlen (ptr) + 2);
  strcpy (copy, ptr);
  strcat (copy, "/");

  return copy;
}


static int
file_p (path)
     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)) == 0);	/* not a directory */
}


/* Find the first element of SEARCH_PATH which exists and is readable,
   after expanding the %-escapes.  Return 0 if we didn't find any, and 
   the path name of the one we found otherwise.  */

static char *
search_magic_path (search_path, class, escaped_suffix, suffix)
     char *search_path, *class, *escaped_suffix, *suffix;
{
  register char *s, *p;

  for (s = search_path; *s; s = p)
    {
      for (p = s; *p && *p != ':'; p++)
	;
      
      if (p > s)
	{
	  char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
	  if (path)
	    return path;
	}
      else if (*p == ':')
	{
	  char *path;

	  s = "%N%S";
	  path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
	  if (path)
	    return path;
	}

      if (*p == ':')
	p++;
    }

  return 0;
}

/* Producing databases for individual sources.  */

#define X_DEFAULT_SEARCH_PATH "/usr/lib/X11/%L/%T/%N%C%S:/usr/lib/X11/%l/%T/%N%C%S:/usr/lib/X11/%T/%N%C%S:/usr/lib/X11/%L/%T/%N%S:/usr/lib/X11/%l/%T/%N%S:/usr/lib/X11/%T/%N%S"

static XrmDatabase
get_system_app (class)
     char *class;
{
  XrmDatabase db = NULL;
  char *path;

  path = getenv ("XFILESEARCHPATH");
  if (! path) path = X_DEFAULT_SEARCH_PATH;

  path = search_magic_path (path, class, 0, 0);
  if (path)
    {
      db = XrmGetFileDatabase (path);
      free (path);
    }

  return db;
}


static XrmDatabase
get_fallback (display)
     Display *display;
{
  XrmDatabase db;

  return NULL;
}


static XrmDatabase
get_user_app (class)
     char *class;
{
  char *path;
  char *file = 0;

  /* Check for XUSERFILESEARCHPATH.  It is a path of complete file
     names, not directories.  */
  if (((path = getenv ("XUSERFILESEARCHPATH"))
       && (file = search_magic_path (path, class, 0, 0)))

      /* Check for APPLRESDIR; it is a path of directories.  In each,
	 we have to search for LANG/CLASS and then CLASS.  */
      || ((path = getenv ("XAPPLRESDIR"))
	  && ((file = search_magic_path (path, class, "/%L/%N", 0))
	      || (file = search_magic_path (path, class, "/%N", 0))))
      
      /* Check in the home directory.  This is a bit of a hack; let's
	 hope one's home directory doesn't contain any %-escapes.  */
      || (path = gethomedir (),
	  ((file = search_magic_path (path, class, "%L/%N", 0))
	   || (file = search_magic_path (path, class, "%N", 0)))))
    {
      XrmDatabase db = XrmGetFileDatabase (file);
      free (file);
      return db;
    }
  else
    return NULL;
}


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

#ifdef PBaseSize		/* Cheap way to test for X11R4 or later.  */
  xdefs = XResourceManagerString (display);
#else
  xdefs = display->xdefaults;
#endif

  if (xdefs != NULL)
    db = XrmGetStringDatabase (xdefs);
  else
    {
      char *home;
      char *xdefault;

      home = gethomedir ();
      xdefault = (char *) malloc (strlen (home) + sizeof (".Xdefaults"));
      strcpy (xdefault, home);
      strcat (xdefault, ".Xdefaults");
      db = XrmGetFileDatabase (xdefault);
      free (home);
      free (xdefault);
    }

#ifdef HAVE_XSCREENRESOURCESTRING
  /* Get the screen-specific resources too.  */
  xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
  if (xdefs != NULL)
    {
      XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
      XFree (xdefs);
    }
#endif

  return db;
}

static XrmDatabase
get_environ_db ()
{
  XrmDatabase db;
  char *p;
  char *path = 0, *home = 0, *host;

  if ((p = getenv ("XENVIRONMENT")) == NULL)
    {
      home = gethomedir ();
      host = get_system_name ();
      path = (char *) malloc (strlen (home)
			      + sizeof (".Xdefaults-")
			      + strlen (host));
      sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
      p = path;
    }

  db = XrmGetFileDatabase (p);

  if (path) free (path);
  if (home) free (home);

  return db;
}

/* External interface.  */

/* Types of values that we can find in a database */

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

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

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

  x_rm_string = XrmStringToQuark (XrmStringType);
#ifndef USE_X_TOOLKIT
  /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
     I suspect it's because the toolkit version does this elsewhere.  */
  XrmInitialize ();
#endif
  rdb = XrmGetStringDatabase ("");

  user_database = get_user_db (display);

  /* Figure out what the "customization string" is, so we can use it
     to decode paths.  */
  if (x_customization_string)
    free (x_customization_string);
  x_customization_string
    = x_get_customization_string (user_database, myname, myclass);

  /* 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 */
  if (user_database != NULL)
    XrmMergeDatabases (user_database, &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 (rdb, name, class, expected_type, ret_value)
     XrmDatabase rdb;
     char *name, *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 (rdb, name, class)
     XrmDatabase rdb;
     char *name, *class;
{
  XrmValue value;

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

  return (char *) 0;
}

/* Stand-alone test facilities.  */

#ifdef TESTRM

typedef char **List;
#define arg_listify(len, list) (list)
#define car(list) (*(list))
#define cdr(list) (list + 1)
#define NIL(list) (! *(list))
#define free_arglist(list)

static List
member (elt, list)
     char *elt;
     List list;
{
  List p;

  for (p = list; ! NIL (p); p = cdr (p))
    if (! strcmp (elt, car (p)))
      return p;

  return p;
}

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, *name;
  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";

  lp = member ("-n", arg_list);
  if (! NIL (lp))
    name = car (cdr (lp));
  else
    name = "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, name, class);

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

  while (1)
    {
      char query_name[90];
      char query_class[90];

      printf ("Name: ");
      gets (query_name);

      if (strlen (query_name))
	{
	  char *value;

	  printf ("Class: ");
	  gets (query_class);

	  value = x_get_string_resource (xdb, query_name, query_class);

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

  XCloseDisplay (display);
}
#endif /* TESTRM */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.