Skip to content

Instantly share code, notes, and snippets.

@brabect1
Created August 13, 2025 10:33
Show Gist options
  • Select an option

  • Save brabect1/90e619c5fb407353b9cfb596f61c587d to your computer and use it in GitHub Desktop.

Select an option

Save brabect1/90e619c5fb407353b9cfb596f61c587d to your computer and use it in GitHub Desktop.
Shows how to override Tcl source command #tcl

Overriding Tcl source command

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment