2022-01-11 04:00:58 +00:00
|
|
|
// Copyright Terence J. Boldt (c)2020-2022
|
2021-11-21 19:56:13 +00:00
|
|
|
// Use of this source code is governed by an MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-01-11 04:00:58 +00:00
|
|
|
// This file contains the handler for executing Linux shell
|
2021-11-21 19:56:13 +00:00
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2021-11-25 23:45:56 +00:00
|
|
|
"fmt"
|
2021-11-27 03:51:52 +00:00
|
|
|
"io"
|
2021-11-21 19:56:13 +00:00
|
|
|
"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",
|
2021-11-27 05:07:14 +00:00
|
|
|
"COLUMNS=79",
|
2021-11-21 19:56:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ws pty.Winsize
|
2021-11-27 05:07:14 +00:00
|
|
|
ws.Cols = 79
|
2021-11-21 19:56:13 +00:00
|
|
|
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)
|
|
|
|
|
2021-11-27 03:51:52 +00:00
|
|
|
go ptyIn(ptmx, outputComplete, inputComplete, userCancelled)
|
|
|
|
go ptyOut(ptmx, outputComplete, userCancelled)
|
2021-11-21 19:56:13 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-outputComplete:
|
2021-11-25 23:45:56 +00:00
|
|
|
ptmx.Close()
|
2021-11-21 19:56:13 +00:00
|
|
|
comm.WriteByte(0)
|
|
|
|
return
|
|
|
|
case <-userCancelled:
|
2021-11-25 23:45:56 +00:00
|
|
|
fmt.Printf("User cancelled, killing process\n")
|
|
|
|
ptmx.Close()
|
2021-11-21 19:56:13 +00:00
|
|
|
cmd.Process.Kill()
|
2021-11-25 23:45:56 +00:00
|
|
|
comm.WriteByte(0)
|
2021-11-21 19:56:13 +00:00
|
|
|
return
|
|
|
|
case <-inputComplete:
|
|
|
|
cmd.Wait()
|
|
|
|
comm.WriteByte(0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 03:51:52 +00:00
|
|
|
|
|
|
|
func ptyOut(stdout io.ReadCloser, outputComplete chan bool, userCancelled chan bool) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-userCancelled:
|
|
|
|
fmt.Printf("User Cancelled stdout\n")
|
|
|
|
stdout.Close()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
bb := make([]byte, 1)
|
|
|
|
n, err := stdout.Read(bb)
|
|
|
|
if err != nil {
|
|
|
|
stdout.Close()
|
|
|
|
outputComplete <- true
|
|
|
|
fmt.Printf("stdout closed\n")
|
|
|
|
return
|
2022-01-11 00:28:41 +00:00
|
|
|
} else if n > 0 {
|
2021-11-27 03:51:52 +00:00
|
|
|
b := bb[0]
|
|
|
|
comm.SendCharacter(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptyIn(stdin io.WriteCloser, done chan bool, inputComplete chan bool, userCancelled chan bool) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
stdin.Close()
|
|
|
|
inputComplete <- true
|
|
|
|
fmt.Printf("stdin closed\n")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
s, err := comm.ReadCharacter()
|
|
|
|
if err == nil {
|
|
|
|
if s == string(byte(0x00)) {
|
|
|
|
stdin.Close()
|
|
|
|
userCancelled <- true
|
|
|
|
fmt.Printf("\nUser cancelled stdin\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.WriteString(stdin, string(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|