Import of readline 4.1
[deliverable/binutils-gdb.git] / readline / tilde.c
index 3741f9767b23652c5514acd7dd0ce12daf682cb2..777b6559223798df6b1c1a5073d5f5f495614afa 100644 (file)
@@ -7,7 +7,7 @@
 
    Readline 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 1, or (at your option) any
+   Free Software Foundation; either version 2, or (at your option) any
    later version.
 
    Readline is distributed in the hope that it will be useful, but
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU General Public License
    along with Readline; see the file COPYING.  If not, write to the Free
-   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #if defined (HAVE_CONFIG_H)
 #  include <config.h>
 
 #include "tilde.h"
 
-#ifdef SHELL
-#include "shell.h"
-#endif
+#if defined (TEST) || defined (STATIC_MALLOC)
+static char *xmalloc (), *xrealloc ();
+#else
+#  if defined __STDC__
+extern char *xmalloc (int);
+extern char *xrealloc (void *, int);
+#  else
+extern char *xmalloc (), *xrealloc ();
+#  endif /* !__STDC__ */
+#endif /* TEST || STATIC_MALLOC */
 
 #if !defined (HAVE_GETPW_DECLS)
 extern struct passwd *getpwuid (), *getpwnam ();
 #endif /* !HAVE_GETPW_DECLS */
 
 #if !defined (savestring)
-extern char *xmalloc ();
 #  ifndef strcpy
 extern char *strcpy ();
 #  endif
@@ -71,11 +77,11 @@ extern char *strcpy ();
 #  endif /* !__STDC__ */
 #endif /* !NULL */
 
-#if defined (TEST) || defined (STATIC_MALLOC)
-static char *xmalloc (), *xrealloc ();
-#else
-extern char *xmalloc (), *xrealloc ();
-#endif /* TEST || STATIC_MALLOC */
+/* If being compiled as part of bash, these will be satisfied from
+   variables.o.  If being compiled as part of readline, they will
+   be satisfied from shell.o. */
+extern char *get_home_dir __P((void));
+extern char *get_env_value __P((char *));
 
 /* The default value of tilde_additional_prefixes.  This is set to
    whitespace preceding a tilde so that simple programs which do not
@@ -120,7 +126,9 @@ tilde_find_prefix (string, len)
      int *len;
 {
   register int i, j, string_len;
-  register char **prefixes = tilde_additional_prefixes;
+  register char **prefixes;
+
+  prefixes = tilde_additional_prefixes;
 
   string_len = strlen (string);
   *len = 0;
@@ -159,7 +167,11 @@ tilde_find_suffix (string)
 
   for (i = 0; i < string_len; i++)
     {
+#if defined (__MSDOS__)
+      if (string[i] == '/' || string[i] == '\\' /* || !string[i] */)
+#else
       if (string[i] == '/' /* || !string[i] */)
+#endif
        break;
 
       for (j = 0; suffixes && suffixes[j]; j++)
@@ -171,15 +183,6 @@ tilde_find_suffix (string)
   return (i);
 }
 
-#if !defined (SHELL)
-static char *
-get_string_value (varname)
-     char *varname;
-{
-  return ((char *)getenv (varname));
-}
-#endif
-
 /* Return a new string which is the result of tilde expanding STRING. */
 char *
 tilde_expand (string)
@@ -232,11 +235,18 @@ tilde_expand (string)
       free (tilde_word);
 
       len = strlen (expansion);
-      if ((result_index + len + 1) > result_size)
-       result = xrealloc (result, 1 + (result_size += (len + 20)));
+#ifdef __CYGWIN32__
+      /* Fix for Cygwin to prevent ~user/xxx from expanding to //xxx when
+         $HOME for `user' is /.  On cygwin, // denotes a network drive. */
+      if (len > 1 || *expansion != '/' || *string != '/')
+#endif
+       {
+         if ((result_index + len + 1) > result_size)
+           result = xrealloc (result, 1 + (result_size += (len + 20)));
 
-      strcpy (result + result_index, expansion);
-      result_index += len;
+         strcpy (result + result_index, expansion);
+         result_index += len;
+       }
       free (expansion);
     }
 
@@ -257,7 +267,11 @@ isolate_tilde_prefix (fname, lenp)
   int i;
 
   ret = xmalloc (strlen (fname));
+#if defined (__MSDOS__)
+  for (i = 1; fname[i] && fname[i] != '/' && fname[i] != '\\'; i++)
+#else
   for (i = 1; fname[i] && fname[i] != '/'; i++)
+#endif
     ret[i - 1] = fname[i];
   ret[i - 1] = '\0';
   if (lenp)
@@ -278,33 +292,12 @@ glue_prefix_and_suffix (prefix, suffix, suffind)
   plen = (prefix && *prefix) ? strlen (prefix) : 0;
   slen = strlen (suffix + suffind);
   ret = xmalloc (plen + slen + 1);
-  if (prefix && *prefix)
+  if (plen)
     strcpy (ret, prefix);
   strcpy (ret + plen, suffix + suffind);
   return ret;
 }
 
-static char *
-get_home_dir ()
-{
-  char *home_dir;
-
-#ifdef SHELL
-  home_dir = (char *)NULL;
-  if (current_user.home_dir == 0)
-    get_current_user_info ();
-  home_dir = current_user.home_dir;
-#else
-  struct passwd *entry;
-
-  home_dir = (char *)NULL;
-  entry = getpwuid (getuid ());
-  if (entry)
-    home_dir = entry->pw_dir;
-#endif
-  return (home_dir);
-}
-
 /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
    tilde.  If there is no expansion, call tilde_expansion_failure_hook.
    This always returns a newly-allocated string, never static storage. */
@@ -328,7 +321,7 @@ tilde_expand_word (filename)
   if (filename[1] == '\0' || filename[1] == '/')
     {
       /* Prefix $HOME to the rest of the string. */
-      expansion = get_string_value ("HOME");
+      expansion = get_env_value ("HOME");
 
       /* If there is no HOME variable, look up the directory in
         the password database. */
This page took 0.025575 seconds and 4 git commands to generate.