Some Tcl interpreters (such as those shipped with most EDA tools) support the -echo option
of the source command to print the commands being executed by the sourced script.
To mimic this from a normal Tcl interpret, one may do something like this:
# re-define `source` to accept `-echo`
if {[info commands "source"] eq "source"} {
rename "source" ::tcl::orig::source;
}
proc source args {
# remove `-echo` from `args`
set ::args [lsearch -inline -all -not -exact ${args} "-echo"];
uplevel { ::tcl::orig::source {*}${::args}; }
unset ::args;
};
The concept is in using rename and the removing the -echo option from
provideded arguments. The interesting part is the use of uplevel which makes
sure the variables from the sourced script do eventually propagate to the callee's
context (as is expected with the use of source).
The above is code somewhat ugly/dirty by using defining the args variable in
the global namespace. There would possibly be a better way through upvar.
To add printing out of the commands executed from within the sourced script, one
may add the use of trace as in the StackOverflow solution https://stackoverflow.com/a/65006756