+proc p {s} {
+ puts -nonewline $s
+}
+
+proc tput {args} {
+ global tput
+ if {[catch { set s $tput($args) }]} {
+ set s [eval exec tput $args]
+ set tput($args) $s
+ }
+ p $s
+}
+
+proc csr_pos {lit bytenum} {
+ set x [expr {
+ (!$lit ? (3*$bytenum) : 53+$bytenum)
+ + ($bytenum>>2) - (2-$lit)*($bytenum==16)
+ + 5
+ }]
+ tput hpa $x
+}
+
+proc csr_this {} { global lit x; csr_pos $lit $x }
+proc csr_other {} { global lit x; csr_pos [expr {!$lit}] $x }
+proc csrs_erase {} { csr_this; p " "; csr_other; p " " }
+proc csr_this_show {} {
+ global h1
+ csr_this; if {[info exists h1]} { p $h1; p "\b" }
+}
+proc csrs_show {} {
+ csr_other; p _
+ csr_this_show
+}
+
+proc echop {} {
+ global echo
+ return [expr {$echo ? "|" : "'"}]
+}
+
+proc newline {} {
+ global x echo count
+ if {[info exists x]} { csrs_erase; p "\r\n" }
+ set x 0
+ p [format "%3x%s%*s|%*s|" $count [echop] 52 "" 21 ""]
+ csrs_show
+}
+
+proc p_ch_spaces {} {
+ global x lit
+ if {$x==15} return
+ if {$lit} { p " " }
+ if {($x & 3) != 3} return
+ p " "
+}
+
+proc p_rmso {smso} {
+ if {[string length $smso]} { tput sgr0 }
+}
+
+proc ch {d smso} {
+ global lit x count
+ if {$x == 16} newline
+ if {[string length $smso]} { tput $smso }
+ set h [format %02x [expr {$d & 0xff}]]
+ set c [format %c [expr {($d > 33 && $d < 127 && $d != 95) ? $d : 46}]]
+ if {$lit} {
+ p $c; csr_other; p $h
+ p_ch_spaces
+ p_rmso $smso
+ p _
+ } else {
+ p $h; csr_other; p $c
+ p_ch_spaces
+ p_rmso $smso
+ p _
+ }
+ incr x
+ set count [expr {($count+1) & 0xfff}]
+ csr_this_show
+}
+
+proc onreadp {} {
+ global p
+ while 1 {
+ set c [read $p 1]
+ binary scan $c c* d
+ if {![llength $d]} {
+ if {[eof $p]} { error "eof on device" }
+ return
+ }
+ ch $d {}
+ }
+}
+
+proc transmit {d} {
+ global p echo
+ puts -nonewline $p [format %c $d]
+ if {$echo} { ch $d bold }
+}
+
+proc k_echo {} {
+ global echo
+ set echo [expr {!$echo}]
+ tput hpa 3
+ p [echop]
+ csr_this
+}
+
+proc k_newline {} {
+ global count x
+ if {$x} {
+ newline
+ } else {
+ set count 0
+ p "\r"
+ p [format %3x $count]
+ csr_this
+ }
+}
+
+proc k_switch {} {
+ global lit h1
+ csrs_erase
+ catch { unset h1 }
+ set lit [expr {!$lit}]
+ csrs_show
+}
+
+proc k_stop {} {
+ restore
+ exit 0
+}
+
+proc k_suspend {} {
+ restore
+ exec kill -TSTP [info pid]
+ setup
+}
+
+proc k_noparthex {} {
+ global h1
+ csrs_erase
+ catch { unset h1 }
+ csrs_show
+}
+
+proc k_hexdigit {c} {
+ global h1 echo
+ if {![info exists h1]} { set h1 $c; p $c; p "\b"; return }
+ set d [expr 0x${h1}${c}]
+ unset h1
+ transmit $d
+ if {!$echo} { p " \b" }
+}
+
+proc onreadk {} {
+ global lit
+ while 1 {
+ set c [read stdin 1]
+ binary scan $c c* d
+ if {![llength $d]} {
+ if {[eof stdin]} { error "eof on stdin" }
+ return
+ }
+ switch -exact $d {
+ 9 { k_switch; continue }
+ 3 - 4 { k_stop; continue }
+ 26 { k_suspend; continue }
+ }
+ if {$lit} { transmit $d; continue }
+ switch -exact $d {
+ 13 { k_newline; continue }
+ 32 { transmit 0; continue }
+ 39 { k_echo; continue }
+ 127 { k_noparthex; continue }
+ }
+ if {$d >= 48 && $d <= 57} { k_hexdigit $c; continue }
+ set kl [expr {$d | 32}]
+ if {$d >= 97 && $d <= 102} { k_hexdigit $c; continue }
+ p "\a"
+ }
+}
+
+proc try {script} {
+ if {[catch { uplevel 1 $script } emsg]} {
+ catch { puts stderr "(warning: $emsg)" }
+ }
+}
+
+proc tryv {variable script} {
+ upvar #0 $variable var
+ if {![info exists var]} return
+ uplevel 1 "
+ global $variable
+ $script
+ "
+ unset var
+}
+
+proc restore {} {
+ tryv x { puts "\r\n" }
+ try { fconfigure stdin -blocking true }
+ try { fconfigure stdout -blocking true }
+ tryv term_stty { exec stty $term_stty }
+ tryv p { close $p }
+}
+
+proc setup {} {
+ global term_stty port p
+
+ set term_stty [exec stty -g]
+
+ set p [open $port {RDWR NONBLOCK} 0]
+
+ exec stty min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \