1# -*- tcl -*-
2# ### ### ### ######### ######### #########
3## Terminal packages - Generic receiver operations
4
5# ### ### ### ######### ######### #########
6## Requirements
7
8namespace eval ::term::receive {}
9
10# ### ### ### ######### ######### #########
11## API. Read character from specific channel,
12##      or default (stdin). Processing of
13##      character sequences.
14
15proc ::term::receive::getch {{chan stdin}} {
16    return [read $chan 1]
17}
18
19proc ::term::receive::listen {cmd {chan stdin}} {
20    fconfigure $chan -blocking 0
21    fileevent  $chan readable \
22	    [list ::term::receive::Foreach $chan $cmd]
23    return
24}
25
26proc ::term::receive::unlisten {{chan stdin}} {
27    fileevent $chan readable {}
28    return
29}
30
31# ### ### ### ######### ######### #########
32## Internals
33
34proc ::term::receive::Foreach {chan cmd} {
35    set string [read $chan]
36    if {[string length $string]} {
37	#puts stderr "F($string)"
38	uplevel #0 [linsert $cmd end process $string]
39    }
40    if {[eof $chan]} {
41	close $chan
42	uplevel #0 [linsert $cmd end eof]
43    }
44    return
45}
46
47# ### ### ### ######### ######### #########
48## Initialization
49
50namespace eval ::term::receive {
51    namespace export getch listen
52}
53
54# ### ### ### ######### ######### #########
55## Ready
56
57package provide term::receive 0.1
58
59##
60# ### ### ### ######### ######### #########
61