Ejemplo n.º 1
0
func (o *oflagSetter) Set(pty *os.File, termios *syscall.Termios, value uint32) error {
	if value == 0 {
		termios.Oflag &^= o.Flag
	} else {
		termios.Oflag |= o.Flag
	}

	return SetAttr(pty, termios)
}
Ejemplo n.º 2
0
func (s *Screen) Init() error {
	var err error
	ttyIn, err = os.OpenFile("/dev/tty", os.O_RDWR, 0666)
	if err != nil {
		return err
	}
	ttyOut, err = os.OpenFile("/dev/tty", os.O_RDONLY, 0666)
	if err != nil {
		return err
	}

	orig = new(syscall.Termios)
	err = tcgetattr(ttyIn.Fd(), orig)
	if err != nil {
		return err
	}

	tios := new(syscall.Termios)
	*tios = *orig
	tios.Iflag &= ^uint32(syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG)
	tios.Iflag &= ^uint32(syscall.BRKINT | syscall.ICRNL | syscall.ISTRIP | syscall.IXON)
	tios.Cflag &= ^uint32(syscall.CSIZE | syscall.PARENB)
	tios.Cflag |= syscall.CS8
	tios.Oflag &= ^uint32(syscall.OPOST)
	tios.Cc[syscall.VMIN] = 1
	tios.Cc[syscall.VTIME] = 0
	err = tcsetattr(ttyIn.Fd(), tios)
	if err != nil {
		return err
	}
	s.HideCursor()
	s.Clear()
	return nil
}
Ejemplo n.º 3
0
Archivo: termios.go Proyecto: pkg/term
// Cfmakeraw modifies attr for raw mode.
func Cfmakeraw(attr *syscall.Termios) {
	attr.Iflag &^= syscall.BRKINT | syscall.ICRNL | syscall.INPCK | syscall.ISTRIP | syscall.IXON
	attr.Oflag &^= syscall.OPOST
	attr.Cflag &^= syscall.CSIZE | syscall.PARENB
	attr.Cflag |= syscall.CS8
	attr.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG
	attr.Cc[syscall.VMIN] = 1
	attr.Cc[syscall.VTIME] = 0
}
Ejemplo n.º 4
0
func OpenSerial(device string, baud uint) (s *serial, err error) {
	myBaud := getBaud(baud)
	if myBaud == 0 {
		err = errInvalidBaud
		return
	}
	fd, err := syscall.Open(
		device,
		os.O_RDWR|syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NDELAY|syscall.O_NONBLOCK,
		0666)
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			syscall.Close(fd)
		}
	}()
	term := syscall.Termios{}
	if err = sys.Ioctl(uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&term))); err != nil {
		return
	}
	term.Ispeed = myBaud
	term.Ospeed = myBaud
	term.Cflag |= (syscall.CLOCAL | syscall.CREAD)
	term.Cflag = uint32(int32(term.Cflag) & ^syscall.PARENB & ^syscall.CSTOPB & ^syscall.CSIZE)
	term.Cflag |= syscall.CS8
	term.Lflag = uint32(int32(term.Lflag) & ^(syscall.ICANON | syscall.ECHO | syscall.ECHOE | syscall.ISIG))
	term.Oflag = uint32(int32(term.Oflag) & ^syscall.OPOST)
	term.Cc[syscall.VMIN] = 0
	term.Cc[syscall.VTIME] = 100
	if err = sys.Ioctl(uintptr(fd), syscall.TCSETS, uintptr(unsafe.Pointer(&term))); err != nil {
		return
	}
	status := 0
	if err = sys.Ioctl(uintptr(fd), syscall.TIOCMGET, uintptr(unsafe.Pointer(&status))); err != nil {
		return
	}
	status |= syscall.TIOCM_DTR | syscall.TIOCM_RTS
	if err = sys.Ioctl(uintptr(fd), syscall.TIOCMSET, uintptr(unsafe.Pointer(&status))); err != nil {
		return
	}

	s = &serial{uintptr(fd)}
	runtime.SetFinalizer(s, func(this *serial) {
		this.Close()
	})
	return
}
Ejemplo n.º 5
0
func setRawMode(settings *syscall.Termios) {
	// Set local mode
	settings.Cflag |= termiosMask(syscall.CREAD | syscall.CLOCAL)

	// Set raw mode
	settings.Lflag &= ^termiosMask(syscall.ICANON | syscall.ECHO | syscall.ECHOE | syscall.ECHOK |
		syscall.ECHONL | syscall.ECHOCTL | syscall.ECHOPRT | syscall.ECHOKE | syscall.ISIG | syscall.IEXTEN)
	settings.Iflag &= ^termiosMask(syscall.IXON | syscall.IXOFF | syscall.IXANY | syscall.INPCK |
		syscall.IGNPAR | syscall.PARMRK | syscall.ISTRIP | syscall.IGNBRK | syscall.BRKINT | syscall.INLCR |
		syscall.IGNCR | syscall.ICRNL | tcIUCLC)
	settings.Oflag &= ^termiosMask(syscall.OPOST)

	// Block reads until at least one char is available (no timeout)
	settings.Cc[syscall.VMIN] = 1
	settings.Cc[syscall.VTIME] = 0
}
Ejemplo n.º 6
0
// saneTerminal sets the necessary tty_ioctl(4)s to ensure that a pty pair
// created by us acts normally. In particular, a not-very-well-known default of
// Linux unix98 ptys is that they have +onlcr by default. While this isn't a
// problem for terminal emulators, because we relay data from the terminal we
// also relay that funky line discipline.
func saneTerminal(terminal *os.File) error {
	// Go doesn't have a wrapper for any of the termios ioctls.
	var termios syscall.Termios

	if err := ioctl(terminal.Fd(), syscall.TCGETS, uintptr(unsafe.Pointer(&termios))); err != nil {
		return fmt.Errorf("ioctl(tty, tcgets): %s", err.Error())
	}

	// Set -onlcr so we don't have to deal with \r.
	termios.Oflag &^= syscall.ONLCR

	if err := ioctl(terminal.Fd(), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != nil {
		return fmt.Errorf("ioctl(tty, tcsets): %s", err.Error())
	}

	return nil
}
Ejemplo n.º 7
0
func setRawMode(settings *syscall.Termios, mode *Mode) {
	// Set local mode
	settings.Cflag |= termiosMask(syscall.CREAD | syscall.CLOCAL)

	// Set raw mode
	settings.Lflag &= ^termiosMask(syscall.ICANON | syscall.ECHO | syscall.ECHOE | syscall.ECHOK |
		syscall.ECHONL | syscall.ECHOCTL | syscall.ECHOPRT | syscall.ECHOKE | syscall.ISIG | syscall.IEXTEN)
	settings.Iflag &= ^termiosMask(syscall.IXON | syscall.IXOFF | syscall.IXANY | syscall.INPCK |
		syscall.IGNPAR | syscall.PARMRK | syscall.ISTRIP | syscall.IGNBRK | syscall.BRKINT | syscall.INLCR |
		syscall.IGNCR | syscall.ICRNL | tc_IUCLC)
	settings.Oflag &= ^termiosMask(syscall.OPOST)

	if mode.Vmin == 0 && mode.Vtimeout == 0 {
		// Switch to default mode
		// Block reads until at least one char is available (no timeout)
		mode.Vmin = 1
	}

	settings.Cc[syscall.VMIN] = mode.Vmin
	settings.Cc[syscall.VTIME] = mode.Vtimeout
}