@@ -149,21 +149,23 @@ typedef NTSTATUS (WINAPI *NtQueryInformationProcessProc)(HANDLE, PROCESSINFOCLAS
149
149
PVOID , ULONG , PULONG );
150
150
typedef NTSTATUS (WINAPI * NtReadVirtualMemoryProc )(HANDLE , PVOID , PVOID , ULONG , PULONG );
151
151
152
- BOOL bExit = FALSE; /* indicates EXIT was typed */
153
- BOOL bCanExit = TRUE; /* indicates if this shell is exitable */
152
+ BOOL bExit = FALSE; /* Indicates EXIT was typed */
153
+ BOOL bCanExit = TRUE; /* Indicates if this shell is exitable */
154
154
BOOL bCtrlBreak = FALSE; /* Ctrl-Break or Ctrl-C hit */
155
155
BOOL bIgnoreEcho = FALSE; /* Set this to TRUE to prevent a newline, when executing a command */
156
- static BOOL bWaitForCommand = FALSE ; /* When we are executing something passed on the commandline after /c or /k */
156
+ static BOOL fSingleCommand = 0 ; /* When we are executing something passed on the command line after /C or /K */
157
157
INT nErrorLevel = 0 ; /* Errorlevel of last launched external program */
158
158
CRITICAL_SECTION ChildProcessRunningLock ;
159
159
BOOL bDisableBatchEcho = FALSE;
160
160
BOOL bEnableExtensions = TRUE;
161
161
BOOL bDelayedExpansion = FALSE;
162
- BOOL bTitleSet = FALSE;
163
162
DWORD dwChildProcessId = 0 ;
164
163
LPTSTR lpOriginalEnvironment ;
165
164
HANDLE CMD_ModuleHandle ;
166
165
166
+ BOOL bTitleSet = FALSE; /* Indicates whether the console title has been changed and needs to be restored later */
167
+ TCHAR szCurTitle [MAX_PATH ];
168
+
167
169
static NtQueryInformationProcessProc NtQueryInformationProcessPtr = NULL ;
168
170
static NtReadVirtualMemoryProc NtReadVirtualMemoryPtr = NULL ;
169
171
@@ -302,23 +304,51 @@ HANDLE RunFile(DWORD flags, LPTSTR filename, LPTSTR params,
302
304
}
303
305
304
306
307
+ static VOID
308
+ SetConTitle (LPCTSTR pszTitle )
309
+ {
310
+ TCHAR szNewTitle [MAX_PATH ];
311
+
312
+ if (!pszTitle )
313
+ return ;
314
+
315
+ /* Don't do anything if we run inside a batch file, or we are just running a single command */
316
+ if (bc || (fSingleCommand == 1 ))
317
+ return ;
318
+
319
+ /* Save the original console title and build a new one */
320
+ GetConsoleTitle (szCurTitle , ARRAYSIZE (szCurTitle ));
321
+ StringCchPrintf (szNewTitle , ARRAYSIZE (szNewTitle ),
322
+ _T ("%s - %s" ), szCurTitle , pszTitle );
323
+ bTitleSet = TRUE;
324
+ ConSetTitle (szNewTitle );
325
+ }
326
+
327
+ static VOID
328
+ ResetConTitle (VOID )
329
+ {
330
+ /* Restore the original console title */
331
+ if (!bc && bTitleSet )
332
+ {
333
+ ConSetTitle (szCurTitle );
334
+ bTitleSet = FALSE;
335
+ }
336
+ }
305
337
306
338
/*
307
- * This command (in first ) was not found in the command table
339
+ * This command (in First ) was not found in the command table
308
340
*
309
- * Full - buffer to hold whole command line
341
+ * Full - output buffer to hold whole command line
310
342
* First - first word on command line
311
343
* Rest - rest of command line
312
344
*/
313
345
static INT
314
346
Execute (LPTSTR Full , LPTSTR First , LPTSTR Rest , PARSED_COMMAND * Cmd )
315
347
{
316
- TCHAR szFullName [MAX_PATH ];
317
348
TCHAR * first , * rest , * dot ;
318
- TCHAR szWindowTitle [MAX_PATH ];
319
- TCHAR szNewTitle [MAX_PATH * 2 ];
320
349
DWORD dwExitCode = 0 ;
321
350
TCHAR * FirstEnd ;
351
+ TCHAR szFullName [MAX_PATH ];
322
352
TCHAR szFullCmdLine [CMDLINE_LENGTH ];
323
353
324
354
TRACE ("Execute: \'%s\' \'%s\'\n" , debugstr_aw (First ), debugstr_aw (Rest ));
@@ -344,10 +374,10 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
344
374
}
345
375
346
376
/* Copy the new first/rest into the buffer */
347
- first = Full ;
348
377
rest = & Full [FirstEnd - First + 1 ];
349
378
_tcscpy (rest , FirstEnd );
350
379
_tcscat (rest , Rest );
380
+ first = Full ;
351
381
* FirstEnd = _T ('\0' );
352
382
_tcscpy (first , First );
353
383
@@ -356,8 +386,8 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
356
386
{
357
387
BOOL working = TRUE;
358
388
if (!SetCurrentDirectory (first ))
359
- /* Guess they changed disc or something, handle that gracefully and get to root */
360
389
{
390
+ /* Guess they changed disc or something, handle that gracefully and get to root */
361
391
TCHAR str [4 ];
362
392
str [0 ]= first [0 ];
363
393
str [1 ]= _T (':' );
@@ -379,19 +409,19 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
379
409
return 1 ;
380
410
}
381
411
382
- /* Save the original console title and build a new one */
383
- GetConsoleTitle (szWindowTitle , ARRAYSIZE (szWindowTitle ));
384
- bTitleSet = FALSE;
385
- StringCchPrintf (szNewTitle , ARRAYSIZE (szNewTitle ),
386
- _T ("%s - %s%s" ), szWindowTitle , First , Rest );
387
- ConSetTitle (szNewTitle );
412
+ /* Set the new console title */
413
+ FirstEnd = first + (FirstEnd - First ); /* Point to the separating NULL in the full built string */
414
+ * FirstEnd = _T (' ' );
415
+ SetConTitle (Full );
388
416
389
417
/* check if this is a .BAT or .CMD file */
390
418
dot = _tcsrchr (szFullName , _T ('.' ));
391
419
if (dot && (!_tcsicmp (dot , _T (".bat" )) || !_tcsicmp (dot , _T (".cmd" ))))
392
420
{
393
421
while (* rest == _T (' ' ))
394
422
rest ++ ;
423
+
424
+ * FirstEnd = _T ('\0' );
395
425
TRACE ("[BATCH: %s %s]\n" , debugstr_aw (szFullName ), debugstr_aw (rest ));
396
426
dwExitCode = Batch (szFullName , first , rest , Cmd );
397
427
}
@@ -402,7 +432,7 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
402
432
STARTUPINFO stui ;
403
433
404
434
/* build command line for CreateProcess(): FullName + " " + rest */
405
- BOOL quoted = !!_tcschr (First , ' ' );
435
+ BOOL quoted = !!_tcschr (First , _T ( ' ' ) );
406
436
_tcscpy (szFullCmdLine , quoted ? _T ("\"" ) : _T ("" ));
407
437
_tcsncat (szFullCmdLine , First , CMDLINE_LENGTH - _tcslen (szFullCmdLine ) - 1 );
408
438
_tcsncat (szFullCmdLine , quoted ? _T ("\"" ) : _T ("" ), CMDLINE_LENGTH - _tcslen (szFullCmdLine ) - 1 );
@@ -416,8 +446,9 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
416
446
TRACE ("[EXEC: %s]\n" , debugstr_aw (szFullCmdLine ));
417
447
418
448
/* fill startup info */
419
- memset (& stui , 0 , sizeof (STARTUPINFO ));
420
- stui .cb = sizeof (STARTUPINFO );
449
+ memset (& stui , 0 , sizeof (stui ));
450
+ stui .cb = sizeof (stui );
451
+ stui .lpTitle = Full ;
421
452
stui .dwFlags = STARTF_USESHOWWINDOW ;
422
453
stui .wShowWindow = SW_SHOWDEFAULT ;
423
454
@@ -448,9 +479,11 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
448
479
SW_SHOWNORMAL );
449
480
}
450
481
482
+ * FirstEnd = _T ('\0' );
483
+
451
484
if (prci .hProcess != NULL )
452
485
{
453
- if (bc != NULL || bWaitForCommand || IsConsoleProcess (prci .hProcess ))
486
+ if (bc != NULL || fSingleCommand != 0 || IsConsoleProcess (prci .hProcess ))
454
487
{
455
488
/* when processing a batch file or starting console processes: execute synchronously */
456
489
EnterCriticalSection (& ChildProcessRunningLock );
@@ -501,8 +534,7 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
501
534
}
502
535
503
536
/* Restore the original console title */
504
- if (!bTitleSet )
505
- ConSetTitle (szWindowTitle );
537
+ ResetConTitle ();
506
538
507
539
return dwExitCode ;
508
540
}
@@ -568,9 +600,19 @@ DoCommand(LPTSTR first, LPTSTR rest, PARSED_COMMAND *Cmd)
568
600
569
601
/* Skip over whitespace to rest of line, exclude 'echo' command */
570
602
if (_tcsicmp (cmdptr -> name , _T ("echo" )) != 0 )
603
+ {
571
604
while (_istspace (* param ))
572
605
param ++ ;
606
+ }
607
+
608
+ /* Set the new console title */
609
+ SetConTitle (com );
610
+
573
611
ret = cmdptr -> func (param );
612
+
613
+ /* Restore the original console title */
614
+ ResetConTitle ();
615
+
574
616
cmd_free (com );
575
617
return ret ;
576
618
}
@@ -1896,6 +1938,7 @@ Initialize(VOID)
1896
1938
else if (option == _T ('C' ) || option == _T ('K' ) || option == _T ('R' ))
1897
1939
{
1898
1940
/* Remainder of command line is a command to be run */
1941
+ fSingleCommand = ((option == _T ('K' )) << 1 ) | 1 ;
1899
1942
break ;
1900
1943
}
1901
1944
else if (option == _T ('D' ))
@@ -1983,14 +2026,13 @@ Initialize(VOID)
1983
2026
{
1984
2027
/* Do the /C or /K command */
1985
2028
GetCmdLineCommand (commandline , & ptr [2 ], AlwaysStrip );
1986
- bWaitForCommand = TRUE;
1987
2029
/* nExitCode = */ ParseCommandLine (commandline );
1988
- bWaitForCommand = FALSE;
1989
- if (option != _T ('K' ))
2030
+ if (fSingleCommand == 1 )
1990
2031
{
1991
2032
// nErrorLevel = nExitCode;
1992
2033
bExit = TRUE;
1993
2034
}
2035
+ fSingleCommand = 0 ;
1994
2036
}
1995
2037
}
1996
2038
0 commit comments