2022-03-29 12:20:33 +00:00
|
|
|
//go:build !windows
|
2018-04-25 08:50:47 +00:00
|
|
|
|
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2021-03-25 10:56:18 +00:00
|
|
|
"os"
|
2024-05-23 11:08:20 +00:00
|
|
|
"sync/atomic"
|
2018-04-25 08:50:47 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var devPrefixes = [...]string{"/dev/pts/", "/dev/"}
|
|
|
|
|
2024-05-23 11:08:20 +00:00
|
|
|
var tty atomic.Value
|
|
|
|
|
2018-04-25 08:50:47 +00:00
|
|
|
func ttyname() string {
|
2024-05-23 11:08:20 +00:00
|
|
|
if cached := tty.Load(); cached != nil {
|
|
|
|
return cached.(string)
|
|
|
|
}
|
|
|
|
|
2018-04-25 08:50:47 +00:00
|
|
|
var stderr syscall.Stat_t
|
|
|
|
if syscall.Fstat(2, &stderr) != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range devPrefixes {
|
2023-07-16 08:14:22 +00:00
|
|
|
files, err := os.ReadDir(prefix)
|
2018-04-25 08:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2023-07-16 08:14:22 +00:00
|
|
|
info, err := file.Info()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if stat, ok := info.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {
|
2024-05-23 11:08:20 +00:00
|
|
|
value := prefix + file.Name()
|
|
|
|
tty.Store(value)
|
|
|
|
return value
|
2018-04-25 08:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-03-25 10:56:18 +00:00
|
|
|
|
2024-05-23 11:08:20 +00:00
|
|
|
// TtyIn returns terminal device to read user input
|
|
|
|
func TtyIn() (*os.File, error) {
|
|
|
|
return openTtyIn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TtyIn returns terminal device to write to
|
|
|
|
func TtyOut() (*os.File, error) {
|
|
|
|
return openTtyOut()
|
2021-03-25 10:56:18 +00:00
|
|
|
}
|