MagicEcho

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

MagicEcho

#1 Post by jeb » 12 May 2015 10:46

Inspired from Supress % (percent sign) escaping in a CMD file? I build a magicEcho that can echo any content behind it.

So when you write

Code: Select all

%magicEcho% "^!%path%<>" ^%!%<> ^

You get literally as output
Output wrote:"^!%path%<>" ^%!%<> ^


Obviously this shouldn't be possible :D

So here is the code.
I will explain it later (the explanation will be long), but for now you can find out how it works for yourself :wink:

Code: Select all

@echo off

call :magicEcho.init
%magicEcho% This is impossible %path% ^& | <> "^& | <>" ^
echo Or not?
exit /b

:magicEcho.init
set "magicEcho=call :gn & REM # #"
exit /b

:magicEcho
setlocal EnableDelayedExpansion
set /a skip=ln-2
setlocal DisableDelayedExpansion
FOR /F "usebackq skip=%skip% delims=" %%L in (`findstr /n "^" "%~f0"`) do (
    set "line=%%L"
    setlocal EnableDelayedExpansion
    set "line=!line:*:=!"
    set "line=!line:*echo%% =!"
    if defined line echo(!line!
    endlocal
    exit /b
)
exit /b

:gn
setlocal EnableDelayedExpansion
for %%F in ("%~f0") do set /a size=%%~zF
set "empty=."
for /L %%n in (1 1 13) DO set "empty=!empty:~0,4000!!empty:~0,4000!"

set LF=^


REM ***
set /a blockSizeHalf=4000, blockSize2=blockSizeHalf*2+2
copy "%~f0" "%~f0.tmp" > NUL && (
set /a charCount=0, lo=0, sl=0, preChars=0, lnr=0, cnt=0
for /F "usebackq delims=:" %%O in (`findstr /o "^" "%~f0"`) DO (
    set "offset=%%O"
    set /a lnr+=1
    set /a diff=offset-lo, lo=offset
    set /a charCount+=diff
    if !diff! GEQ 12 (
        set "output=%%#:#=!sl!,!preChars!%%"
        set "s=!output!#"
        set "len=0"
        for %%P in (16 8 4 2 1) do (
            if "!s:~%%P,1!" NEQ "" (
                set /a "len+=%%P"
                set "s=!s:~%%P!"
            )
        )
        set /a fill=charCount-len-1
        for /F %%L in ("!fill!") DO set "output=!output!!empty:~0,%%L!!LF!"               
        set /a cnt+=1
        set "line!cnt!=!output!"               
        set /a charCount=0, sl=lnr, preChars=offset
    )
)
(
    (
        for /L %%n in (1 1 !cnt!) DO (
            <nul set /p ".=!line%%n!"
        )
        (echo()
        echo :"
        echo (
        echo    setlocal EnableDelayedExpansion
        echo    FOR /F "tokens=1,2 delims=," %%%%A in ("^!offset^!"^) DO set /a line=%%%%A,label=%%%%B
        echo    copy "%~f0.tmp" "%~f0" > NUL
        echo    set /a size=%size%
        echo    set L=^^
        (echo()
        (echo()
        echo    for /L %%%%n in (1 1 13^) DO set "L=^!L:~0,%blockSizeHalf%^!^!L:~0,%blockSizeHalf%^!"
        echo    set /a remainSize=label-11, blocks=remainSize/%blockSize2%, remainSize=remainSize-%blockSize2%*blocks-2
        echo    if ^^!remainSize^^! LSS 2 set /a remainSize+=%blockSize2%, blocks-=1
        echo    FOR /F %%%%R in ("^!remainSize^!"^) DO set "lastBlock=^!L:~-%%%%R^!)"
        echo    (
        echo        (echo :#^)
        echo        (echo (%%%%#%%%%^)
        echo        FOR /L %%%%n in (1 1 ^^!blocks^^!^) DO (echo(^^!L^^!^)
        echo        (echo(^^!lastBlock^^!^)
        echo        echo(
        echo    ^) ^> "%~f0"
        echo    set ^^^^^"#=endlocal ^^^^^& (copy "%~f0.tmp" "%~f0" ^^^^^&^^^^^& del "%~f0.tmp"^^^^^) ^^^^^> nul ^^^^^& set "ln=^!line^!" ^^^^^& call :magicEcho"
        echo    goto :#
        echo ^)
    ) > "%~f0"

    endlocal
    set "#=<nul set offset=# & goto :":"
    exit /b
)
)
echo FAIL
exit /b



PS:
The code would be much easier if unlimited nested blocks where possible,
but they fail with a strange side effect Enabling the internal debug outputs of cmd.exe

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: MagicEcho

#2 Post by Squashman » 12 May 2015 12:08

Interesting

Code: Select all

for %%G in (1) DO %magicEcho% This is impossible %path% ^& | <> "^& | <>" ^ %%G

Output

Code: Select all

This is impossible %path% ^& | <> "^& | <>" ^ %%G


But some problems here.

Code: Select all

for %%G in (1 2) DO %magicEcho% This is impossible %path% ^& | <> "^& | <>" ^ %%G

Output

Code: Select all

The system cannot find the batch label specified - gn
This is impossible %path% ^& | <> "^& | <>" ^ %%G

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: MagicEcho

#3 Post by jeb » 12 May 2015 12:26

Squashman wrote:But some problems here.
Code:
for %%G in (1 2) DO %magicEcho% This is impossible %path% ^& | <> "^& | <>" ^ %%G

Ahh, yes, I didn't say it works in every setting :)

It doesn't work in code blocks (and therefore it can't work in FOR loops).

That's an inherent limitation by the technic I used.

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: MagicEcho

#4 Post by Squashman » 12 May 2015 12:38

Regardless of that it is still pretty cool.
Averages about .45 seconds on my old Duo Core machine.

dbenham
Expert
Posts: 2273
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: MagicEcho

#5 Post by dbenham » 12 May 2015 16:24

:? :shock: :?: :!:

I haven't figured out how your code works yet. But couldn't you use a variation of your new technique to solve (or partially solve) How to get the current line number? :idea: :?:


Dave Benham

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: MagicEcho

#6 Post by jeb » 12 May 2015 16:47

dbenham wrote:I haven't figured out how your code works yet. But couldn't you use a variation of your new technique to solve (or partially solve) How to get the current line number? :idea: :?:

You got me :D
And you figured out enough of the code to see it.

Yes, the magicEcho is only an add on to the technic for determin the current line number.

But now it's too late in the night to explain all the stuff.

Post Reply