scan: make some structure members const
[jimtcl.git] / tclcompat.tcl
blobe2ae56e91bb374fe6333b6b0eaf7d66d0f5edd12
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, optional argument is a glob pattern
67 # Third, optional argument is a "putter" function
68 proc parray {arrayname {pattern *} {puts puts}} {
69 upvar $arrayname a
71 set max 0
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]
78 incr max 2
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} {
86 try {
87 if {$force ni {{} -force}} {
88 error "bad option \"$force\": should be -force"
91 set in [open $source rb]
93 if {[file exists $target]} {
94 if {$force eq ""} {
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} {
99 return
101 # Hard linked, or case-insensitive filesystem
102 # Note: mingw returns ino=0 for every file :-(
103 file stat $source ss
104 file stat $target ts
105 if {$ss(dev) == $ts(dev) && $ss(ino) == $ts(ino) && $ss(ino)} {
106 return
109 set out [open $target wb]
110 $in copyto $out
111 $out close
112 } on error {msg opts} {
113 incr opts(-level)
114 return {*}$opts $msg
115 } finally {
116 catch {$in close}
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
124 try {
125 if {[string match "w*" $mode]} {
126 lappend cmd <@$r &
127 set pids [exec {*}$cmd]
128 $r close
129 set f $w
130 } else {
131 lappend cmd >@$w &
132 set pids [exec {*}$cmd]
133 $w close
134 set f $r
136 lambda {cmd args} {f pids} {
137 if {$cmd eq "pid"} {
138 return $pids
140 if {$cmd eq "close"} {
141 $f close
142 # And wait for the child processes to complete
143 foreach p $pids { os.wait $p }
144 return
146 tailcall $f $cmd {*}$args
148 } on error {error opts} {
149 $r close
150 $w close
151 error $error
155 # A wrapper around 'pid' which can return the pids for 'popen'
156 local proc pid {{channelId {}}} {
157 if {$channelId eq ""} {
158 tailcall upcall pid
160 if {[catch {$channelId tell}]} {
161 return -code error "can not find channel named \"$channelId\""
163 if {[catch {$channelId pid} pids]} {
164 return ""
166 return $pids
169 # try/on/finally conceptually similar to Tcl 8.6
171 # Usage: try ?catchopts? script ?onclause ...? ?finallyclause?
173 # Where:
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
177 proc try {args} {
178 set catchopts {}
179 while {[string match -* [lindex $args 0]]} {
180 set args [lassign $args opt]
181 if {$opt eq "--"} {
182 break
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]
192 set handled 0
194 foreach {on codes vars script} $args {
195 switch -- $on \
196 on {
197 if {!$handled && ($codes eq "*" || [info returncode $code] in $codes)} {
198 lassign $vars msgvar optsvar
199 if {$msgvar ne ""} {
200 upvar $msgvar hmsg
201 set hmsg $msg
203 if {$optsvar ne ""} {
204 upvar $optsvar hopts
205 set hopts $opts
207 # Override any body result
208 set code [catch {uplevel 1 $script} msg opts]
209 incr handled
212 finally {
213 set finalcode [catch {uplevel 1 $codes} finalmsg finalopts]
214 if {$finalcode} {
215 # Override any body or handler result
216 set code $finalcode
217 set msg $finalmsg
218 set opts $finalopts
220 break
222 default {
223 return -code error "try: expected 'on' or 'finally', got '$on'"
227 if {$code} {
228 incr opts(-level)
229 return {*}$opts $msg
231 return $msg
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
245 file delete $path