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

issue: redirecting GOSUB output

Discussion in 'Support' started by Stefano Piccardi, Aug 9, 2012.

  1. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    The following batch file shows an issue with "gosub label > file"; TCC prints a command log to the file instead of printing the output of the labeled sub-routine. The first "gosub EMIT1" is sufficient to show the issue, the rest of the script simply tries variations of gosub syntax.
    Although I also tried - to no avail - enclosing gosub within brackets the help file explicitly says that gosub can't be used in a command group. It doesn't say (I couldn'd find) that gosub can't be redirected.
    I tested TCC 14.00.31, TCC 13.04.63, TCCLE 13.04.63 on WinXP SP3 x32 and Win7 SP1 x32. All tested versions are affected. If this indeed turns out to be a bug to be fixed, would you please fix TCCLE as well? Thank you.
    issue.btm:
    Code:
    setlocal
    gosub EMIT1 a1 > issue1.txt
    call :EMIT2 a2 > issue2.txt
    gosub "%_batchname" EMIT1 a3 > issue3.txt
    type issue1.txt
    echo ---
    type issue2.txt
    echo ===
    type issue3.txt
    echo ***
    endlocal
    quit
    :EMIT1 [a]
    rem gosub [batchname] EMIT1 > FILE prints command log to FILE
    rem istead of printing the output of EMIT1
    echo (%a) foo
    return
    :EMIT2 [a]
    rem CMD-compatible subroutine
    rem same issue as gosub EMIT1
    echo (%a) bar
    quit 
    
    output:
    Code:
     
    V:\temp>issue.btm
    setlocal
    gosub EMIT1 a1 > issue1.txt
    call :EMIT2 a2 > issue2.txt
    gosub "V:\temp\issue.btm" EMIT1 a3 > issue3.txt
    type issue1.txt
    rem gosub [batchname] EMIT1 > FILE prints command log to FILE
    rem istead of printing the output of EMIT1
    echo (a1) foo
    (a1) foo
    return
    echo ---
    ---
    type issue2.txt
    rem CMD-compatible subroutine
    rem same issue as gosub EMIT1
    echo () bar
    () bar
    quit
    echo ===
    ===
    type issue3.txt
    rem gosub [batchname] EMIT1 > FILE prints command log to FILE
    rem istead of printing the output of EMIT1
    echo (a3) foo
    (a3) foo
    return
    echo ***
    ***
    endlocal
    quit
    
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    10,036
    Likes Received:
    84
    If I add the missing "@echo off" to the beginning of your batch file, I get:

    (a1) foo
    ---
    () bar
    ===
    (a3) foo
    ***

    which seems to be what you're looking for.
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,075
    Likes Received:
    30
    I have BatchEcho off (OPTION). And I don't get the commands echoed in the files. I also took the TYPEs and ECHOs out of the BTM's main section because they confused me. All looks good except that in the test that uses CALL, the argument "a2" is either not passed or not used. Is that expected? The help shows CALL with a label and parameters.
    Code:
    v:\> type issue.btm
    setlocal
    gosub EMIT1 a1 > issue1.txt
    call :EMIT2 a2 > issue2.txt
    gosub "%_batchname" EMIT1 a3 > issue3.txt
    endlocal
    quit
    :EMIT1 [a]
    echo (%a) foo
    return
    :EMIT2 [a]
    echo (%a) bar
    quit
     
    v:\> issue.btm
     
    v:\> type issue1.txt
    (a1) foo
     
    v:\> type issue2.txt
    () bar                            <===== there's no "a2" here?
     
    v:\> type issue3.txt
    (a3) foo
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    When you use "call" parameters are received by %1, %2, etc. - try the following changed code in the middle:

    :EMIT2
    echo (%1) bar
    quit
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,075
    Likes Received:
    30
    Aha! So it says in the help. Thanks.
     
  6. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    Why missing? Is it mandatory to @echo off a batch file?
    I don't usually need to @echo off a batch file in order to redirect output from DO like in the test script below
    Code:
    @echo on
    (do i in /L a b c (echo %i)) > clip:
    type clip:
    
    output:
    Code:
    M:\>test.btm
    (do i in /L a b c (echo %i)) > clip:
    type clip:
    a
    b
    c
    
    Why is gosub different?
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Is ECHO an alias?
     
  8. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
  9. Frank

    Joined:
    Aug 2, 2011
    Messages:
    258
    Likes Received:
    4
    Hi, by reading this thread I learned that I can use redirection with "call :label" or "gosub"!
    I'm using 4dos for quiet a while, but I never would have that idea by myself :rolleyes:
    Sometimes I can't use my eyes. *
    Thanks.

    * is this a suitable collocation for beeing blind?
     
  10. vefatica

    Joined:
    May 20, 2008
    Messages:
    8,075
    Likes Received:
    30
    They don't even seem related. In the DO example, the line is echoed (because ECHO is ON) and then it's executed; you have not redirected the batch-echoing. In the GOSUB example you have redirected the whatever output the gosub produces (which includes the batch-echoing itself). It seems right to me.
     
  11. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    Aha, this is more clear now. So I can just add @echo off as the first instruction of each redirected subroutine.
     
  12. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    I also have a lot of "WOW! I didn't know that! That's cool!" moments when reading the forum.
     

Share This Page