2ea7b0d
To: vim_dev@googlegroups.com
2ea7b0d
Subject: Patch 7.3.240
2ea7b0d
Fcc: outbox
2ea7b0d
From: Bram Moolenaar <Bram@moolenaar.net>
2ea7b0d
Mime-Version: 1.0
2ea7b0d
Content-Type: text/plain; charset=UTF-8
2ea7b0d
Content-Transfer-Encoding: 8bit
2ea7b0d
------------
2ea7b0d
2ea7b0d
Note: I haven't verified this works or even compiles.  Please send me a
2ea7b0d
patch if you see a problem and can fix it.
2ea7b0d
2ea7b0d
Patch 7.3.240
2ea7b0d
Problem:    External commands can't use pipes on MS-Windows.
2ea7b0d
Solution:   Implement pipes and use them when 'shelltemp' isn't set. (Vincent
2ea7b0d
	    Berthoux)
2ea7b0d
Files:	    src/eval.c, src/ex_cmds.c, src/misc2.c, src/os_unix.c,
2ea7b0d
	    src/os_win32.c, src/proto/misc2.pro, src/ui.c
2ea7b0d
2ea7b0d
2ea7b0d
*** ../vim-7.3.239/src/eval.c	2011-06-19 02:55:32.000000000 +0200
2ea7b0d
--- src/eval.c	2011-07-07 15:44:56.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 11931,11937 ****
2ea7b0d
  #ifdef FEAT_SEARCHPATH
2ea7b0d
  	"file_in_path",
2ea7b0d
  #endif
2ea7b0d
! #if defined(UNIX) && !defined(USE_SYSTEM)
2ea7b0d
  	"filterpipe",
2ea7b0d
  #endif
2ea7b0d
  #ifdef FEAT_FIND_ID
2ea7b0d
--- 11931,11937 ----
2ea7b0d
  #ifdef FEAT_SEARCHPATH
2ea7b0d
  	"file_in_path",
2ea7b0d
  #endif
2ea7b0d
! #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
2ea7b0d
  	"filterpipe",
2ea7b0d
  #endif
2ea7b0d
  #ifdef FEAT_FIND_ID
2ea7b0d
*** ../vim-7.3.239/src/ex_cmds.c	2011-06-12 22:03:15.000000000 +0200
2ea7b0d
--- src/ex_cmds.c	2011-07-07 15:44:56.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 1107,1113 ****
2ea7b0d
      if (do_out)
2ea7b0d
  	shell_flags |= SHELL_DOOUT;
2ea7b0d
  
2ea7b0d
! #if !defined(USE_SYSTEM) && defined(UNIX)
2ea7b0d
      if (!do_in && do_out && !p_stmp)
