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, option argument is a glob pattern
67 # Third, optional argument is a "putter" function
69 proc parray {arrayname
{pattern
*} {puts puts}} {
73 foreach name
[array names a
$pattern]] {
74 if {[string length
$name] > $max} {
75 set max
[string length
$name]
78 incr max
[string length
$arrayname]
80 foreach name
[lsort [array names a
$pattern]] {
81 $puts [format "%-${max}s = %s" $arrayname\($name\) $a($name)]
85 # Implements 'file copy' - single file mode only
86 proc {file copy
} {{force
{}} source target
} {
88 if {$force ni
{{} -force}} {
89 error "bad option \"$force\": should be -force"
92 set in
[open $source rb
]
94 if {[file exists
$target]} {
96 error "error copying \"$source\" to \"$target\": file already exists"
98 # If source and target are the same, nothing to do
99 if {$source eq
$target} {
102 # Hard linked, or case-insensitive filesystem
103 # Note: mingw returns ino=0 for every file :-(
106 if {$ss(dev
) == $ts(dev
) && $ss(ino
) == $ts(ino
) && $ss(ino
)} {
110 set out
[open $target wb
]
113 } on
error {msg opts
} {
121 # 'open "|..." ?mode?" will invoke this wrapper around exec/pipe
122 # Note that we return a lambda which also provides the 'pid' command
123 proc popen
{cmd
{mode r
}} {
124 lassign
[socket pipe
] r w
126 if {[string match
"w*" $mode]} {
128 set pids
[exec {*}$cmd]
133 set pids
[exec {*}$cmd]
137 lambda
{cmd args
} {f pids
} {
141 if {$cmd eq
"close"} {
143 # And wait for the child processes to complete
144 foreach p
$pids { os.wait
$p }
147 tailcall
$f $cmd {*}$args
149 } on
error {error opts
} {
156 # A wrapper around 'pid' which can return the pids for 'popen'
157 local
proc pid {{channelId
{}}} {
158 if {$channelId eq
""} {
161 if {[catch {$channelId tell}]} {
162 return -code error "can not find channel named \"$channelId\""
164 if {[catch {$channelId pid} pids
]} {
170 # try/on/finally conceptually similar to Tcl 8.6
172 # Usage: try ?catchopts? script ?onclause ...? ?finallyclause?
175 # onclause is: on codes {?resultvar? ?optsvar?} script
177 # codes is: a list of return codes (ok, error, etc. or integers), or * for any
179 # finallyclause is: finally script
182 # Where onclause is: on codes {?resultvar? ?optsvar?}
185 while {[string match
-* [lindex $args 0]]} {
186 set args
[lassign
$args opt
]
190 lappend catchopts
$opt
192 if {[llength $args] == 0} {
193 return -code error {wrong
# args: should be "try ?options? script ?argument ...?"}
195 set args
[lassign
$args script
]
196 set code
[catch -eval {*}$catchopts {uplevel 1 $script} msg opts
]
200 foreach {on codes vars script
} $args {
203 if {!$handled && ($codes eq
"*" ||
[info returncode
$code] in
$codes)} {
204 lassign
$vars msgvar optsvar
209 if {$optsvar ne
""} {
213 # Override any body result
214 set code
[catch {uplevel 1 $script} msg opts
]
219 set finalcode
[catch {uplevel 1 $codes} finalmsg finalopts
]
221 # Override any body or handler result
229 return -code error "try: expected 'on' or 'finally', got '$on'"
240 # Generates an exception with the given code (ok, error, etc. or an integer)
241 # and the given message
242 proc throw
{code
{msg
""}} {
243 return -code $code $msg
246 # Helper for "file delete -force"
247 proc {file delete force
} {path
} {
248 foreach e
[readdir
$path] {
249 file delete
-force $path/$e