07a490
			     BASH PATCH REPORT
07a490
			     =================
07a490
07a490
Bash-Release:	4.2
07a490
Patch-ID:	bash42-029
07a490
07a490
Bug-Reported-by:	"Michael Kalisz" <michael@kalisz.homelinux.net>
07a490
Bug-Reference-ID:	<50241.78.69.11.112.1298585641.squirrel@kalisz.homelinux.net>
07a490
Bug-Reference-URL:	http://lists.gnu.org/archive/html/bug-bash/2011-02/msg00274.html
07a490
07a490
Bug-Description:
07a490
07a490
Bash-4.2 tries to leave completed directory names as the user typed them,
07a490
without expanding them to a full pathname.  One effect of this is that
07a490
shell variables used in pathnames being completed (e.g., $HOME) are left
07a490
unchanged, but the `$' is quoted by readline because it is a special
07a490
character to the shell.
07a490
07a490
This patch introduces two things:
07a490
07a490
1.  A new shell option, `direxpand', which, if set, attempts to emulate the
07a490
    bash-4.1 behavior of expanding words to full pathnames during
07a490
    completion;
07a490
2.  A set of heuristics that reduce the number of times special characters
07a490
    such as `$' are quoted when the directory name is not expanded.
07a490
07a490
Patch (apply with `patch -p0'):
07a490
07a490
diff -NrC 2 ../bash-4.2-patched/bashline.c ./bashline.c
07a490
*** ../bash-4.2-patched/bashline.c	2011-01-16 15:32:47.000000000 -0500
07a490
--- ./bashline.c	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 122,125 ****
07a490
--- 122,128 ----
07a490
  static int bash_push_line __P((void));
07a490
  
07a490
+ static rl_icppfunc_t *save_directory_hook __P((void));
07a490
+ static void reset_directory_hook __P((rl_icppfunc_t *));
07a490
+ 
07a490
  static void cleanup_expansion_error __P((void));
07a490
  static void maybe_make_readline_line __P((char *));
07a490
***************
07a490
*** 244,251 ****
07a490
--- 247,261 ----
07a490
  int dircomplete_spelling = 0;
07a490
  
07a490
+ /* Expand directory names during word/filename completion. */
07a490
+ int dircomplete_expand = 0;
07a490
+ int dircomplete_expand_relpath = 0;
07a490
+ 
07a490
  static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";
07a490
  static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";
07a490
  /* )) */
07a490
  
07a490
+ static const char *default_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~";	/*}*/
07a490
+ static char *custom_filename_quote_characters = 0;
07a490
+ 
07a490
  static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;
07a490
  
07a490
***************
07a490
*** 502,506 ****
07a490
    /* Tell the completer that we might want to follow symbolic links or
07a490
       do other expansion on directory names. */
07a490
!   rl_directory_rewrite_hook = bash_directory_completion_hook;
07a490
  
07a490
    rl_filename_rewrite_hook = bash_filename_rewrite_hook;
07a490
--- 512,516 ----
07a490
    /* Tell the completer that we might want to follow symbolic links or
07a490
       do other expansion on directory names. */
07a490
!   set_directory_hook ();
07a490
  
07a490
    rl_filename_rewrite_hook = bash_filename_rewrite_hook;
07a490
***************
07a490
*** 530,534 ****
07a490
  
07a490
    /* characters that need to be quoted when appearing in filenames. */
07a490
!   rl_filename_quote_characters = " \t\n\\\"'@<>=;|&()#$`?*[!:{~";	/*}*/
07a490
  
07a490
    rl_filename_quoting_function = bash_quote_filename;
07a490
--- 540,544 ----
07a490
  
07a490
    /* characters that need to be quoted when appearing in filenames. */
07a490
!   rl_filename_quote_characters = default_filename_quote_characters;
07a490
  
07a490
    rl_filename_quoting_function = bash_quote_filename;
