utf8-ize @ARGV when -C switch is used on Windows
[p5sagit/p5-mst-13.2.git] / win32 / win32.c
index b172759..87d4111 100644 (file)
@@ -15,6 +15,7 @@
 #define Win32_Winsock
 #endif
 #include <windows.h>
+#include <shellapi.h>
 #include <winnt.h>
 #include <io.h>
 
@@ -2713,7 +2714,12 @@ _fixed_read(int fh, void *buf, unsigned cnt)
        return -1;
     }
 
-    EnterCriticalSection(&(_pioinfo(fh)->lock));  /* lock file */
+    /*
+     * If lockinitflag is FALSE, assume fd is device
+     * lockinitflag is set to TRUE by open.
+     */
+    if (_pioinfo(fh)->lockinitflag)
+       EnterCriticalSection(&(_pioinfo(fh)->lock));  /* lock file */
 
     bytes_read = 0;                 /* nothing read yet */
     buffer = (char*)buf;
@@ -2861,7 +2867,8 @@ _fixed_read(int fh, void *buf, unsigned cnt)
     }
 
 functionexit:  
-    LeaveCriticalSection(&(_pioinfo(fh)->lock));    /* unlock file */
+    if (_pioinfo(fh)->lockinitflag)
+       LeaveCriticalSection(&(_pioinfo(fh)->lock));    /* unlock file */
 
     return bytes_read;
 }
@@ -3123,6 +3130,7 @@ win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
     int ret;
     void* env;
     char* dir;
+    child_IO_table tbl;
     STARTUPINFO StartupInfo;
     PROCESS_INFORMATION ProcessInformation;
     DWORD create = 0;
@@ -3151,9 +3159,10 @@ win32_spawnvp(int mode, const char *cmdname, const char *const *argv)
     }
     memset(&StartupInfo,0,sizeof(StartupInfo));
     StartupInfo.cb = sizeof(StartupInfo);
-    StartupInfo.hStdInput  = GetStdHandle(STD_INPUT_HANDLE);
-    StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
-    StartupInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);
+    PerlEnv_get_child_IO(&tbl);
+    StartupInfo.hStdInput  = tbl.childStdIn;
+    StartupInfo.hStdOutput = tbl.childStdOut;
+    StartupInfo.hStdError  = tbl.childStdErr;
     if (StartupInfo.hStdInput != INVALID_HANDLE_VALUE &&
        StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
        StartupInfo.hStdError != INVALID_HANDLE_VALUE)
@@ -3964,6 +3973,15 @@ Perl_win32_init(int *argcp, char ***argvp)
     MALLOC_INIT;
 }
 
+void
+win32_get_child_IO(child_IO_table* ptbl)
+{
+    ptbl->childStdIn   = GetStdHandle(STD_INPUT_HANDLE);
+    ptbl->childStdOut  = GetStdHandle(STD_OUTPUT_HANDLE);
+    ptbl->childStdErr  = GetStdHandle(STD_ERROR_HANDLE);
+}
+
+
 #ifdef USE_ITHREADS
 
 #  ifdef PERL_OBJECT
@@ -3987,3 +4005,31 @@ Perl_sys_intern_dup(pTHX_ struct interp_intern *src, struct interp_intern *dst)
 }
 #endif
 
+static void
+win32_free_argvw(pTHXo_ void *ptr)
+{
+    char** argv = (char**)ptr;
+    while(*argv) {
+       Safefree(*argv);
+       *argv++ = Nullch;
+    }
+}
+
+void
+win32_argv2utf8(pTHX_ int argc, char** argv)
+{
+    char* psz;
+    int length, wargc;
+    LPWSTR* lpwStr = CommandLineToArgvW(GetCommandLineW(), &wargc);
+    if (lpwStr && argc) {
+       while (argc--) {
+           length = WideCharToMultiByte(CP_UTF8, 0, lpwStr[--wargc], -1, NULL, 0, NULL, NULL);
+           Newz(0, psz, length, char);
+           WideCharToMultiByte(CP_UTF8, 0, lpwStr[wargc], -1, psz, length, NULL, NULL);
+           argv[argc] = psz;
+       }
+       call_atexit(win32_free_argvw, argv);
+    }
+    GlobalFree((HGLOBAL)lpwStr);
+}
+