2ea7b0d
      {
2ea7b0d
  	/* Use a pipe to fetch stdout of the command, do not use a temp file. */
2ea7b0d
--- 1107,1113 ----
2ea7b0d
      if (do_out)
2ea7b0d
  	shell_flags |= SHELL_DOOUT;
2ea7b0d
  
2ea7b0d
! #if (!defined(USE_SYSTEM) && defined(UNIX)) || defined(WIN3264)
2ea7b0d
      if (!do_in && do_out && !p_stmp)
2ea7b0d
      {
2ea7b0d
  	/* Use a pipe to fetch stdout of the command, do not use a temp file. */
2ea7b0d
*** ../vim-7.3.239/src/misc2.c	2011-07-07 15:08:53.000000000 +0200
2ea7b0d
--- src/misc2.c	2011-07-07 15:55:42.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 2146,2151 ****
2ea7b0d
--- 2146,2170 ----
2ea7b0d
      }
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
+ #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(WIN3264)
2ea7b0d
+ /*
2ea7b0d
+  * Append the text in "gap" below the cursor line and clear "gap".
2ea7b0d
+  */
2ea7b0d
+     void
2ea7b0d
+ append_ga_line(gap)
2ea7b0d
+     garray_T	*gap;
2ea7b0d
+ {
2ea7b0d
+     /* Remove trailing CR. */
2ea7b0d
+     if (gap->ga_len > 0
2ea7b0d
+ 	    && !curbuf->b_p_bin
2ea7b0d
+ 	    && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2ea7b0d
+ 	--gap->ga_len;
2ea7b0d
+     ga_append(gap, NUL);
2ea7b0d
+     ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2ea7b0d
+     gap->ga_len = 0;
2ea7b0d
+ }
2ea7b0d
+ #endif
2ea7b0d
+ 
2ea7b0d
  /************************************************************************
2ea7b0d
   * functions that use lookup tables for various things, generally to do with
2ea7b0d
   * special key codes.
2ea7b0d
*** ../vim-7.3.239/src/os_unix.c	2011-04-11 16:56:29.000000000 +0200
2ea7b0d
--- src/os_unix.c	2011-07-07 15:54:58.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 3660,3686 ****
2ea7b0d
      /* Nothing to do. */
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
- #ifndef USE_SYSTEM
2ea7b0d
- static void append_ga_line __ARGS((garray_T *gap));
2ea7b0d
- 
2ea7b0d
- /*
2ea7b0d
-  * Append the text in "gap" below the cursor line and clear "gap".
2ea7b0d
-  */
2ea7b0d
-     static void
2ea7b0d
- append_ga_line(gap)
2ea7b0d
-     garray_T	*gap;
2ea7b0d
- {
2ea7b0d
-     /* Remove trailing CR. */
2ea7b0d
-     if (gap->ga_len > 0
2ea7b0d
- 	    && !curbuf->b_p_bin
2ea7b0d
- 	    && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
2ea7b0d
- 	--gap->ga_len;
2ea7b0d
-     ga_append(gap, NUL);
2ea7b0d
-     ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
2ea7b0d
-     gap->ga_len = 0;
2ea7b0d
- }
2ea7b0d
- #endif
2ea7b0d
- 
2ea7b0d
      int
2ea7b0d
  mch_call_shell(cmd, options)
2ea7b0d
      char_u	*cmd;
2ea7b0d
--- 3660,3665 ----
2ea7b0d
*** ../vim-7.3.239/src/os_win32.c	2011-05-25 17:06:16.000000000 +0200
2ea7b0d
--- src/os_win32.c	2011-07-07 16:08:30.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 417,422 ****
2ea7b0d
--- 417,427 ----
2ea7b0d
  static PGNSECINFO pGetNamedSecurityInfo;
2ea7b0d
  #endif
2ea7b0d
  
2ea7b0d
+ typedef BOOL (WINAPI *PSETHANDLEINFORMATION)(HANDLE, DWORD, DWORD);
2ea7b0d
+ 
2ea7b0d
+ static BOOL allowPiping = FALSE;
2ea7b0d
+ static PSETHANDLEINFORMATION pSetHandleInformation;
2ea7b0d
+ 
2ea7b0d
  /*
2ea7b0d
   * Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or
2ea7b0d
   * VER_PLATFORM_WIN32_WINDOWS (Win95).
2ea7b0d
***************
2ea7b0d
*** 467,472 ****
2ea7b0d
--- 472,489 ----
2ea7b0d
  	    }
2ea7b0d
  	}
2ea7b0d
  #endif
2ea7b0d
+ 	/*
2ea7b0d
+ 	 * If we are on windows NT, try to load the pipe functions, only
2ea7b0d
+ 	 * available from Win2K.
2ea7b0d
+ 	 */
2ea7b0d
+ 	if (g_PlatformId == VER_PLATFORM_WIN32_NT)
2ea7b0d
+ 	{
2ea7b0d
+ 	    HANDLE kernel32 = GetModuleHandle("kernel32");
2ea7b0d
+ 	    pSetHandleInformation = (PSETHANDLEINFORMATION)GetProcAddress(
2ea7b0d
+ 					    kernel32, "SetHandleInformation");
2ea7b0d
+ 
2ea7b0d
+ 	    allowPiping = pSetHandleInformation != NULL;
2ea7b0d
+ 	}
2ea7b0d
  	done = TRUE;
2ea7b0d
      }
2ea7b0d
  }
2ea7b0d
***************
2ea7b0d
*** 1635,1641 ****
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
  #if ((defined(__MINGW32__) || defined (__CYGWIN32__)) && \
2ea7b0d
!         __MSVCRT_VERSION__ >= 0x800) || (defined(_MSC_VER) && _MSC_VER >= 1400)
2ea7b0d
  /*
2ea7b0d
   * Bad parameter handler.
2ea7b0d
   *
2ea7b0d
--- 1652,1658 ----
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
  #if ((defined(__MINGW32__) || defined (__CYGWIN32__)) && \
2ea7b0d
!        __MSVCRT_VERSION__ >= 0x800) || (defined(_MSC_VER) && _MSC_VER >= 1400)
2ea7b0d
  /*
2ea7b0d
   * Bad parameter handler.
2ea7b0d
   *
2ea7b0d
***************
2ea7b0d
*** 3210,3216 ****
2ea7b0d
   *    4. Prompt the user to press a key to close the console window
2ea7b0d
   */
2ea7b0d
      static int