07a490
***************
07a490
*** 565,570 ****
07a490
    rl_attempted_completion_function = attempt_shell_completion;
07a490
    rl_completion_entry_function = NULL;
07a490
-   rl_directory_rewrite_hook = bash_directory_completion_hook;
07a490
    rl_ignore_some_completions_function = filename_completion_ignore;
07a490
  }
07a490
  
07a490
--- 575,582 ----
07a490
    rl_attempted_completion_function = attempt_shell_completion;
07a490
    rl_completion_entry_function = NULL;
07a490
    rl_ignore_some_completions_function = filename_completion_ignore;
07a490
+   rl_filename_quote_characters = default_filename_quote_characters;
07a490
+ 
07a490
+   set_directory_hook ();
07a490
  }
07a490
  
07a490
***************
07a490
*** 1280,1283 ****
07a490
--- 1292,1298 ----
07a490
    rl_ignore_some_completions_function = filename_completion_ignore;
07a490
  
07a490
+   rl_filename_quote_characters = default_filename_quote_characters;
07a490
+   set_directory_hook ();
07a490
+ 
07a490
    /* Determine if this could be a command word.  It is if it appears at
07a490
       the start of the line (ignoring preceding whitespace), or if it
07a490
***************
07a490
*** 1592,1595 ****
07a490
--- 1607,1616 ----
07a490
  	  else
07a490
  	    {
07a490
+ 	     if (dircomplete_expand && dot_or_dotdot (filename_hint))
07a490
+ 		{
07a490
+ 		  dircomplete_expand = 0;
07a490
+ 		  set_directory_hook ();
07a490
+ 		  dircomplete_expand = 1;
07a490
+ 		}
07a490
  	      mapping_over = 4;
07a490
  	      goto inner;
07a490
***************
07a490
*** 1792,1795 ****
07a490
--- 1813,1819 ----
07a490
   inner:
07a490
    val = rl_filename_completion_function (filename_hint, istate);
07a490
+   if (mapping_over == 4 && dircomplete_expand)
07a490
+     set_directory_hook ();
07a490
+ 
07a490
    istate = 1;
07a490
  
07a490
***************
07a490
*** 2694,2697 ****
07a490
--- 2718,2767 ----
07a490
  }
07a490
  
07a490
+ /* Functions to save and restore the appropriate directory hook */
07a490
+ /* This is not static so the shopt code can call it */
07a490
+ void
07a490
+ set_directory_hook ()
07a490
+ {
07a490
+   if (dircomplete_expand)
07a490
+     {
07a490
+       rl_directory_completion_hook = bash_directory_completion_hook;
07a490
+       rl_directory_rewrite_hook = (rl_icppfunc_t *)0;
07a490
+     }
07a490
+   else
07a490
+     {
07a490
+       rl_directory_rewrite_hook = bash_directory_completion_hook;
07a490
+       rl_directory_completion_hook = (rl_icppfunc_t *)0;
07a490
+     }
07a490
+ }
07a490
+ 
07a490
+ static rl_icppfunc_t *
07a490
+ save_directory_hook ()
07a490
+ {
07a490
+   rl_icppfunc_t *ret;
07a490
+ 
07a490
+   if (dircomplete_expand)
07a490
+     {
07a490
+       ret = rl_directory_completion_hook;
07a490
+       rl_directory_completion_hook = (rl_icppfunc_t *)NULL;
07a490
+     }
07a490
+   else
07a490
+     {
07a490
+       ret = rl_directory_rewrite_hook;
07a490
+       rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
07a490
+     }
07a490
+ 
07a490
+   return ret;
07a490
+ }
07a490
+ 
07a490
+ static void
07a490
+ restore_directory_hook (hookf)
07a490
+      rl_icppfunc_t *hookf;
07a490
+ {
07a490
+   if (dircomplete_expand)
07a490
+     rl_directory_completion_hook = hookf;
07a490
+   else
07a490
+     rl_directory_rewrite_hook = hookf;
07a490
+ }
07a490
+ 
07a490
  /* Handle symbolic link references and other directory name
07a490
     expansions while hacking completion.  This should return 1 if it modifies
07a490
***************
07a490
*** 2703,2720 ****
07a490
  {
07a490
    char *local_dirname, *new_dirname, *t;
07a490
!   int return_value, should_expand_dirname;
07a490
    WORD_LIST *wl;
07a490
    struct stat sb;
07a490
  
07a490
!   return_value = should_expand_dirname = 0;
07a490
    local_dirname = *dirname;
07a490
  
07a490
!   if (mbschr (local_dirname, '$'))
07a490
!     should_expand_dirname = 1;
07a490
    else
07a490
      {
07a490
        t = mbschr (local_dirname, '`');
07a490
        if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
07a490
! 	should_expand_dirname = 1;
07a490
      }
07a490
  
07a490
--- 2773,2801 ----
07a490
  {
07a490
    char *local_dirname, *new_dirname, *t;
07a490
!   int return_value, should_expand_dirname, nextch, closer;
07a490
    WORD_LIST *wl;
07a490
    struct stat sb;
07a490
  
07a490
!   return_value = should_expand_dirname = nextch = closer = 0;
07a490
    local_dirname = *dirname;
07a490
  
07a490
!   if (t = mbschr (local_dirname, '$'))
07a490
!     {
07a490
!       should_expand_dirname = '$';
07a490
!       nextch = t[1];
07a490
!       /* Deliberately does not handle the deprecated $[...] arithmetic
07a490
! 	 expansion syntax */
07a490
!       if (nextch == '(')
07a490
! 	closer = ')';
07a490
!       else if (nextch == '{')
07a490
! 	closer = '}';
07a490
!       else
07a490
! 	nextch = 0;
07a490
!     }
07a490
    else
