1818// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919// IN THE SOFTWARE.
2020
21+ // MSYS's sys/cygwin.h header only declares cygwin_internal if WINVER is
22+ // defined, which is defined in windows.h. Therefore, include windows.h early.
23+ #include < windows.h>
24+
2125#include < stdio.h>
2226#include < stdlib.h>
2327#include < string.h>
@@ -218,7 +222,7 @@ static void setFdNonBlock(int fd)
218222static std::string convertPosixPathToWin (const std::string &path)
219223{
220224 char *tmp;
221- #if CYGWIN_VERSION_API_MINOR >= 181
225+ #if !defined(__MSYS__) && CYGWIN_VERSION_API_MINOR >= 181
222226 ssize_t newSize = cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_RELATIVE,
223227 path.c_str (), NULL , 0 );
224228 assert (newSize >= 0 );
@@ -296,6 +300,41 @@ static wchar_t *heapMbsToWcs(const char *text)
296300 return ret;
297301}
298302
303+ void setupWin32Environment ()
304+ {
305+ std::string dbgValue;
306+ const char *dbgValueCStr = getenv (" WINPTYDBG" );
307+ if (dbgValueCStr != NULL )
308+ dbgValue = dbgValueCStr;
309+
310+ #if defined(__MSYS__) && CYGWIN_VERSION_API_MINOR >= 48 || \
311+ !defined (__MSYS__) && CYGWIN_VERSION_API_MINOR >= 153
312+ // Use CW_SYNC_WINENV to copy the Unix environment to the Win32
313+ // environment. The command performs special translation on some variables
314+ // (such as PATH and TMP). It also copies the WINPTYDBG variable.
315+ //
316+ // Note that the API minor versions have diverged in Cygwin and MSYS.
317+ // CW_SYNC_WINENV was added to Cygwin in version 153. (Cygwin's
318+ // include/cygwin/version.h says that CW_SETUP_WINENV was added in 153.
319+ // The flag was renamed 8 days after it was added, but the API docs weren't
320+ // updated.) The flag was added to MSYS in version 48.
321+ //
322+ // Also, in my limited testing, this call seems to be necessary with Cygwin
323+ // but unnecessary with MSYS. Perhaps MSYS is automatically syncing the
324+ // Unix environment with the Win32 environment before starting console.exe?
325+ // It shouldn't hurt to call it for MSYS.
326+ cygwin_internal (CW_SYNC_WINENV);
327+ #endif
328+
329+ // Copy the WINPTYDBG environment variable from the Cygwin environment
330+ // to the Win32 environment so the agent will inherit it.
331+ if (!dbgValue.empty ()) {
332+ wchar_t *dbgvarW = heapMbsToWcs (dbgValue.c_str ());
333+ SetEnvironmentVariableW (L" WINPTYDBG" , dbgvarW);
334+ delete [] dbgvarW;
335+ }
336+ }
337+
299338int main (int argc, char *argv[])
300339{
301340 if (argc == 1 ) {
@@ -304,14 +343,7 @@ int main(int argc, char *argv[])
304343 return 0 ;
305344 }
306345
307- {
308- // Copy the WINPTYDBG environment variable from the Cygwin environment
309- // to the Win32 environment so the agent will inherit it.
310- const char *dbgvar = getenv (" WINPTYDBG" );
311- if (dbgvar != NULL ) {
312- SetEnvironmentVariableW (L" WINPTYDBG" , L" 1" );
313- }
314- }
346+ setupWin32Environment ();
315347
316348 winsize sz;
317349 ioctl (STDIN_FILENO, TIOCGWINSZ, &sz);
0 commit comments