2022ac3
To: vim-dev@vim.org
2022ac3
Subject: Patch 7.1.223
2022ac3
Fcc: outbox
2022ac3
From: Bram Moolenaar <Bram@moolenaar.net>
2022ac3
Mime-Version: 1.0
2022ac3
Content-Type: text/plain; charset=ISO-8859-1
2022ac3
Content-Transfer-Encoding: 8bit
2022ac3
------------
2022ac3
2022ac3
Patch 7.1.223
2022ac3
Problem:    glob() doesn't work properly when 'shell' is "sh" or "bash" and
2022ac3
	    the expanded name contains spaces, '~', single quotes and other
2022ac3
	    special characters.  (Adri Verhoef, Charles Campbell)
2022ac3
Solution:   For Posix shells define a vimglob() function to list the matches
2022ac3
	    instead of using "echo" directly.
2022ac3
Files:	    src/os_unix.c
2022ac3
2022ac3
2022ac3
*** ../vim-7.1.222/src/os_unix.c	Thu Jan  3 18:55:21 2008
2022ac3
--- src/os_unix.c	Sun Jan 13 13:52:53 2008
2022ac3
***************
2022ac3
*** 4946,4951 ****
2022ac3
--- 4946,4954 ----
2022ac3
      char_u	*p;
2022ac3
      int		dir;
2022ac3
  #ifdef __EMX__
2022ac3
+     /*
2022ac3
+      * This is the OS/2 implementation.
2022ac3
+      */
2022ac3
  # define EXPL_ALLOC_INC	16
2022ac3
      char_u	**expl_files;
2022ac3
      size_t	files_alloced, files_free;
2022ac3
***************
2022ac3
*** 5056,5075 ****
2022ac3
      return OK;
2022ac3
  
2022ac3
  #else /* __EMX__ */
2022ac3
! 
2022ac3
      int		j;
2022ac3
      char_u	*tempname;
2022ac3
      char_u	*command;
2022ac3
      FILE	*fd;
2022ac3
      char_u	*buffer;
2022ac3
! #define STYLE_ECHO  0	    /* use "echo" to expand */
2022ac3
! #define STYLE_GLOB  1	    /* use "glob" to expand, for csh */
2022ac3
! #define STYLE_PRINT 2	    /* use "print -N" to expand, for zsh */
2022ac3
! #define STYLE_BT    3	    /* `cmd` expansion, execute the pattern directly */
2022ac3
      int		shell_style = STYLE_ECHO;
2022ac3
      int		check_spaces;
2022ac3
      static int	did_find_nul = FALSE;
2022ac3
      int		ampersent = FALSE;
2022ac3
  
2022ac3
      *num_file = 0;	/* default: no files found */
2022ac3
      *file = NULL;
2022ac3
--- 5059,5084 ----
2022ac3
      return OK;
2022ac3
  
2022ac3
  #else /* __EMX__ */
2022ac3
!     /*
2022ac3
!      * This is the non-OS/2 implementation (really Unix).
2022ac3
!      */
2022ac3
      int		j;
2022ac3
      char_u	*tempname;
2022ac3
      char_u	*command;
2022ac3
      FILE	*fd;
2022ac3
      char_u	*buffer;
2022ac3
! #define STYLE_ECHO	0	/* use "echo", the default */
2022ac3
! #define STYLE_GLOB	1	/* use "glob", for csh */
2022ac3
! #define STYLE_VIMGLOB	2	/* use "vimglob", for Posix sh */
2022ac3
! #define STYLE_PRINT	3	/* use "print -N", for zsh */
2022ac3
! #define STYLE_BT	4	/* `cmd` expansion, execute the pattern
2022ac3
! 				 * directly */
2022ac3
      int		shell_style = STYLE_ECHO;
2022ac3
      int		check_spaces;
2022ac3
      static int	did_find_nul = FALSE;
2022ac3
      int		ampersent = FALSE;
2022ac3
+ 		/* vimglob() function to define for Posix shell */
2022ac3
+     static char *sh_vimglob_func = "vimglob() { while [ $# -ge 1 ]; do echo -n \"$1\"; echo; shift; done }; vimglob >";
2022ac3
  
2022ac3
      *num_file = 0;	/* default: no files found */
2022ac3
      *file = NULL;
2022ac3
***************
2022ac3
*** 5107,5115 ****
2022ac3
  
