Если вы можете загрузить пакет TclX (старый, но все еще полезный), тогда вы можете сделать:
package require Tclx; # Lower case at the end for historical reasons
# Your stuff here
commandloop
Это очень похоже на работу собственной интерактивной командной строки Tcl.
Иначе, вот сценарий, который делает большую часть того, что делает интерактивная командная сессия:
if {![info exists tcl_prompt1]} {
set tcl_prompt1 {puts -nonewline "% ";flush stdout}
}
if {![info exists tcl_prompt2]} {
# Note that tclsh actually defaults to not printing anything for this prompt
set tcl_prompt2 {puts -nonewline "> ";flush stdout}
}
set script ""
set prompt $tcl_prompt1
while {![eof stdin]} {
eval $prompt; # Print the prompt by running its script
if {[gets stdin line] >= 0} {
append script $line "\n"; # The newline is important
if {[info complete $script]} { # Magic! Parse for syntactic completeness
if {[catch $script msg]} { # Evaluates the script and catches the result
puts stderr $msg
} elseif {$msg ne ""} { # Don't print empty results
puts stdout $msg
}
# Accumulate the next command
set script ""
set prompt $tcl_prompt1
} else {
# We have a continuation line
set prompt $tcl_prompt2
}
}
}
Правильное получение оставшихся битов (например, взаимодействие с циклом событий при загрузке пакета Tk) потребовало бы немного большей сложности ...