Update years in copyright notice; nfc.
[bpt/emacs.git] / src / xrdb.c
index 7bced53..822fb6e 100644 (file)
@@ -1,41 +1,51 @@
 /* Deal with the X Resource Manager.
-   Copyright (C) 1990, 1993 Free Software Foundation.
+   Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
+                 2005 Free Software Foundation, Inc.
 
-This program is free software; you can redistribute it and/or modify
+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.
 
-This program is distributed in the hope that it will be useful,
+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 this program; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+along with GNU Emacs; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* Written by jla, 4/90 */
 
 #ifdef emacs
-#include "config.h"
+#include <config.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
 #endif
 
+#include <epaths.h>
+
 #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
+/* This avoids lossage in the `dual-universe' headers on AT&T SysV
+   X11.  Don't do it on Solaris, because it breaks compilation with
+   XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
+   2 */
+#if defined(USG5) && !defined(SOLARIS2)
+#ifndef SYSV
 #define SYSV
-#include <unistd.h>
-#endif /* USG5 */
+#endif
+#endif /* USG5 && !SOLARIS2 */
 
 #endif /* 1 */
 
-/* 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/Xlib.h>
 #include <X11/Xatom.h>
 #if 0
@@ -44,17 +54,17 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <X11/X.h>
 #include <X11/Xutil.h>
 #include <X11/Xresource.h>
-#ifdef VMS
-#include "vms-pwd.h"
-#else
+#ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 #include <sys/stat.h>
 
-#ifndef MAXPATHLEN
-#define MAXPATHLEN     256
+#if !defined(S_ISDIR) && defined(S_IFDIR)
+#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 #endif
 
+#include "lisp.h"
+
 extern char *getenv ();
 
 /* This does cause trouble on AIX.  I'm going to take the comment at
@@ -65,7 +75,7 @@ extern short getuid ();               /* If this causes portability problems,
                                   default to `int' anyway.  */
 #endif
 
-#if defined (__bsdi__) || defined (DECLARE_GETPWUID_WITH_UID_T)
+#ifdef DECLARE_GETPWUID_WITH_UID_T
 extern struct passwd *getpwuid (uid_t);
 extern struct passwd *getpwnam (const char *);
 #else
@@ -73,6 +83,8 @@ 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
@@ -113,7 +125,11 @@ x_get_customization_string (db, name, class)
   result = x_get_string_resource (db, full_name, full_class);
 
   if (result)
-    return strcpy ((char *) malloc (strlen (result) + 1), result);
+    {
+      char *copy = (char *) malloc (strlen (result) + 1);
+      strcpy (copy, result);
+      return copy;
+    }
   else
     return 0;
 }
@@ -166,7 +182,7 @@ magic_file_p (string, string_len, class, escaped_suffix, suffix)
   while (p < string + string_len)
     {
       /* The chunk we're about to stick on the end of result.  */
-      char *next;
+      char *next = NULL;
       int next_len;
 
       if (*p == '%')
@@ -212,11 +228,11 @@ magic_file_p (string, string_len, class, escaped_suffix, suffix)
                    free (path);
                    return NULL;
                  }
-               
+
                next = lang;
                next_len = strlen (next);
                break;
-             
+
              case 't':
              case 'c':
                free (path);
@@ -232,7 +248,7 @@ magic_file_p (string, string_len, class, escaped_suffix, suffix)
          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;
 
@@ -278,26 +294,23 @@ magic_file_p (string, string_len, class, escaped_suffix, suffix)
 static char *
 gethomedir ()
 {
-  int uid;
   struct passwd *pw;
   char *ptr;
   char *copy;
 
   if ((ptr = getenv ("HOME")) == NULL)
     {
-      if ((ptr = getenv ("USER")) != NULL)
+      if ((ptr = getenv ("LOGNAME")) != NULL
+         || (ptr = getenv ("USER")) != NULL)
        pw = getpwnam (ptr);
       else
-       {
-         uid = getuid ();
-         pw = getpwuid (uid);
-       }
+       pw = getpwuid (getuid ());
 
       if (pw)
        ptr = pw->pw_dir;
     }
 
-  if (ptr == NULL) 
+  if (ptr == NULL)
     return "/";
 
   copy = (char *) malloc (strlen (ptr) + 2);
@@ -309,19 +322,19 @@ gethomedir ()
 
 
 static int
-file_p (path)
-     char *path;
+file_p (filename)
+     char *filename;
 {
   struct stat status;
 
-  return (access (path, 4) == 0                        /* exists and is readable */
-         && stat (path, &status) == 0          /* get the status */
-         && (status.st_mode & S_IFDIR) == 0);  /* not a directory */
+  return (access (filename, 4) == 0             /* exists and is readable */
+         && stat (filename, &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 
+   after expanding the %-escapes.  Return 0 if we didn't find any, and
    the path name of the one we found otherwise.  */
 
 static char *
@@ -334,23 +347,19 @@ search_magic_path (search_path, class, escaped_suffix, suffix)
     {
       for (p = s; *p && *p != ':'; p++)
        ;
-      
-      if (*p == ':' && *(p + 1) == ':')
-       {
-         char *path;
 
-         s = "%N%S";
-         path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
+      if (p > s)
+       {
+         char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
          if (path)
            return path;
-
-         s = p + 1;
-         continue;
        }
-
-      if (p > s)
+      else if (*p == ':')
        {
-         char *path = magic_file_p (s, p - s, class, escaped_suffix, suffix);
+         char *path;
+
+         s = "%N%S";
+         path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
          if (path)
            return path;
        }
@@ -364,8 +373,6 @@ search_magic_path (search_path, class, escaped_suffix, suffix)
 \f
 /* 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;
@@ -374,7 +381,7 @@ get_system_app (class)
   char *path;
 
   path = getenv ("XFILESEARCHPATH");
-  if (! path) path = X_DEFAULT_SEARCH_PATH;
+  if (! path) path = PATH_X_DEFAULTS;
 
   path = search_magic_path (path, class, 0, 0);
   if (path)
@@ -391,8 +398,6 @@ static XrmDatabase
 get_fallback (display)
      Display *display;
 {
-  XrmDatabase db;
-
   return NULL;
 }
 
@@ -403,6 +408,7 @@ get_user_app (class)
 {
   char *path;
   char *file = 0;
+  char *free_it = 0;
 
   /* Check for XUSERFILESEARCHPATH.  It is a path of complete file
      names, not directories.  */
@@ -414,19 +420,23 @@ get_user_app (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)))))
+      || (free_it = gethomedir (),
+         ((file = search_magic_path (free_it, class, "%L/%N", 0))
+          || (file = search_magic_path (free_it, class, "%N", 0)))))
     {
       XrmDatabase db = XrmGetFileDatabase (file);
       free (file);
+      if (free_it)
+       free (free_it);
       return db;
     }
-  else
-    return NULL;
+
+  if (free_it)
+    free (free_it);
+  return NULL;
 }
 
 
