mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-14 01:14:25 +00:00
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
|
// +build !windows
|
||
|
|
||
|
package pty
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"syscall"
|
||
|
)
|
||
|
|
||
|
// Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
|
||
|
// and c.Stderr, calls c.Start, and returns the File of the tty's
|
||
|
// corresponding pty.
|
||
|
func Start(c *exec.Cmd) (pty *os.File, err error) {
|
||
|
return StartWithSize(c, nil)
|
||
|
}
|
||
|
|
||
|
// StartWithSize assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout,
|
||
|
// and c.Stderr, calls c.Start, and returns the File of the tty's
|
||
|
// corresponding pty.
|
||
|
//
|
||
|
// This will resize the pty to the specified size before starting the command
|
||
|
func StartWithSize(c *exec.Cmd, sz *Winsize) (pty *os.File, err error) {
|
||
|
pty, tty, err := Open()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer tty.Close()
|
||
|
if sz != nil {
|
||
|
err = Setsize(pty, sz)
|
||
|
if err != nil {
|
||
|
pty.Close()
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
if c.Stdout == nil {
|
||
|
c.Stdout = tty
|
||
|
}
|
||
|
if c.Stderr == nil {
|
||
|
c.Stderr = tty
|
||
|
}
|
||
|
if c.Stdin == nil {
|
||
|
c.Stdin = tty
|
||
|
}
|
||
|
if c.SysProcAttr == nil {
|
||
|
c.SysProcAttr = &syscall.SysProcAttr{}
|
||
|
}
|
||
|
c.SysProcAttr.Setctty = true
|
||
|
c.SysProcAttr.Setsid = true
|
||
|
c.SysProcAttr.Ctty = int(tty.Fd())
|
||
|
err = c.Start()
|
||
|
if err != nil {
|
||
|
pty.Close()
|
||
|
return nil, err
|
||
|
}
|
||
|
return pty, err
|
||
|
}
|