remove Lisp_Free struct type
[bpt/emacs.git] / src / xrdb.c
index 73672c9..589856d 100644 (file)
@@ -1,5 +1,6 @@
 /* Deal with the X Resource Manager.
-   Copyright (C) 1990, 1993-1994, 2000-2012 Free Software Foundation, Inc.
+   Copyright (C) 1990, 1993-1994, 2000-2014 Free Software Foundation,
+   Inc.
 
 Author: Joseph Arceneaux
 Created: 4/90
@@ -24,7 +25,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <unistd.h>
 #include <errno.h>
 #include <epaths.h>
-
+#include <stdlib.h>
 #include <stdio.h>
 
 #include "lisp.h"
@@ -41,23 +42,12 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
-#include <sys/stat.h>
 
 #ifdef USE_MOTIF
 /* For Vdouble_click_time.  */
 #include "keyboard.h"
 #endif
 
-extern char *getenv (const char *);
-
-extern struct passwd *getpwuid (uid_t);
-extern struct passwd *getpwnam (const char *);
-
-char *x_get_string_resource (XrmDatabase rdb, const char *name,
-                            const char *class);
-static int file_p (const char *filename);
-
-\f
 /* X file search path processing.  */
 
 
@@ -81,18 +71,9 @@ x_get_customization_string (XrmDatabase db, const char *name,
   sprintf (full_class, "%s.%s", class, "Customization");
 
   result = x_get_string_resource (db, full_name, full_class);
-
-  if (result)
-    {
-      char *copy = xmalloc (strlen (result) + 1);
-      strcpy (copy, result);
-      return copy;
-    }
-  else
-    return 0;
+  return result ? xstrdup (result) : NULL;
 }
 
-
 /* Expand all the Xt-style %-escapes in STRING, whose length is given
    by STRING_LEN.  Here are the escapes we're supposed to recognize:
 
@@ -113,7 +94,7 @@ x_get_customization_string (XrmDatabase db, const char *name,
                database associated with display.
                (This is x_customization_string.)
 
-   Return the expanded file name if it exists and is readable, and
+   Return the resource database if its file was read successfully, and
    refers to %L only when the LANG environment variable is set, or
    otherwise provided by X.
 
@@ -122,14 +103,15 @@ x_get_customization_string (XrmDatabase db, const char *name,
 
    Return NULL otherwise.  */
 
-static char *
-magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
-             const char *escaped_suffix)
+static XrmDatabase
+magic_db (const char *string, ptrdiff_t string_len, const char *class,
+         const char *escaped_suffix)
 {
+  XrmDatabase db;
   char *lang = getenv ("LANG");
 
   ptrdiff_t path_size = 100;
-  char *path = xmalloc (path_size);
+  char *path = xmalloc_atomic (path_size);
   ptrdiff_t path_len = 0;
 
   const char *p = string;
@@ -197,7 +179,7 @@ magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
       else
        next = p, next_len = 1;
 
-      /* Do we have room for this component followed by a '\0' ?  */
+      /* Do we have room for this component followed by a '\0'?  */
       if (path_size - path_len <= next_len)
        {
          if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
@@ -222,14 +204,9 @@ magic_file_p (const char *string, ptrdiff_t string_len, const char *class,
     }
 
   path[path_len] = '\0';
-
-  if (! file_p (path))
-    {
-      xfree (path);
-      return NULL;
-    }
-
-  return path;
+  db = XrmGetFileDatabase (path);
+  xfree (path);
+  return db;
 }
 
 
@@ -255,22 +232,9 @@ gethomedir (void)
   if (ptr == NULL)
     return xstrdup ("/");
 
-  copy = xmalloc (strlen (ptr) + 2);
+  copy = xmalloc_atomic (strlen (ptr) + 2);
   strcpy (copy, ptr);
-  strcat (copy, "/");
-
-  return copy;
-}
-
-
-static int
-file_p (const char *filename)
-{
-  struct stat status;
-
-  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 */
+  return strcat (copy, "/");
 }
 
 
@@ -278,7 +242,7 @@ file_p (const char *filename)
    after expanding the %-escapes.  Return 0 if we didn't find any, and
    the path name of the one we found otherwise.  */
 