07a490
      {
07a490
        t = mbschr (local_dirname, '`');
07a490
        if (t && unclosed_pair (local_dirname, strlen (local_dirname), "`") == 0)
07a490
! 	should_expand_dirname = '`';
07a490
      }
07a490
  
07a490
***************
07a490
*** 2740,2743 ****
07a490
--- 2821,2841 ----
07a490
  	  dispose_words (wl);
07a490
  	  local_dirname = *dirname;
07a490
+ 	  /* XXX - change rl_filename_quote_characters here based on
07a490
+ 	     should_expand_dirname/nextch/closer.  This is the only place
07a490
+ 	     custom_filename_quote_characters is modified. */
07a490
+ 	  if (rl_filename_quote_characters && *rl_filename_quote_characters)
07a490
+ 	    {
07a490
+ 	      int i, j, c;
07a490
+ 	      i = strlen (default_filename_quote_characters);
07a490
+ 	      custom_filename_quote_characters = xrealloc (custom_filename_quote_characters, i+1);
07a490
+ 	      for (i = j = 0; c = default_filename_quote_characters[i]; i++)
07a490
+ 		{
07a490
+ 		  if (c == should_expand_dirname || c == nextch || c == closer)
07a490
+ 		    continue;
07a490
+ 		  custom_filename_quote_characters[j++] = c;
07a490
+ 		}
07a490
+ 	      custom_filename_quote_characters[j] = '\0';
07a490
+ 	      rl_filename_quote_characters = custom_filename_quote_characters;
07a490
+ 	    }
07a490
  	}
07a490
        else
07a490
***************
07a490
*** 2759,2762 ****
07a490
--- 2857,2871 ----
07a490
      }
07a490
  
