Blame SOURCES/bash-4.3-dircomp-append-slash.patch

705d00
diff --git a/bashline.c b/bashline.c
705d00
--- a/bashline.c
705d00
+++ b/bashline.c
705d00
@@ -117,6 +117,7 @@ static char *restore_tilde __P((char *, char *));
705d00
 
705d00
 static char *bash_filename_rewrite_hook __P((char *, int));
705d00
 static void bash_directory_expansion __P((char **));
705d00
+static int bash_filename_stat_hook __P((char **));
705d00
 static int bash_directory_completion_hook __P((char **));
705d00
 static int filename_completion_ignore __P((char **));
705d00
 static int bash_push_line __P((void));
705d00
@@ -1414,7 +1415,7 @@ bash_default_completion (text, start, end, qc, compflags)
705d00
      const char *text;
705d00
      int start, end, qc, compflags;
705d00
 {
705d00
-  char **matches;
705d00
+  char **matches, *t;
705d00
 
705d00
   matches = (char **)NULL;
705d00
 
705d00
@@ -1424,7 +1425,19 @@ bash_default_completion (text, start, end, qc, compflags)
705d00
       if (qc != '\'' && text[1] == '(') /* ) */
705d00
 	matches = rl_completion_matches (text, command_subst_completion_function);
705d00
       else
705d00
-	matches = rl_completion_matches (text, variable_completion_function);
705d00
+	{
705d00
+	  matches = rl_completion_matches (text, variable_completion_function);
705d00
+	  if (matches && matches[0] && matches[1] == 0)
705d00
+	    {
705d00
+	      t = savestring (matches[0]);
705d00
+	      bash_filename_stat_hook (&t);
705d00
+	      /* doesn't use test_for_directory because that performs tilde
705d00
+		 expansion */
705d00
+	      if (file_isdir (t))
705d00
+		rl_completion_append_character = '/';
705d00
+	      free (t);
705d00
+	    }
705d00
+	}
705d00
     }
705d00
 
705d00
   /* If the word starts in `~', and there is no slash in the word, then
705d00
@@ -2763,6 +2776,57 @@ restore_directory_hook (hookf)
705d00
     rl_directory_rewrite_hook = hookf;
705d00
 }
705d00
 
705d00
+static int
705d00
+bash_filename_stat_hook (dirname)
705d00
+     char **dirname;
705d00
+{
705d00
+  char *local_dirname, *new_dirname, *t;
705d00
+  int should_expand_dirname, return_value;
705d00
+  WORD_LIST *wl;
705d00
+  struct stat sb;
705d00
+
705d00
+  local_dirname = *dirname;
705d00
+  should_expand_dirname = return_value = 0;
705d00
+  if (t = mbschr (local_dirname, '$'))
705d00
+    should_expand_dirname = '$';
705d00
+  else if (t = mbschr (local_dirname, '`'))	/* XXX */
705d00
+    should_expand_dirname = '`';
705d00
+
705d00
+#if defined (HAVE_LSTAT)
705d00
+  if (should_expand_dirname && lstat (local_dirname, &sb) == 0)
705d00
+#else
705d00
+  if (should_expand_dirname && stat (local_dirname, &sb) == 0)
705d00
+#endif
705d00
+    should_expand_dirname = 0;
705d00
+  
705d00
+  if (should_expand_dirname)  
705d00
+    {
705d00
+      new_dirname = savestring (local_dirname);
705d00
+      wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB);    /* does the right thing */ 
705d00
+      if (wl)
705d00
+	{
705d00
+	  free (new_dirname);
705d00
+	  new_dirname = string_list (wl);
705d00
+	  /* Tell the completer we actually expanded something and change
705d00
+	     *dirname only if we expanded to something non-null -- stat
705d00
+	     behaves unpredictably when passed null or empty strings */
705d00
+	  if (new_dirname && *new_dirname)
705d00
+	    {
705d00
+          free (local_dirname); /* XXX */
705d00
+	      local_dirname = *dirname = new_dirname; 
705d00
+	      return_value = STREQ (local_dirname, *dirname) == 0;
705d00
+	    }
705d00
+      else
705d00
+	      free (new_dirname);
705d00
+	  dispose_words (wl);
705d00
+	}
705d00
+      else
705d00
+	free (new_dirname);
705d00
+    }	
705d00
+
705d00
+  return (return_value);
705d00
+}
705d00
+
705d00
 /* Handle symbolic link references and other directory name
705d00
    expansions while hacking completion.  This should return 1 if it modifies
705d00
    the DIRNAME argument, 0 otherwise.  It should make sure not to modify