forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal_darwin.go
49 lines (40 loc) · 1.34 KB
/
terminal_darwin.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
// 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.TIOCGETA)
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.TIOCSETA, termios); err != nil {
return nil, err
}
return &savedTermios, nil
}
func restoreTerminal(fd int, termios *unix.Termios) error {
return unix.IoctlSetTermios(fd, unix.TIOCSETA, termios)
}