@@ -168,12 +168,28 @@ static LPWSTR fixup_commandline(LPWSTR exepath, LPWSTR *exep, int *wait,
168
168
return cmd ;
169
169
}
170
170
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
+
171
187
static int configure_via_resource (LPWSTR basename , LPWSTR exepath , LPWSTR exep ,
172
188
LPWSTR * prefix_args , int * prefix_args_len ,
173
189
int * is_git_command , LPWSTR * working_directory , int * full_path ,
174
- int * skip_arguments )
190
+ int * skip_arguments , int * allocate_console )
175
191
{
176
- int id = 0 , minimal_search_path , wargc ;
192
+ int id = 0 , minimal_search_path , needs_a_console , wargc ;
177
193
LPWSTR * wargv ;
178
194
179
195
#define BUFSIZE 65536
@@ -182,17 +198,15 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
182
198
183
199
for (id = 0 ; ; id ++ ) {
184
200
minimal_search_path = 0 ;
201
+ needs_a_console = 0 ;
185
202
len = LoadString (NULL , id , buf , BUFSIZE );
186
203
187
204
if (!len ) {
188
205
if (!id )
189
206
return 0 ; /* no resources found */
190
207
191
208
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" );
196
210
exit (1 );
197
211
}
198
212
@@ -202,10 +216,13 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
202
216
exit (1 );
203
217
}
204
218
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 ;
209
226
}
210
227
211
228
buf [len ] = L'\0' ;
@@ -284,6 +301,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
284
301
}
285
302
if (minimal_search_path )
286
303
* full_path = 0 ;
304
+ if (needs_a_console )
305
+ * allocate_console = 1 ;
287
306
LocalFree (wargv );
288
307
289
308
return 1 ;
@@ -292,7 +311,8 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
292
311
int main (void )
293
312
{
294
313
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 ;
296
316
WCHAR exepath [MAX_PATH ], exe [MAX_PATH ];
297
317
LPWSTR cmd = NULL , exep = exe , prefix_args = NULL , basename ;
298
318
LPWSTR working_directory = NULL ;
@@ -312,11 +332,12 @@ int main(void)
312
332
if (configure_via_resource (basename , exepath , exep ,
313
333
& prefix_args , & prefix_args_len ,
314
334
& is_git_command , & working_directory ,
315
- & full_path , & skip_arguments )) {
335
+ & full_path , & skip_arguments , & allocate_console )) {
316
336
/* do nothing */
317
337
}
318
338
else if (!wcsicmp (basename , L"git-gui.exe" )) {
319
339
static WCHAR buffer [BUFSIZE ];
340
+ allocate_console = 1 ;
320
341
if (!PathRemoveFileSpec (exepath )) {
321
342
fwprintf (stderr ,
322
343
L"Invalid executable path: %s\n" , exepath );
@@ -372,6 +393,7 @@ int main(void)
372
393
}
373
394
else if (!wcsicmp (basename , L"gitk.exe" )) {
374
395
static WCHAR buffer [BUFSIZE ];
396
+ allocate_console = 1 ;
375
397
if (!PathRemoveFileSpec (exepath )) {
376
398
fwprintf (stderr ,
377
399
L"Invalid executable path: %s\n" , exepath );
@@ -423,16 +445,20 @@ int main(void)
423
445
ZeroMemory (& si , sizeof (STARTUPINFO ));
424
446
si .cb = sizeof (STARTUPINFO );
425
447
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 ,
427
451
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 )
430
454
CloseHandle (console_handle );
431
455
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 );
432
460
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
+
436
462
437
463
creation_flags |= CREATE_NO_WINDOW ;
438
464
}
@@ -441,7 +467,8 @@ int main(void)
441
467
cmd , /* modified command line */
442
468
NULL , /* process handle inheritance */
443
469
NULL , /* thread handle inheritance */
444
- TRUE, /* handles inheritable? */
470
+ /* handles inheritable? */
471
+ allocate_console ? FALSE : TRUE,
445
472
creation_flags ,
446
473
NULL , /* environment: use parent */
447
474
working_directory , /* use parent's */
0 commit comments