forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal_linux.go
51 lines (42 loc) · 1.36 KB
/
terminal_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// +build linux
//
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"golang.org/x/sys/unix"
)
const (
termiosIFlagRawTermInvMask = (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
termiosOFlagRawTermInvMask = unix.OPOST
termiosLFlagRawTermInvMask = (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
termiosCFlagRawTermInvMask = unix.PARENB
termiosCFlagRawTermMask = unix.CS8
termiosCcVMinRawTermVal = 1
termiosCcVTimeRawTermVal = 0
)
func setupTerminal(fd int) (*unix.Termios, error) {
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return nil, err
}
var savedTermios unix.Termios
savedTermios = *termios
// Set the terminal in raw mode
termios.Iflag &^= termiosIFlagRawTermInvMask
termios.Oflag &^= termiosOFlagRawTermInvMask
termios.Lflag &^= termiosLFlagRawTermInvMask
termios.Cflag &^= termiosCFlagRawTermInvMask
termios.Cflag |= termiosCFlagRawTermMask
termios.Cc[unix.VMIN] = termiosCcVMinRawTermVal
termios.Cc[unix.VTIME] = termiosCcVTimeRawTermVal
if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
return nil, err
}
return &savedTermios, nil
}
func restoreTerminal(fd int, termios *unix.Termios) error {
return unix.IoctlSetTermios(fd, unix.TCSETS, termios)
}