2ea7b0d
! mch_system(char *cmd, int options)
2ea7b0d
  {
2ea7b0d
      STARTUPINFO		si;
2ea7b0d
      PROCESS_INFORMATION pi;
2ea7b0d
--- 3227,3233 ----
2ea7b0d
   *    4. Prompt the user to press a key to close the console window
2ea7b0d
   */
2ea7b0d
      static int
2ea7b0d
! mch_system_classic(char *cmd, int options)
2ea7b0d
  {
2ea7b0d
      STARTUPINFO		si;
2ea7b0d
      PROCESS_INFORMATION pi;
2ea7b0d
***************
2ea7b0d
*** 3315,3320 ****
2ea7b0d
--- 3332,3829 ----
2ea7b0d
  
2ea7b0d
      return ret;
2ea7b0d
  }
2ea7b0d
+ 
2ea7b0d
+ /*
2ea7b0d
+  * Thread launched by the gui to send the current buffer data to the
2ea7b0d
+  * process. This way avoid to hang up vim totally if the children
2ea7b0d
+  * process take a long time to process the lines.
2ea7b0d
+  */
2ea7b0d
+     static DWORD WINAPI
2ea7b0d
+ sub_process_writer(LPVOID param)
2ea7b0d
+ {
2ea7b0d
+     HANDLE	    g_hChildStd_IN_Wr = param;
2ea7b0d
+     linenr_T	    lnum = curbuf->b_op_start.lnum;
2ea7b0d
+     DWORD	    len = 0;
2ea7b0d
+     DWORD	    l;
2ea7b0d
+     char_u	    *lp = ml_get(lnum);
2ea7b0d
+     char_u	    *s;
2ea7b0d
+     int		    written = 0;
2ea7b0d
+ 
2ea7b0d
+     for (;;)
2ea7b0d
+     {
2ea7b0d
+ 	l = (DWORD)STRLEN(lp + written);
2ea7b0d
+ 	if (l == 0)
2ea7b0d
+ 	    len = 0;
2ea7b0d
+ 	else if (lp[written] == NL)
2ea7b0d
+ 	{
2ea7b0d
+ 	    /* NL -> NUL translation */
2ea7b0d
+ 	    WriteFile(g_hChildStd_IN_Wr, "", 1, &len, NULL);
2ea7b0d
+ 	}
2ea7b0d
+ 	else
2ea7b0d
+ 	{
2ea7b0d
+ 	    s = vim_strchr(lp + written, NL);
2ea7b0d
+ 	    WriteFile(g_hChildStd_IN_Wr, (char *)lp + written,
2ea7b0d
+ 		      s == NULL ? l : (DWORD)(s - (lp + written)),
2ea7b0d
+ 		      &len, NULL);
2ea7b0d
+ 	}
2ea7b0d
+ 	if (len == (int)l)
2ea7b0d
+ 	{
2ea7b0d
+ 	    /* Finished a line, add a NL, unless this line should not have
2ea7b0d
+ 	     * one. */
2ea7b0d
+ 	    if (lnum != curbuf->b_op_end.lnum
2ea7b0d
+ 		|| !curbuf->b_p_bin
2ea7b0d
+ 		|| (lnum != curbuf->b_no_eol_lnum
2ea7b0d
+ 		    && (lnum != curbuf->b_ml.ml_line_count
2ea7b0d
+ 			|| curbuf->b_p_eol)))
2ea7b0d
+ 	    {
2ea7b0d
+ 		WriteFile(g_hChildStd_IN_Wr, "\n", 1, &ignored, NULL);
2ea7b0d
+ 	    }
2ea7b0d
+ 
2ea7b0d
+ 	    ++lnum;
2ea7b0d
+ 	    if (lnum > curbuf->b_op_end.lnum)
2ea7b0d
+ 		break;
2ea7b0d
+ 
2ea7b0d
+ 	    lp = ml_get(lnum);
2ea7b0d
+ 	    written = 0;
2ea7b0d
+ 	}
2ea7b0d
+ 	else if (len > 0)
2ea7b0d
+ 	    written += len;
2ea7b0d
+     }
2ea7b0d
+ 
2ea7b0d
+     /* finished all the lines, close pipe */
2ea7b0d
+     CloseHandle(g_hChildStd_IN_Wr);
2ea7b0d
+     ExitThread(0);
2ea7b0d
+ }
2ea7b0d
+ 
2ea7b0d
+ 
2ea7b0d
+ # define BUFLEN 100	/* length for buffer, stolen from unix version */
2ea7b0d
+ 
2ea7b0d
+ /*
2ea7b0d
+  * This function read from the children's stdout and write the
2ea7b0d
+  * data on screen or in the buffer accordingly.
2ea7b0d
+  */
2ea7b0d
+     static void
2ea7b0d
+ dump_pipe(int	    options,
2ea7b0d
+ 	  HANDLE    g_hChildStd_OUT_Rd,
2ea7b0d
+ 	  garray_T  *ga,
2ea7b0d
+ 	  char_u    buffer[],
2ea7b0d
+ 	  DWORD	    *buffer_off)
2ea7b0d
+ {
2ea7b0d
+     DWORD	availableBytes = 0;
2ea7b0d
+     DWORD	i;
2ea7b0d
+     int		c;
2ea7b0d
+     char_u	*p;
2ea7b0d
+     int		ret;
2ea7b0d
+     DWORD	len;
2ea7b0d
+     DWORD	toRead;
2ea7b0d
+     int		repeatCount;
2ea7b0d
+ 
2ea7b0d
+     /* we query the pipe to see if there is any data to read
2ea7b0d
+      * to avoid to perform a blocking read */
2ea7b0d
+     ret = PeekNamedPipe(g_hChildStd_OUT_Rd, /* pipe to query */
2ea7b0d
+ 			NULL,		    /* optional buffer */
2ea7b0d
+ 			0,		    /* buffe size */
2ea7b0d
+ 			NULL,		    /* number of read bytes */
2ea7b0d
+ 			&availableBytes,    /* available bytes total */
2ea7b0d
+ 			NULL);		    /* byteLeft */
2ea7b0d
+ 
2ea7b0d
+     repeatCount = 0;
2ea7b0d
+     /* We got real data in the pipe, read it */
2ea7b0d
+     while (ret != 0 && availableBytes > 0 && availableBytes > 0)
2ea7b0d
+     {
2ea7b0d
+ 	repeatCount++;
2ea7b0d
+ 	toRead =
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+ 		 (DWORD)(BUFLEN - *buffer_off);
2ea7b0d
+ # else
2ea7b0d
+ 		 (DWORD)BUFLEN;
2ea7b0d
+ # endif
2ea7b0d
+ 	toRead = availableBytes < toRead ? availableBytes : toRead;
2ea7b0d
+ 	ReadFile(g_hChildStd_OUT_Rd, buffer
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+ 		 + *buffer_off, toRead
2ea7b0d
+ # else
2ea7b0d
+ 		 , toRead
2ea7b0d
+ # endif
2ea7b0d
+ 		 , &len, NULL);
2ea7b0d
+ 
2ea7b0d
+ 	/* If we haven't read anything, there is a problem */
2ea7b0d
+ 	if (len == 0)
2ea7b0d
+ 	    break;
2ea7b0d
+ 
2ea7b0d
+ 	availableBytes -= len;
2ea7b0d
+ 
2ea7b0d
+ 	if (options & SHELL_READ)
2ea7b0d
+ 	{
2ea7b0d
+ 	    /* Do NUL -> NL translation, append NL separated
2ea7b0d
+ 	     * lines to the current buffer. */
2ea7b0d
+ 	    for (i = 0; i < len; ++i)
2ea7b0d
+ 	    {
2ea7b0d
+ 		if (buffer[i] == NL)
2ea7b0d
+ 		    append_ga_line(ga);
2ea7b0d
+ 		else if (buffer[i] == NUL)
2ea7b0d
+ 		    ga_append(ga, NL);
2ea7b0d
+ 		else
2ea7b0d
+ 		    ga_append(ga, buffer[i]);
2ea7b0d
+ 	    }
2ea7b0d
+ 	}
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+ 	else if (has_mbyte)
2ea7b0d
+ 	{
2ea7b0d
+ 	    int		l;
2ea7b0d
+ 
2ea7b0d
+ 	    len += *buffer_off;
2ea7b0d
+ 	    buffer[len] = NUL;
2ea7b0d
+ 
2ea7b0d
+ 	    /* Check if the last character in buffer[] is
2ea7b0d
+ 	     * incomplete, keep these bytes for the next
2ea7b0d
+ 	     * round. */
2ea7b0d
+ 	    for (p = buffer; p < buffer + len; p += l)
2ea7b0d
+ 	    {
2ea7b0d
+ 		l = mb_cptr2len(p);
2ea7b0d
+ 		if (l == 0)
2ea7b0d
+ 		    l = 1;  /* NUL byte? */
2ea7b0d
+ 		else if (MB_BYTE2LEN(*p) != l)
2ea7b0d
+ 		    break;
2ea7b0d
+ 	    }
2ea7b0d
+ 	    if (p == buffer)	/* no complete character */
2ea7b0d
+ 	    {
2ea7b0d
+ 		/* avoid getting stuck at an illegal byte */
2ea7b0d
+ 		if (len >= 12)
2ea7b0d
+ 		    ++p;
2ea7b0d
+ 		else
2ea7b0d
+ 		{
2ea7b0d
+ 		    *buffer_off = len;
2ea7b0d
+ 		    return;
2ea7b0d
+ 		}
2ea7b0d
+ 	    }
2ea7b0d
+ 	    c = *p;
2ea7b0d
+ 	    *p = NUL;
2ea7b0d
+ 	    msg_puts(buffer);
2ea7b0d
+ 	    if (p < buffer + len)
2ea7b0d
+ 	    {
2ea7b0d
+ 		*p = c;
2ea7b0d
+ 		*buffer_off = (DWORD)((buffer + len) - p);
2ea7b0d
+ 		mch_memmove(buffer, p, *buffer_off);
2ea7b0d
+ 		return;
2ea7b0d
+ 	    }
2ea7b0d
+ 	    *buffer_off = 0;
2ea7b0d
+ 	}
2ea7b0d
+ # endif /* FEAT_MBYTE */
2ea7b0d
+ 	else
2ea7b0d
+ 	{
2ea7b0d
+ 	    buffer[len] = NUL;
2ea7b0d
+ 	    msg_puts(buffer);
2ea7b0d
+ 	}
2ea7b0d
+ 
2ea7b0d
+ 	windgoto(msg_row, msg_col);
2ea7b0d
+ 	cursor_on();
2ea7b0d
+ 	out_flush();
2ea7b0d
+     }
2ea7b0d
+ }
2ea7b0d
+ 
2ea7b0d
+ /*
2ea7b0d
+  * Version of system to use for windows NT > 5.0 (Win2K), use pipe
2ea7b0d
+  * for communication and doesn't open any new window.
2ea7b0d
+  */
2ea7b0d
+     static int
2ea7b0d
+ mch_system_piped(char *cmd, int options)
2ea7b0d
+ {
2ea7b0d
+     STARTUPINFO		si;
2ea7b0d
+     PROCESS_INFORMATION pi;
2ea7b0d
+     DWORD		ret = 0;
2ea7b0d
+ 
2ea7b0d
+     HANDLE g_hChildStd_IN_Rd = NULL;
2ea7b0d
+     HANDLE g_hChildStd_IN_Wr = NULL;
2ea7b0d
+     HANDLE g_hChildStd_OUT_Rd = NULL;
2ea7b0d
+     HANDLE g_hChildStd_OUT_Wr = NULL;
2ea7b0d
+ 
2ea7b0d
+     char_u	buffer[BUFLEN + 1]; /* reading buffer + size */
2ea7b0d
+     DWORD	len;
2ea7b0d
+ 
2ea7b0d
+     /* buffer used to receive keys */
2ea7b0d
+     char_u	ta_buf[BUFLEN + 1];	/* TypeAHead */
2ea7b0d
+     int		ta_len = 0;		/* valid bytes in ta_buf[] */
2ea7b0d
+ 
2ea7b0d
+     DWORD	i;
2ea7b0d
+     int		c;
2ea7b0d
+     int		noread_cnt = 0;
2ea7b0d
+     garray_T	ga;
2ea7b0d
+     int	    delay = 1;
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+     DWORD	buffer_off = 0;	/* valid bytes in buffer[] */
2ea7b0d
+ # endif
2ea7b0d
+ 
2ea7b0d
+     SECURITY_ATTRIBUTES saAttr;
2ea7b0d
+ 
2ea7b0d
+     /* Set the bInheritHandle flag so pipe handles are inherited. */
2ea7b0d
+     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
2ea7b0d
+     saAttr.bInheritHandle = TRUE;
2ea7b0d
+     saAttr.lpSecurityDescriptor = NULL;
2ea7b0d
+ 
2ea7b0d
+     if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0)
2ea7b0d
+ 	/* Ensure the read handle to the pipe for STDOUT is not inherited. */
2ea7b0d
+        || ! pSetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)
2ea7b0d
+ 	/* Create a pipe for the child process's STDIN. */
2ea7b0d
+        || ! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)
2ea7b0d
+ 	/* Ensure the write handle to the pipe for STDIN is not inherited. */
2ea7b0d
+        || ! pSetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
2ea7b0d
+     {
2ea7b0d
+ 	CloseHandle(g_hChildStd_IN_Rd);
2ea7b0d
+ 	CloseHandle(g_hChildStd_IN_Wr);
2ea7b0d
+ 	CloseHandle(g_hChildStd_OUT_Rd);
2ea7b0d
+ 	CloseHandle(g_hChildStd_OUT_Wr);
2ea7b0d
+ 	MSG_PUTS(_("\nCannot create pipes\n"));
2ea7b0d
+     }
2ea7b0d
+ 
2ea7b0d
+     si.cb = sizeof(si);
2ea7b0d
+     si.lpReserved = NULL;
2ea7b0d
+     si.lpDesktop = NULL;
2ea7b0d
+     si.lpTitle = NULL;
2ea7b0d
+     si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
2ea7b0d
+ 
2ea7b0d
+     /* set-up our file redirection */
2ea7b0d
+     si.hStdError = g_hChildStd_OUT_Wr;
2ea7b0d
+     si.hStdOutput = g_hChildStd_OUT_Wr;
2ea7b0d
+     si.hStdInput = g_hChildStd_IN_Rd;
2ea7b0d
+     si.wShowWindow = SW_HIDE;
2ea7b0d
+     si.cbReserved2 = 0;
2ea7b0d
+     si.lpReserved2 = NULL;
2ea7b0d
+ 
2ea7b0d
+     if (options & SHELL_READ)
2ea7b0d
+ 	ga_init2(&ga, 1, BUFLEN);
2ea7b0d
+ 
2ea7b0d
+     /* Now, run the command */
2ea7b0d
+     CreateProcess(NULL,			/* Executable name */
2ea7b0d
+ 		  cmd,			/* Command to execute */
2ea7b0d
+ 		  NULL,			/* Process security attributes */
2ea7b0d
+ 		  NULL,			/* Thread security attributes */
2ea7b0d
+ 
2ea7b0d
+ 		  // this command can be litigeous, handle inheritence was
2ea7b0d
+ 		  // deactivated for pending temp file, but, if we deactivate
2ea7b0d
+ 		  // it, the pipes don't work for some reason.
2ea7b0d
+ 		  TRUE,			/* Inherit handles, first deactivated,
2ea7b0d
+ 					 * but needed */
2ea7b0d
+ 		  CREATE_DEFAULT_ERROR_MODE, /* Creation flags */
2ea7b0d
+ 		  NULL,			/* Environment */
2ea7b0d
+ 		  NULL,			/* Current directory */
2ea7b0d
+ 		  &si,			/* Startup information */
2ea7b0d
+ 		  &pi);			/* Process information */
2ea7b0d
+ 
2ea7b0d
+ 
2ea7b0d
+     /* Close our unused side of the pipes */
2ea7b0d
+     CloseHandle(g_hChildStd_IN_Rd);
2ea7b0d
+     CloseHandle(g_hChildStd_OUT_Wr);
2ea7b0d
+ 
2ea7b0d
+     if (options & SHELL_WRITE)
2ea7b0d
+     {
2ea7b0d
+ 	HANDLE thread =
2ea7b0d
+ 	   CreateThread(NULL,  /* security attributes */
2ea7b0d
+ 			0,     /* default stack size */
2ea7b0d
+ 			sub_process_writer, /* function to be executed */
2ea7b0d
+ 			g_hChildStd_IN_Wr,  /* parameter */
2ea7b0d
+ 			0,		 /* creation flag, start immediately */
2ea7b0d
+ 			NULL);		    /* we don't care about thread id */
2ea7b0d
+ 	CloseHandle(thread);
2ea7b0d
+ 	g_hChildStd_IN_Wr = NULL;
2ea7b0d
+     }
2ea7b0d
+ 
2ea7b0d
+     /* Keep updating the window while waiting for the shell to finish. */
2ea7b0d
+     for (;;)
2ea7b0d
+     {
2ea7b0d
+ 	MSG	msg;
2ea7b0d
+ 
2ea7b0d
+ 	if (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
2ea7b0d
+ 	{
2ea7b0d
+ 	    TranslateMessage(&msg;;
2ea7b0d
+ 	    DispatchMessage(&msg;;
2ea7b0d
+ 	}
2ea7b0d
+ 
2ea7b0d
+ 	/* write pipe information in the window */
2ea7b0d
+ 	if ((options & (SHELL_READ|SHELL_WRITE))
2ea7b0d
+ # ifdef FEAT_GUI
2ea7b0d
+ 		|| gui.in_use
2ea7b0d
+ # endif
2ea7b0d
+ 	    )
2ea7b0d
+ 	{
2ea7b0d
+ 	    len = 0;
2ea7b0d
+ 	    if (!(options & SHELL_EXPAND)
2ea7b0d
+ 		&& ((options &
2ea7b0d
+ 			(SHELL_READ|SHELL_WRITE|SHELL_COOKED))
2ea7b0d
+ 		    != (SHELL_READ|SHELL_WRITE|SHELL_COOKED)
2ea7b0d
+ # ifdef FEAT_GUI
2ea7b0d
+ 		    || gui.in_use
2ea7b0d
+ # endif
2ea7b0d
+ 		    )
2ea7b0d
+ 		&& (ta_len > 0 || noread_cnt > 4))
2ea7b0d
+ 	    {
2ea7b0d
+ 		if (ta_len == 0)
2ea7b0d
+ 		{
2ea7b0d
+ 		    /* Get extra characters when we don't have any.  Reset the
2ea7b0d
+ 		     * counter and timer. */
2ea7b0d
+ 		    noread_cnt = 0;
2ea7b0d
+ # if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
2ea7b0d
+ 		    gettimeofday(&start_tv, NULL);
2ea7b0d
+ # endif
2ea7b0d
+ 		    len = ui_inchar(ta_buf, BUFLEN, 10L, 0);
2ea7b0d
+ 		}
2ea7b0d
+ 		if (ta_len > 0 || len > 0)
2ea7b0d
+ 		{
2ea7b0d
+ 		    /*
2ea7b0d
+ 		     * For pipes: Check for CTRL-C: send interrupt signal to
2ea7b0d
+ 		     * child.  Check for CTRL-D: EOF, close pipe to child.
2ea7b0d
+ 		     */
2ea7b0d
+ 		    if (len == 1 && cmd != NULL)
2ea7b0d
+ 		    {
2ea7b0d
+ 			if (ta_buf[ta_len] == Ctrl_C)
2ea7b0d
+ 			{
2ea7b0d
+ 			    /* Learn what exit code is expected, for
2ea7b0d
+ 				* now put 9 as SIGKILL */
2ea7b0d
+ 			    TerminateProcess(pi.hProcess, 9);
2ea7b0d
+ 			}
2ea7b0d
+ 			if (ta_buf[ta_len] == Ctrl_D)
2ea7b0d
+ 			{
2ea7b0d
+ 			    CloseHandle(g_hChildStd_IN_Wr);
2ea7b0d
+ 			    g_hChildStd_IN_Wr = NULL;
2ea7b0d
+ 			}
2ea7b0d
+ 		    }
2ea7b0d
+ 
2ea7b0d
+ 		    /* replace K_BS by <BS> and K_DEL by  */
2ea7b0d
+ 		    for (i = ta_len; i < ta_len + len; ++i)
2ea7b0d
+ 		    {
2ea7b0d
+ 			if (ta_buf[i] == CSI && len - i > 2)
2ea7b0d
+ 			{
2ea7b0d
+ 			    c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
2ea7b0d
+ 			    if (c == K_DEL || c == K_KDEL || c == K_BS)
2ea7b0d
+ 			    {
2ea7b0d
+ 				mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
2ea7b0d
+ 					    (size_t)(len - i - 2));
2ea7b0d
+ 				if (c == K_DEL || c == K_KDEL)
2ea7b0d
+ 				    ta_buf[i] = DEL;
2ea7b0d
+ 				else
2ea7b0d
+ 				    ta_buf[i] = Ctrl_H;
2ea7b0d
+ 				len -= 2;
2ea7b0d
+ 			    }
2ea7b0d
+ 			}
2ea7b0d
+ 			else if (ta_buf[i] == '\r')
2ea7b0d
+ 			    ta_buf[i] = '\n';
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+ 			if (has_mbyte)
2ea7b0d
+ 			    i += (*mb_ptr2len_len)(ta_buf + i,
2ea7b0d
+ 						    ta_len + len - i) - 1;
2ea7b0d
+ # endif
2ea7b0d
+ 		    }
2ea7b0d
+ 
2ea7b0d
+ 		    /*
2ea7b0d
+ 		     * For pipes: echo the typed characters.  For a pty this
2ea7b0d
+ 		     * does not seem to work.
2ea7b0d
+ 		     */
2ea7b0d
+ 		    for (i = ta_len; i < ta_len + len; ++i)
2ea7b0d
+ 		    {
2ea7b0d
+ 			if (ta_buf[i] == '\n' || ta_buf[i] == '\b')
2ea7b0d
+ 			    msg_putchar(ta_buf[i]);
2ea7b0d
+ # ifdef FEAT_MBYTE
2ea7b0d
+ 			else if (has_mbyte)
2ea7b0d
+ 			{
2ea7b0d
+ 			    int l = (*mb_ptr2len)(ta_buf + i);
2ea7b0d
+ 
2ea7b0d
+ 			    msg_outtrans_len(ta_buf + i, l);
2ea7b0d
+ 			    i += l - 1;
2ea7b0d
+ 			}
2ea7b0d
+ # endif
2ea7b0d
+ 			else
2ea7b0d
+ 			    msg_outtrans_len(ta_buf + i, 1);
2ea7b0d
+ 		    }
2ea7b0d
+ 		    windgoto(msg_row, msg_col);
2ea7b0d
+ 		    out_flush();
2ea7b0d
+ 
2ea7b0d
+ 		    ta_len += len;
2ea7b0d
+ 
2ea7b0d
+ 		    /*
2ea7b0d
+ 		     * Write the characters to the child, unless EOF has been
2ea7b0d
+ 		     * typed for pipes.  Write one character at a time, to
2ea7b0d
+ 		     * avoid losing too much typeahead.  When writing buffer
2ea7b0d
+ 		     * lines, drop the typed characters (only check for
2ea7b0d
+ 		     * CTRL-C).
2ea7b0d
+ 		     */
2ea7b0d
+ 		    if (options & SHELL_WRITE)
2ea7b0d
+ 			ta_len = 0;
2ea7b0d
+ 		    else if (g_hChildStd_IN_Wr != NULL)
2ea7b0d
+ 		    {
2ea7b0d
+ 			WriteFile(g_hChildStd_IN_Wr, (char*)ta_buf,
2ea7b0d
+ 				    1, &len, NULL);
2ea7b0d
+ 			// if we are typing in, we want to keep things reactive
2ea7b0d
+ 			delay = 1;
2ea7b0d
+ 			if (len > 0)
2ea7b0d
+ 			{
2ea7b0d
+ 			    ta_len -= len;
2ea7b0d
+ 			    mch_memmove(ta_buf, ta_buf + len, ta_len);
2ea7b0d
+ 			}
2ea7b0d
+ 		    }
2ea7b0d
+ 		}
2ea7b0d
+ 	    }
2ea7b0d
+ 	}
2ea7b0d
+ 
2ea7b0d
+ 	if (ta_len)
2ea7b0d
+ 	    ui_inchar_undo(ta_buf, ta_len);
2ea7b0d
+ 
2ea7b0d
+ 	if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT)
2ea7b0d
+ 	{
2ea7b0d
+ 	    dump_pipe(options, g_hChildStd_OUT_Rd,
2ea7b0d
+ 			&ga, buffer, &buffer_off);
2ea7b0d
+ 	    break;
2ea7b0d
+ 	}
2ea7b0d
+ 
2ea7b0d
+ 	++noread_cnt;
2ea7b0d
+ 	dump_pipe(options, g_hChildStd_OUT_Rd,
2ea7b0d
+ 		    &ga, buffer, &buffer_off);
2ea7b0d
+ 
2ea7b0d
+ 	/* We start waiting for a very short time and then increase it, so
2ea7b0d
+ 	 * that we respond quickly when the process is quick, and don't
2ea7b0d
+ 	 * consume too much overhead when it's slow. */
2ea7b0d
+ 	if (delay < 50)
2ea7b0d
+ 	    delay += 10;
2ea7b0d
+     }
2ea7b0d
+ 
2ea7b0d
+     /* Close the pipe */
2ea7b0d
+     CloseHandle(g_hChildStd_OUT_Rd);
2ea7b0d
+     if (g_hChildStd_IN_Wr != NULL)
2ea7b0d
+ 	CloseHandle(g_hChildStd_IN_Wr);
2ea7b0d
+ 
2ea7b0d
+     WaitForSingleObject(pi.hProcess, INFINITE);
2ea7b0d
+ 
2ea7b0d
+     /* Get the command exit code */
2ea7b0d
+     GetExitCodeProcess(pi.hProcess, &ret;;
2ea7b0d
+ 
2ea7b0d
+     if (options & SHELL_READ)
2ea7b0d
+     {
2ea7b0d
+ 	if (ga.ga_len > 0)
2ea7b0d
+ 	{
2ea7b0d
+ 	    append_ga_line(&ga);
2ea7b0d
+ 	    /* remember that the NL was missing */
2ea7b0d
+ 	    curbuf->b_no_eol_lnum = curwin->w_cursor.lnum;
2ea7b0d
+ 	}
2ea7b0d
+ 	else
2ea7b0d
+ 	    curbuf->b_no_eol_lnum = 0;
2ea7b0d
+ 	ga_clear(&ga);
2ea7b0d
+     }
2ea7b0d
+ 
2ea7b0d
+     /* Close the handles to the subprocess, so that it goes away */
2ea7b0d
+     CloseHandle(pi.hThread);
2ea7b0d
+     CloseHandle(pi.hProcess);
2ea7b0d
+ 
2ea7b0d
+     return ret;
2ea7b0d
+ }
2ea7b0d
+ 
2ea7b0d
+     static int
2ea7b0d
+ mch_system(char *cmd, int options)
2ea7b0d
+ {
2ea7b0d
+     /* if we can pipe and the shelltemp option is off */
2ea7b0d
+     if (allowPiping && !p_stmp)
2ea7b0d
+ 	return mch_system_piped(cmd, options);
2ea7b0d
+     else
2ea7b0d
+ 	return mch_system_classic(cmd, options);
2ea7b0d
+ }
2ea7b0d
  #else
2ea7b0d
  
2ea7b0d
  # define mch_system(c, o) system(c)
2ea7b0d
***************
2ea7b0d
*** 3388,3394 ****
2ea7b0d
  	char_u *newcmd;
2ea7b0d
  	long_u cmdlen =  (
2ea7b0d
  #ifdef FEAT_GUI_W32
2ea7b0d
! 		STRLEN(vimrun_path) +
2ea7b0d
  #endif
2ea7b0d
  		STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10);
2ea7b0d
  
2ea7b0d
--- 3897,3903 ----
2ea7b0d
  	char_u *newcmd;
2ea7b0d
  	long_u cmdlen =  (
2ea7b0d
  #ifdef FEAT_GUI_W32
2ea7b0d
! 		(allowPiping && !p_stmp ? 0 : STRLEN(vimrun_path)) +
2ea7b0d
  #endif
2ea7b0d
  		STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10);
2ea7b0d
  
2ea7b0d
***************
2ea7b0d
*** 3497,3503 ****
2ea7b0d
  			    MB_ICONWARNING);