@@ -459,8 +469,7 @@ get_user_db (display)
       free (xdefault);
     }
 
-#ifdef XlibSpecificationRelease
-#if XlibSpecificationRelease >= 5
+#ifdef HAVE_XSCREENRESOURCESTRING
   /* Get the screen-specific resources too.  */
   xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
   if (xdefs != NULL)
@@ -468,7 +477,6 @@ get_user_db (display)
       XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
       XFree (xdefs);
     }
-#endif
 #endif
 
   return db;
@@ -479,26 +487,12 @@ get_environ_db ()
 {
   XrmDatabase db;
   char *p;
-  char *path = 0, *home = 0, *host = 0;
+  char *path = 0, *home = 0, *host;
 
   if ((p = getenv ("XENVIRONMENT")) == NULL)
     {
       home = gethomedir ();
-
-      {
-       int host_size = 100;
-       host = (char *) malloc (host_size);
-       
-       for (;;)
-         {
-           host[host_size - 1] = '\0';
-           gethostname (host, host_size - 1);
-           if (strlen (host) < host_size - 1)
-             break;
-           host = (char *) realloc (host, host_size *= 2);
-         }
-      }
-
+      host = get_system_name ();
       path = (char *) malloc (strlen (home)
                              + sizeof (".Xdefaults-")
                              + strlen (host));
@@ -510,7 +504,6 @@ get_environ_db ()
 
   if (path) free (path);
   if (home) free (home);
-  if (host) free (host);
 
   return db;
 }
@@ -529,15 +522,90 @@ x_load_resources (display, xrm_string, myname, myclass)
      Display *display;
      char *xrm_string, *myname, *myclass;
 {
-  char *xdefs;
   XrmDatabase user_database;
   XrmDatabase rdb;
   XrmDatabase db;
+  char line[256];
+
+  char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
+
+#ifdef USE_MOTIF
+  char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
+  extern Lisp_Object Vdouble_click_time;
+#endif
 
   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 ("");
 
+  /* Add some font defaults.  If the font `helv' doesn't exist, widgets
+     will use some other default font.  */
+#ifdef USE_MOTIF
+
+  sprintf (line, "%s.pane.background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fontList: %s", myclass, helv);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*menu*background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*menubar*background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s.dialog*.background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb.Text.background: white", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb.FilterText.background: white", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb*DirList.background: white", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb*background: grey75", myclass);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
+  XrmPutLineResource (&rdb, line);
+
+  /* Set double click time of list boxes in the file selection
+     dialog from `double-click-time'.  */
+  if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
+    {
+      sprintf (line, "%s*fsb*DirList.doubleClickInterval: %d",
+              myclass, XFASTINT (Vdouble_click_time));
+      XrmPutLineResource (&rdb, line);
+      sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %d",
+              myclass, XFASTINT (Vdouble_click_time));
+      XrmPutLineResource (&rdb, line);
+    }
+
+#else /* not USE_MOTIF */
+
+  sprintf (line, "Emacs.dialog*.font: %s", helv);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "Emacs.dialog*.background: grey75");
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "*XlwMenu*font: %s", helv);
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "*XlwMenu*background: grey75");
+  XrmPutLineResource (&rdb, line);
+  sprintf (line, "Emacs*verticalScrollBar.background: grey75");
+  XrmPutLineResource (&rdb, line);
+
+#endif /* not USE_MOTIF */
+
   user_database = get_user_db (display);
 
   /* Figure out what the "customization string" is, so we can use it
@@ -570,7 +638,7 @@ x_load_resources (display, xrm_string, myname, myclass)
   db = get_environ_db ();
   if (db != NULL)
     XrmMergeDatabases (db, &rdb);
-  
+
   /* Last, merge in any specification from the command line. */
   if (xrm_string != NULL)
     {
@@ -745,3 +813,6 @@ main (argc, argv)
   XCloseDisplay (display);
 }
 #endif /* TESTRM */
+
+/* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
+   (do not change this comment) */