07a490
+   /* no_symbolic_links == 0 -> use (default) logical view of the file system.
07a490
+      local_dirname[0] == '.' && local_dirname[1] == '/' means files in the
07a490
+      current directory (./).
07a490
+      local_dirname[0] == '.' && local_dirname[1] == 0 means relative pathnames
07a490
+      in the current directory (e.g., lib/sh).
07a490
+      XXX - should we do spelling correction on these? */
07a490
+ 
07a490
+   /* This is test as it was in bash-4.2: skip relative pathnames in current
07a490
+      directory.  Change test to
07a490
+       (local_dirname[0] != '.' || (local_dirname[1] && local_dirname[1] != '/'))
07a490
+      if we want to skip paths beginning with ./ also. */
07a490
    if (no_symbolic_links == 0 && (local_dirname[0] != '.' || local_dirname[1]))
07a490
      {
07a490
***************
07a490
*** 2764,2767 ****
07a490
--- 2873,2885 ----
07a490
        int len1, len2;
07a490
  
07a490
+       /* If we have a relative path
07a490
+       		(local_dirname[0] != '/' && local_dirname[0] != '.')
07a490
+ 	 that is canonical after appending it to the current directory, then
07a490
+ 	 	temp1 = temp2+'/'
07a490
+ 	 That is,
07a490
+ 	 	strcmp (temp1, temp2) == 0
07a490
+ 	 after adding a slash to temp2 below.  It should be safe to not
07a490
+ 	 change those.
07a490
+       */
07a490
        t = get_working_directory ("symlink-hook");
07a490
        temp1 = make_absolute (local_dirname, t);
07a490
***************
07a490
*** 2798,2802 ****
07a490
  	    }
07a490
  	}
07a490
!       return_value |= STREQ (local_dirname, temp2) == 0;
07a490
        free (local_dirname);
07a490
        *dirname = temp2;
07a490
--- 2916,2928 ----
07a490
  	    }
07a490
  	}
07a490
! 
07a490
!       /* dircomplete_expand_relpath == 0 means we want to leave relative
07a490
! 	 pathnames that are unchanged by canonicalization alone.
07a490
! 	 *local_dirname != '/' && *local_dirname != '.' == relative pathname
07a490
! 	 (consistent with general.c:absolute_pathname())
07a490
! 	 temp1 == temp2 (after appending a slash to temp2) means the pathname
07a490
! 	 is not changed by canonicalization as described above. */
07a490
!       if (dircomplete_expand_relpath || ((local_dirname[0] != '/' && local_dirname[0] != '.') && STREQ (temp1, temp2) == 0))
07a490
! 	return_value |= STREQ (local_dirname, temp2) == 0;
07a490
        free (local_dirname);
07a490
        *dirname = temp2;
07a490
***************
07a490
*** 3003,3012 ****
07a490
    orig_func = rl_completion_entry_function;
07a490
    orig_attempt_func = rl_attempted_completion_function;
07a490
-   orig_dir_func = rl_directory_rewrite_hook;
07a490
    orig_ignore_func = rl_ignore_some_completions_function;
07a490
    orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
07a490
    rl_completion_entry_function = rl_filename_completion_function;
07a490
    rl_attempted_completion_function = (rl_completion_func_t *)NULL;
07a490
-   rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;
07a490
    rl_ignore_some_completions_function = filename_completion_ignore;
07a490
    rl_completer_word_break_characters = " \t\n\"\'";
07a490
--- 3129,3139 ----
07a490
    orig_func = rl_completion_entry_function;
07a490
    orig_attempt_func = rl_attempted_completion_function;
07a490
    orig_ignore_func = rl_ignore_some_completions_function;
07a490
    orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
07a490
+ 
07a490
+   orig_dir_func = save_directory_hook ();
07a490
+ 
07a490
    rl_completion_entry_function = rl_filename_completion_function;
07a490
    rl_attempted_completion_function = (rl_completion_func_t *)NULL;
07a490
    rl_ignore_some_completions_function = filename_completion_ignore;
07a490
    rl_completer_word_break_characters = " \t\n\"\'";
