3 # vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
5 package require tclreadline
11 proc connect {ip port} { #<<<
18 set ::sock [socket $ip $port]
20 puts "Error connecting to $ip:$port: $msg"
23 fconfigure $::sock -blocking 1 -translation binary -encoding binary
24 set ::node "$ip:$port"
30 # Initial connection <<<
32 if {[llength $argv] != 0} {
33 set addr [lindex $argv 0]
34 set tmp [split $addr :]
35 switch -- [llength $tmp] {
37 set ip [lindex $addr 0]
41 foreach {ip port} $tmp break
44 puts stderr "Syntax: $argv0 \[ip\[:port\]\]"
56 "quit" "Exits the probe console"
57 "bye" "Exits the probe console"
58 "exit" "Exits the probe console"
59 "help" "Displays builtin command help"
60 "connect" "Connects to node"
63 proc process_builtin {cmd} { #<<<
64 switch -- [lindex $cmd 0] {
70 if {[llength $cmd] == 1} {
71 foreach {key val} [array get ::builtins] {
75 set term [lindex $cmd 1]
76 if {[info exists ::builtins($term)]} {
77 puts $::builtins($term)
79 puts "No such command"
86 set addr [lindex $cmd 1]
87 set tmp [split $addr :]
88 switch -- [llength $tmp] {
90 if {[info exists ::last_ip] && [info exists ::last_port]} {
94 puts "Syntax: connect ip\[:port\]"
99 set ip [lindex $tmp 0]
103 foreach {ip port} $tmp break
106 puts "Syntax: connect ip\[:port\]"
115 puts "Command not implemented yet"
122 proc sendcmd {cmd} { #<<<
124 puts "Not connected to a node"
128 puts $::sock [string length $cmd]
129 puts -nonewline $::sock $cmd
132 set len [gets $::sock]
137 puts "Connection closed by far side"
138 if {rand()*100 <= 5} {
139 puts "Please contact Garry Larson for more info"
143 set data [read $::sock $len]
148 tclreadline::readline builtincompleter 1
149 foreach cmd [array names builtins] {
150 tclreadline::readline add $cmd
155 tclreadline::readline add $rcmd
159 set cmd [tclreadline::readline read "$node> "]
160 set cmd [string trim $cmd]
161 if {$cmd == ""} continue
162 if {[info exists builtins([lindex $cmd 0])]} {