summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/term/receive.tcl
diff options
context:
space:
mode:
authorWilliam Joye <wjoye@cfa.harvard.edu>2016-10-27 19:39:39 (GMT)
committerWilliam Joye <wjoye@cfa.harvard.edu>2016-10-27 19:39:39 (GMT)
commitea28451286d3ea4a772fa174483f9a7a66bb1ab3 (patch)
tree6ee9d8a7848333a7ceeee3b13d492e40225f8b86 /tcllib/modules/term/receive.tcl
parentb5ca09bae0d6a1edce939eea03594dd56383f2c8 (diff)
parent7c621da28f07e449ad90c387344f07a453927569 (diff)
downloadblt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.zip
blt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.tar.gz
blt-ea28451286d3ea4a772fa174483f9a7a66bb1ab3.tar.bz2
Merge commit '7c621da28f07e449ad90c387344f07a453927569' as 'tcllib'
Diffstat (limited to 'tcllib/modules/term/receive.tcl')
-rw-r--r--tcllib/modules/term/receive.tcl60
1 files changed, 60 insertions, 0 deletions
diff --git a/tcllib/modules/term/receive.tcl b/tcllib/modules/term/receive.tcl
new file mode 100644
index 0000000..393549c
--- /dev/null
+++ b/tcllib/modules/term/receive.tcl
@@ -0,0 +1,60 @@
+# -*- tcl -*-
+# ### ### ### ######### ######### #########
+## Terminal packages - Generic receiver operations
+
+# ### ### ### ######### ######### #########
+## Requirements
+
+namespace eval ::term::receive {}
+
+# ### ### ### ######### ######### #########
+## API. Read character from specific channel,
+## or default (stdin). Processing of
+## character sequences.
+
+proc ::term::receive::getch {{chan stdin}} {
+ return [read $chan 1]
+}
+
+proc ::term::receive::listen {cmd {chan stdin}} {
+ fconfigure $chan -blocking 0
+ fileevent $chan readable \
+ [list ::term::receive::Foreach $chan $cmd]
+ return
+}
+
+proc ::term::receive::unlisten {{chan stdin}} {
+ fileevent $chan readable {}
+ return
+}
+
+# ### ### ### ######### ######### #########
+## Internals
+
+proc ::term::receive::Foreach {chan cmd} {
+ set string [read $chan]
+ if {[string length $string]} {
+ #puts stderr "F($string)"
+ uplevel #0 [linsert $cmd end process $string]
+ }
+ if {[eof $chan]} {
+ close $chan
+ uplevel #0 [linsert $cmd end eof]
+ }
+ return
+}
+
+# ### ### ### ######### ######### #########
+## Initialization
+
+namespace eval ::term::receive {
+ namespace export getch listen
+}
+
+# ### ### ### ######### ######### #########
+## Ready
+
+package provide term::receive 0.1
+
+##
+# ### ### ### ######### ######### #########