07a490
***************
07a490
*** 3016,3023 ****
07a490
    rl_completion_entry_function = orig_func;
07a490
    rl_attempted_completion_function = orig_attempt_func;
07a490
-   rl_directory_rewrite_hook = orig_dir_func;
07a490
    rl_ignore_some_completions_function = orig_ignore_func;
07a490
    rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
07a490
  
07a490
    return r;
07a490
  }
07a490
--- 3143,3151 ----
07a490
    rl_completion_entry_function = orig_func;
07a490
    rl_attempted_completion_function = orig_attempt_func;
07a490
    rl_ignore_some_completions_function = orig_ignore_func;
07a490
    rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
07a490
  
07a490
+   restore_directory_hook (orig_dir_func);
07a490
+ 
07a490
    return r;
07a490
  }
07a490
diff -NrC 2 ../bash-4.2-patched/bashline.h ./bashline.h
07a490
*** ../bash-4.2-patched/bashline.h	2009-01-04 14:32:22.000000000 -0500
07a490
--- ./bashline.h	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 34,41 ****
07a490
--- 34,46 ----
07a490
  extern int bash_re_edit __P((char *));
07a490
  
07a490
+ extern void bashline_set_event_hook __P((void));
07a490
+ extern void bashline_reset_event_hook __P((void));
07a490
+ 
07a490
  extern int bind_keyseq_to_unix_command __P((char *));
07a490
  
07a490
  extern char **bash_default_completion __P((const char *, int, int, int, int));
07a490
  
07a490
+ void set_directory_hook __P((void));
07a490
+ 
07a490
  /* Used by programmable completion code. */
07a490
  extern char *command_word_completion_function __P((const char *, int));
07a490
diff -NrC 2 ../bash-4.2-patched/builtins/shopt.def ./builtins/shopt.def
07a490
*** ../bash-4.2-patched/builtins/shopt.def	2010-07-02 22:42:44.000000000 -0400
07a490
--- ./builtins/shopt.def	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 62,65 ****
07a490
--- 62,69 ----
07a490
  #include "bashgetopt.h"
07a490
  
07a490
+ #if defined (READLINE)
07a490
+ #  include "../bashline.h"
07a490
+ #endif
07a490
+ 
07a490
  #if defined (HISTORY)
07a490
  #  include "../bashhist.h"
07a490
***************
07a490
*** 95,99 ****
07a490
  extern int no_empty_command_completion;
07a490
  extern int force_fignore;
07a490
! extern int dircomplete_spelling;
07a490
  
07a490
  extern int enable_hostname_completion __P((int));
07a490
--- 99,103 ----
07a490
  extern int no_empty_command_completion;
07a490
  extern int force_fignore;
07a490
! extern int dircomplete_spelling, dircomplete_expand;
07a490
  
07a490
  extern int enable_hostname_completion __P((int));
07a490
***************
07a490
*** 122,125 ****
07a490
--- 126,133 ----
07a490
  #endif
07a490
  
07a490
+ #if defined (READLINE)
07a490
+ static int shopt_set_complete_direxpand __P((char *, int));
07a490
+ #endif
07a490
+ 
07a490
  static int shopt_login_shell;
07a490
  static int shopt_compat31;
07a490
***************
07a490
*** 151,154 ****
07a490
--- 159,163 ----
07a490
    { "compat41", &shopt_compat41, set_compatibility_level },
07a490
  #if defined (READLINE)
07a490
+   { "direxpand", &dircomplete_expand, shopt_set_complete_direxpand },
07a490
    { "dirspell", &dircomplete_spelling, (shopt_set_func_t *)NULL },
07a490
  #endif
07a490
***************
07a490
*** 536,539 ****
07a490
--- 545,559 ----
07a490
  }
07a490
  