2022ac3
      /*
2022ac3
       * Let the shell expand the patterns and write the result into the temp
2022ac3
!      * file.  if expanding `cmd` execute it directly.
2022ac3
!      * If we use csh, glob will work better than echo.
2022ac3
!      * If we use zsh, print -N will work better than glob.
2022ac3
       */
2022ac3
      if (num_pat == 1 && *pat[0] == '`'
2022ac3
  	    && (len = STRLEN(pat[0])) > 2
2022ac3
--- 5116,5132 ----
2022ac3
  
2022ac3
      /*
2022ac3
       * Let the shell expand the patterns and write the result into the temp
2022ac3
!      * file.
2022ac3
!      * STYLE_BT:	NL separated
2022ac3
!      *	    If expanding `cmd` execute it directly.
2022ac3
!      * STYLE_GLOB:	NUL separated
2022ac3
!      *	    If we use *csh, "glob" will work better than "echo".
2022ac3
!      * STYLE_PRINT:	NL or NUL separated
2022ac3
!      *	    If we use *zsh, "print -N" will work better than "glob".
2022ac3
!      * STYLE_VIMGLOB:	NL separated
2022ac3
!      *	    If we use *sh*, we define "vimglob()".
2022ac3
!      * STYLE_ECHO:	space separated.
2022ac3
!      *	    A shell we don't know, stay safe and use "echo".
2022ac3
       */
2022ac3
      if (num_pat == 1 && *pat[0] == '`'
2022ac3
  	    && (len = STRLEN(pat[0])) > 2
2022ac3
***************
2022ac3
*** 5122,5130 ****
2022ac3
  	else if (STRCMP(p_sh + len - 3, "zsh") == 0)
2022ac3
  	    shell_style = STYLE_PRINT;
2022ac3
      }
2022ac3
! 
2022ac3
!     /* "unset nonomatch; print -N >" plus two is 29 */
2022ac3
      len = STRLEN(tempname) + 29;
2022ac3
      for (i = 0; i < num_pat; ++i)
2022ac3
      {
2022ac3
  	/* Count the length of the patterns in the same way as they are put in
2022ac3
--- 5139,5155 ----
2022ac3
  	else if (STRCMP(p_sh + len - 3, "zsh") == 0)
2022ac3
  	    shell_style = STYLE_PRINT;
2022ac3
      }
2022ac3
!     if (shell_style == STYLE_ECHO && strstr((char *)gettail(p_sh),
2022ac3
! 								"sh") != NULL)
2022ac3
! 	shell_style = STYLE_VIMGLOB;
2022ac3
! 
2022ac3
!     /* Compute the length of the command.  We need 2 extra bytes: for the
2022ac3
!      * optional '&' and for the NUL.
2022ac3
!      * Worst case: "unset nonomatch; print -N >" plus two is 29 */
2022ac3
      len = STRLEN(tempname) + 29;
2022ac3
+     if (shell_style == STYLE_VIMGLOB)
2022ac3
+ 	len += STRLEN(sh_vimglob_func);
2022ac3
+ 
2022ac3
      for (i = 0; i < num_pat; ++i)
2022ac3
      {
2022ac3
  	/* Count the length of the patterns in the same way as they are put in
2022ac3
***************
2022ac3
*** 5183,5192 ****
2022ac3
--- 5208,5221 ----
2022ac3
  	    STRCAT(command, "glob >");
2022ac3
  	else if (shell_style == STYLE_PRINT)
2022ac3
  	    STRCAT(command, "print -N >");
2022ac3
+ 	else if (shell_style == STYLE_VIMGLOB)
2022ac3
+ 	    STRCAT(command, sh_vimglob_func);
2022ac3
  	else
2022ac3
  	    STRCAT(command, "echo >");
2022ac3
      }
2022ac3
+ 
2022ac3
      STRCAT(command, tempname);
2022ac3
+ 
2022ac3
      if (shell_style != STYLE_BT)
2022ac3
  	for (i = 0; i < num_pat; ++i)
2022ac3
  	{
2022ac3
***************
2022ac3
*** 5232,5239 ****
2022ac3
      if (flags & EW_SILENT)
2022ac3
  	show_shell_mess = FALSE;
2022ac3
      if (ampersent)
2022ac3
! 	STRCAT(command, "&";;		/* put the '&' back after the
2022ac3
! 					   redirection */
2022ac3
  
2022ac3
      /*
2022ac3
       * Using zsh -G: If a pattern has no matches, it is just deleted from
2022ac3
--- 5261,5267 ----
2022ac3
      if (flags & EW_SILENT)
2022ac3
  	show_shell_mess = FALSE;
2022ac3
      if (ampersent)
2022ac3
! 	STRCAT(command, "&";;		/* put the '&' after the redirection */
2022ac3
  
2022ac3
      /*
2022ac3
       * Using zsh -G: If a pattern has no matches, it is just deleted from
2022ac3
***************
2022ac3
*** 5265,5271 ****
2022ac3
      show_shell_mess = TRUE;
2022ac3
      vim_free(command);
2022ac3
  
2022ac3
!     if (i)				/* mch_call_shell() failed */
2022ac3
      {
2022ac3
  	mch_remove(tempname);
2022ac3
  	vim_free(tempname);
2022ac3
--- 5293,5299 ----
2022ac3
      show_shell_mess = TRUE;
2022ac3
      vim_free(command);
2022ac3
  
2022ac3
!     if (i != 0)				/* mch_call_shell() failed */
2022ac3
      {
2022ac3
  	mch_remove(tempname);
2022ac3
  	vim_free(tempname);
2022ac3
***************
2022ac3
*** 5336,5342 ****
2022ac3
      }
2022ac3
      vim_free(tempname);
2022ac3
  
2022ac3
! #if defined(__CYGWIN__) || defined(__CYGWIN32__)
2022ac3
      /* Translate <CR><NL> into <NL>.  Caution, buffer may contain NUL. */
2022ac3
      p = buffer;
2022ac3
      for (i = 0; i < len; ++i)
2022ac3
--- 5364,5370 ----
2022ac3
      }
2022ac3
      vim_free(tempname);
2022ac3
  
2022ac3
! # if defined(__CYGWIN__) || defined(__CYGWIN32__)
2022ac3
      /* Translate <CR><NL> into <NL>.  Caution, buffer may contain NUL. */
2022ac3
      p = buffer;
2022ac3
      for (i = 0; i < len; ++i)
2022ac3
***************
2022ac3
*** 5359,5365 ****
2022ac3
  	}
2022ac3
      }
2022ac3
      /* file names are separated with NL */
2022ac3
!     else if (shell_style == STYLE_BT)
2022ac3
      {
2022ac3
  	buffer[len] = NUL;		/* make sure the buffer ends in NUL */
2022ac3
  	p = buffer;
2022ac3
--- 5387,5393 ----
2022ac3
  	}
2022ac3
      }
