-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fef0545
commit d43ebe7
Showing
9 changed files
with
230 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package testscript | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func ptyName(f *os.File) (string, error) { | ||
const IOC_PARAM_SHIFT = 13 | ||
const IOC_PARAM_MASK = (1 << IOC_PARAM_SHIFT) - 1 | ||
const TIOCPTYGNAME_PARM_LEN = (syscall.TIOCPTYGNAME >> 16) & IOC_PARAM_MASK | ||
out := make([]byte, TIOCPTYGNAME_PARM_LEN) | ||
|
||
err := ioctl(f, syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&out[0]))) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
i := bytes.IndexByte(out, 0x00) | ||
return string(out[:i]), nil | ||
} | ||
|
||
func ptyGrant(f *os.File) error { | ||
return ioctl(f, syscall.TIOCPTYGRANT, 0) | ||
} | ||
|
||
func ptyUnlock(f *os.File) error { | ||
return ioctl(f, syscall.TIOCPTYUNLK, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package testscript | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func ptyName(f *os.File) (string, error) { | ||
var out uint | ||
err := ioctl(f, syscall.TIOCGPTN, uintptr(unsafe.Pointer(&out))) | ||
if err != nil { | ||
return "", err | ||
} | ||
return "/dev/pts/" + strconv.Itoa(int(out)), nil | ||
} | ||
|
||
func ptyGrant(f *os.File) error { | ||
return nil | ||
} | ||
|
||
func ptyUnlock(f *os.File) error { | ||
var zero int | ||
return ioctl(f, syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&zero))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package testscript | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
func setCtty(cmd *exec.Cmd, tty *os.File) error { | ||
cmd.SysProcAttr = &syscall.SysProcAttr{} | ||
cmd.SysProcAttr.Setctty = true | ||
cmd.SysProcAttr.Setsid = true | ||
cmd.SysProcAttr.Ctty = 3 | ||
cmd.ExtraFiles = []*os.File{tty} | ||
return nil | ||
} | ||
|
||
func ptyOpen() (pty, tty *os.File, err error) { | ||
p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
name, err := ptyName(p) | ||
if err != nil { | ||
p.Close() | ||
return nil, nil, err | ||
} | ||
|
||
if err := ptyGrant(p); err != nil { | ||
p.Close() | ||
return nil, nil, err | ||
} | ||
|
||
if err := ptyUnlock(p); err != nil { | ||
p.Close() | ||
return nil, nil, err | ||
} | ||
|
||
t, err := os.OpenFile(name, os.O_RDWR|syscall.O_NOCTTY, 0) | ||
if err != nil { | ||
p.Close() | ||
return nil, nil, err | ||
} | ||
|
||
return p, t, nil | ||
} | ||
|
||
func ioctl(f *os.File, cmd, ptr uintptr) error { | ||
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), cmd, ptr) | ||
if err != 0 { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package testscript | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func setCtty(cmd *exec.Cmd, tty *os.File) error { | ||
return errors.New("setctty unsupported on Windows") | ||
} | ||
|
||
func ptyOpen() (pty, tty *os.File, err error) { | ||
return nil, nil, errors.New("pty unsupported on Windows") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters