nalika / rpms / bash

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