Apple2-IO-RPi/RaspberryPi/apple2driver/handlers/shell.go
Terence Boldt 453a644a0b
Add shell command with TTY support (#44)
* Add tty support for shell

* Update shell command
2021-11-21 14:56:13 -05:00

60 lines
1.1 KiB
Go
Executable File

// Copyright Terence J. Boldt (c)2020-2021
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file is contains the handler for executing Linux shell
package handlers
import (
"os"
"os/exec"
"github.com/creack/pty"
)
// ShellCommand handles requests for the Apple II executing a Linux shell
func ShellCommand() {
cmd := exec.Command("bash", "-i")
cmd.Env = append(os.Environ(),
"TERM=vt100",
"LINES=24",
"COLUMNS=80",
)
var ws pty.Winsize
ws.Cols = 80
ws.Rows = 24
ws.X = 0
ws.Y = 0
ptmx, _ := pty.StartWithSize(cmd, &ws)
defer func() { _ = ptmx.Close() }()
outputComplete := make(chan bool)
inputComplete := make(chan bool)
userCancelled := make(chan bool)
go getStdin(ptmx, outputComplete, inputComplete, userCancelled)
go getStdout(ptmx, outputComplete, userCancelled)
for {
select {
case <-outputComplete:
outputComplete <- true
cmd.Wait()
comm.WriteByte(0)
return
case <-userCancelled:
userCancelled <- true
comm.WriteString("^C\r")
cmd.Process.Kill()
return
case <-inputComplete:
cmd.Wait()
comm.WriteByte(0)
return
}
}
}