2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* BATCH.H - A structure to preserve the context of a batch file
|
|
|
|
*/
|
|
|
|
|
2010-02-26 11:43:19 +00:00
|
|
|
#pragma once
|
2006-02-16 23:23:37 +00:00
|
|
|
|
[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).
2020-07-01 00:15:52 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2020-07-18 21:41:56 +00:00
|
|
|
|
|
|
|
/* Enable this define for Windows' CMD batch-echo behaviour compatibility */
|
|
|
|
#define MSCMD_BATCH_ECHO
|
|
|
|
|
2020-07-26 18:30:03 +00:00
|
|
|
typedef struct _BATCH_CONTEXT
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2020-07-26 18:30:03 +00:00
|
|
|
struct _BATCH_CONTEXT *prev;
|
2013-06-29 23:50:13 +00:00
|
|
|
char *mem; /* batchfile content in memory */
|
|
|
|
DWORD memsize; /* size of batchfile */
|
|
|
|
DWORD mempos; /* current position to read from */
|
|
|
|
BOOL memfree; /* true if it need to be freed when exitbatch is called */
|
|
|
|
TCHAR BatchFilePath[MAX_PATH];
|
|
|
|
LPTSTR params;
|
|
|
|
LPTSTR raw_params; /* Holds the raw params given by the input */
|
|
|
|
INT shiftlevel[10];
|
2020-07-18 21:41:56 +00:00
|
|
|
#ifndef MSCMD_BATCH_ECHO
|
2013-06-29 23:50:13 +00:00
|
|
|
BOOL bEcho; /* Preserve echo flag across batch calls */
|
2020-07-18 21:41:56 +00:00
|
|
|
#endif
|
2013-06-29 23:50:13 +00:00
|
|
|
REDIRECTION *RedirList;
|
2013-06-30 13:23:30 +00:00
|
|
|
PARSED_COMMAND *current;
|
2013-06-29 23:50:13 +00:00
|
|
|
struct _SETLOCAL *setlocal;
|
2020-07-26 18:30:03 +00:00
|
|
|
} BATCH_CONTEXT, *PBATCH_CONTEXT;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2020-07-26 18:30:03 +00:00
|
|
|
typedef struct _FOR_CONTEXT
|
2009-03-02 19:08:25 +00:00
|
|
|
{
|
2020-07-26 18:30:03 +00:00
|
|
|
struct _FOR_CONTEXT *prev;
|
2013-06-29 23:50:13 +00:00
|
|
|
TCHAR firstvar;
|
|
|
|
UINT varcount;
|
|
|
|
LPTSTR *values;
|
2020-07-26 18:30:03 +00:00
|
|
|
} FOR_CONTEXT, *PFOR_CONTEXT;
|
2009-03-02 19:08:25 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2013-06-29 23:50:13 +00:00
|
|
|
/*
|
|
|
|
* The stack of current batch contexts.
|
2020-07-26 18:30:03 +00:00
|
|
|
* NULL when no batch is active.
|
2006-02-16 23:23:37 +00:00
|
|
|
*/
|
[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).
2020-07-01 00:15:52 +00:00
|
|
|
extern BATCH_TYPE BatType;
|
2020-07-26 18:30:03 +00:00
|
|
|
extern PBATCH_CONTEXT bc;
|
|
|
|
extern PFOR_CONTEXT fc;
|
2009-03-02 19:08:25 +00:00
|
|
|
|
2020-07-18 21:41:56 +00:00
|
|
|
#ifdef MSCMD_BATCH_ECHO
|
|
|
|
extern BOOL bBcEcho;
|
|
|
|
#endif
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
extern BOOL bEcho; /* The echo flag */
|
|
|
|
|
2009-01-12 16:13:06 +00:00
|
|
|
#define BATCH_BUFFSIZE 8192
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
extern TCHAR textline[BATCH_BUFFSIZE]; /* Buffer for reading Batch file lines */
|
|
|
|
|
|
|
|
|
2020-07-04 15:40:58 +00:00
|
|
|
BOOL
|
|
|
|
FindArg(
|
|
|
|
IN TCHAR Char,
|
|
|
|
OUT PCTSTR* ArgPtr,
|
|
|
|
OUT BOOL* IsParam0);
|
|
|
|
|
2017-12-03 17:49:41 +00:00
|
|
|
VOID ExitBatch(VOID);
|
[CMD] Fixes for Batch error execution control flow.
CORE-13713 CORE-13736
- In case execution of all batch contexts is stopped (by selecting "All"
at the Ctrl-C/Ctrl-Break prompt), notify as well the CheckCtrlBreak()
signal handler once there are no more batch contexts (this in effect
resets the internal 'bLeaveAll' static flag in CheckCtrlBreak).
This is an adaptation of the fix present in FreeCOM 1.5, first
described in https://gcfl.net/FreeDOS/command.com/bugs074g.html .
- Introduce a ParseErrorEx() helper that sets the 'bParseError' flag and
displays a customized syntax-error message, only for the first syntax
error encountered. Implement ParseError() around the *Ex function.
- In batch mode, echo the original pre-parsed batch file line if a parse
error has been encountered.
- When running a compound command - including IF, FOR, command blocks -,
and that control flow is modified by any CALL/GOTO/EXIT command,
detect this while running the compound command so as to stop it and go
back to the main batch execution loop, that will then set up the actual
new command to run.
- In GOTO, do not process any more parts of a compound command only when
we have found a valid label.
2020-05-18 00:05:53 +00:00
|
|
|
VOID ExitAllBatches(VOID);
|
2017-12-03 17:49:41 +00:00
|
|
|
INT Batch(LPTSTR, LPTSTR, LPTSTR, PARSED_COMMAND *);
|
|
|
|
BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength);
|
2011-03-13 20:56:27 +00:00
|
|
|
LPTSTR ReadBatchLine(VOID);
|
2011-06-01 19:33:20 +00:00
|
|
|
VOID AddBatchRedirection(REDIRECTION **);
|