stty/stty.go

48 lines
918 B
Go
Raw Normal View History

2025-04-28 22:57:43 +03:00
package stty
import (
"os"
"golang.org/x/sys/unix"
"golang.org/x/term"
2025-04-28 22:57:43 +03:00
"os/exec"
)
2025-05-05 00:00:56 +03:00
type stty interface{
SetBaudRate(portPath string, baudrate int) error
CheckBaudRate(portPath string) (baudrate int, err error)
TestBaudRate(portPath string, baudrate int) error
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
var cmd stty
2025-05-05 00:00:56 +03:00
func CMD() stty {
return cmd
}
2025-05-05 00:00:56 +03:00
func init() {
2025-04-28 22:57:43 +03:00
checkSttyCMD := exec.Command("stty", "--version")
sttyOutput, err := checkSttyCMD.Output()
if err != nil || len(sttyOutput) == 0 {
2025-05-05 00:00:56 +03:00
cmd = &dummy{errStack(ERR_STTY_MISSING(),err)}
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
cmd = &real{}
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
func ValidateTtyFile(filepath string) error {
file, err := os.Open(filepath)
2025-04-28 22:57:43 +03:00
if err != nil {
2025-05-05 00:00:56 +03:00
return errStack(ERR_CANNOT_OPEN_FILE(), err)
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
defer file.Close()
2025-04-28 22:57:43 +03:00
2025-05-05 00:00:56 +03:00
err = unix.Access(filepath, unix.W_OK)
2025-04-28 22:57:43 +03:00
if err != nil {
2025-05-05 00:00:56 +03:00
return errStack(ERR_NO_WRITE_ACCESS(), err)
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
if term.IsTerminal(int(file.Fd())) {
return nil
2025-04-28 22:57:43 +03:00
}
2025-05-05 00:00:56 +03:00
return ERR_NOT_TTY()
2025-04-28 22:57:43 +03:00
}