Commit graph

16 commits

Author SHA1 Message Date
Hermès Bélusca-Maïto 37bda06eed
[CMD] CALL: Fix the implementation of the CALL command, make it more compatible with Windows' CMD.
- Fail if no parameter is provided.

- The "CALL :label args..." syntax is available only when command extensions
  are enabled. Fail if this syntax is used outside of a batch context.

- Reparse the CALL command parameter with the command parser, in order
  to accurately parse and interpret it as a possible command (including
  escape carets, etc...) and not duplicate the logic.
  ** CURRENT Windows' CMD-compatibility LIMITATION ** (may be lifted in
  a "ROS-specific" running mode of CMD): only allow standard commands to
  be specified as parameter of the CALL command.

  This reparsing behaviour can be observed in Windows' CMD, by dumping
  the interpreted commands after enabling the cmd!fDumpParse flag from
  a debugger (using public symbols).

- When reparsing, we should tell the parser to NOT ignore lines that
  start with a colon, because in this situation these are to be
  considered as valid "commands" (for parsing "CALL :label").

  * For Windows' CMD-compatibility, the remaining escape carets need to
    be doubled again so that, after the new parser step, they are escaped
    back to their original form. But then we also need to do it the "buggy"
    way à la Windows, where carets in quotes are doubled either! However
    when being re-parsed, since they are in quotes they remain doubled!!
    (see "Phase 6" in https://stackoverflow.com/a/4095133/13530036 ).

  * A MSCMD_CALL_QUIRKS define allows to disable this buggy behaviour,
    and instead tell the parser to not not interpret the escape carets.

- When initializing a new batch context when the "CALL :label" syntax is
  used, ensure that we reuse the same batch file position pointer as its
  parent, so as to have correct call label ordering behaviour.

  That is,

  :label
  ECHO hi
  CALL :label
  :label
  ECHO bye

  should display:

  hi
  bye
  bye

  i.e., the CALL calls the second label instead of the first one (and
  thus entering into an infinite loop).

  Finally, the "CALL :label" syntax strips the first ':' away, so, as a
  side-effect, the command "CALL :EOF" fails (otherwise it would perform
  a "GOTO :EOF" and succeeds), while "CALL ::EOF" succeeds.

Fixes some cmd_winetests.
2020-09-27 19:05:23 +02:00
Hermès Bélusca-Maïto d029a626e9
[CMD] Make the command-line parser more compatible with Windows' CMD one.
All these modifications have been verified with Windows' CMD, either
by using written cmd_rostests and the existing cmd_winetests, or
manually by enabling the flags cmd!fDumpTokens and cmd!fDumpParse
(available in the public symbols) and analyzing how the tokens are
being parsed, as well as the generated command tree.

See also the following links for more details (but remember that these
observations have to be double-checked in Windows' CMD!):

* Parser rules: https://stackoverflow.com/a/4095133/13530036
* Discussion: https://www.dostips.com/forum/viewtopic.php?f=3&t=8355
* Numbers parsing: https://www.dostips.com/forum/viewtopic.php?t=3758
* Label names vs. GOTO and CALL: https://www.dostips.com/forum/viewtopic.php?f=3&t=3803
  and: https://www.dostips.com/forum/viewtopic.php?f=3&t=3803&p=55405#p55405

- Fix REM command parsing. A C_COMMAND-like structure should still
  be built, so that it can show up during batch command echo. However
  some specific handling needs to be done, so use instead a new C_REM
  command type.
  Escape carets are parsed differently than usual: they are explicitly
  kept in the command line and don't participate in line continuations.
  Also, the Windows' CMD behaviour is to discards everything before the
  last line continuation.

- Prefix operator '@' (the "silent" operator) is parsed as a separate
  command. Thus, the command @@foo@bar is parsed as: '@', '@', 'foo@bar'.

- Improve the checks for numbered redirection.
  For this purpose, we check whether this is a number, that is in first
  position in the current parsing buffer or is preceded by a whitespace-
  like separator, including standard command operators (excepting '@' !)
  and double-quotes.

- Empty command blocks, i.e. "( )", standing by themselves, or present
  in IF or FOR commands, are considered invalid. (The closing parenthesis
  is considered "unexpected".)

- Ignore single closing parenthesis when being outside of command blocks,
  thus interpreting it as a command, and ignore explicitly everything
  following on the same line, including line continuations.
  This very specific situation can happen e.g. while running in batch mode,
  when jumping to a label present inside a command block.
  See the code for a thorough explanation.

- Detect whether a parenthesized block is not terminated at the end
  of a command stream (getting a NUL character instead of a newline),
  and if so, bail out early instead of entering into an infinite loop.

- Perform a similar check for the parenthesized list in FOR commands.

- Initialize the static 'InsideBlock' value to a known value.

- The '&' operator (multi-commmand) is allowed to have an empty RHS.
  When such situation occurs, turn the CurrentTokenType to TOK_END
  so as to avoid a parse error later on.

- The main body of a IF statement, or its 'else' clause, as well as
  the main body of a FOR statement, must not be empty, otherwise this
  is considered a syntax error. If so, call ParseError() that sets
  the 'bParseError' flag, and forcing all batch execution to stop.
2020-09-27 02:27:15 +02:00
Hermès Bélusca-Maïto 00ce3c48fe
[CMD] Use pointers to const strings in error functions, where applicable. 2020-09-13 22:50:09 +02:00
Hermès Bélusca-Maïto 41a93a4e58
[CMD] FOR: Some functionality is available only when extensions are enabled.
This is basically all the advanced functionality enabled with the /D,
/R, /L and /F flags, and the usage of enhanced variables.
2020-08-19 20:36:11 +02:00
Hermès Bélusca-Maïto fedc68aea8
[CMD] IF: Some functionality is available only when extensions are enabled.
This functionality is: case insensitivity comparisons (/I);
CMDEXTVERSION and DEFINED unary operators; EQU, NEQ, LSS, LEQ, GTR, GEQ
generic string comparators.
2020-08-19 20:36:11 +02:00
Hermès Bélusca-Maïto 2e4c8c019e
[CMD] GOTO: Fix label parsing.
We note two things, when CMD searches for the corresponding label in the
batch file:
- the first character of the line is always ignored, unless it's a colon;
- the escape caret ^ is supported and interpreted.

Fixes some cmd_winetests.
2020-08-19 20:36:01 +02:00
Hermès Bélusca-Maïto 6eb1cae348
[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-08-19 20:35:58 +02:00
Hermès Bélusca-Maïto c93f511241
[CMD] Reduce stack memory usage when parsing or echoing commands recursively.
- Use a common large string buffer for temporary command substitutions/expansions,
  instead of having such a buffer in the local stack of the parsing and
  echoing functions that may be called recursively. Since CMD executes
  synchronously we know that this common buffer can only be used once at
  a time.

- Also do a small code cleanup in ParseIf(), ParseFor() and ParseCommandPart().
2020-08-19 20:35:57 +02:00
Hermès Bélusca-Maïto 26cfadc352
[CMD] Make the command echoer Windows-CMD-compatible. CORE-14025
Add a MSCMD_ECHO_COMMAND_COMPAT define to be able to switch back to our
older but less broken behaviour at compile-time.

- Append a trailing space to commands when those have a parameter,
  as well as after a command-block closing parenthesis.

- Space around redirection strings need to be switched around.
2020-08-19 20:35:56 +02:00
Hermès Bélusca-Maïto 47ea3f1faa
[CMD] Fix the command echo-er/unparser.
- Use ConOutPuts() instead of ConOutPrintf() for displaying strings that
  are not printf formatted.

- When echo-ing/unparsing FOR command, any possible FOR variables
  present in the FOR parenthesized list (before the "do" part) should be
  substituted as well.
2020-08-19 20:35:56 +02:00
Hermès Bélusca-Maïto 6f87d45e1c
[CMD] Add a command tree dumper, for debugging purposes of the parser code.
This feature is also present in Windows' CMD, and has been documented
e.g. at:
https://www.fireeye.com/blog/threat-research/2018/11/cmd-and-conquer-de-dosfuscation-with-flare-qdb.html
https://www.real-sec.com/2019/08/cmd-and-conquer-de-dosfuscation-with-flare-qdb/
2020-08-19 20:35:55 +02:00
Hermès Bélusca-Maïto 04eef6d20c
[CMD] parser.c, cmd.c/h : Code style and formatting fixes.
- Change C_IFFAILURE, C_IFSUCCESS into C_OR, C_AND respectively.
2020-08-19 20:35:54 +02:00
Hermès Bélusca-Maïto 9c11be5a3a
[CMD] Quick fix for the REM command parser. CORE-17030 2020-05-11 04:27:12 +02:00
Hermès Bélusca-Maïto 3f892a8d6b
[CMD] Add missing memory allocation NULL checks (#161). CORE-8304
Adapted from a patch by Jacob S. Preciado.

Bring also the code suggestions emitted during review.
2018-08-21 14:02:24 +02:00
Hermès Bélusca-Maïto 8340574fe3
[CMD] Fix support for newlines in the emulated parenthesed set block of the "for" command.
CORE-7998
2017-11-17 00:58:45 +01:00
Colin Finck c2c66aff7d Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers into modules, and delete rossubsys. 2017-10-03 07:45:34 +00:00
Renamed from reactos/base/shell/cmd/parser.c (Browse further)