a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
1package ssh 2 3import ( 4 "bytes" 5 "errors" 6 "io" 7 "os/exec" 8) 9 10// ErrUnsupported is returned when the platform does not support PTY. 11var ErrUnsupported = errors.New("pty unsupported") 12 13// NewPtyWriter creates a writer that handles when the session has a active 14// PTY, replacing the \n with \r\n. 15func NewPtyWriter(w io.Writer) io.Writer { 16 return ptyWriter{ 17 w: w, 18 } 19} 20 21var _ io.Writer = ptyWriter{} 22 23type ptyWriter struct { 24 w io.Writer 25} 26 27func (w ptyWriter) Write(p []byte) (int, error) { 28 m := len(p) 29 // normalize \n to \r\n when pty is accepted. 30 // this is a hardcoded shortcut since we don't support terminal modes. 31 p = bytes.Replace(p, []byte{'\n'}, []byte{'\r', '\n'}, -1) 32 p = bytes.Replace(p, []byte{'\r', '\r', '\n'}, []byte{'\r', '\n'}, -1) 33 n, err := w.w.Write(p) 34 if n > m { 35 n = m 36 } 37 return n, err 38} 39 40// NewPtyReadWriter return an io.ReadWriter that delegates the read to the 41// given io.ReadWriter, and the writes to a ptyWriter. 42func NewPtyReadWriter(rw io.ReadWriter) io.ReadWriter { 43 return readWriterDelegate{ 44 w: NewPtyWriter(rw), 45 r: rw, 46 } 47} 48 49var _ io.ReadWriter = readWriterDelegate{} 50 51type readWriterDelegate struct { 52 w io.Writer 53 r io.Reader 54} 55 56func (rw readWriterDelegate) Read(p []byte) (n int, err error) { 57 return rw.r.Read(p) 58} 59 60func (rw readWriterDelegate) Write(p []byte) (n int, err error) { 61 return rw.w.Write(p) 62} 63 64// Start starts a *exec.Cmd attached to the Session. If a PTY is allocated, 65// it will use that for I/O. 66// On Windows, the process execution lifecycle is not managed by Go and has to 67// be managed manually. This means that c.Wait() won't work. 68// See https://github.com/charmbracelet/x/blob/main/exp/term/conpty/conpty_windows.go 69func (p *Pty) Start(c *exec.Cmd) error { 70 return p.start(c) 71}