added -echo and changed to non-buffering mode

This commit is contained in:
zimoch
2003-06-26 12:55:49 +00:00
parent 639a7299a3
commit f74b787557

View File

@@ -25,10 +25,11 @@
regsub -all {\$} {shellbox.tcl by Dirk Zimoch
$Source: /cvs/G/EPICS/App/scripts/shellbox.tcl,v $
$Revision: 1.5 $
$Date: 2003/04/10 14:16:41 $} {} version
$Revision: 1.6 $
$Date: 2003/06/26 12:55:49 $} {} version
proc createServer {port {paranoid 0}} {
proc createServer {port} {
global paranoid
if [catch {
if $paranoid {
socket -server connectionHandler -myaddr localhost $port
@@ -54,9 +55,9 @@ proc connectionHandler {channel addr port} {
global channels command clientinfo
set peer [fconfigure $channel -peername]
set clientinfo($channel) [lindex $peer 1]:[lindex $peer 2]
fconfigure $channel -blocking no -buffering line
fconfigure $channel -blocking no -buffering none
fileevent $channel readable "inputHandler $channel"
forwardOutput "**** new client $clientinfo($channel) ****"
forwardOutput "**** new client $clientinfo($channel) ****\n"
catch {puts $channel "**** '$command' running ****"}
lappend channels $channel
catch {puts $channel "clients:\n[getClientlist]"}
@@ -74,11 +75,13 @@ proc closeChannel {channel} {
}
proc inputHandler {channel} {
global echo
set data [read $channel]
if [eof $channel] {
closeChannel $channel
return
}
if $echo {forwardOutput $data $channel}
binary scan $data H* hex
switch $data {
"clients\n" {forwardOutput "clients:\n[getClientlist]"}
@@ -116,11 +119,13 @@ proc outputHandler {channel} {
forwardOutput $data
}
proc forwardOutput {data} {
proc forwardOutput {data {exclude ""}} {
global channels
catch {puts -nonewline $data}
foreach channel $channels {
catch {puts -nonewline $channel $data}
if {$channel != $exclude} {
catch {puts -nonewline $channel $data}
}
}
}
@@ -144,7 +149,7 @@ proc startProgram {} {
catch {puts stderr $msg}
exit 3
}
fconfigure $pipe -blocking no -buffering line
fconfigure $pipe -blocking no -buffering none
gets $pipe
if [eof $pipe] {
forwardOutput "**** '$command' died immediately. I will quit ****\n"
@@ -161,16 +166,25 @@ proc killProgram {} {
startProgram
}
if [regexp {^-(v(er(sion)?)?)$} [lindex $argv 0]] {
puts $version
exit 0
}
set paranoid 0
set echo 0
if {[lindex $argv 0] == "-paranoid"} {
set paranoid 1
while {[string match -* $argv]} {
switch -exact -- [lindex $argv 0] {
"-?" - "-help" {
puts "usage: shellbox.tcl \[options\] port command ..."
puts "options: -help : show this text"
puts " -version : show cvs release"
puts " -paranoid : allow connections from localhost only"
puts " -echo : echo input of one client to all others"
exit 0
}
"-v" - "-version" {puts $version; exit 0}
"-paranoid" {set paranoid 1}
"-echo" {set echo 1}
default {puts stderr "unknown option [lindex $argv 0]"; exit 1}
}
set argv [lrange $argv 1 end]
} else {
set paranoid 0
}
set port [lindex $argv 0]
@@ -180,7 +194,8 @@ if {![regexp {^[0-9]+$} $port] || [llength $command] == 0} {
exit 1
}
fconfigure stdout -buffering none
startProgram
createServer $port $paranoid
createServer $port
vwait forever