@@ -168,12 +168,28 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
168168 return cmd ;
169169}
170170
171+ static int strip_prefix (LPWSTR str , int * len , LPCWSTR prefix )
172+ {
173+ LPWSTR start = str ;
174+ do {
175+ if (str - start > * len )
176+ return 0 ;
177+ if (!* prefix ) {
178+ * len -= str - start ;
179+ memmove (start , str ,
180+ sizeof (WCHAR ) * (wcslen (str ) + 1 ));
181+ return 1 ;
182+ }
183+ } while (* str ++ == * prefix ++ );
184+ return 0 ;
185+ }
186+
171187static int configure_via_resource (LPWSTR basename , LPWSTR exepath , LPWSTR exep ,
172188 LPWSTR * prefix_args , int * prefix_args_len ,
173189 int * is_git_command , LPWSTR * working_directory , int * full_path ,
174- int * skip_arguments )
190+ int * skip_arguments , int * allocate_console )
175191{
176- int id = 0 , minimal_search_path , wargc ;
192+ int id = 0 , minimal_search_path , needs_a_console , wargc ;
177193 LPWSTR * wargv ;
178194
179195#define BUFSIZE 65536
@@ -182,17 +198,15 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
182198
183199 for (id = 0 ; ; id ++ ) {
184200 minimal_search_path = 0 ;
201+ needs_a_console = 0 ;
185202 len = LoadString (NULL , id , buf , BUFSIZE );
186203
187204 if (!len ) {
188205 if (!id )
189206 return 0 ; /* no resources found */
190207
191208 fwprintf (stderr , L"Need a valid command-line; "
192- L"Copy %s to edit-res.exe and call\n"
193- L"\n\tedit-res.exe command %s "
194- L"\"<command-line>\"\n" ,
195- basename , basename );
209+ L"Edit the string resources accordingly\n" );
196210 exit (1 );
197211 }
198212
@@ -202,10 +216,13 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
202216 exit (1 );
203217 }
204218
205- if (!wcsncmp (L"MINIMAL_PATH=1 " , buf , 15 )) {
206- minimal_search_path = 1 ;
207- memmove (buf , buf + 15 ,
208- sizeof (WCHAR ) * (wcslen (buf + 15 ) + 1 ));
219+ for (;;) {
220+ if (strip_prefix (buf , & len , L"MINIMAL_PATH=1 " ))
221+ minimal_search_path = 1 ;
222+ else if (strip_prefix (buf , & len , L"ALLOC_CONSOLE=1 " ))
223+ needs_a_console = 1 ;
224+ else
225+ break ;
209226 }
210227
211228 buf [len ] = L'\0' ;
@@ -284,6 +301,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
284301 }
285302 if (minimal_search_path )
286303 * full_path = 0 ;
304+ if (needs_a_console )
305+ * allocate_console = 1 ;
287306 LocalFree (wargv );
288307
289308 return 1 ;
@@ -292,7 +311,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
292311int main (void )
293312{
294313 int r = 1 , wait = 1 , prefix_args_len = -1 , needs_env_setup = 1 ,
295- is_git_command = 1 , full_path = 1 , skip_arguments = 0 ;
314+ is_git_command = 1 , full_path = 1 , skip_arguments = 0 ,
315+ allocate_console = 0 ;
296316 WCHAR exepath [MAX_PATH ], exe [MAX_PATH ];
297317 LPWSTR cmd = NULL , exep = exe , prefix_args = NULL , basename ;
298318 LPWSTR working_directory = NULL ;
@@ -312,11 +332,12 @@ int main(void)
312332 if (configure_via_resource (basename , exepath , exep ,
313333 & prefix_args , & prefix_args_len ,
314334 & is_git_command , & working_directory ,
315- & full_path , & skip_arguments )) {
335+ & full_path , & skip_arguments , & allocate_console )) {
316336 /* do nothing */
317337 }
318338 else if (!wcsicmp (basename , L"git-gui.exe" )) {
319339 static WCHAR buffer [BUFSIZE ];
340+ allocate_console = 1 ;
320341 if (!PathRemoveFileSpec (exepath )) {
321342 fwprintf (stderr ,
322343 L"Invalid executable path: %s\n" , exepath );
@@ -372,6 +393,7 @@ int main(void)
372393 }
373394 else if (!wcsicmp (basename , L"gitk.exe" )) {
374395 static WCHAR buffer [BUFSIZE ];
396+ allocate_console = 1 ;
375397 if (!PathRemoveFileSpec (exepath )) {
376398 fwprintf (stderr ,
377399 L"Invalid executable path: %s\n" , exepath );
@@ -423,16 +445,20 @@ int main(void)
423445 ZeroMemory (& si , sizeof (STARTUPINFO ));
424446 si .cb = sizeof (STARTUPINFO );
425447
426- console_handle = CreateFile (L"CONOUT$" , GENERIC_WRITE ,
448+ if (allocate_console )
449+ creation_flags |= CREATE_NEW_CONSOLE ;
450+ else if ((console_handle = CreateFile (L"CONOUT$" , GENERIC_WRITE ,
427451 FILE_SHARE_WRITE , NULL , OPEN_EXISTING ,
428- FILE_ATTRIBUTE_NORMAL , NULL );
429- if ( console_handle != INVALID_HANDLE_VALUE )
452+ FILE_ATTRIBUTE_NORMAL , NULL )) !=
453+ INVALID_HANDLE_VALUE )
430454 CloseHandle (console_handle );
431455 else {
456+ #define STD_HANDLE (field , id ) si.hStd##field = GetStdHandle(STD_##id); if (!si.hStd##field) si.hStd##field = INVALID_HANDLE_VALUE
457+ STD_HANDLE (Input , INPUT_HANDLE );
458+ STD_HANDLE (Output , OUTPUT_HANDLE );
459+ STD_HANDLE (Error , ERROR_HANDLE );
432460 si .dwFlags = STARTF_USESTDHANDLES ;
433- si .hStdInput = GetStdHandle (STD_INPUT_HANDLE );
434- si .hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE );
435- si .hStdError = GetStdHandle (STD_ERROR_HANDLE );
461+
436462
437463 creation_flags |= CREATE_NO_WINDOW ;
438464 }
@@ -441,7 +467,8 @@ int main(void)
441467 cmd , /* modified command line */
442468 NULL , /* process handle inheritance */
443469 NULL , /* thread handle inheritance */
444- TRUE, /* handles inheritable? */
470+ /* handles inheritable? */
471+ allocate_console ? FALSE : TRUE,
445472 creation_flags ,
446473 NULL , /* environment: use parent */
447474 working_directory , /* use parent's */
0 commit comments