1 # Loads some Tcl-compatible features.
2 # I/O commands, case, lassign, parray, errorInfo, ::tcl_platform, ::env
3 # try, throw, file copy, file delete -force
5 # (c) 2008 Steve Bennett <steveb@workware.net.au>
8 # Set up the ::env array
11 # Provide Tcl-compatible I/O commands
12 if {[info commands stdout
] ne
""} {
13 # Tcl-compatible I/O commands
14 foreach p
{gets flush close eof seek tell} {
15 proc $p {chan args
} {p
} {
16 tailcall
$chan $p {*}$args
21 # puts is complicated by -nonewline
23 proc puts {{-nonewline {}} {chan stdout
} msg
} {
24 if {${-nonewline} ni
{-nonewline {}}} {
25 tailcall
${-nonewline} puts $msg
27 tailcall
$chan puts {*}${-nonewline} $msg
30 # read is complicated by -nonewline
32 # read chan ?maxchars?
33 # read -nonewline chan
34 proc read {{-nonewline {}} chan
} {
35 if {${-nonewline} ni
{-nonewline {}}} {
36 tailcall
${-nonewline} read {*}${chan
}
38 tailcall
$chan read {*}${-nonewline}
41 proc fconfigure {f args
} {
51 # Just ignore -translation
54 return -code error "fconfigure: unknown option $n"
61 # fileevent isn't needed in Jim, but provide it for compatibility
62 proc fileevent {args
} {
66 # Second, optional argument is a glob pattern
67 # Third, optional argument is a "putter" function
68 proc parray {arrayname
{pattern
*} {puts puts}} {
72 foreach name
[array names a
$pattern]] {
73 if {[string length
$name] > $max} {
74 set max
[string length
$name]
77 incr max
[string length
$arrayname]
79 foreach name
[lsort [array names a
$pattern]] {
80 $puts [format "%-${max}s = %s" $arrayname\($name\) $a($name)]
84 # Implements 'file copy' - single file mode only
85 proc {file copy
} {{force
{}} source target
} {
87 if {$force ni
{{} -force}} {
88 error "bad option \"$force\": should be -force"
91 set in
[open $source rb
]
93 if {[file exists
$target]} {
95 error "error copying \"$source\" to \"$target\": file already exists"
97 # If source and target are the same, nothing to do
98 if {$source eq
$target} {
101 # Hard linked, or case-insensitive filesystem
102 # Note: mingw returns ino=0 for every file :-(
105 if {$ss(dev
) == $ts(dev
) && $ss(ino
) == $ts(ino
) && $ss(ino
)} {
109 set out
[open $target wb
]
112 } on
error {msg opts
} {
120 # 'open "|..." ?mode?" will invoke this wrapper around exec/pipe
121 # Note that we return a lambda which also provides the 'pid' command
122 proc popen
{cmd
{mode r
}} {
123 lassign
[socket pipe
] r w
125 if {[string match
"w*" $mode]} {
127 set pids
[exec {*}$cmd]
132 set pids
[exec {*}$cmd]
136 lambda
{cmd args
} {f pids
} {
140 if {$cmd eq
"close"} {
142 # And wait for the child processes to complete
143 foreach p
$pids { os.wait
$p }
146 tailcall
$f $cmd {*}$args
148 } on
error {error opts
} {
155 # A wrapper around 'pid' which can return the pids for 'popen'
156 local
proc pid {{channelId
{}}} {
157 if {$channelId eq
""} {
160 if {[catch {$channelId tell}]} {
161 return -code error "can not find channel named \"$channelId\""
163 if {[catch {$channelId pid} pids
]} {
169 # try/on/finally conceptually similar to Tcl 8.6
171 # Usage: try ?catchopts? script ?onclause ...? ?finallyclause?
174 # onclause is: on codes {?resultvar? ?optsvar?} script
175 # codes is: a list of return codes (ok, error, etc. or integers), or * for any
176 # finallyclause is: finally script
179 while {[string match
-* [lindex $args 0]]} {
180 set args
[lassign
$args opt
]
184 lappend catchopts
$opt
186 if {[llength $args] == 0} {
187 return -code error {wrong
# args: should be "try ?options? script ?argument ...?"}
189 set args
[lassign
$args script
]
190 set code
[catch -eval {*}$catchopts {uplevel 1 $script} msg opts
]
194 foreach {on codes vars script
} $args {
197 if {!$handled && ($codes eq
"*" ||
[info returncode
$code] in
$codes)} {
198 lassign
$vars msgvar optsvar
203 if {$optsvar ne
""} {
207 # Override any body result
208 set code
[catch {uplevel 1 $script} msg opts
]
213 set finalcode
[catch {uplevel 1 $codes} finalmsg finalopts
]
215 # Override any body or handler result
223 return -code error "try: expected 'on' or 'finally', got '$on'"
234 # Generates an exception with the given code (ok, error, etc. or an integer)
235 # and the given message
236 proc throw
{code
{msg
""}} {
237 return -code $code $msg
240 # Helper for "file delete -force"
241 proc {file delete force
} {path
} {
242 foreach e
[readdir
$path] {
243 file delete
-force $path/$e