Skip to content

Commit

Permalink
Merge branch 'xp-console'
Browse files Browse the repository at this point in the history
This branch wants to be merged into the git-wrapper branch; it fixes an
issue with the way we called the MSys2 terminal `mintty` from the GUI
application `git-bash.exe`.

Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed May 4, 2015
2 parents 5294e72 + dbd0783 commit a105bde
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions compat/win32/git-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,28 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
return cmd;
}

static int strip_prefix(LPWSTR str, int *len, LPCWSTR prefix)
{
LPWSTR start = str;
do {
if (str - start > *len)
return 0;
if (!*prefix) {
*len -= str - start;
memmove(start, str,
sizeof(WCHAR) * (wcslen(str) + 1));
return 1;
}
} while (*str++ == *prefix++);
return 0;
}

static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
LPWSTR *prefix_args, int *prefix_args_len,
int *is_git_command, LPWSTR *working_directory, int *full_path,
int *skip_arguments)
int *skip_arguments, int *allocate_console)
{
int id = 0, minimal_search_path, wargc;
int id = 0, minimal_search_path, needs_a_console, wargc;
LPWSTR *wargv;

#define BUFSIZE 65536
Expand All @@ -182,17 +198,15 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,

for (id = 0; ; id++) {
minimal_search_path = 0;
needs_a_console = 0;
len = LoadString(NULL, id, buf, BUFSIZE);

if (!len) {
if (!id)
return 0; /* no resources found */

fwprintf(stderr, L"Need a valid command-line; "
L"Copy %s to edit-res.exe and call\n"
L"\n\tedit-res.exe command %s "
L"\"<command-line>\"\n",
basename, basename);
L"Edit the string resources accordingly\n");
exit(1);
}

Expand All @@ -202,10 +216,13 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
exit(1);
}

if (!wcsncmp(L"MINIMAL_PATH=1 ", buf, 15)) {
minimal_search_path = 1;
memmove(buf, buf + 15,
sizeof(WCHAR) * (wcslen(buf + 15) + 1));
for (;;) {
if (strip_prefix(buf, &len, L"MINIMAL_PATH=1 "))
minimal_search_path = 1;
else if (strip_prefix(buf, &len, L"ALLOC_CONSOLE=1 "))
needs_a_console = 1;
else
break;
}

buf[len] = L'\0';
Expand Down Expand Up @@ -284,6 +301,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
}
if (minimal_search_path)
*full_path = 0;
if (needs_a_console)
*allocate_console = 1;
LocalFree(wargv);

return 1;
Expand All @@ -292,7 +311,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
int main(void)
{
int r = 1, wait = 1, prefix_args_len = -1, needs_env_setup = 1,
is_git_command = 1, full_path = 1, skip_arguments = 0;
is_git_command = 1, full_path = 1, skip_arguments = 0,
allocate_console = 0;
WCHAR exepath[MAX_PATH], exe[MAX_PATH];
LPWSTR cmd = NULL, exep = exe, prefix_args = NULL, basename;
LPWSTR working_directory = NULL;
Expand All @@ -312,11 +332,12 @@ int main(void)
if (configure_via_resource(basename, exepath, exep,
&prefix_args, &prefix_args_len,
&is_git_command, &working_directory,
&full_path, &skip_arguments)) {
&full_path, &skip_arguments, &allocate_console)) {
/* do nothing */
}
else if (!wcsicmp(basename, L"git-gui.exe")) {
static WCHAR buffer[BUFSIZE];
allocate_console = 1;
if (!PathRemoveFileSpec(exepath)) {
fwprintf(stderr,
L"Invalid executable path: %s\n", exepath);
Expand Down Expand Up @@ -372,6 +393,7 @@ int main(void)
}
else if (!wcsicmp(basename, L"gitk.exe")) {
static WCHAR buffer[BUFSIZE];
allocate_console = 1;
if (!PathRemoveFileSpec(exepath)) {
fwprintf(stderr,
L"Invalid executable path: %s\n", exepath);
Expand Down Expand Up @@ -423,16 +445,20 @@ int main(void)
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);

console_handle = CreateFile(L"CONOUT$", GENERIC_WRITE,
if (allocate_console)
creation_flags |= CREATE_NEW_CONSOLE;
else if ((console_handle = CreateFile(L"CONOUT$", GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (console_handle != INVALID_HANDLE_VALUE)
FILE_ATTRIBUTE_NORMAL, NULL)) !=
INVALID_HANDLE_VALUE)
CloseHandle(console_handle);
else {
#define STD_HANDLE(field, id) si.hStd##field = GetStdHandle(STD_##id); if (!si.hStd##field) si.hStd##field = INVALID_HANDLE_VALUE
STD_HANDLE(Input, INPUT_HANDLE);
STD_HANDLE(Output, OUTPUT_HANDLE);
STD_HANDLE(Error, ERROR_HANDLE);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);


creation_flags |= CREATE_NO_WINDOW;
}
Expand All @@ -441,7 +467,8 @@ int main(void)
cmd, /* modified command line */
NULL, /* process handle inheritance */
NULL, /* thread handle inheritance */
TRUE, /* handles inheritable? */
/* handles inheritable? */
allocate_console ? FALSE : TRUE,
creation_flags,
NULL, /* environment: use parent */
working_directory, /* use parent's */
Expand Down

0 comments on commit a105bde

Please sign in to comment.