home » torr/wssrv.git
ID: 57770d92abf4ef7cf1c8e6dc799f5c7c1f744058
63 lines — 1K — View raw


package ws

import (
	"fmt"
	"os"

	"golang.org/x/sys/unix"
)

func restore(fd int, t *unix.Termios) error {
	return unix.IoctlSetTermios(fd, unix.TCSETS, t)
}

// setupTerm sets the terminal with raw mode attributes for input, whereby
// 'OPOST' is not set.
func setupTerm() (int, *unix.Termios, error) {
	var (
		err  error
		fd   int
		term *unix.Termios

		oldTerm = &unix.Termios{}
	)

	if fd, err = unix.Open("/dev/tty", unix.O_RDONLY, 0); err != nil {
		return 0, nil, err
	}
	if term, err = unix.IoctlGetTermios(fd, unix.TCGETS); err != nil {
		return 0, nil, err
	}
	*oldTerm = *term

	term.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP |
		unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
	term.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
	term.Cflag &^= unix.CSIZE | unix.PARENB
	term.Cflag |= unix.CS8
	term.Cc[unix.VMIN] = 1
	term.Cc[unix.VTIME] = 0

	if err = unix.IoctlSetTermios(fd, unix.TCSETS, term); err != nil {
		return 0, nil, err
	}
	return fd, oldTerm, nil
}

func checkFile(name string) error {
	var (
		err  error
		stat os.FileInfo
	)

	if stat, err = os.Stat(name); err != nil {
		return fmt.Errorf("unable to check metadata for file '%s': %v", name, err)
	}
	if !stat.Mode().IsRegular() {
		return fmt.Errorf("file '%s' is not regular")
	}
	if stat.Mode()&0444 == 0 {
		return fmt.Errorf("file '%s' is not readable")
	}
	return nil
}