2018-04-25 08:50:47 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-03-25 10:56:18 +00:00
|
|
|
"os"
|
2018-04-25 08:50:47 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var devPrefixes = [...]string{"/dev/pts/", "/dev/"}
|
|
|
|
|
|
|
|
func ttyname() string {
|
|
|
|
var stderr syscall.Stat_t
|
|
|
|
if syscall.Fstat(2, &stderr) != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range devPrefixes {
|
|
|
|
files, err := ioutil.ReadDir(prefix)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if stat, ok := file.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {
|
|
|
|
return prefix + file.Name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-03-25 10:56:18 +00:00
|
|
|
|
|
|
|
// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin
|
|
|
|
func TtyIn() *os.File {
|
|
|
|
in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
tty := ttyname()
|
|
|
|
if len(tty) > 0 {
|
|
|
|
if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os.Stdin
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|