07a490
+ #if defined (READLINE)
07a490
+ static int
07a490
+ shopt_set_complete_direxpand (option_name, mode)
07a490
+      char *option_name;
07a490
+      int mode;
07a490
+ {
07a490
+   set_directory_hook ();
07a490
+   return 0;
07a490
+ }
07a490
+ #endif
07a490
+ 
07a490
  #if defined (RESTRICTED_SHELL)
07a490
  /* Don't allow the value of restricted_shell to be modified. */
07a490
Binary files ../bash-4.2-patched/doc/._bashref.pdf and ./doc/._bashref.pdf differ
07a490
diff -NrC 2 ../bash-4.2-patched/doc/bash.1 ./doc/bash.1
07a490
*** ../bash-4.2-patched/doc/bash.1	2011-01-16 15:31:39.000000000 -0500
07a490
--- ./doc/bash.1	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 8949,8952 ****
07a490
--- 8949,8962 ----
07a490
  The default bash behavior remains as in previous versions.
07a490
  .TP 8
07a490
+ .B direxpand
07a490
+ If set,
07a490
+ .B bash
07a490
+ replaces directory names with the results of word expansion when performing
07a490
+ filename completion.  This changes the contents of the readline editing
07a490
+ buffer.
07a490
+ If not set,
07a490
+ .B bash
07a490
+ attempts to preserve what the user typed.
07a490
+ .TP 8
07a490
  .B dirspell
07a490
  If set,
07a490
diff -NrC 2 ../bash-4.2-patched/doc/bashref.texi ./doc/bashref.texi
07a490
*** ../bash-4.2-patched/doc/bashref.texi	2011-01-16 15:31:57.000000000 -0500
07a490
--- ./doc/bashref.texi	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 4536,4539 ****
07a490
--- 4536,4546 ----
07a490
  The default Bash behavior remains as in previous versions.
07a490
  
07a490
+ @item direxpand
07a490
+ If set, Bash
07a490
+ replaces directory names with the results of word expansion when performing
07a490
+ filename completion.  This changes the contents of the readline editing
07a490
+ buffer.
07a490
+ If not set, Bash attempts to preserve what the user typed.
07a490
+ 
07a490
  @item dirspell
07a490
  If set, Bash
07a490
diff -NrC 2 ../bash-4.2-patched/tests/shopt.right ./tests/shopt.right
07a490
*** ../bash-4.2-patched/tests/shopt.right	2010-07-02 23:36:30.000000000 -0400
07a490
--- ./tests/shopt.right	2012-05-07 16:27:18.000000000 -0400
07a490
***************
07a490
*** 13,16 ****
07a490
--- 13,17 ----
07a490
  shopt -u compat40
07a490
  shopt -u compat41
07a490
+ shopt -u direxpand
07a490
  shopt -u dirspell
07a490
  shopt -u dotglob
07a490
***************
07a490
*** 69,72 ****
07a490
--- 70,74 ----
07a490
  shopt -u compat40
07a490
  shopt -u compat41
07a490
+ shopt -u direxpand
07a490
  shopt -u dirspell
07a490
  shopt -u dotglob
07a490
***************
07a490
*** 102,105 ****
07a490
--- 104,108 ----
07a490
  compat40       	off
07a490
  compat41       	off
07a490
+ direxpand      	off
07a490
  dirspell       	off
07a490
  dotglob        	off
07a490
*** ../bash-4.2-patched/patchlevel.h	Sat Jun 12 20:14:48 2010
07a490
--- patchlevel.h	Thu Feb 24 21:41:34 2011
07a490
***************
07a490
*** 26,30 ****
07a490
     looks for to find the patch level (for the sccs version string). */
07a490
  
07a490
! #define PATCHLEVEL 28
07a490
  
07a490
  #endif /* _PATCHLEVEL_H_ */
07a490
--- 26,30 ----
07a490
     looks for to find the patch level (for the sccs version string). */
07a490
  
07a490
! #define PATCHLEVEL 29
07a490
  
07a490
  #endif /* _PATCHLEVEL_H_ */