Commit graph

98 commits

Author SHA1 Message Date
Hermès Bélusca-Maïto fe9aa42d5f
[CMD] SET: Fix displaying the environment variables with a given prefix.
- Restore any truncated space in the name prefix, before displaying
  any error message.

- When trimming the name prefix from "special" characters (spaces, comma
  and semicolon), so that e.g. "set ,; ,;FOO" displays all the variables
  starting by "FOO", save also a pointer to the original name prefix, that
  we will use for variables lookup as well.

  This is done, because the SET command allows setting an environment variable
  whose name actually contains these characters (e.g. "set ,; ,;FOO=42"),
  however, by trimming the characters, doing "set ,; ,;FOO" would not allow
  seeing such variables.
  With the fix, it is now possible to show them.
2020-08-19 21:39:18 +02:00
Hermès Bélusca-Maïto 8cea82b14c
[CMD] REPLACE: Fix a memory leak. 2020-08-19 20:36:13 +02:00
Hermès Bélusca-Maïto 1cb7e08522
[CMD] SETLOCAL / ENDLOCAL: Save / Restore as well the current drive and current directory.
That's an actual fact, done on original MS-DOS COMMAND.COM, FreeCOM,
Windows' CMD.EXE, etc., but is strangely undocumented on MSDN documentation.

See https://www.dostips.com/forum/viewtopic.php?t=4436

Fixes some cmd_winetests.
2020-08-19 20:36:13 +02:00
Hermès Bélusca-Maïto e8e31267c5
[CMD] setlocal.c : Code style and formatting fixes 2020-08-19 20:36:13 +02:00
Hermès Bélusca-Maïto 90159e1e51
[CMD] Implement provisional support for the HIGHESTNUMANODENUMBER environment-like variable.
This variable is available only in Win7+, even if the underlying API
GetNumaHighestNodeNumber() is available in Win2003+
2020-08-19 20:36:12 +02:00
Hermès Bélusca-Maïto 04e0fe0652
[CMD] The "special" environment-like variables are available only when extensions are enabled.
The "special" variables are: CD, DATE, TIME, RANDOM,
CMDCMDLINE, CMDEXTVERSION, ERRORLEVEL (and on Win7+,
HIGHESTNUMANODENUMBER).
2020-08-19 20:36:12 +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 80844dc185
[CMD] DATE: Simplify the input loop, based on the TIME command.
Set also the ERRORLEVEL in case of error.
2020-08-19 20:36:10 +02:00
Hermès Bélusca-Maïto a151893351
[CMD] DATE: The /T option is available only when extensions are enabled. 2020-08-19 20:36:10 +02:00
Hermès Bélusca-Maïto 682875d070
[CMD] TIME: The /T option is available only when extensions are enabled.
And merge two string buffers into one.
2020-08-19 20:36:09 +02:00
Hermès Bélusca-Maïto aab632644f
[CMD] Some code style and formatting fixes 2020-08-19 20:36:09 +02:00
Hermès Bélusca-Maïto 47d7de4b7b
[CMD] Simplify GetRootPath() and mark its InPath parameter pointer to const string. 2020-08-19 20:36:08 +02:00
Hermès Bélusca-Maïto f43ee81573
[CMD] CHDIR: Adjust ERROR_FILE_NOT_FOUND into ERROR_PATH_NOT_FOUND if _tchdir() fails. 2020-08-19 20:36:08 +02:00
Hermès Bélusca-Maïto 9871becced
[CMD] It is not the job of ErrorMessage() to set the errorlevel! It is set only by the commands that want it. 2020-08-19 20:36:08 +02:00
Hermès Bélusca-Maïto 3b960a1c21
[CMD] MOVE: Set the errorlevel on failure.
CORE-14261
2020-08-19 20:36:07 +02:00
Hermès Bélusca-Maïto ae649656db
[CMD] RMDIR: Improve some aspects of the /S option.
- First, the option and the APIs called by it can work directly on
  paths relative to the current directory. So there is no need to
  call GetFullPathName(), with the risk of going over MAX_PATH if the
  current path is quite long (or nested) but the RMDIR is called on a
  (short-length) relative sub-directory.

