ZetCode

Tcl auto_reset Command

last modified April 3, 2025

The Tcl auto_reset command controls automatic reset behavior in Tcl. It determines whether unknown commands trigger an automatic reset of the interpreter. This is useful for security and debugging purposes.

Basic Definition

The auto_reset command enables or disables automatic reset when an unknown command is encountered. By default, this feature is enabled in Tcl.

Syntax: auto_reset ?boolean?. With no arguments, it returns the current setting. With a boolean argument, it sets the auto-reset behavior.

Checking Current auto_reset Setting

This example shows how to check the current auto_reset setting in your Tcl interpreter.

check_auto_reset.tcl
set current_setting [auto_reset]
puts "Current auto_reset setting: $current_setting"

This code retrieves the current auto_reset setting and prints it. The default value is typically 1 (enabled) in most Tcl installations.

Disabling auto_reset

This example demonstrates how to disable the auto_reset feature in Tcl.

disable_auto_reset.tcl
auto_reset 0
puts "auto_reset is now disabled"

After running this code, the interpreter won't automatically reset when it encounters unknown commands. This can be useful for debugging scripts.

Enabling auto_reset

This example shows how to enable the auto_reset feature if it was previously disabled.

enable_auto_reset.tcl
auto_reset 1
puts "auto_reset is now enabled"

Enabling auto_reset provides security by preventing execution of unknown commands. The interpreter will reset instead of trying to execute them.

Testing auto_reset Behavior

This example demonstrates the difference in behavior when auto_reset is enabled versus disabled.

test_auto_reset.tcl
# First with auto_reset enabled
auto_reset 1
puts "Testing with auto_reset enabled"
catch {unknown_command} result
puts "Result: $result"

# Then with auto_reset disabled
auto_reset 0
puts "\nTesting with auto_reset disabled"
catch {unknown_command} result
puts "Result: $result"

The script shows how the interpreter handles unknown commands differently based on the auto_reset setting. The catch command prevents errors from stopping execution.

auto_reset in Safe Interpreters

This example shows how auto_reset behaves differently in safe interpreters.

safe_interp_auto_reset.tcl
set safe_interp [interp create -safe]
interp eval $safe_interp {
    puts "Safe interpreter auto_reset: [auto_reset]"
    auto_reset 0
    puts "After attempting to disable: [auto_reset]"
}
interp delete $safe_interp

In safe interpreters, the auto_reset setting is typically forced to be on for security reasons. Attempts to disable it will have no effect.

Combining auto_reset with Unknown Handler

This advanced example shows how auto_reset interacts with a custom unknown command handler.

auto_reset_unknown_handler.tcl
proc unknown {args} {
    puts "Custom unknown handler called with: $args"
    return "Handled unknown command"
}

auto_reset 0
puts [nonexistent_command 1 2 3]

auto_reset 1
catch {puts [nonexistent_command 1 2 3]} result
puts "With auto_reset 1: $result"

When auto_reset is disabled, the custom unknown handler processes unknown commands. When enabled, the interpreter resets instead of using the handler.

Best Practices

This tutorial covered the Tcl auto_reset command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.