carlos-montiers / enhancedbatch

Enhances your windows command prompt https://www.enhancedbatch.com
Other
5 stars 1 forks source link

fix CALL quoted caret doubling design flaw? #38

Closed DaveBenham closed 4 years ago

DaveBenham commented 4 years ago

One of many serious design flaws of cmd.exe is how CALL will always double any quoted carets in the arguments. For example - echo ^^"^" properly yields ^"^", but issuing call echo ^^"^" yields the undesired ^"^^". There is no form of escaping that can avoid the doubling of the quoted carets.

Workarounds are very cumbersome.

batch only

set "c=^"
call echo ^^"%%c%%"

or

set q=^^"
call echo ^^%%q%%^^%%q%%

command line only

set q=^^"
call echo ^^%q^%^^%q^%

Each CALL causes the statement to go through an extra round of Phase 2 parsing, which consumes caret escapes. So some bonehead MS developer attempted to protect carets from the extra round of phase 2 by doubling them just prior to the extra round. But the code naively doubles all carets, instead of just the unquoted ones.

Can you implement a fix for this nasty design flaw? You are already intercepting CALL to implement extensions like call @clear. If your current intercept is after the carets have already been doubled, then I'm thinking it wouldn't be hard to scan the arguments and transform each quoted pair of carets to a single caret, but leave unquoted pairs alone.

Obviously I'm assuming you have access to the CALL arguments, and are in a position to modify them.

If you are able to make this enhancement, then both echo ^^"^" and call echo ^^"^" would yield the same desired result: ^"^"

adoxa commented 4 years ago

Have a look at the fix-call-caret branch. I think it works, but if you could put it through its paces, that'd be great.

DaveBenham commented 4 years ago

Seems to be working great.

A complex case like call call call echo a^^b"^c^d""e^f""^g"^^h""^^i"^j^k gives the correct result of a^b"^c^d""e^f""^g"^h""^i"^j^k

Thanks