问题
We have a Tcl built in our C/C++ application, I found the place in our code where Tcl_EvalObjv is called if the command was not found. I have to admit that the code is pretty old and not many of our developers know what happens in this module.
It looks like this:
// ... there is some checking if command is registered etc., it fails and the code goes here:
std::vector<Tcl_Obj*> tclArgs = { NULL };
for (int i = 1; i < objc; ++i)
tclArgs.push_back(objv[i]);
tclArgs.shrink_to_fit();
// ...
tclArgs[0] = ::Tcl_NewStringObj(ORIGINAL_UNKNOWN, ORIGINAL_UNKNOWN_SIZE);
Tcl_IncrRefCount(tclArgs[0]);
::Tcl_ExposeCommand(pInterp, ORIGINAL_UNKNOWN, ORIGINAL_UNKNOWN);
result = ::Tcl_EvalObjv(pInterp, objc, &tclArgs[0], TCL_EVAL_GLOBAL); //<--
::Tcl_HideCommand(pInterp, ORIGINAL_UNKNOWN, ORIGINAL_UNKNOWN);
// ORIGINAL_UNKNOWN is char* it is just "unknown"
We have handlers for commands in our application, while executing Tcl_EvalObjv in CmdUnknown() function Tcl sometimes calls different commands. Examples below:
List of existing commands: "banana", "applepie", "carpet", "card"
Command: "apple", Tcl calls "applepie" (wrong, "apple" is not "applepie")
Command: "blah", Tcl gives error (correctly).
Command: "car", Tcl gives error (correctly, maybe because of 2 similar commands).
Is there are some kind of mechanism that Tcl does when it fails in searching for command? The thing is that I can't find anything that is related to our code that would complete the commands so maybe Tcl does?
回答1:
As glenn hinted at, Tcl in interactive (REPL) mode allows for dispatching commands using some minimal but unambiguous name prefix. I cannot tell how your embedded Tcl is configured, initialised, and ended up being run as in interactive mode. However, you may want to try to "turn off" (toggle) the interactive mode, by either:
unset ::tcl_interactive
or
set ::tcl_interactive 0
All of this is implemented by the default unknown
handler. Watch out for how the list of cmds
is looked up and how it is treated differently when tcl_interactive
is true or false:
puts [info body unknown]
来源:https://stackoverflow.com/questions/53302415/embedded-tcl-does-tcl-autocomplete-commands