[CMD] Change ERRORLEVEL behaviour for commands ASSOC, PATH, PROMPT and SET.

Commands APPEND/DPATH and FTYPE are also concerned by this; however
we do not implement them in our CMD.EXE yet.

These commands set the ERRORLEVEL differently, whether or not they are
run manually from the command-line/from a .BAT file, or from a .CMD file:

- From command-line/.BAT file, these commands set the ERRORLEVEL only if
  an error occurs. So, if two commands are run consecutively and the first
  one fails, the ERRORLEVEL will remain set even if the second command
  succeeds.

- However, when being run from a .CMD file, these command will always
  set the ERRORLEVEL. In the example case described above, the second
  command that succeeds will reset the ERRORLEVEL to 0.

This behaviour is determined from the top-level batch/script file being
run. This means that, if a .BAT file is first started, then starts a
.CMD file, the commands will still behave the .BAT way; on the opposite,
if a .CMD file is first started, then starts a .BAT file, these commands
will still behave the .CMD way.

To implement this we introduce one global BATCH_TYPE enum variable that
is initialized to the corresponding batch/script file type when the
top-level script is loaded. It is reset to "none" when that script
terminates.

See https://ss64.com/nt/errorlevel.html for more details,
section "Old style .bat Batch files vs .cmd Batch scripts",
and https://groups.google.com/forum/#!msg/microsoft.public.win2000.cmdprompt.admin/XHeUq8oe2wk/LIEViGNmkK0J
(comment by Mark Zbikowski).
This commit is contained in:
Hermès Bélusca-Maïto 2020-07-01 02:15:52 +02:00
parent 0b400bbb98
commit 63316df520
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
6 changed files with 152 additions and 40 deletions

View file

@ -4,6 +4,21 @@
#pragma once
/*
* This batch type enumeration allows us to adjust the behaviour of some commands
* depending on whether they are run from within a .BAT or a .CMD file.
* The behaviour is selected when the top-level batch file is loaded,
* and it remains the same for any child batch file that may be loaded later.
*
* See https://ss64.com/nt/errorlevel.html for more details.
*/
typedef enum _BATCH_TYPE
{
NONE,
BAT_TYPE, /* Old-style DOS batch file */
CMD_TYPE /* New-style NT OS/2 batch file */
} BATCH_TYPE;
typedef struct _BATCH_CONTEXT
{
struct _BATCH_CONTEXT *prev;
@ -34,6 +49,7 @@ typedef struct _FOR_CONTEXT
* The stack of current batch contexts.
* NULL when no batch is active.
*/
extern BATCH_TYPE BatType;
extern PBATCH_CONTEXT bc;
extern PFOR_CONTEXT fc;