Apple2-IO-RPi/RaspberryPi/apple2driver/handlers/shell.go
Terence Boldt 5910d66fa2
Auto boot with recovery and improved VT100 support (#45)
* Improve vt100 support to make vim work

* Fix home and add line clear

* Auto boot with recovery
2021-11-25 18:45:56 -05:00

61 lines
1.2 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 (
"fmt"
"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:
ptmx.Close()
comm.WriteByte(0)
return
case <-userCancelled:
fmt.Printf("User cancelled, killing process\n")
ptmx.Close()
cmd.Process.Kill()
comm.WriteByte(0)
return
case <-inputComplete:
cmd.Wait()
comm.WriteByte(0)
return
}
}
}