issue: redirecting GOSUB output

May 31, 2008
382
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
 
May 20, 2008
12,178
133
Syracuse, NY, USA
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
 
May 31, 2008
382
2
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.
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?
 
Aug 2, 2011
258
4
Berlin, Germany
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?
 
May 20, 2008
12,178
133
Syracuse, NY, USA
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?
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.
 
Jan 19, 2011
614
15
Norman, OK
Hi, by reading this thread I learned that I can use redirection with "call :label" or "gosub"!
I also have a lot of "WOW! I didn't know that! That's cool!" moments when reading the forum.
 

Similar threads