# lib/remote.tcl
#
# --remote untunnel code.
#
# Copyright (C) 2003, 2004
# Andy Goth <unununium@openverse.com>
#
# This code is available under the GNU General Public License; see COPYING.

# Remote mode.  This process was started by rsh/ssh, which in turn was started
# by the user command on the source host.
proc main {} {
    global untunnel_cmd

    # Fork another untunnel process.  This is necessary to allow rsh/ssh to
    # terminate while leaving the untunnel server up.
    set chan [open "|$untunnel_cmd --server < /dev/null 2> un.server.log" r]

    # Pass on any connection information to stdout so that the original
    # untunnel can see it via the rsh/ssh link.  Then break said link by
    # terminating.
    while {1} {
        gets $chan line
        if {[eof $chan] || $line eq "end"} {
            fconfigure $chan -blocking 0
            if {[catch {close $chan} result]} {
                puts "fatal [escape $result]"
            } elseif {$line ne "end"} {
                puts "fatal Unexpected EOF on server untunnel"
            }
            puts "end"
            break
        } else {
            puts $line
        }
    }
}

# vim: set ts=4 sts=4 sw=4 tw=80 et:

