#!/bin/sh # the next line restarts using wish. The ulimit prevents corefiles. \ ulimit -c 0; exec wish "$0" "$@" global env set show_source 0 foreach item [split $argv] { if {$item == "--s"} { set show_source 1 } } if [info exists env(SHOW_SOURCE)] { set show_source 1 } if {$show_source} { set fh [open $argv0 r] set on 0 set gather "" while {[gets $fh line] > -1} { if [regexp {^#END_CODE} $line] { set on 0 } if {$on} { append gather "$line\n" } if [regexp {^#START_CODE} $line] { set on 1 } } puts stdout $gather exit 0 } proc runit {} { set handle [tty_pipe "autodial" "scat.do"] puts stdout "handle: $handle" } #START_CODE proc tty_clean {handle} { set debug 0 if {$debug} {puts stdout "tty_clean: $handle"} set tty [lindex [split $handle ","] 0] set text [lindex [split $handle ","] 1] set tmpfile [lindex [split $handle ","] 2] set inpipe [lindex [split $handle ","] 3] set outpipe [lindex [split $handle ","] 4] if {$debug} {puts stdout "inpipe: $inpipe"} catch {fileevent $inpipe readable ""} catch {puts $outpipe ""; flush $outpipe} foreach pipe [list $inpipe $outpipe] { catch {set pid [pid $pipe]} if {$debug} {puts stderr "pid: $pid"} if {$pid != ""} { catch {$text insert end "kill -TERM $pid\n"; $text see end; update} catch {exec kill -TERM $pid} catch {$text insert end "kill -KILL $pid\n"; $text see end; update} catch {exec kill -KILL $pid} } if {$debug} {puts stderr "close $pipe"} catch {$text insert end "close $pipe\n"; $text see end; update} catch {close $pipe} } if {$debug} {puts stderr "delete $tmpfile"} catch {$text insert end "file delete -force $tmpfile\n"; $text see end; update} catch {file delete -force $tmpfile} if {$debug} {puts stderr "destroy $tty"} after 500 catch {destroy $tty} } proc tty_handle {which handle} { set tty [lindex [split $handle ","] 0] set text [lindex [split $handle ","] 1] set tmpfile [lindex [split $handle ","] 2] set inpipe [lindex [split $handle ","] 3] set outpipe [lindex [split $handle ","] 4] if {$which == "tty"} { return $tty } elseif {$which == "cr_button"} { return "$tty.f.n" } elseif {$which == "quit_button"} { return "$tty.f.q" } elseif {$which == "text"} { return $text } elseif {$which == "tmpfile"} { return $tmpfile } elseif {$which == "inpipe"} { return $inpipe } elseif {$which == "outpipe"} { return $outpipe } } proc tty_pipe {{name "tty_pipe"} {cmd ""} {script ""}} { set debug 0 global TTY_count if {$cmd == ""} { return; } if ![info exists TTY_count] { set TTY_count 0 } else { incr TTY_count } set t ".tty$TTY_count" set Tty $t catch {destroy $Tty} toplevel $t wm title $t $name set f "$t.f" frame $f set Tty_text $f.t text $Tty_text -bd 2 set tmp "/tmp/tktty.[pid].$TTY_count" exec echo "Starting Pipe: $cmd ..." > $tmp set init "__NOT_INITIALIZED__" set Tty_out_pipe $init catch {set Tty_out_pipe [open "| $cmd >>& $tmp" w]} if {$Tty_out_pipe == $init} { file delete -force $tmp destroy $Tty return "" } set Tty_in_pipe $init catch {set Tty_in_pipe [open "| fast_tail -f $tmp" r]} if {$Tty_in_pipe == $init} { close $Tty_out_pipe file delete -force $tmp destroy $Tty return "" } set handle "$Tty,$Tty_text,$tmp,$Tty_in_pipe,$Tty_out_pipe" button $f.n -text "" -command "tty_send_newline $Tty_out_pipe" button $f.q -text "Dismiss" -command "tty_clean $handle" bind $t "tty_clean $handle" pack $f.t $f.n $f.q -side top -fill x pack $f fconfigure $Tty_out_pipe -buffering line fconfigure $Tty_out_pipe -blocking 0 fconfigure $Tty_in_pipe -blocking 0 if {$script != ""} { fileevent $Tty_in_pipe readable "tty_append $Tty_in_pipe $Tty_text \"$script $handle\"" } else { fileevent $Tty_in_pipe readable "tty_append $Tty_in_pipe $Tty_text" } return $handle } proc tty_append {{pipe ""} {text ""} {script ""}} { if {$pipe == ""} { return } set ret [gets $pipe line] if {$ret < 0} { if [fblocked $pipe] { after 100 set line [read $pipe] } if {$line == ""} { fileevent $pipe readable "" catch {close $pipe} return } } else { set line "$line\n"; } if ![winfo exists $text] { return } set nbell [regsub {} $line "" line] if { $nbell > 0 } { for {set i 0} {$i < $nbell} {incr i} { bell if { $nbell > 1 } { after 500 } } } $text insert end $line $text see end if {$script != ""} { catch {eval $script \$line} } } proc tty_send_newline {{pipe ""}} { if {$pipe == ""} { return } puts $pipe "" flush $pipe } #END_CODE button .b -text TryIt -command "runit" button .q -text Quit -command "destroy .; exit 0" pack .b .q -fill x