##
## tkcon_proc.tcl
## Enhanced Tk Console, part of the VerTcl system
##
## Originally based off Brent Welch's Tcl Shell Widget
## (from "Practical Programming in Tcl and Tk")
##
## Thanks to the following (among many) for early bug reports & code ideas:
## Steven Wahl <steven@indra.com>, Jan Nijtmans <nijtmans@nici.kun.nl>
## Crimmins <markcrim@umich.edu>, Wart <wart@ugcs.caltech.edu>
##
## Copyright 1995-1999 Jeffrey Hobbs
## Initiated: Thu Aug 17 15:36:47 PDT 1995
##
## jeff.hobbs@acm.org
##
## source standard_disclaimer.tcl
## source bourbon_ware.tcl
##
package require StdLib
package require Interface
PkgProvide TkConProc
# Initialize the ::tkcon namespace
#
namespace eval ::tkcon {
set PRIV(WWW) [info exists embed_args]
}
##
## Some procedures to make up for lack of built-in shell commands
##
proc help {} {
puts "
\[alias\]
akin to the csh alias command
If called with no args, then it dumps out all current aliases
If called with one arg, returns the alias of that arg (or {} if none)
ARGS: newcmd - (optional) command to bind alias to
args - command and args being aliased
\[cct\]
compiler interface, type: \"cct -h\" for additional help
\[clear\]
clear - clears the buffer of the console (not the history though)
\[dir\]
directory list
ARGS: args - names/glob patterns of directories to list
OPTS: -all - list hidden files as well (Unix dot files)
-long - list in full format \"permissions size date filename\"
-full - displays / after directories and link paths for links
RET: a directory listing
\[dump\]
outputs variables/procedure/widget info in source'able form.
Accepts glob style pattern matching for the names
ARGS: type - type of thing to dump: must be variable, procedure, widget
OPTS: -nocomplain
don't complain if no vars match something
-filter pattern
specifies a glob filter pattern to be used by the variable
method as an array filter pattern (it filters down for
nested elements) and in the widget method as a config
option filter pattern
--
forcibly ends options recognition
RET: the values of the requested items in a 'source'able form
\[echo\]
relaxes the one string restriction of 'puts'
ARGS: any number of strings to output to stdout
\[help\]
print this message
\[idebug\]
interactive debugger
ARGS: opt
args - additional arguments
\[lremove\]
remove items from a list
OPTS: -all remove all instances of each item
-glob remove all instances matching glob pattern
-regexp remove all instances matching regexp pattern
ARGS: l a list to remove items from
args items to remove (these are 'join'ed together)
\[observe\]
like trace, but not
ARGS: opt - option
name - name of variable or command
args - additional arguments
\[observe_var\]
auxilary function for observing vars, called by trace
via observe
ARGS: name - variable name
el - array element name, if any
op - operation type (rwu)
\[unalias\]
unaliases an alias'ed command
ARGS: cmd - command to unbind as an alias
\[what\]
tells you what a string is recognized as
ARGS: str - string to id
RET: id types of command as list
\[which\]
tells you where a command is found
ARGS: cmd - command name
RET: where command is found (internal / external / unknown)
"
puts "\n \[predefined aliases\]\n"
set List {}
set Max 0
foreach {A P T} [ alias ] {
lappend List [ list $A $T ]
set L [ string length $A ]
if { $L > $Max } { set Max $L }
}
foreach {V} [ lsort -index 0 $List ] {
foreach {A T} $V break
puts [ format { %*s -> %s} $Max $A $T ]
}
puts "\n \[environment\]\n"
set List {}
set Max 0
foreach {A T} [ array get ::env TK_CON* ] {
lappend List [ list $A $T ]
set L [ string length $A ]
if { $L > $Max } { set Max $L }
}
foreach {V} [ lsort -index 0 $List ] {
foreach {A T} $V break
puts [ format { %-*s -> %s} $Max $A $T ]
}
}
proc cctputs { FH } {
if {[ eof $FH ]} {
puts "finish background process"
catch { close $FH }
return
}
puts [gets $FH]
}
proc cct {args} {
::StdLib::Push ERROR PUTS
set FH [ ::ITclSh::Open [ concat package Cct $args ] ]
::StdLib::Pop ERROR
fileevent $FH readable [ list cctputs $FH ]
}
## tkcon_puts -
## This allows me to capture all stdout/stderr to the console window
## This will be renamed to 'puts' at the appropriate time during init
##
# ARGS: same as usual
# Outputs: the string with a color-coded text tag
##
proc tkcon_puts args {
set len [llength $args]
foreach {arg1 arg2 arg3} $args { break }
if {$len == 1} {
tkcon console insert output "$arg1\n" stdout
} elseif {$len == 2} {
if {![string compare $arg1 -nonewline]} {
tkcon console insert output $arg2 stdout
} elseif {![string compare $arg1 stdout] \
|| ![string compare $arg1 stderr]} {
tkcon console insert output "$arg2\n" $arg1
} else {
set len 0
}
} elseif {$len == 3} {
if {![string compare $arg1 -nonewline] \
&& (![string compare $arg2 stdout] \
|| ![string compare $arg2 stderr])} {
tkcon console insert output $arg3 $arg2
} elseif {(![string compare $arg1 stdout] \
|| ![string compare $arg1 stderr]) \
&& ![string compare $arg3 nonewline]} {
tkcon console insert output $arg2 $arg1
} else {
set len 0
}
} else {
set len 0
}
## $len == 0 means it wasn't handled by tkcon above.
##
if {$len == 0} {
global errorCode errorInfo
if {[catch "tkcon_tcl_puts $args" msg]} {
regsub tkcon_tcl_puts $msg puts msg
regsub -all tkcon_tcl_puts $errorInfo puts errorInfo
return -code error $msg
}
return $msg
}
## WARNING: This update should behave well because it uses idletasks,
## however, if there are weird looping problems with events, or
## hanging in waits, try commenting this out.
if {$len} {
tkcon console see output
update idletasks
}
}
## tkcon_gets -
## This allows me to capture all stdin input without needing to stdin
## This will be renamed to 'gets' at the appropriate time during init
##
# ARGS: same as gets
# Outputs: same as gets
##
proc tkcon_gets args {
set len [llength $args]
if {$len != 1 && $len != 2} {
return -code error \
"wrong # args: should be \"gets channelId ?varName?\""
}
if {[string compare stdin [lindex $args 0]]} {
return [uplevel 1 tkcon_tcl_gets $args]
}
set gtype [tkcon set ::tkcon::OPT(gets)]
if {$gtype == ""} { set gtype congets }
set data [tkcon $gtype]
if {$len == 2} {
upvar 1 [lindex $args 1] var
set var $data
return [string length $data]
}
return $data
}
## echo
## Relaxes the one string restriction of 'puts'
# ARGS: any number of strings to output to stdout
##
proc echo args { puts [concat $args] }
## clear - clears the buffer of the console (not the history though)
## This is executed in the parent interpreter
##
proc clear {{pcnt 100}} {
if {![regexp {^[0-9]*$} $pcnt] || $pcnt < 1 || $pcnt > 100} {
return -code error \
"invalid percentage to clear: must be 1-100 (100 default)"
} elseif {$pcnt == 100} {
tkcon console delete 1.0 end
} else {
set tmp [expr {$pcnt/100.0*[tkcon console index end]}]
tkcon console delete 1.0 "$tmp linestart"
}
}
## alias - akin to the csh alias command
## If called with no args, then it dumps out all current aliases
## If called with one arg, returns the alias of that arg (or {} if none)
# ARGS: newcmd - (optional) command to bind alias to
# args - command and args being aliased
##
proc alias {{newcmd {}} args} {
if {[string match {} $newcmd]} {
set res {}
foreach a [interp aliases] {
lappend res [list $a -> [interp alias {} $a]]
}
return [join $res \n]
} elseif {![llength $args]} {
interp alias {} $newcmd
} else {
eval interp alias [list {} $newcmd {}] $args
}
}
## unalias - unaliases an alias'ed command
# ARGS: cmd - command to unbind as an alias
##
proc unalias {cmd} {
interp alias {} $cmd {}
}
## dump - outputs variables/procedure/widget info in source'able form.
## Accepts glob style pattern matching for the names
# ARGS: type - type of thing to dump: must be variable, procedure, widget
# OPTS: -nocomplain
# don't complain if no vars match something
# -filter pattern
# specifies a glob filter pattern to be used by the variable
# method as an array filter pattern (it filters down for
# nested elements) and in the widget method as a config
# option filter pattern
# -- forcibly ends options recognition
# Returns: the values of the requested items in a 'source'able form
##
proc dump {type args} {
set whine 1
set code ok
if {![llength $args]} {
## If no args, assume they gave us something to dump and
## we'll try anything
set args $type
set type any
}
while {[string match -* [lindex $args 0]]} {
switch -glob -- [lindex $args 0] {
-n* { set whine 0; set args [lreplace $args 0 0] }
-f* { set fltr [lindex $args 1]; set args [lreplace $args 0 1] }
-- { set args [lreplace $args 0 0]; break }
default {return -code error "unknown option \"[lindex $args 0]\""}
}
}
if {$whine && ![llength $args]} {
return -code error "wrong \# args: [lindex [info level 0] 0] type\
?-nocomplain? ?-filter pattern? ?--? pattern ?pattern ...?"
}
set res {}
switch -glob -- $type {
c* {
# command
# outputs commands by figuring out, as well as possible, what it is
# this does not attempt to auto-load anything
foreach arg $args {
if {[llength [set cmds [info commands $arg]]]} {
foreach cmd [lsort $cmds] {
if {[lsearch -exact [interp aliases] $cmd] > -1} {
append res "\#\# ALIAS: $cmd =>\
[interp alias {} $cmd]\n"
} elseif {
[llength [info procs $cmd]] ||
([string match *::* $cmd] &&
[llength [namespace eval [namespace qual $cmd] \
info procs [namespace tail $cmd]]])
} {
if {[catch {dump p -- $cmd} msg] && $whine} {
set code error
}
append res $msg\n
} else {
append res "\#\# COMMAND: $cmd\n"
}
}
} elseif {$whine} {
append res "\#\# No known command $arg\n"
set code error
}
}
}
v* {
# variable
# outputs variables value(s), whether array or simple.
if {![info exists fltr]} { set fltr * }
foreach arg $args {
if {![llength [set vars [uplevel 1 info vars [list $arg]]]]} {
if {[uplevel 1 info exists $arg]} {
set vars $arg
} elseif {$whine} {
append res "\#\# No known variable $arg\n"
set code error
continue
} else { continue }
}
foreach var [lsort $vars] {
if {[uplevel 1 [list info locals $var]] == ""} {
# use the proper scope of the var, but
# namespace which won't id locals correctly
set var [uplevel 1 \
[list namespace which -variable $var]]
}
upvar 1 $var v
if {[array exists v] || [catch {string length $v}]} {
set nst {}
append res "array set [list $var] \{\n"
if {[array size v]} {
foreach i [lsort [array names v $fltr]] {
upvar 0 v\($i\) __a
if {[array exists __a]} {
append nst "\#\# NESTED ARRAY ELEM: $i\n"
append nst "upvar 0 [list $var\($i\)] __a;\
[dump v -filter $fltr __a]\n"
} else {
append res " [list $i]\t[list $v($i)]\n"
}
}
} else {
## empty array
append res " empty array\n"
append nst "unset [list $var](empty)\n"
}
append res "\}\n$nst"
} else {
append res [list set $var $v]\n
}
}
}
}
p* {
# procedure
foreach arg $args {
if {
![llength [set procs [info proc $arg]]] &&
([string match *::* $arg] &&
[llength [set ps [namespace eval \
[namespace qualifier $arg] \
info procs [namespace tail $arg]]]])
} {
set procs {}
set namesp [namespace qualifier $arg]
foreach p $ps {
lappend procs ${namesp}::$p
}
}
if {[llength $procs]} {
foreach p [lsort $procs] {
set as {}
foreach a [info args $p] {
if {[info default $p $a tmp]} {
lappend as [list $a $tmp]
} else {
lappend as $a
}
}
append res [list proc $p $as [info body $p]]\n
}
} elseif {$whine} {
append res "\#\# No known proc $arg\n"
set code error
}
}
}
w* {
# widget
## The user should have Tk loaded
if {![llength [info command winfo]]} {
return -code error "winfo not present, cannot dump widgets"
}
if {![info exists fltr]} { set fltr .* }
foreach arg $args {
if {[llength [set ws [info command $arg]]]} {
foreach w [lsort $ws] {
if {[winfo exists $w]} {
if {[catch {$w configure} cfg]} {
append res "\#\# Widget $w\
does not support configure method"
set code error
} else {
append res "\#\# [winfo class $w]\
$w\n$w configure"
foreach c $cfg {
if {[llength $c] != 5} continue
## Check to see that the option does
## not match the default, then check
## the item against the user filter
if {[string compare [lindex $c 3] \
[lindex $c 4]] && \
[regexp -nocase -- $fltr $c]} {
append res " \\\n\t[list [lindex $c 0]\
[lindex $c 4]]"
}
}
append res \n
}
}
}
} elseif {$whine} {
append res "\#\# No known widget $arg\n"
set code error
}
}
}
a* {
## see if we recognize it, other complain
if {[regexp {(var|com|proc|widget)} \
[set types [uplevel 1 what $args]]]} {
foreach type $types {
if {[regexp {(var|com|proc|widget)} $type]} {
append res "[uplevel 1 dump $type $args]\n"
}
}
} else {
set res "dump was unable to resolve type for \"$args\""
set code error
}
}
default {
return -code error "bad [lindex [info level 0] 0] option\
\"$type\": must be variable, command, procedure,\
or widget"
}
}
return -code $code [string trimright $res \n]
}
## idebug - interactive debugger
# ARGS: opt
#
##
proc idebug {opt args} {
global IDEBUG
if {![info exists IDEBUG(on)]} {
array set IDEBUG { on 0 id * debugging 0 }
}
set level [expr {[info level]-1}]
switch -glob -- $opt {
on {
if {[llength $args]} { set IDEBUG(id) $args }
return [set IDEBUG(on) 1]
}
off { return [set IDEBUG(on) 0] }
id {
if {![llength $args]} {
return $IDEBUG(id)
} else { return [set IDEBUG(id) $args] }
}
break {
if {!$IDEBUG(on) || $IDEBUG(debugging) || \
([llength $args] && \
![string match $IDEBUG(id) $args]) || [info level]<1} {
return
}
set IDEBUG(debugging) 1
puts stderr "idebug at level \#$level: [lindex [info level -1] 0]"
set tkcon [llength [info command tkcon]]
if {$tkcon} {
tkcon show
tkcon master eval set ::tkcon::OPT(prompt2) \$::tkcon::OPT(prompt1)
tkcon master eval set ::tkcon::OPT(prompt1) \$::tkcon::OPT(debugPrompt)
set slave [tkcon set ::tkcon::OPT(exec)]
set event [tkcon set ::tkcon::PRIV(event)]
tkcon set ::tkcon::OPT(exec) [tkcon master interp create debugger]
tkcon set ::tkcon::PRIV(event) 1
}
set max $level
while 1 {
set err {}
if {$tkcon} {
# tkcon's overload of gets is advanced enough to not need
# this, but we get a little better control this way.
tkcon evalSlave set level $level
tkcon prompt
set line [tkcon getcommand]
tkcon console mark set output end
} else {
puts -nonewline stderr "(level \#$level) debug > "
gets stdin line
while {![info complete $line]} {
puts -nonewline "> "
append line "\n[gets stdin]"
}
}
if {[string match {} $line]} continue
set key [lindex $line 0]
if {![regexp {^([#-]?[0-9]+)} [lreplace $line 0 0] lvl]} {
set lvl \#$level
}
set res {}; set c 0
switch -- $key {
+ {
## Allow for jumping multiple levels
if {$level < $max} {
idebug trace [incr level] $level 0 VERBOSE
}
}
- {
## Allow for jumping multiple levels
if {$level > 1} {
idebug trace [incr level -1] $level 0 VERBOSE
}
}
. { set c [catch {idebug trace $level $level 0 VERBOSE} res] }
v { set c [catch {idebug show vars $lvl } res] }
V { set c [catch {idebug show vars $lvl VERBOSE} res] }
l { set c [catch {idebug show locals $lvl } res] }
L { set c [catch {idebug show locals $lvl VERBOSE} res] }
g { set c [catch {idebug show globals $lvl } res] }
G { set c [catch {idebug show globals $lvl VERBOSE} res] }
t { set c [catch {idebug trace 1 $max $level } res] }
T { set c [catch {idebug trace 1 $max $level VERBOSE} res]}
b { set c [catch {idebug body $lvl} res] }
o { set res [set IDEBUG(on) [expr {!$IDEBUG(on)}]] }
h - ? {
puts stderr " + Move down in call stack
- Move up in call stack
. Show current proc name and params
v Show names of variables currently in scope
V Show names of variables currently in scope with values
l Show names of local (transient) variables
L Show names of local (transient) variables with values
g Show names of declared global variables
G Show names of declared global variables with values
t Show a stack trace
T Show a verbose stack trace
b Show body of current proc
o Toggle on/off any further debugging
c,q Continue regular execution (Quit debugger)
h,? Print this help
default Evaluate line at current level (\#$level)"
}
c - q break
default { set c [catch {uplevel \#$level $line} res] }
}
if {$tkcon} {
tkcon set ::tkcon::PRIV(event) \
[tkcon evalSlave eval history add [list $line]\
\; history nextid]
}
if {$c} {
puts stderr $res
} elseif {[string compare {} $res]} {
puts $res
}
}
set IDEBUG(debugging) 0
if {$tkcon} {
tkcon master interp delete debugger
tkcon master eval set ::tkcon::OPT(prompt1) \$::tkcon::OPT(prompt2)
tkcon set ::tkcon::OPT(exec) $slave
tkcon set ::tkcon::PRIV(event) $event
tkcon prompt
}
}
bo* {
if {[regexp {^([#-]?[0-9]+)} $args level]} {
return [uplevel $level {dump c -no [lindex [info level 0] 0]}]
}
}
t* {
if {[llength $args]<2} return
set min [set max [set lvl $level]]
set exp {^#?([0-9]+)? ?#?([0-9]+) ?#?([0-9]+)? ?(VERBOSE)?}
if {![regexp $exp $args junk min max lvl verbose]} return
for {set i $max} {
$i>=$min && ![catch {uplevel \#$i info level 0} info]
} {incr i -1} {
if {$i==$lvl} {
puts -nonewline stderr "* \#$i:\t"
} else {
puts -nonewline stderr " \#$i:\t"
}
set name [lindex $info 0]
if {[string compare VERBOSE $verbose] || \
![llength [info procs $name]]} {
puts $info
} else {
puts "proc $name {[info args $name]} { ... }"
set idx 0
foreach arg [info args $name] {
if {[string match args $arg]} {
puts "\t$arg = [lrange $info [incr idx] end]"
break
} else {
puts "\t$arg = [lindex $info [incr idx]]"
}
}
}
}
}
s* {
#var, local, global
set level \#$level
if {![regexp {^([vgl][^ ]*) ?([#-]?[0-9]+)? ?(VERBOSE)?} \
$args junk type level verbose]} return
switch -glob -- $type {
v* { set vars [uplevel $level {lsort [info vars]}] }
l* { set vars [uplevel $level {lsort [info locals]}] }
g* { set vars [lremove [uplevel $level {info vars}] \
[uplevel $level {info locals}]] }
}
if {[string match VERBOSE $verbose]} {
return [uplevel $level dump var -nocomplain $vars]
} else {
return $vars
}
}
e* - pu* {
if {[llength $opt]==1 && [catch {lindex [info level -1] 0} id]} {
set id [lindex [info level 0] 0]
} else {
set id [lindex $opt 1]
}
if {$IDEBUG(on) && [string match $IDEBUG(id) $id]} {
if {[string match e* $opt]} {
puts [concat $args]
} else { eval puts $args }
}
}
default {
return -code error "bad [lindex [info level 0] 0] option \"$opt\",\
must be: [join [lsort [list on off id break print body\
trace show puts echo]] {, }]"
}
}
}
## observe - like trace, but not
# ARGS: opt - option
# name - name of variable or command
##
proc observe {opt name args} {
global tcl_observe
switch -glob -- $opt {
co* {
if {[regexp {^(catch|lreplace|set|puts|for|incr|info|uplevel)$} \
$name]} {
return -code error "cannot observe \"$name\":\
infinite eval loop will occur"
}
set old ${name}@
while {[llength [info command $old]]} { append old @ }
rename $name $old
set max 4
regexp {^[0-9]+} $args max
## idebug trace could be used here
proc $name args "
for {set i \[info level\]; set max \[expr \[info level\]-$max\]} {
\$i>=\$max && !\[catch {uplevel \#\$i info level 0} info\]
} {incr i -1} {
puts -nonewline stderr \" \#\$i:\t\"
puts \$info
}
uplevel \[lreplace \[info level 0\] 0 0 $old\]
"
set tcl_observe($name) $old
}
cd* {
if {[info exists tcl_observe($name)] && [catch {
rename $name {}
rename $tcl_observe($name) $name
unset tcl_observe($name)
} err]} { return -code error $err }
}
ci* {
## What a useless method...
if {[info exists tcl_observe($name)]} {
set i $tcl_observe($name)
set res "\"$name\" observes true command \"$i\""
while {[info exists tcl_observe($i)]} {
append res "\n\"$name\" observes true command \"$i\""
set i $tcl_observe($name)
}
return $res
}
}
va* - vd* {
set type [lindex $args 0]
set args [lrange $args 1 end]
if {![regexp {^[rwu]} $type type]} {
return -code error "bad [lindex [info level 0] 0] $opt type\
\"$type\", must be: read, write or unset"
}
if {![llength $args]} { set args observe_var }
uplevel 1 [list trace $opt $name $type $args]
}
vi* {
uplevel 1 [list trace vinfo $name]
}
default {
return -code error "bad [lindex [info level 0] 0] option\
\"[lindex $args 0]\", must be: [join [lsort \
[list command cdelete cinfo variable vdelete vinfo]] {, }]"
}
}
}
## observe_var - auxilary function for observing vars, called by trace
## via observe
# ARGS: name - variable name
# el - array element name, if any
# op - operation type (rwu)
##
proc observe_var {name el op} {
if {[string match u $op]} {
if {[string compare {} $el]} {
puts "unset \"${name}($el)\""
} else {
puts "unset \"$name\""
}
} else {
upvar 1 $name $name
if {[info exists ${name}($el)]} {
puts [dump v ${name}($el)]
} else {
puts [dump v $name]
}
}
}
## which - tells you where a command is found
# ARGS: cmd - command name
# Returns: where command is found (internal / external / unknown)
##
proc which cmd {
## This tries to auto-load a command if not recognized
set types [what $cmd 1]
if {[llength $types]} {
set out {}
foreach type $types {
switch -- $type {
alias { set res "$cmd: aliased to [alias $cmd]" }
procedure { set res "$cmd: procedure" }
command { set res "$cmd: internal command" }
executable { lappend out [auto_execok $cmd] }
variable { lappend out "$cmd: variable" }
}
if {[info exists res]} {
global auto_index
if {[info exists auto_index($cmd)]} {
## This tells you where the command MIGHT have come from -
## not true if the command was redefined interactively or
## existed before it had to be auto_loaded. This is just
## provided as a hint at where it MAY have come from
append res " ($auto_index($cmd))"
}
lappend out $res
unset res
}
}
return [join $out \n]
} else {
return -code error "$cmd: command not found"
}
}
## what - tells you what a string is recognized as
# ARGS: str - string to id
# Returns: id types of command as list
##
proc what {str {autoload 0}} {
set types {}
if {[llength [info commands $str]] || ($autoload && \
[auto_load $str] && [llength [info commands $str]])} {
if {[lsearch -exact [interp aliases] $str] > -1} {
lappend types "alias"
} elseif {
[llength [info procs $str]] ||
([string match *::* $str] &&
[llength [namespace eval [namespace qualifier $str] \
info procs [namespace tail $str]]])
} {
lappend types "procedure"
} else {
lappend types "command"
}
}
if {[llength [uplevel 1 info vars $str]]} {
lappend types "variable"
}
if {[file isdirectory $str]} {
lappend types "directory"
}
if {[file isfile $str]} {
lappend types "file"
}
if {[llength [info commands winfo]] && [winfo exists $str]} {
lappend types "widget"
}
if {[string compare {} [auto_execok $str]]} {
lappend types "executable"
}
return $types
}
## dir - directory list
# ARGS: args - names/glob patterns of directories to list
# OPTS: -all - list hidden files as well (Unix dot files)
# -long - list in full format "permissions size date filename"
# -full - displays / after directories and link paths for links
# Returns: a directory listing
##
proc dir {args} {
array set s {
all 0 full 0 long 0
0 --- 1 --x 2 -w- 3 -wx 4 r-- 5 r-x 6 rw- 7 rwx
}
while {[string match \-* [lindex $args 0]]} {
set str [lindex $args 0]
set args [lreplace $args 0 0]
switch -glob -- $str {
-a* {set s(all) 1} -f* {set s(full) 1}
-l* {set s(long) 1} -- break
default {
return -code error "unknown option \"$str\",\
should be one of: -all, -full, -long"
}
}
}
set sep [string trim [file join . .] .]
if {![llength $args]} { set args . }
foreach arg $args {
if {[file isdir $arg]} {
set arg [string trimright $arg $sep]$sep
if {$s(all)} {
lappend out [list $arg [lsort [glob -nocomplain -- $arg.* $arg*]]]
} else {
lappend out [list $arg [lsort [glob -nocomplain -- $arg*]]]
}
} else {
lappend out [list [file dirname $arg]$sep \
[lsort [glob -nocomplain -- $arg]]]
}
}
if {$s(long)} {
set old [clock scan {1 year ago}]
set fmt "%s%9d %s %s\n"
foreach o $out {
set d [lindex $o 0]
append res $d:\n
foreach f [lindex $o 1] {
file lstat $f st
set f [file tail $f]
if {$s(full)} {
switch -glob $st(type) {
d* { append f $sep }
l* { append f "@ -> [file readlink $d$sep$f]" }
default { if {[file exec $d$sep$f]} { append f * } }
}
}
if {[string match file $st(type)]} {
set mode -
} else {
set mode [string index $st(type) 0]
}
foreach j [split [format %03o [expr {$st(mode)&0777}]] {}] {
append mode $s($j)
}
if {$st(mtime)>$old} {
set cfmt {%b %d %H:%M}
} else {
set cfmt {%b %d %Y}
}
append res [format $fmt $mode $st(size) \
[clock format $st(mtime) -format $cfmt] $f]
}
append res \n
}
} else {
foreach o $out {
set d [lindex $o 0]
append res "$d:\n"
set i 0
foreach f [lindex $o 1] {
if {[string len [file tail $f]] > $i} {
set i [string len [file tail $f]]
}
}
set i [expr {$i+2+$s(full)}]
set j 80
## This gets the number of cols in the tkcon console widget
if {[llength [info commands tkcon]]} {
set j [expr {[tkcon master set ::tkcon::OPT(cols)]/$i}]
}
set k 0
foreach f [lindex $o 1] {
set f [file tail $f]
if {$s(full)} {
switch -glob [file type $d$sep$f] {
d* { append f $sep }
l* { append f @ }
default { if {[file exec $d$sep$f]} { append f * } }
}
}
append res [format "%-${i}s" $f]
if {[incr k]%$j == 0} {set res [string trimright $res]\n}
}
append res \n\n
}
}
return [string trimright $res]
}
interp alias {} ::ls {} ::dir -full
## lremove - remove items from a list
# OPTS:
# -all remove all instances of each item
# -glob remove all instances matching glob pattern
# -regexp remove all instances matching regexp pattern
# ARGS: l a list to remove items from
# args items to remove (these are 'join'ed together)
##
proc lremove {args} {
array set opts {-all 0 pattern -exact}
while {[string match -* [lindex $args 0]]} {
switch -glob -- [lindex $args 0] {
-a* { set opts(-all) 1 }
-g* { set opts(pattern) -glob }
-r* { set opts(pattern) -regexp }
-- { set args [lreplace $args 0 0]; break }
default {return -code error "unknown option \"[lindex $args 0]\""}
}
set args [lreplace $args 0 0]
}
set l [lindex $args 0]
foreach i [join [lreplace $args 0 0]] {
if {[set ix [lsearch $opts(pattern) $l $i]] == -1} continue
set l [lreplace $l $ix $ix]
if {$opts(-all)} {
while {[set ix [lsearch $opts(pattern) $l $i]] != -1} {
set l [lreplace $l $ix $ix]
}
}
}
return $l
}
if {!$::tkcon::PRIV(WWW)} {;
## Unknown changed to get output into tkcon window
# unknown:
# Invoked automatically whenever an unknown command is encountered.
# Works through a list of "unknown handlers" that have been registered
# to deal with unknown commands. Extensions can integrate their own
# handlers into the 'unknown' facility via 'unknown_handler'.
#
# If a handler exists that recognizes the command, then it will
# take care of the command action and return a valid result or a
# Tcl error. Otherwise, it should return "-code continue" (=2)
# and responsibility for the command is passed to the next handler.
#
# Arguments:
# args - A list whose elements are the words of the original
# command, including the command name.
proc unknown args {
global unknown_handler_order unknown_handlers errorInfo errorCode
#
# Be careful to save error info now, and restore it later
# for each handler. Some handlers generate their own errors
# and disrupt handling.
#
set savedErrorCode $errorCode
set savedErrorInfo $errorInfo
if {![info exists unknown_handler_order] || \
![info exists unknown_handlers]} {
set unknown_handlers(tcl) tcl_unknown
set unknown_handler_order tcl
}
foreach handler $unknown_handler_order {
set status [catch {uplevel 1 $unknown_handlers($handler) $args} result]
if {$status == 1} {
#
# Strip the last five lines off the error stack (they're
# from the "uplevel" command).
#
set new [split $errorInfo \n]
set new [join [lrange $new 0 [expr {[llength $new]-6}]] \n]
return -code $status -errorcode $errorCode \
-errorinfo $new $result
} elseif {$status != 4} {
return -code $status $result
}
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
}
set name [lindex $args 0]
return -code error "invalid command name \"$name\""
}
# tcl_unknown:
# Invoked when a Tcl command is invoked that doesn't exist in the
# interpreter:
#
# 1. See if the autoload facility can locate the command in a
# Tcl script file. If so, load it and execute it.
# 2. If the command was invoked interactively at top-level:
# (a) see if the command exists as an executable UNIX program.
# If so, "exec" the command.
# (b) see if the command requests csh-like history substitution
# in one of the common forms !!, !<number>, or ^old^new. If
# so, emulate csh's history substitution.
# (c) see if the command is a unique abbreviation for another
# command. If so, invoke the command.
#
# Arguments:
# args - A list whose elements are the words of the original
# command, including the command name.
proc tcl_unknown args {
global auto_noexec auto_noload env unknown_pending tcl_interactive
global errorCode errorInfo
# If the command word has the form "namespace inscope ns cmd"
# then concatenate its arguments onto the end and evaluate it.
set cmd [lindex $args 0]
if {[regexp "^namespace\[ \t\n\]+inscope" $cmd] && [llength $cmd] == 4} {
set arglist [lrange $args 1 end]
set ret [catch {uplevel 1 $cmd $arglist} result]
if {$ret == 0} {
return $result
} else {
return -code $ret -errorcode $errorCode $result
}
}
# Save the values of errorCode and errorInfo variables, since they
# may get modified if caught errors occur below. The variables will
# be restored just before re-executing the missing command.
set savedErrorCode $errorCode
set savedErrorInfo $errorInfo
set name [lindex $args 0]
if {![info exists auto_noload]} {
#
# Make sure we're not trying to load the same proc twice.
#
if {[info exists unknown_pending($name)]} {
return -code error "self-referential recursion in \"unknown\" for command \"$name\""
}
set unknown_pending($name) pending
if {[llength [info args auto_load]]==1} {
set ret [catch {auto_load $name} msg]
} else {
set ret [catch {auto_load $name [uplevel 1 {namespace current}]} msg]
}
unset unknown_pending($name)
if {$ret} {
return -code $ret -errorcode $errorCode \
"error while autoloading \"$name\": $msg"
}
if {![array size unknown_pending]} { unset unknown_pending }
if {$msg} {
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
set code [catch {uplevel 1 $args} msg]
if {$code == 1} {
#
# Strip the last five lines off the error stack (they're
# from the "uplevel" command).
#
set new [split $errorInfo \n]
set new [join [lrange $new 0 [expr {[llength $new]-6}]] \n]
return -code error -errorcode $errorCode \
-errorinfo $new $msg
} else {
return -code $code $msg
}
}
}
if {[info level] == 1 && [string match {} [info script]] \
&& [info exists tcl_interactive] && $tcl_interactive} {
if {![info exists auto_noexec]} {
set new [auto_execok $name]
if {[string compare {} $new]} {
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
# set FH [uplevel [list open [concat |$new [lrange $args 1 end] 2>@stdout ] r]]
# while {![eof $FH]} { puts [gets $FH] }
# catch {close $FH} ERR
# return
return [uplevel 1 exec $new [lrange $args 1 end]]
#return [uplevel exec >&@stdout <@stdin $new [lrange $args 1 end]]
}
}
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
##
## History substitution moved into ::tkcon::EvalCmd
##
if {[string compare $name "::"] == 0} {
set name ""
}
if {$ret != 0} {
return -code $ret -errorcode $errorCode \
"error in unknown while checking if \"$name\" is a unique command abbreviation: $msg"
}
set cmds [info commands $name*]
if {[llength $cmds] == 1} {
return [uplevel 1 [lreplace $args 0 0 $cmds]]
}
if {[llength $cmds]} {
if {$name == ""} {
return -code error "empty command name \"\""
} else {
return -code error \
"ambiguous command name \"$name\": [lsort $cmds]"
}
}
## We've got nothing so far
## Check and see if Tk wasn't loaded, but it appears to be a Tk cmd
if {![uplevel \#0 info exists tk_version]} {
lappend tkcmds bell bind bindtags button \
canvas checkbutton clipboard destroy \
entry event focus font frame grab grid image \
label listbox lower menu menubutton message \
option pack place radiobutton raise \
scale scrollbar selection send spinbox \
text tk tkwait toplevel winfo wm
if {[lsearch -exact $tkcmds $name] >= 0 && \
[tkcon master tk_messageBox -icon question -parent . \
-title "Load Tk?" -type retrycancel -default retry \
-message "This appears to be a Tk command, but Tk\
has not yet been loaded. Shall I retry the command\
with loading Tk first?"] == "retry"} {
return [uplevel 1 "load {} Tk; $args"]
}
}
}
return -code continue
}
} ; # end exclusionary code for WWW
## read the user based init-files
if {[info exists env(TK_CON_COMMAND)] && [ file exists $env(TK_CON_COMMAND) ]} {
uplevel #0 source $env(TK_CON_COMMAND)
}
if {[info exists env(TK_CON_LOCAL)] && [ file exists $env(TK_CON_LOCAL) ]} {
uplevel #0 source $env(TK_CON_LOCAL)
}