vninteractive.tcl 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#http://wiki.tcl.tk/1968
#from Jeffrey Hobbs

 puts -nonewline "vnman (0) % "
 if {!$tcl_interactive} {
     set long_command ""
     set verbose_history 0
     if {![catch {rename unknown tcl_unknown}]} {
 	proc unknown {cmdname args} {
 	    if {[regexp "^!..*" $cmdname]} {
 		banghist [string range $cmdname 1 end]
 	    } else {
 		tcl_unknown $cmdname $args
 	    }
 	}
     }

     proc banghist {val} {
 	global verbose_history
 	if {![string compare $val "!"]} {set val ""}
 	if {$verbose_history} {puts "[history event $val]"}
 	history redo $val
     }

     if {![info exists tcl_prompt1]} {
 	set tcl_prompt1 {puts -nonewline "vnman ([history nextid]) % "}
     }
     proc read_stdin {} {
 	global eventLoop tcl_prompt1 long_command
 	set l [gets stdin]
 	if {[eof stdin]} {
 	    set eventLoop "done"     ;# terminate the vwait eventloop
 	} else {
 	    if {[string compare $l {}]} {
 		append long_command "\n$l"
 		set l $long_command
 		if {[info complete $l]} {
 		    if {[catch {uplevel \#0 history add [list $l] exec} err]} {
 			puts stderr $err
 		    } elseif {[string compare $err {}]} {
 			puts $err
 		    }
 		    set long_command ""
 		    catch $tcl_prompt1
 		} else {
 		    puts -nonewline "> "
 		}
 	    } elseif {![string compare $long_command {}]} {
 		catch $tcl_prompt1
 	    } else {
 		puts -nonewline "> "
 	    }
 	    flush stdout
 	}
     }

     # set up our keyboard read event handler:
     # Vector stdin data to the socket
     fileevent stdin readable read_stdin

     catch $tcl_prompt1
     flush stdout
     # wait for and handle or stdin events...
     #vwait eventLoop
 }