Update autosetup to latest version
[jimtcl.git] / tclcompat.tcl
blobd1516b3082c2ff4b121e7ec659f39b2ad9702b23
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
9 set env [env]
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
19 unset p
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} {
42 foreach {n v} $args {
43 switch -glob -- $n {
44 -bl* {
45 $f ndelay $(!$v)
47 -bu* {
48 $f buffering $v
50 -tr* {
51 # Just ignore -translation
53 default {
54 return -code error "fconfigure: unknown option $n"
61 # fileevent isn't needed in Jim, but provide it for compatibility
62 proc fileevent {args} {
63 tailcall {*}$args
66 # Second, option argument is a glob pattern
67 # Third, optional argument is a "putter" function
69 proc parray {arrayname {pattern *} {puts puts}} {
70 upvar $arrayname a
72 set max 0
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]
79 incr max 2
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} {
87 try {
88 if {$force ni {{} -force}} {
89 error "bad option \"$force\": should be -force"
92 set in [open $source rb]
94 if {[file exists $target]} {
95 if {$force eq ""} {
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} {
100 return
102 # Hard linked, or case-insensitive filesystem
103 # Note: mingw returns ino=0 for every file :-(
104 file stat $source ss
105 file stat $target ts
106 if {$ss(dev) == $ts(dev) && $ss(ino) == $ts(ino) && $ss(ino)} {
107 return
110 set out [open $target wb]
111 $in copyto $out
112 $out close
113 } on error {msg opts} {
114 incr opts(-level)
115 return {*}$opts $msg
116 } finally {
117 catch {$in close}
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
125 try {
126 if {[string match "w*" $mode]} {
127 lappend cmd <@$r &
128 set pids [exec {*}$cmd]
129 $r close
130 set f $w
131 } else {
132 lappend cmd >@$w &
133 set pids [exec {*}$cmd]
134 $w close
135 set f $r
137 lambda {cmd args} {f pids} {
138 if {$cmd eq "pid"} {
139 return $pids
141 if {$cmd eq "close"} {
142 $f close
143 # And wait for the child processes to complete
144 foreach p $pids { os.wait $p }
145 return
147 tailcall $f $cmd {*}$args
149 } on error {error opts} {
150 $r close
151 $w close
152 error $error
156 # A wrapper around 'pid' which can return the pids for 'popen'
157 local proc pid {{channelId {}}} {
158 if {$channelId eq ""} {
159 tailcall upcall pid
161 if {[catch {$channelId tell}]} {
162 return -code error "can not find channel named \"$channelId\""
164 if {[catch {$channelId pid} pids]} {
165 return ""
167 return $pids
170 # try/on/finally conceptually similar to Tcl 8.6
172 # Usage: try ?catchopts? script ?onclause ...? ?finallyclause?
174 # Where:
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?}
183 proc try {args} {
184 set catchopts {}
185 while {[string match -* [lindex $args 0]]} {
186 set args [lassign $args opt]
187 if {$opt eq "--"} {
188 break
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]
198 set handled 0
200 foreach {on codes vars script} $args {
201 switch -- $on \
202 on {
203 if {!$handled && ($codes eq "*" || [info returncode $code] in $codes)} {
204 lassign $vars msgvar optsvar
205 if {$msgvar ne ""} {
206 upvar $msgvar hmsg
207 set hmsg $msg
209 if {$optsvar ne ""} {
210 upvar $optsvar hopts
211 set hopts $opts
213 # Override any body result
214 set code [catch {uplevel 1 $script} msg opts]
215 incr handled
218 finally {
219 set finalcode [catch {uplevel 1 $codes} finalmsg finalopts]
220 if {$finalcode} {
221 # Override any body or handler result
222 set code $finalcode
223 set msg $finalmsg
224 set opts $finalopts
226 break
228 default {
229 return -code error "try: expected 'on' or 'finally', got '$on'"
233 if {$code} {
234 incr opts(-level)
235 return {*}$opts $msg
237 return $msg
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
251 file delete $path