2ea7b0d
  		    need_vimrun_warning = FALSE;
2ea7b0d
  		}
2ea7b0d
! 		if (!s_dont_use_vimrun)
2ea7b0d
  		    /* Use vimrun to execute the command.  It opens a console
2ea7b0d
  		     * window, which can be closed without killing Vim. */
2ea7b0d
  		    vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
2ea7b0d
--- 4006,4012 ----
2ea7b0d
  			    MB_ICONWARNING);
2ea7b0d
  		    need_vimrun_warning = FALSE;
2ea7b0d
  		}
2ea7b0d
! 		if (!s_dont_use_vimrun && (!allowPiping || p_stmp))
2ea7b0d
  		    /* Use vimrun to execute the command.  It opens a console
2ea7b0d
  		     * window, which can be closed without killing Vim. */
2ea7b0d
  		    vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s",
2ea7b0d
***************
2ea7b0d
*** 3521,3527 ****
2ea7b0d
      /* Print the return value, unless "vimrun" was used. */
2ea7b0d
      if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
2ea7b0d
  #if defined(FEAT_GUI_W32)
2ea7b0d
! 		&& ((options & SHELL_DOOUT) || s_dont_use_vimrun)
2ea7b0d
  #endif
2ea7b0d
  	    )
2ea7b0d
      {
2ea7b0d
--- 4030,4037 ----
2ea7b0d
      /* Print the return value, unless "vimrun" was used. */
2ea7b0d
      if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent
2ea7b0d
  #if defined(FEAT_GUI_W32)
2ea7b0d
! 		&& ((options & SHELL_DOOUT) || s_dont_use_vimrun
2ea7b0d
! 						  || (allowPiping && !p_stmp))
2ea7b0d
  #endif
2ea7b0d
  	    )
