1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Escape sequences and DO?

Discussion in 'Support' started by vefatica, Mar 24, 2011.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,788
    Likes Received:
    29
    I think these should act the same, but they don't. DO seems to get it wrong.

    Code:
    v:\> for /l %i in (1,1,3) ( echo %i^r^n )
    1
    
    2
    
    3
    
    
    v:\> do i=1 to 3 ( echo %i^r^n )
    1
    2
    3
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    WAD (It has *always* worked this way.)

    The parser expands the DO command line before calling DO. It does not expand the FOR command line, for compatibility reasons. Almost all of the internal commands are expanded before they're called, so if you want to accuse something of being "wrong", blame FOR and CMD.EXE.
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,788
    Likes Received:
    29
    On Thu, 24 Mar 2011 12:41:40 -0400, rconn <> wrote:

    |---Quote (Originally by vefatica)---
    |I think these should act the same, but they don't. DO seems to get it wrong.
    |---End Quote---
    |
    |WAD (It has *always* worked this way.)
    |
    |The parser expands the DO command line before calling DO. It does not expand the FOR command line, for compatibility reasons. Almost all of the internal commands are expanded before they're called, so if you want to accuse something of being "wrong", blame FOR and CMD.EXE.

    I'm not complaining about FOR. I'm complaining about the command line DO (which
    hasn't been around that long). What could be simpler than I want "foo^r^n" to
    be echoed three times? It doesn't do it.

    Code:
    v:\> do i=1 to 3 ( echo foo^r^n )
    foo
    foo
    foo
    Regardless of when it's expanded, what happens to my hard-coded newline? Was it
    seen as terminating the command "echo foo" and discarded? That's totally
    unexpected; I don't think 4NT/TCC users ever expect their intended and
    meaningful escape sequences to be discarded (does it happen anywhere else?). It
    seems especially anomalous considering that the newline is not discarded here:

    Code:
    v:\> do i=1 to 3 ( echo foo^r^nbar )
    foo
    bar
    foo
    bar
    foo
    bar
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    Still WAD, regardless of what you're complaining about or how long it's been around.

    Think for a minute what you're doing -- you've passed a command line to DO that looks like:

    do i=1 to 3 ( echo fooCRLF )

    Do duly sends "echo fooCRLF" back to the parser for another pass, at which time the trailing CR/LF is stripped and "foo" is passed to ECHO. If you want to protect the escape characters on the first pass, double them or use back quoting.

    This will never, ever, change -- it would break every existing batch file that uses DO. (And I'm absolutely not going to create a polymorphic version of DO that behaves differently depending on whether it's a single-line or multiline DO.)
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,788
    Likes Received:
    29
    On Thu, 24 Mar 2011 13:31:42 -0400, rconn <> wrote:

    |---Quote (Originally by vefatica)---
    |I'm complaining about the command line DO (which hasn't been around that long). What could be simpler than I want "foo^r^n" to be echoed three times? It doesn't do it.
    |---End Quote---
    |
    |Still WAD, regardless of what you're complaining about or how long it's been around.
    |
    |Think for a minute what you're doing -- you've passed a command line to DO that looks like:
    |
    |do i=1 to 3 ( echo fooCRLF )
    |
    |Do duly sends "echo fooCRLF" back to the parser for another pass, at which time the trailing CR/LF is stripped and "foo" is passed to ECHO. If you want to protect the escape characters on the first pass, double them or use back quoting.
    |
    |This will never, ever, change -- it would break every existing batch file that uses DO. (And I'm absolutely not going to create a polymorphic version of DO that behaves differently depending on whether it's a single-line or multiline DO.)

    The long version of DO (in batch files) doesn't do that. That's what makes it
    so surprising. I doubt any users (new or long-time) would expect to need a
    gadget here, like "DO I=1 TO 3 ( ECHO foo^^r^^n )".

    Code:
    v:\> type dotest.btm
    do i=1 to 3
            echo foo^r^n
    enddo
    
    v:\> dotest.btm
    foo
    
    foo
    
    foo
    I won't get used to it but when it bites me I should recall what's needed. :-)
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    Not true -- there's only one version of DO, and it ALWAYS does it. But the TCC parser only expands the first line (the DO line), and subsequent lines are passed by DO to the parser, so there's no double expansion in the multi-line variation.
     
  7. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    Then perhaps on the single line version, DO needs to send the command in the parentheses to the parser so that it works the same way as the multi-line version.

    < soapbox >
    Rex,
    Instead of taking comments like these as a personal attack, which puts you on a defensive footing and causes you to lash out at the person with the comment, you should step back and read it objectively.

    You say that
    Code:
    do i=1 to 3 ( echo foo^r^n )
    
    works the same way as
    Code:
    do i=1 to 3
            echo foo^r^n
    enddo
    
    yet they create different output.

    You say
    when it appears that it already exists that way.

    Please remember, we're just trying to help make TC into an even MORE AWESOME version of its already AWESOME self.
    < /soapbox>
     
  8. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    You've got that backwards -- the command parser is sending the expanded DO command line to DO, not DO to the parser. That cannot be changed without writing a custom preparser just for the DO command (and breaking all existing batch files using DO).

    What I said is that the first line (the DO command line) works the same way in both cases. What happens on subsequent lines (inside the DO loop) works differently than how it is handled in a single-line DO -- and as I said, that is unavoidable and unchangeable.
     
  9. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    I don't feel I'm being defensive about this; I think I've simply been definitive (albeit mostly ignored) about how and why it works this way, and why it WILL NOT EVER be changed. OK, I may be getting a bit cranky when the thread goes on and on and on ... :-)
     
  10. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,788
    Likes Received:
    29
    On Thu, 24 Mar 2011 14:02:16 -0400, rconn <> wrote:

    |---Quote (Originally by vefatica)---
    | The long version of DO (in batch files) doesn't do that.
    |---End Quote---
    |
    |Not true -- there's only one version of DO, and it ALWAYS does it. But the TCC parser only expands the first line (the DO line), and subsequent lines are passed by DO to the parser, so there's no double expansion in the multi-line variation.

    Besides ^^r^^n, are there other gadgets that will work around it?

    P.S. I never intend to be offensive. And Rex never seems defensive. I've been
    using JPSoft's prpoducts for 20 years and testing them for about half that time.
    I prefer to assume the naive position (or I am simply naive). I'm confident Rex
    knows that by now and never takes any offense. My only interest has always been
    in what's best for the product.
     
  11. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,730
    Likes Received:
    80
    Single quoting (stripped on the first pass), and using the multiline variation. (The ONLY reason you should ever use the single-line DO is if you want it in an alias.)
     
  12. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    Please add all the quirks of the single-line version to the docs. In particular, add the disclaimer
     
  13. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,788
    Likes Received:
    29
    On Fri, 25 Mar 2011 00:15:12 -0400, rconn <> wrote:

    |---Quote (Originally by vefatica)---
    |Besides ^^r^^n, are there other gadgets that will work around it?
    |---End Quote---
    |
    |Single quoting (stripped on the first pass), and using the multiline variation. (The ONLY reason you should ever use the single-line DO is if you want it in an alias.)

    I don't understand either of those remarks. Single quoting (do you mean with
    ``?) doesn't do it:

    Code:
    v:\> do i=1 to 3 ( echo `foo^r^n` )
    foo
    foo
    foo
    And isn't being at the command line a good reason to use the single line DO? You
    can't use the multi-line version at the command line ... can you?
     
  14. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    From: vefatica
    | And isn't being at the command line a good reason to use the single line DO? You can't use the multi-line version at the command line ... can you?

    That would be my comment, too! DO offers greater flexibility than FOR, the reason for its use at the command line and in aliases.
    --
    Steve
     

Share This Page