- Append a path-separator (backslash), only if the specified directory
  does not have one already, and, that it does not specify a current
  directory via the "drive-root" method, e.g. "C:" without any trailing
  backslash.

- In case there are errors during deletion of sub-directories or
  sub-files, print the error but continue deleting the other sub-dirs
  or files.

- Monitor the Ctrl-C breaker as well, and stop deleting if it has been
  triggered.

- When removing file/directory read-only attribute, just remove this
  attribute, but keep the other ones.

- When deleting the directory, first try to do it directly; if it fails
  with access denied, check whether it was read-only, and if so, remove
  this attribute and retry deletion, otherwise fails.

- When recursively deleting a drive root directory, ultimately resolve
  the dir pattern and check whether it's indeed a drive root, e.g.
  "C:\\", and if so, just return success. Indeed, calling
  RemoveDirectory() on such drive roots will return ERROR_ACCESS_DENIED
  otherwise, but we want to succeed even if, of course, we won't
  actually "delete" the drive root.
2020-08-19 20:36:07 +02:00
Hermès Bélusca-Maïto 2f9b4a2e9f
[CMD] RMDIR: Force directory deletion even if it's read-only, only when recursing over subdirectories (via the /S option). 2020-08-19 20:36:06 +02:00
Hermès Bélusca-Maïto 7c175d4999
[CMD] PATH, SET: Fix the returned error message when an environment variable does not exist.
Translators, please update the translations!
2020-08-19 20:36:06 +02:00
Hermès Bélusca-Maïto 6e09a6a3ff
[CMD] Use kernel32!lstrcmp(i) when comparing strings with the IF command.
Use kernel32!lstrcmp(i) instead of CRT!_tcs(i)cmp, so as to use the correct
current thread locale information when comparing user-specific strings.
As a result, the following comparison: 'b LSS B' will return TRUE,
instead of FALSE as it would be by using the CRT functions (and by
naively considering the lexicographical order in ANSI).
This behaviour has been introduced in Windows 2000 onwards.
2020-08-19 20:36:05 +02:00
Hermès Bélusca-Maïto 5830ccb85e
[CMD] Improve the way the ErrorMessage() helper and the MKDIR and RMDIR commands report their errors.
For MKDIR, also properly support the case of ERROR_FILE_EXISTS and
ERROR_ALREADY_EXISTS last-errors by displaying the standard error
"A subdirectory or file XXX already exists.\n"
2020-08-19 20:36:05 +02:00
Hermès Bélusca-Maïto 1efbcd3d5d
[CMD] Improve RMDIR help and MD error message. 2020-08-19 20:36:04 +02:00
Hermès Bélusca-Maïto e3ed502bb6
[CMD] Fix the output of the SET command. 2020-08-19 20:36:04 +02:00
Hermès Bélusca-Maïto ca4523658c
[CMD] Some code style and formatting fixes 2020-08-19 20:36:04 +02:00
Hermès Bélusca-Maïto 82bcb3f9f0
[CMD] Fix the implementation of EXIT /B when a batch context is active. 2020-08-19 20:36:03 +02:00
Hermès Bélusca-Maïto d78e8029b8
[CMD] Additional fixes for ERRORLEVEL and last returned exit code from EXIT, CALL commands and CMD.
CORE-10495 CORE-13672

- Fix how the ERRORLEVEL and the last returned exit code are set by
  EXIT and CALL commands, when batch contexts terminate, and when CMD
  runs in single-command mode (with /C).

  Addendum to commit 26ff2c8e, and reverts commit 7bd33ac4.
  See also commit 8cf11060 (r40474).

  More information can be found at:
  https://ss64.com/nt/exit.html
  https://stackoverflow.com/a/34987886/13530036
  https://stackoverflow.com/a/34937706/13530036