2ea7b0d
      {
2ea7b0d
*** ../vim-7.3.239/src/proto/misc2.pro	2011-07-07 15:08:53.000000000 +0200
2ea7b0d
--- src/proto/misc2.pro	2011-07-07 15:56:16.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 58,63 ****
2ea7b0d
--- 58,64 ----
2ea7b0d
  char_u *ga_concat_strings __ARGS((garray_T *gap));
2ea7b0d
  void ga_concat __ARGS((garray_T *gap, char_u *s));
2ea7b0d
  void ga_append __ARGS((garray_T *gap, int c));
2ea7b0d
+ void append_ga_line __ARGS((garray_T *gap));
2ea7b0d
  int name_to_mod_mask __ARGS((int c));
2ea7b0d
  int simplify_key __ARGS((int key, int *modifiers));
2ea7b0d
  int handle_x_keys __ARGS((int key));
2ea7b0d
*** ../vim-7.3.239/src/ui.c	2011-06-19 01:14:23.000000000 +0200
2ea7b0d
--- src/ui.c	2011-07-07 15:44:56.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 58,64 ****
2ea7b0d
  #endif
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
! #if defined(UNIX) || defined(VMS) || defined(PROTO)
2ea7b0d
  /*
2ea7b0d
   * When executing an external program, there may be some typed characters that
2ea7b0d
   * are not consumed by it.  Give them back to ui_inchar() and they are stored
2ea7b0d
--- 58,64 ----
2ea7b0d
  #endif
2ea7b0d
  }
2ea7b0d
  
2ea7b0d
! #if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264)
2ea7b0d
  /*
2ea7b0d
   * When executing an external program, there may be some typed characters that
2ea7b0d
   * are not consumed by it.  Give them back to ui_inchar() and they are stored
2ea7b0d
*** ../vim-7.3.239/src/version.c	2011-07-07 15:08:53.000000000 +0200
2ea7b0d
--- src/version.c	2011-07-07 16:14:20.000000000 +0200
2ea7b0d
***************
2ea7b0d
*** 711,712 ****
2ea7b0d
--- 711,714 ----
2ea7b0d
  {   /* Add new patch number below this line */
2ea7b0d
+ /**/
2ea7b0d
+     240,
2ea7b0d
  /**/
2ea7b0d
2ea7b0d
-- 
2ea7b0d
hundred-and-one symptoms of being an internet addict:
2ea7b0d
257. Your "hundred-and-one" lists include well over 101 items, since you
2ea7b0d
     automatically interpret all numbers in hexadecimal notation.
2ea7b0d
     (hex 101 = decimal 257)
2ea7b0d
2ea7b0d
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
2ea7b0d
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2ea7b0d
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
2ea7b0d
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///