2022ac3
      /* file names are separated with NL */
2022ac3
!     else if (shell_style == STYLE_BT || shell_style == STYLE_VIMGLOB)
2022ac3
      {
2022ac3
  	buffer[len] = NUL;		/* make sure the buffer ends in NUL */
2022ac3
  	p = buffer;
2022ac3
***************
2022ac3
*** 5438,5444 ****
2022ac3
      {
2022ac3
  	(*file)[i] = p;
2022ac3
  	/* Space or NL separates */
2022ac3
! 	if (shell_style == STYLE_ECHO || shell_style == STYLE_BT)
2022ac3
  	{
2022ac3
  	    while (!(shell_style == STYLE_ECHO && *p == ' ')
2022ac3
  						   && *p != '\n' && *p != NUL)
2022ac3
--- 5466,5473 ----
2022ac3
      {
2022ac3
  	(*file)[i] = p;
2022ac3
  	/* Space or NL separates */
2022ac3
! 	if (shell_style == STYLE_ECHO || shell_style == STYLE_BT
2022ac3
! 					      || shell_style == STYLE_VIMGLOB)
2022ac3
  	{
2022ac3
  	    while (!(shell_style == STYLE_ECHO && *p == ' ')
2022ac3
  						   && *p != '\n' && *p != NUL)
2022ac3
*** ../vim-7.1.222/src/version.c	Sun Jan 13 13:30:34 2008
2022ac3
--- src/version.c	Sun Jan 13 13:45:04 2008
2022ac3
***************
2022ac3
*** 668,669 ****
2022ac3
--- 668,671 ----
2022ac3
  {   /* Add new patch number below this line */
2022ac3
+ /**/
2022ac3
+     223,
2022ac3
  /**/
2022ac3
2022ac3
-- 
2022ac3
User:       I'm having problems with my text editor.
2022ac3
Help desk:  Which editor are you using?
2022ac3
User:       I don't know, but it's version VI (pronounced: 6).
2022ac3
Help desk:  Oh, then you should upgrade to version VIM (pronounced: 994).
2022ac3
2022ac3
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
2022ac3
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2022ac3
\\\        download, build and distribute -- http://www.A-A-P.org        ///
2022ac3
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///