Skip to content

Commit

Permalink
Issue #84: pass UTF-16 command line down to my_perl (using _wspawnvp)
Browse files Browse the repository at this point in the history
  • Loading branch information
rschupp committed Feb 2, 2024
1 parent b031b97 commit e8192fd
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions myldr/boot.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#undef readdir

#ifdef _MSC_VER
#include <io.h>
#include <windows.h>
#include <stdio.h>
#include <shellapi.h>
#include <stringapiset.h>
#else
#include <sys/types.h>
#include <unistd.h>
Expand Down Expand Up @@ -206,15 +209,6 @@ int main ( int argc, char **argv, char **env )
char *my_file;
char *my_perl;
char *my_prog;
#ifdef WIN32
typedef BOOL (WINAPI *pALLOW)(DWORD);
HINSTANCE hinstLib;
pALLOW ProcAdd;
char **argp;
#ifndef ASFW_ANY
#define ASFW_ANY -1
#endif
#endif

par_init_env();

Expand Down Expand Up @@ -350,20 +344,39 @@ typedef BOOL (WINAPI *pALLOW)(DWORD);
}

/* finally spawn the custom Perl interpreter */
argv[0] = my_perl;
#ifdef WIN32
hinstLib = LoadLibrary("user32");
if (hinstLib != NULL) {
ProcAdd = (pALLOW) GetProcAddress(hinstLib, "AllowSetForegroundWindow");
if (ProcAdd != NULL)
{
(ProcAdd)(ASFW_ANY);
par_setenv("PAR_SPAWNED", "1");

{
typedef BOOL (WINAPI *pALLOW)(DWORD);
HINSTANCE hinstLib;
pALLOW ProcAdd;
#ifndef ASFW_ANY
#define ASFW_ANY -1
#endif
hinstLib = LoadLibrary("user32");
if (hinstLib != NULL) {
ProcAdd = (pALLOW) GetProcAddress(hinstLib, "AllowSetForegroundWindow");
if (ProcAdd != NULL)
{
(ProcAdd)(ASFW_ANY);
}
}
}

par_setenv("PAR_SPAWNED", "1");
{
LPWSTR *w_argv;
LPWSTR w_my_perl;
int i, len;

w_argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (w_argv == NULL)
par_die("%s: CommandLineToArgvW failed", argv[0]); // TODO GetLastError

#if 0
TODO reimplement for wchar_t?
/* quote argv strings if necessary, cf. Win32::ShellQuote */
char **argp;
for (argp = argv; *argp; argp++)
{
int len = strlen(*argp);
Expand All @@ -374,12 +387,25 @@ typedef BOOL (WINAPI *pALLOW)(DWORD);
*argp = shell_quote(*argp);
}
}
#endif

rc = spawnvp(P_WAIT, my_perl, (char* const*)argv);
len = MultiByteToWideChar(CP_THREAD_ACP, 0, my_perl, -1, NULL, 0);
if (len == 0)
par_die("%s: failed to convert string to UTF-16", argv[0]); // TODO GetLastError
w_my_perl = malloc((len + 1) * sizeof(wchar_t)); // TODO +1 not required
len = MultiByteToWideChar(CP_THREAD_ACP, 0, my_perl, -1, w_my_perl, len);
w_argv[0] = w_my_perl;

rc = _wspawnvp(P_WAIT, w_my_perl, (char* const*)w_argv);

free(w_my_perl);
LocalFree(w_argv);
}

par_cleanup(stmpdir);
exit(rc);
#else
argv[0] = my_perl;
execvp(my_perl, argv);
par_die("%s: exec of %s (custom Perl interpreter) failed (errno=%i)\n",
argv[0], my_perl, errno);
Expand Down

0 comments on commit e8192fd

Please sign in to comment.