1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
## -- Tcl Module -- -*- tcl -*-
# # ## ### ##### ######## #############
# @@ Meta Begin
# Package coroutine::auto 1.1.2
# Meta platform tcl
# Meta require {Tcl 8.6}
# Meta require {coroutine 1.1}
# Meta license BSD
# Meta as::author {Andreas Kupries}
# Meta as::origin http://wiki.tcl.tk/21555
# Meta summary Coroutine Event and Channel Support
# Meta description Built on top of coroutine, this
# Meta description package intercepts various builtin
# Meta description commands to make the code using them
# Meta description coroutine-oblivious, i.e. able to run
# Meta description inside and outside of a coroutine
# Meta description without changes.
# @@ Meta End
# Copyright (c) 2009-2014 Andreas Kupries
## $Id: coro_auto.tcl,v 1.3 2011/11/17 08:00:45 andreas_kupries Exp $
# # ## ### ##### ######## #############
## Requisites, and ensemble setup.
package require Tcl 8.6
package require coroutine
namespace eval ::coroutine::auto {}
# # ## ### ##### ######## #############
## API implementations. Uses the coroutine commands where
## possible.
proc ::coroutine::auto::wrap_global {args} {
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_global {*}$args
}
tailcall ::coroutine::util::global {*}$args
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_after {delay args} {
if {
([info coroutine] eq {}) ||
([llength $args] > 0)
} {
# We use the core builtin when called from either outside of a
# coroutine, or for an asynchronous delay.
tailcall ::coroutine::auto::core_after $delay {*}$args
}
# Inside of coroutine, and synchronous delay (args == "").
tailcall ::coroutine::util::after $delay
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_exit {{status 0}} {
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_exit $status
}
tailcall ::coroutine::util::exit $status
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_vwait {varname} {
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_vwait $varname
}
tailcall ::coroutine::util::vwait $varname
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_update {{what {}}} {
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_update {*}$what
}
# This is a full re-implementation of mode (1), because the
# coroutine-aware part uses the builtin itself for some
# functionality, and this part cannot be taken as is.
if {$what eq "idletasks"} {
after idle [info coroutine]
} elseif {$what ne {}} {
# Force proper error message for bad call.
tailcall ::coroutine::auto::core_update $what
} else {
after 0 [info coroutine]
}
yield
return
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_gets {args} {
# Process arguments.
# Acceptable syntax:
# * gets CHAN ?VARNAME?
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_gets {*}$args
}
# This is a full re-implementation of mode (1), because the
# coroutine-aware part uses the builtin itself for some
# functionality, and this part cannot be taken as is.
if {[llength $args] == 2} {
# gets CHAN VARNAME
lassign $args chan varname
upvar 1 $varname line
} elseif {[llength $args] == 1} {
# gets CHAN
lassign $args chan
} else {
# not enough, or too many arguments (0, or > 2): Calling the
# builtin gets command with the bogus arguments gives us the
# necessary error with the proper message.
tailcall ::coroutine::auto::core_gets {*}$args
}
# Loop until we have a complete line. Yield to the event loop
# where necessary. During
while {1} {
set blocking [::chan configure $chan -blocking]
::chan configure $chan -blocking 0
try {
set result [::coroutine::auto::core_gets $chan line]
} on error {result opts} {
::chan configure $chan -blocking $blocking
return -code $result -options $opts
}
if {[::chan blocked $chan]} {
::chan event $chan readable [list [info coroutine]]
yield
::chan event $chan readable {}
} else {
::chan configure $chan -blocking $blocking
if {[llength $args] == 2} {
return $result
} else {
return $line
}
}
}
}
# - -- --- ----- -------- -------------
proc ::coroutine::auto::wrap_read {args} {
# Process arguments.
# Acceptable syntax:
# * read ?-nonewline ? CHAN
# * read CHAN ?n?
if {[info coroutine] eq {}} {
tailcall ::coroutine::auto::core_read {*}$args
}
# This is a full re-implementation of mode (1), because the
# coroutine-aware part uses the builtin itself for some
# functionality, and this part cannot be taken as is.
if {[llength $args] > 2} {
# Calling the builtin read command with the bogus arguments
# gives us the necessary error with the proper message.
::coroutine::auto::core_read {*}$args
return
}
set total Inf ; # Number of characters to read. Here: Until eof.
set chop no ; # Boolean flag. Determines if we have to trim a
# # \n from the end of the read string.
if {[llength $args] == 2} {
lassign $args a b
if {$a eq "-nonewline"} {
set chan $b
set chop yes
} else {
lassign $args chan total
}
} else {
lassign $args chan
}
# Run the read loop. Yield to the event loop where
# necessary. Differentiate between loop until eof, and loop until
# n characters have been read (or eof reached).
set buf {}
if {$total eq "Inf"} {
# Loop until eof.
while {1} {
set blocking [::chan configure $chan -blocking]
::chan configure $chan -blocking 0
try {
set result [::coroutine::auto::core_read $chan]
} on error {result opts} {
::chan configure $chan -blocking $blocking
return -code $result -options $opts
}
if {[::chan blocked $chan]} {
::chan event $chan readable [list [info coroutine]]
yield
::chan event $chan readable {}
} else {
::chan configure $chan -blocking $blocking
append buf $result
if {[::chan eof $chan]} {
::chan close $chan
break
}
}
}
} else {
# Loop until total characters have been read, or eof found,
# whichever is first.
set left $total
while {1} {
set blocking [::chan configure $chan -blocking]
::chan configure $chan -blocking 0
try {
set result [::coroutine::auto::core_read $chan $left]
} on error {result opts} {
::chan configure $chan -blocking $blocking
return -code $result -options $opts
}
if {[::chan blocked $chan]} {
::chan event $chan readable [list [info coroutine]]
yield
::chan event $chan readable {}
} else {
::chan configure $chan -blocking $blocking
append buf $result
incr left -[string length $result]
if {[::chan eof $chan]} {
::chan close $chan
break
} elseif {!$left} {
break
}
}
}
}
if {$chop && [string index $buf end] eq "\n"} {
set buf [string range $buf 0 end-1]
}
return $buf
}
# # ## ### ##### ######## #############
## Internal. Setup.
::apply {{} {
# Replaces the builtin commands with coroutine-aware
# counterparts. We cannot use the coroutine commands directly,
# because the replacements have to use the saved builtin commands
# when called outside of a coroutine. And some (read, gets,
# update) even need full re-implementations, as they use the
# builtin command they replace themselves to implement their
# functionality.
foreach cmd {
global
exit
after
vwait
update
} {
rename ::$cmd [namespace current]::core_$cmd
rename [namespace current]::wrap_$cmd ::$cmd
}
foreach cmd {
gets
read
} {
rename ::tcl::chan::$cmd [namespace current]::core_$cmd
rename [namespace current]::wrap_$cmd ::tcl::chan::$cmd
}
return
} ::coroutine::auto}
# # ## ### ##### ######## #############
## Ready
package provide coroutine::auto 1.1.3
return
|