the_meter413
the_meter413

Reputation: 299

How do I find where a function is declared in Tcl?

I think this is more of a Tcl configuration question rather than a Tcl coding question...

I inherited a whole series of Tcl scripts that are used within a simulation tool that my company built in-house. In my scripts, I'm finding numerous instances where there are function calls to functions that don't seem to be declared anywhere. How can I trace the path to these phantom functions?

For example, rather than use source, someone build a custom include function that they named INCLUDE. Tclsh obviously balks when I try to run it there, but with my simulation software, it runs fine.

I've tried grep-ing through the entire simulation software for INCLUDE, but I'm not having any luck. Are there any other obvious locations outside the simulation software where a Tcl function might be defined?

Upvotes: 1

Views: 1121

Answers (2)

Brad Lanam
Brad Lanam

Reputation: 5723

The possibilities:

  • Within your software. (you have checked for this).
  • Within some other package included by the software. Check and see if the environment variable TCLLIBPATH is set. Also check and see if the simulation software sets TCLLIBPATH. This will be a list of directories to search for Tcl packages, and you will need to search the packages that are located outside of the main source tree.

    Another possibility is that the locations are specified in the pkgIndex.tcl file. Check any pkgIndex.tcl files and look for locations outside the main source tree.

  • Within an unknown command handler. This could be in your software or within some other package. You should be able to find some code that processes the INCLUDE statement.
  • Within a binary package. These are shared libraries that are loaded by Tcl. If this is the case, there should be some C code used to build the shared library that can be searched.

Since you say there are numerous instances of unknown functions, my first guess is that you have not found all the directories where packages are loaded from. But an ''unknown'' command handler is also a possibility.

Edit:

One more possibility I forgot. Check and see if your software sets the auto_path variable. Check any directories added to the auto_path for other packages.

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137557

This isn't a great answer for you, but I suspect it is the best you're going to get...

The procedure could be defined in a great many places. Your best bet for finding it is to use a tool like findstr (on Windows) or grep -R (on POSIX platforms) to search across all the relevant source files. But that still might not help! It might not be a procedure but instead a general command, which could be implemented in C and not as a procedure, or it could be defined in a packaged application archive (which are usually awkward to look inside). There are also other types of script-implemented command too, which could make things awkward. Generally searching and investigating is your best bet, but it might not work.

Tcl doesn't really differentiate strongly between different types of command except in some introspection operations. If you're lucky, you could find that info body tells you the definition of the procedure (and info args and info default tell you about the arguments) but that won't help with other command types at all. Tcl 8.7 will include a command (info cmdtype) that would help a lot with narrowing down what to do next, but that's no use to you now and it definitely doesn't exist in older versions.

Upvotes: 0

Related Questions