-static char *
+static XrmDatabase
 search_magic_path (const char *search_path, const char *class,
                   const char *escaped_suffix)
 {
@@ -291,18 +255,16 @@ search_magic_path (const char *search_path, const char *class,
 
       if (p > s)
        {
-         char *path = magic_file_p (s, p - s, class, escaped_suffix);
-         if (path)
-           return path;
+         XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
+         if (db)
+           return db;
        }
       else if (*p == ':')
        {
-         char *path;
-
-         s = "%N%S";
-         path = magic_file_p (s, strlen (s), class, escaped_suffix);
-         if (path)
-           return path;
+         static char const ns[] = "%N%S";
+         XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
+         if (db)
+           return db;
        }
 
       if (*p == ':')
@@ -317,21 +279,12 @@ search_magic_path (const char *search_path, const char *class,
 static XrmDatabase
 get_system_app (const char *class)
 {
-  XrmDatabase db = NULL;
   const char *path;
-  char *p;
 
   path = getenv ("XFILESEARCHPATH");
   if (! path) path = PATH_X_DEFAULTS;
 
-  p = search_magic_path (path, class, 0);
-  if (p)
-    {
-      db = XrmGetFileDatabase (p);
-      xfree (p);
-    }
-
-  return db;
+  return search_magic_path (path, class, 0);
 }
 
 
@@ -345,35 +298,40 @@ get_fallback (Display *display)
 static XrmDatabase
 get_user_app (const char *class)
 {
+  XrmDatabase db = 0;
   const char *path;
-  char *file = 0;
-  char *free_it = 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)))
+  path = getenv ("XUSERFILESEARCHPATH");
+  if (path)
+    db = search_magic_path (path, class, 0);
 
+  if (! db)
+    {
       /* 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"))
-             || (file = search_magic_path (path, class, "/%N"))))
+      path = getenv ("XAPPLRESDIR");
+      if (path)
+       {
+         db = search_magic_path (path, class, "/%L/%N");
+         if (!db)
+           db = search_magic_path (path, class, "/%N");
+       }
+    }
 
+  if (! db)
+    {
       /* Check in the home directory.  This is a bit of a hack; let's
         hope one's home directory doesn't contain any %-escapes.  */
-      || (free_it = gethomedir (),
-         ((file = search_magic_path (free_it, class, "%L/%N"))
-          || (file = search_magic_path (free_it, class, "%N")))))
-    {
-      XrmDatabase db = XrmGetFileDatabase (file);
-      xfree (file);
-      xfree (free_it);
-      return db;
+      char *home = gethomedir ();
+      db = search_magic_path (home, class, "%L/%N");
+      if (! db)
+       db = search_magic_path (home, class, "%N");
+      xfree (home);
     }
 
-  xfree (free_it);
-  return NULL;
+  return db;
 }
 
 
@@ -397,7 +355,7 @@ get_user_db (Display *display)
       char *xdefault;
 
       home = gethomedir ();
-      xdefault = xmalloc (strlen (home) + sizeof ".Xdefaults");
+      xdefault = xmalloc_atomic (strlen (home) + sizeof (".Xdefaults"));
       strcpy (xdefault, home);
       strcat (xdefault, ".Xdefaults");
       db = XrmGetFileDatabase (xdefault);
@@ -429,8 +387,9 @@ get_environ_db (void)
     {
       static char const xdefaults[] = ".Xdefaults-";
       char *home = gethomedir ();
-      char const *host = get_system_name ();
-      ptrdiff_t pathsize = strlen (home) + sizeof xdefaults + strlen (host);
+      char const *host = SSDATA (Vsystem_name);
+      ptrdiff_t pathsize = (strlen (home) + sizeof xdefaults
+                           + SBYTES (Vsystem_name));
       path = xrealloc (home, pathsize);
       strcat (strcat (path, xdefaults), host);
       p = path;
@@ -631,7 +590,7 @@ x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
   if (x_get_resource (rdb, name, class, x_rm_string, &value))
     return (char *) value.addr;
 
-  return (char *) 0;
+  return 0;
 }
 \f
 /* Stand-alone test facilities.  */
@@ -660,10 +619,7 @@ member (char *elt, List list)
 static void
 fatal (char *msg, char *prog)
 {
-  if (errno)
-    perror (prog);
-
-  (void) fprintf (stderr, msg, prog);
+  fprintf (stderr, msg, prog);
   exit (1);
 }
 
@@ -684,10 +640,7 @@ main (int argc, char **argv)
     displayname = "localhost:0.0";
 
   lp = member ("-xrm", arg_list);
-  if (! NIL (lp))
-    resource_string = car (cdr (lp));
-  else
-    resource_string = (char *) 0;
+  resource_string = NIL (lp) ? 0 : car (cdr (lp));
 
   lp = member ("-c", arg_list);
   if (! NIL (lp))