- Move the actual execution of the CMD command-line (in /C or /K
  single-command mode) from Initialize() to _tmain(), to put it on par
  with the ProcessInput() interactive mode.

- Make ProcessInput() also return the last command's exit code.
2020-08-19 20:36:03 +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 c5e6e5a19c
[CMD] GOTO: Fix handling of the ':EOF' label handling.
- The ':EOF' label feature is available only when extensions are enabled.

- Anything that follows the ':EOF' label, separated by at least one
  whitespace character, is ignored, and the batch file terminates.
2020-08-19 20:36:01 +02:00
Hermès Bélusca-Maïto f911bb482d
[CMD] GOTO: The command should search labels from its position down to the end, then loop back to the beginning of the batch and down to the original position. 2020-08-19 20:36:00 +02:00
Hermès Bélusca-Maïto 495c82ccde
[CMD] Syntax errors during parsing of batch parameters expansion, or FOR and IF commands, are fatal, and batch execution should stop.
- To this purpose use the ParseErrorEx() that correctly sets the
  bParseError flag, and return the partially-parsed command so that
  it gets echoed as well for diagnostics purposes (Windows-compatible).

- Any other parameters specified after (or before) the '/?' switch for
  the FOR and IF commands, are considered fatal syntax errors as well,
  thus we employ the ParseErrorEx() as well.
2020-08-19 20:35:59 +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 ca912d7b36
[CMD] Further code style and formatting fixes. 2020-08-19 20:35:57 +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 93ee4e7efa
[CMD] Fix some messages formatting.
- Fix ECHO state output string.
- Add missing newlines to some help and error messages.
- Add trailing space after the PAUSE command message.
2020-08-19 20:35:54 +02:00
Hermès Bélusca-Maïto ea801338f6
[CMD] todo.txt: Remove some points that have already been implemented since ages. 2020-08-19 20:35:52 +02:00
Hermès Bélusca-Maïto 86c5e7e41f
[CMD] Remove deprecated definition in CMakeLists.txt. 2020-08-19 20:35:52 +02:00
Hermès Bélusca-Maïto 240f6737e9
[CMD] Add a ExecuteCommandWithEcho() helper and use it in Batch() and as the implementation of RunInstance() FOR-loop helper. 2020-05-18 02:21:57 +02:00
Hermès Bélusca-Maïto ce543fbb72
[CMD] Fix the stop-batch prompt appearance: do not display it more than once in a row. 2020-05-18 02:21:57 +02:00
Hermès Bélusca-Maïto d0ced4ffdf
[CMD] Fix the ErrorMessage() function.
Specify its szFormat parameter optional, and correctly initialize the
szMessage buffer.
2020-05-18 02:21:56 +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
Piotr Hetnarowicz cd5364ae8e [cmd] pl-pl update 2020-04-16 18:32:08 +02:00
Thomas Faber 111721a64a
[CMD] Fix another NtReadVirtualMemory parameter.
Spotted by Hermès.
2020-03-14 21:23:06 +01:00
Thomas Faber 72c51aabba
[NTOS:IO][GDI32][CMD] Fix some under-sized variables. CORE-14922
Fixes x64 boot with RTC.
2020-03-13 20:45:06 +01:00
Serge Gautherie 92aabb07fb
[REACTOS] Fix remaining ' \n' typos, in *.rc (#2393)
Addendum to commit 554c41e4 (#2387).
Co-authored-by: Hermès BÉLUSCA - MAÏTO <hermes.belusca-maito@reactos.org>
2020-03-06 19:08:25 +01:00
Katayama Hirofumi MZ 4e263367f8
[CMD] Fix 'if' command for root directories (#2394)
CORE-14797
2020-02-27 23:12:19 +09:00
Katayama Hirofumi MZ a9760098f7
[CMD] Correctly parse drive-decorated pathname (#2382)
Correctly interpret the drive-decorated pathnames (C: or D:dir1 etc). CORE-15871
2020-02-26 22:28:02 +09:00