_CMDLINE in TCEXIT (only sometimes)

#1
It seems that sometimes _CMDLINE is set in TCEXIT and sometimes it's not. Here are four tries in a row. In two, the variable was set; in two it was not set.
upload_2016-5-9_22-53-5.png
 
#2
And with this TCEXIT
Code:
v:\> type g:\tc19\tcexit.btm
ECHO %_CMDLINE
PAUSE
if I "X" the console, TCC takes 5 seconds to close, even if I dismiss the PAUSE immediately.

And "@EXIT" seems to always be set during TCEXIT, while "exit" is almost never set during TCEXIT.

And, with TCEXIT as above, if, in a newly opened console, I issue (by typing it)
Code:
echo foo & exit
TCC exits immediately (no TCEXIT).

OTOH, if I issue that command via history recall, TCEXIT is called (and _CMDLINE is usually not set).
 
#3
I was a little off on my last comment. With this TCEXIT
Code:
BEEP 440 2
ECHO %_CMDLINE
PAUSE
If I type (enter at the command line)
Code:
echo foo & exit
I hear the beep and there's no pause; TCC exits immediately.
If, instead, I recall that same command from the history, the pause in TCEXIT happens.

It seems that just mentioning _CMDLINE screws up TCEXIT.
 
#5
Then, with this TCEXIT
Code:
echo %_cmdline
pause
1. Why does is @exit command, when given on the command line, ALWAYS echoed by TCEXIT, while a simple "exit" is never echoed?
2. Why does "echo foo & exit", (typed, not history-recalled), cause the PAUSE not to be executed? The history-recalled version does it too if any changes were made to the recalled line ... adding a trailing space, for example, or simply moving the cursor in the recalled line without making a change.
 
#6
This is pointless (we've had this conversation before) -- %_cmdline shows the content of the current command line, and you don't have a command line when you're in TCEXIT. %_cmdline is for use by key aliases that want to modify the contents of the command line.
If aliases are the only place it can be used reliable (and I'm not sure of that) it should be made clear in the halp file.

What about batch files? Can they reliably use _CMDLINE? It would appear not to be so.
Code:
g:\tc19> type tcexit.btm
echo %_cmdline
pause

g:\tc19> tcexit.btm
tce
Press any key when ready...
 

rconn

Administrator
Staff member
May 14, 2008
10,742
97
#7
Then, with this TCEXIT
Code:
echo %_cmdline
pause
1. Why does is @exit command, when given on the command line, ALWAYS echoed by TCEXIT, while a simple "exit" is never echoed?
2. Why does "echo foo & exit", (typed, not history-recalled), cause the PAUSE not to be executed? The history-recalled version does it too if any changes were made to the recalled line ... adding a trailing space, for example, or simply moving the cursor in the recalled line without making a change.
You're doing a bad, bad, bad thing with %_cmdline. I would expect unpleasant things to happen with your usage, and I don't think it's worth spending the time to trace all the unexpected things you're triggering.

Don't ever use %_cmdline in a batch file. It cannot *possibly* return anything that would ever be of the slightest use.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,703
47
Albuquerque, NM
prospero.unm.edu
#9
Vincent, I'm not sure what you're trying to do. But it seems to me that if you want to hook EXIT, the smart way to do it would be through a plugin EXIT command. You can examine the command line, do anything you want with it -- or to it -- and then return the magic value 0xFEDCBA98 to let TCC do all its cleanup and exit. Or return anything else to prevent TCC from exiting.
 
#10
I think the help does make it clear that it's intended to be used by key aliases. What did you think it would return in a batch file (where you don't have a command line)?
I don't think a parenthetical remark with "most useful in key aliases" makes it clear that that's about the only place it's useful.
_CMDLINE returns the current command line. (This is most useful in key aliases.)
And I never would have dreamed that batch files don't have a command line!
 
#11
Vincent, I'm not sure what you're trying to do. But it seems to me that if you want to hook EXIT, the smart way to do it would be through a plugin EXIT command. You can examine the command line, do anything you want with it -- or to it -- and then return the magic value 0xFEDCBA98 to let TCC do all its cleanup and exit. Or return anything else to prevent TCC from exiting.
In another thread, someone asked if TCEXIT could tell why TCC was exiting. I immediately thought of _CMDLINE naively thinking batch files had command lines. That led me to an investigation that I'll gladly abandon.
 
#14
What you want is %_EXIT, and perhaps %CMDLINE or %CMDLINE2. Definitely not %_cmdline!
Speaking of CMDLINE2, what can you do with it? Any mention of it in a BTM file throws TCC into some sort of infinite loop which you can't Ctrl-C out of.

Try a simple BTM like this.
Code:
v:\> type cl2.btm
if "%CMDLINE2" == "foo" echo foo

v:\> cl2.btm
(it goes forever)
After reading the help, I would have expected (above) "CMDLINE2" to be "cl2.btm" (the original
unexpanded command line before doing any alias expansion, variable expansion, compound command processing, etc.).
 

rconn

Administrator
Staff member
May 14, 2008
10,742
97
#15
Speaking of CMDLINE2, what can you do with it? Any mention of it in a BTM file throws TCC into some sort of infinite loop which you can't Ctrl-C out of.
No, but you certainly shouldn't be doing something like an IF expansion or ECHO with it. Consider what you're doing:

Everything goes fine until IF is expanding the line:

if "%CMDLINE2" == "foo" echo foo

which is first translated to:

if "if "%CMDLINE2" == "foo" echo foo" == "foo" echo foo

Ah, TCC says, there's a nested variable here. Let me expand it to:

if "if "if "%CMDLINE2" == "foo" echo foo" == "foo" echo foo" == "foo" echo foo

And so on, ad infinitum.

Either turn off nested variable expansion, or don't use CMDLINE2 where it's going to be recursively parsed. It's not a good idea to embed a %CMDLINE2 variable inside a %CMDLINE2 variable.