Initial commit

This commit is contained in:
Liam Galvin 2018-06-27 17:29:16 +01:00
commit 785ac2b60e
4 changed files with 120 additions and 0 deletions

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# Terminal
## Platform Support
| Linux | Yes
| MacOSX | Not yet...
| Windows | Not yet...

3
gui/gui.go Normal file
View File

@ -0,0 +1,3 @@
package gui

21
main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"io"
"os"
"gitlab.com/liamg/terminal/pty"
)
func main() {
pty, err := pty.NewPtyWithShell()
if err != nil {
panic(err)
}
go io.Copy(pty, os.Stdin)
io.Copy(os.Stdout, pty)
// return pty, err
}

89
pty/pty.go Normal file
View File

@ -0,0 +1,89 @@
package pty
import (
"fmt"
"os"
"os/exec"
"syscall"
"unsafe"
)
func NewPtyWithShell() (*os.File, error) {
pty, tty, err := open()
if err != nil {
return nil, err
}
defer tty.Close()
shell := exec.Command("/bin/bash")
shell.Stdout = tty
shell.Stdin = tty
shell.Stderr = tty
shell.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
if err := shell.Start(); err != nil {
pty.Close()
return nil, err
}
return pty, nil
}
// todo: port these for darwin/windows:
func open() (*os.File, *os.File, error) {
pty, err := getpt()
if err != nil {
panic(err)
}
ptsName, err := ptsname(pty)
if err != nil {
panic(err)
}
// err = grantpt(pty)
// if err != nil {
// return nil, nil, err
// }
err = unlockpt(pty)
if err != nil {
return nil, nil, err
}
tty, err := os.OpenFile(ptsName, os.O_RDWR, 0)
if err != nil {
return nil, nil, err
}
return pty, tty, nil
}
func getpt() (file *os.File, err error) {
return os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
}
func ptsname(file *os.File) (name string, err error) {
n, err := ioctl(file, syscall.TIOCGPTN, 0)
return fmt.Sprintf("/dev/pts/%d", n), err
}
func ioctl(file *os.File, command uint, arg int) (int, error) {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(file.Fd()),
uintptr(command), uintptr(unsafe.Pointer(&arg)))
if err != 0 {
return 0, fmt.Errorf("Error no %d", err)
}
return arg, nil
}
/*
func grantpt(f *os.File) error {
_, err := ioctl(f, syscall.TIOCPTYGRANT, 0)
syscall.SYS
return err
}
*/
func unlockpt(f *os.File) error {
_, err := ioctl(f, syscall.TIOCSPTLCK, 0)
return err
}