diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-25 20:56:26 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2012-01-25 20:56:26 +0000 |
commit | df1304ee03f41aed179545d1e8b4684cfd22bbdf (patch) | |
tree | c68d6b2a9f5b82a23171b0a488a4b7e5c63ad860 /libgo/go/exp/ssh/server_terminal.go | |
parent | 3be18e47c33b61365786831e0f967f42b09922c9 (diff) | |
download | gcc-df1304ee03f41aed179545d1e8b4684cfd22bbdf.zip gcc-df1304ee03f41aed179545d1e8b4684cfd22bbdf.tar.gz gcc-df1304ee03f41aed179545d1e8b4684cfd22bbdf.tar.bz2 |
libgo: Update to weekly.2012-01-15.
From-SVN: r183539
Diffstat (limited to 'libgo/go/exp/ssh/server_terminal.go')
-rw-r--r-- | libgo/go/exp/ssh/server_terminal.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/libgo/go/exp/ssh/server_terminal.go b/libgo/go/exp/ssh/server_terminal.go new file mode 100644 index 0000000..708a915 --- /dev/null +++ b/libgo/go/exp/ssh/server_terminal.go @@ -0,0 +1,81 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package ssh + +// A Terminal is capable of parsing and generating virtual terminal +// data from an SSH client. +type Terminal interface { + ReadLine() (line string, err error) + SetSize(x, y int) + Write([]byte) (int, error) +} + +// ServerTerminal contains the state for running a terminal that is capable of +// reading lines of input. +type ServerTerminal struct { + Term Terminal + Channel Channel +} + +// parsePtyRequest parses the payload of the pty-req message and extracts the +// dimensions of the terminal. See RFC 4254, section 6.2. +func parsePtyRequest(s []byte) (width, height int, ok bool) { + _, s, ok = parseString(s) + if !ok { + return + } + width32, s, ok := parseUint32(s) + if !ok { + return + } + height32, _, ok := parseUint32(s) + width = int(width32) + height = int(height32) + if width < 1 { + ok = false + } + if height < 1 { + ok = false + } + return +} + +func (ss *ServerTerminal) Write(buf []byte) (n int, err error) { + return ss.Term.Write(buf) +} + +// ReadLine returns a line of input from the terminal. +func (ss *ServerTerminal) ReadLine() (line string, err error) { + for { + if line, err = ss.Term.ReadLine(); err == nil { + return + } + + req, ok := err.(ChannelRequest) + if !ok { + return + } + + ok = false + switch req.Request { + case "pty-req": + var width, height int + width, height, ok = parsePtyRequest(req.Payload) + ss.Term.SetSize(width, height) + case "shell": + ok = true + if len(req.Payload) > 0 { + // We don't accept any commands, only the default shell. + ok = false + } + case "env": + ok = true + } + if req.WantReply { + ss.Channel.AckRequest(ok) + } + } + panic("unreachable") +} |