simosh 2.04 KB
#!/usr/local/bin/tcl
#
# Copyright (C) 1996-1998 by the Board of Trustees
#    of Leland Stanford Junior University.
# 
# This file is part of the SimOS distribution. 
# See LICENSE file for terms of the license. 
#

set simosfd 0

proc main {argc argv0 argv} {
    global simosfd

    set remoteMode 1
    
    if {$argc == 3} {
        if {[lindex $argv 0] == "-l"} {
            set remoteMode 0
            set machine [lindex $argv 1]
            set port [lindex $argv 2]
        } else {
            puts "usage: $argv0 ?-l? machine port"
            exit
        }
        
    } elseif {$argc == 2} {
        set machine [lindex $argv 0]
        set port [lindex $argv 1]

    } else  {
        puts "usage: $argv0 ?-l? machine port"
        exit
    }

    set simosfd [server_connect $machine $port]

    if $remoteMode {
        cmdloop "remote\# " "\# " sendcommand 
    } else {
        cmdloop "simosh> " "> " eval
    }
    
    close $simosfd
}

proc remote {{command ""}} {
    if {$command == ""} {
        cmdloop "remote\# " "\# " sendcommand 
    } else {
        sendcommand $command
    }
}

proc sendcommand {command} {
    global simosfd

    server_send $simosfd [string length $command]
    server_send -nonewline $simosfd $command

    set len [gets $simosfd]
    set result [read $simosfd $len]

    return $result
}

proc cmdloop {prompt1 prompt2 evalcmd} {
    set command ""
    puts -nonewline $prompt1

    while {[gets stdin line] != -1} {

        if {$command != ""} {
            set command "$command\n$line"
        } else {
            set command $line
        }
        
        if {[info complete $command]} {
            if {$command == "exit"} {
                return
            }
            
            catch {
                $evalcmd $command
            } result
            
            if {$result != ""} {
                puts $result
            }
            
            set command ""
            puts -nonewline $prompt1

        } else {
            puts -nonewline $prompt2
        }
    }
}

main $argc $argv0 $argv