diff options
author | Steve Bennett <steveb@workware.net.au> | 2016-09-02 22:58:50 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2016-09-09 17:38:47 +1000 |
commit | 5e33fe80ddeb02f9b7184babfc027761e5893040 (patch) | |
tree | 2fdae625e8fa95a28408f2d1dcb8ee238f2983dd /examples | |
parent | 7db6feaa274af4199bf3f2704e5a4d4195cc5c1c (diff) | |
download | jimtcl-5e33fe80ddeb02f9b7184babfc027761e5893040.zip jimtcl-5e33fe80ddeb02f9b7184babfc027761e5893040.tar.gz jimtcl-5e33fe80ddeb02f9b7184babfc027761e5893040.tar.bz2 |
Add examples/tip.tcl
Example of using the aio termios support
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/tip.tcl | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/examples/tip.tcl b/examples/tip.tcl new file mode 100755 index 0000000..2770dcb --- /dev/null +++ b/examples/tip.tcl @@ -0,0 +1,148 @@ +#!/usr/bin/env jimsh + +# tip.tcl is like a simple version of cu, written in pure Jim Tcl +# It makes use of the new aio tty support + +# Note: On Mac OS X, be sure to open /dev/cu.* devices, not /dev/tty.* devices + +set USAGE \ +{Usage: tip ?settings? device + or tip help + +Where settings are as follows: +1|2 stop bits (default 1) +5|6|7|8 data bits (default 8) +even|odd parity (default none) +xonxoff|rtscts handshaking (default none) +<number> baud rate (default 115200) + +e.g. tip 9600 8 1 rtscts /dev/ttyUSB0} + +set settings { + baud 115200 + stop 1 + data 8 + parity none + handshake none + input raw + output raw + vmin 1 + vtime 1 +} + +set showhelp 0 + +foreach i $argv { + if {[string match -h* $i] || [string match help* $i]} { + puts $USAGE + return 0 + } + if {$i in {even odd}} { + set settings(parity) $i + continue + } + if {$i in {ixonixoff rtscts}} { + set settings(handshake) $i + continue + } + if {$i in {1 2}} { + set settings(stop) $i + continue + } + if {$i in {5 6 7 8}} { + set settings(data) $i + continue + } + if {[string is integer -strict $i]} { + set settings(baud) $i + continue + } + if {[file exists $i]} { + set device $i + continue + } + puts "Warning: unrecognised setting $i" +} + +if {![exists device]} { + puts $USAGE + exit 1 +} + +# save stdin and stdout tty settings +# note that stdin and stdout are probably the same file descriptor, +# but it doesn't hurt to treat them independently +set stdin_save [stdin tty] +set stdout_save [stdout tty] + +try { + set f [open $device r+] +} on error msg { + puts "Failed to open $device" + return 1 +} + +try { + $f tty {*}$settings +} on error msg { + puts "$device: $msg" + return 1 +} + +puts "\[$device\] Use ~. to exit" + +$f ndelay 1 +$f buffering none + +stdin tty input raw +stdin ndelay 1 + +stdout tty output raw +stdout buffering none + +# I/O loop + +set tilde 0 + +$f readable { + set c [$f read] + stdout puts -nonewline $c +} + +proc tilde_timeout {} { + global tilde f + if {$tilde} { + $f puts -nonewline ~ + set tilde 0 + } +} + +stdin readable { + set c [stdin read] + # may receive more than one char here, but only need to consider + # ~. processing if we receive them as separate chars + if {$tilde == 0 && $c eq "~"} { + incr tilde + # Need ~. within 1 second of each other + after 1000 tilde_timeout + } else { + if {$tilde} { + after cancel tilde_timeout + set tilde 0 + if {$c eq "."} { + incr done + return + } + $f puts -nonewline ~ + } + $f puts -nonewline $c + } +} + +vwait done + +# restore previous settings +stdin tty {*}$stdin_save +stdout tty {*}$stdout_save + +puts "" |