[CMD] Fix the errorlevel value set by the EXIT command, and when a batch file has run.

CORE-10495 CORE-13672

- Fix the behaviour of the EXIT command, where it set the last errorlevel
  value ONLY when it was called with the /b switch and otherwise kept the
  ambient one, instead of always using the value that the user specified
  on the command-line.

- When a batch file has terminated, make the Batch() helper returning the
  errorlevel value so that, when the batch file has been started with the
  CALL command, CALL can in turn set the correct errorlevel value.

Verified with respect to Windows' cmd.exe.
This commit is contained in:
Hermès Bélusca-Maïto 2017-11-18 23:41:31 +01:00
parent 24ed534474
commit 26ff2c8ef3
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 26 additions and 9 deletions

View file

@ -299,7 +299,8 @@ INT Batch (LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
* return until this context has been exited */
new.prev = bc;
/* copy some fields in the new structure if it is the same file */
if (same_fn) {
if (same_fn)
{
new.mem = bc->mem;
new.memsize = bc->memsize;
new.mempos = 0;
@ -366,6 +367,9 @@ INT Batch (LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
FreeCommand(Cmd);
}
/* Always return the current errorlevel */
ret = nErrorLevel;
TRACE ("Batch: returns TRUE\n");
fc = saved_fc;

View file

@ -514,26 +514,39 @@ INT CommandExit(LPTSTR param)
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPaging(TRUE, STRING_EXIT_HELP);
/* Just make sure */
/* Just make sure we don't exit */
bExit = FALSE;
/* Don't exit */
return 0;
}
if (bc != NULL && _tcsnicmp(param, _T("/b"), 2) == 0)
if (_tcsnicmp(param, _T("/b"), 2) == 0)
{
param += 2;
while (_istspace(*param))
param++;
if (_istdigit(*param))
nErrorLevel = _ttoi(param);
ExitBatch();
/*
* If a current batch file is running, exit it,
* otherwise exit this command interpreter instance.
*/
if (bc)
ExitBatch();
else
bExit = TRUE;
}
else
{
/* Exit this command interpreter instance */
bExit = TRUE;
}
/* Search for an optional exit code */
while (_istspace(*param))
param++;
/* Set the errorlevel to the exit code */
if (_istdigit(*param))
nErrorLevel = _ttoi(param);
return 0;
}