Code: Select all
:CmdLoop
call :ShowPrompt
set /P "sCmd= "
rem ParseCmd " %sCmd% "
call ExecCmd " %sCmd% "
set "sCmd= "
goto :CmdLoop
ExecCmd is a .cmd file to execute the command, simply execute %*, it is a separate command.
It works with any sort of command, including pipes and redirections. Now I want to 'parse' the command line to change some internal commands of cmd.exe, like CD and add new.
This following code escape all special characters in %sCmd% that has a leading '" ' (dbl quote + space) and a trailing ' "' (space + double quote), Im not sure at 100% but this is necessary to have in-string double quotes and special characters in a variable.
Code: Select all
:ParseCmd
set s=%*
set s=%s:|=^|%
set s=%s:&=^&%
set s=%s:<=^<%
set s=%s:>=^>%
:: Trim dbl quotes
set s=%s:~1,-1%
:: Trim leading spaces
for /f "tokens=* delims= " %%i in ("%s%") do set s=%%i
goto :eof
In :ParseCmd the execution breaks depending on the content of %s%. My debugger is the echo command, so not sure of what happens. I also used the variant with surrounding dbl quote in %s% substitutions, like
Code: Select all
set "s=%s:|=^|%"
Is there a solution for this?