3 # tip.tcl is like a simple version of cu, written in pure Jim Tcl
4 # It makes use of the new aio tty support
6 # Note: On Mac OS X, be sure to open /dev/cu.* devices, not /dev/tty.* devices
9 {Usage
: tip ?settings? device
12 Where settings are as follows
:
13 1|
2 stop bits
(default 1)
14 5|
6|
7|
8 data bits
(default 8)
15 even|odd parity
(default none
)
16 xonxoff|rtscts handshaking
(default none
)
17 <number
> baud rate
(default 115200)
19 e.g. tip
9600 8 1 rtscts
/dev
/ttyUSB0
}
36 if {[string match
-h* $i] ||
[string match help
* $i]} {
40 if {$i in
{even odd
}} {
41 set settings
(parity
) $i
44 if {$i in
{ixonixoff rtscts
}} {
45 set settings
(handshake
) $i
52 if {$i in
{5 6 7 8}} {
56 if {[string is integer
-strict $i]} {
60 if {[file exists
$i]} {
64 puts "Warning: unrecognised setting $i"
67 if {![exists device
]} {
72 # save stdin and stdout tty settings
73 # note that stdin and stdout are probably the same file descriptor,
74 # but it doesn't hurt to treat them independently
75 set stdin_save
[stdin tty
]
76 set stdout_save
[stdout tty
]
79 set f
[open $device r
+]
81 puts "Failed to open $device"
86 puts "Device is in use: $device"
97 puts "\[$device\] Use ~. to exit"
105 stdout tty output raw
106 stdout buffering none
112 # To avoid sending too much data and blocking,
113 # this sends str in chunks of 1000 bytes via writable
114 proc output-on-writable
{fh str
} {
115 # Add it to the buffer to send
116 append ::tosend($fh) $str
118 if {[string length
[$fh writable
]] == 0} {
119 # Start the writable event handler
120 $fh writable
[list output-is-writable
$fh]
124 # This is the writable callback
125 proc output-is-writable
{fh
} {
128 if {[string bytelength
$buf] >= 1000} {
129 set tosend
($fh) [string byterange
$buf 1000 end
]
130 set buf
[string byterange
$buf 0 999]
133 # All sent, so cancel the writable event handler
136 $fh puts -nonewline $buf
139 proc bgerror {args
} {
149 set status
"$device: disconnected"
153 output-on-writable stdout
$c
156 proc tilde_timeout
{} {
159 output-on-writable
$f ~
166 # may receive more than one char here, but only need to consider
167 # ~. processing if we receive them as separate chars
168 if {$tilde == 0 && $c eq
"~"} {
170 # Need ~. within 1 second of each other
171 after 1000 tilde_timeout
174 after cancel tilde_timeout
180 output-on-writable
$f ~
182 output-on-writable
$f $c
188 # restore previous settings
189 stdin tty
{*}$stdin_save
190 stdout tty
{*}$stdout_save