where was bat file called from?

Apr 1, 2013
22
0
#1
4nt8 or ttc le 13

I have a bat file called from many other bat files..

Is there a way to determine, from within the bat file the name of the bat file from which it was called?

Or alternatively when one quits the bat file can one find out where it's returning to?
Guess there's got to be some sort of stack inside 4nt or tcc where this is kept?

PS There's %~dpnx0 that I didn't know about that tells one the name of the bat file, which I just discovered from googling my problem.

john
 
Apr 1, 2013
22
0
#3
%_BATCHNAME gives the full name of the current batch file. But I don't think there is any way to read the CALL stack.
Thanks - pity, because it'd have been relatively easy I guess when implementing to put it as one of the %x things! Not sure where to look Suppose this data is in memory, not a file, so can't try eg using grep to hack/find it?

Any ideas welcome...

john
 
Apr 1, 2013
22
0
#5
Trouble is there are lots,100? , of places where it's called from. Maybe I could alias call to `set caller=%_batchname^call` - this would be overhead on all calls -not sure whether that'd matter, but suppose could just alias call my_prog=`set caller=%_batchname^call my_prog`. Not sure whether this'd work...

Still be better if there was a variable called _caller or similar. How about this for next version of tcc.
john
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,586
46
Albuquerque, NM
prospero.unm.edu
#6
I'm trying to figure out how to implement this as a plugin. You could intercept CALL easily enough, and push the current batch name onto a stack. But how to know when to pop it off again? Intercept QUIT and CANCEL too -- but TCC can also return from a CALLed batch simply by running off the end of the file. How to detect that? And then there's labeled subroutines, which can be started by either CALL or GOSUB, and ended by QUIT, CANCEL, or EXIT, or the end of the file. Second-guessing the CALL stack would be a Lovecraftian monstrosity.
 
Apr 1, 2013
22
0
#7
I don't need a 100% fix so maybe plugin is ott for my problem- I only use call filename and gosubs inside file. The nice thing about aliasing call is that it doesn''t matter about the exits.

Tried alias call=`set caller=%_batchname^call` - seem to get loop when I try it (example below)- know why? Maybe it is because I have lots of other stuff running? Anyway to fix?

tmp.bat =
@echo off
call l
quit

l.bat=
@echo off
echo --%_batchname called by:%caller
...
quit

john
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,586
46
Albuquerque, NM
prospero.unm.edu
#8
Your alias uses CALL. Which is an alias, which uses CALL. Which is an alias, which uses CALL, which is an alias....
You can prevent the problem like this:
Code:
alias call='set caller=%_batchname %+ *call'
The asterisk disables alias expansion for that one command.
 
Apr 1, 2013
22
0
#9
Thanks that fixes it. I'm thinking of using this just as debug aid so not permanently. I run some scripts in background using 'start' so if I set it in my startup scripts, assume it'll effect all calls so suppose this'd be some sort of performance impact invoking the alias code for every call. Guess time will tell whether this'll matter. Any ideas welcome.

I do use gosub "filename" label too in few places so suppose I could use same idea with gosub ie
alias gosub=`set caller=%_batchname^*gosub` - would this work too? Suppose overhead might be greater tho' since there are an awful lot of gosubs, cf calls .

You mentioned other ways of entering a subroutine? Could I extend alias idea to these

Thanks for your help

john