Browse code

Merge pull request #3330 from shykes/pkg-term

Move utility package 'term' to pkg/term

Victor Vieux authored on 2013/12/24 09:11:42
Showing 12 changed files
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/dotcloud/docker/auth"
14 14
 	"github.com/dotcloud/docker/engine"
15 15
 	"github.com/dotcloud/docker/registry"
16
-	"github.com/dotcloud/docker/term"
16
+	"github.com/dotcloud/docker/pkg/term"
17 17
 	"github.com/dotcloud/docker/utils"
18 18
 	"io"
19 19
 	"io/ioutil"
... ...
@@ -8,7 +8,7 @@ import (
8 8
 	"github.com/dotcloud/docker/archive"
9 9
 	"github.com/dotcloud/docker/graphdriver"
10 10
 	"github.com/dotcloud/docker/mount"
11
-	"github.com/dotcloud/docker/term"
11
+	"github.com/dotcloud/docker/pkg/term"
12 12
 	"github.com/dotcloud/docker/utils"
13 13
 	"github.com/kr/pty"
14 14
 	"io"
... ...
@@ -5,7 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker"
7 7
 	"github.com/dotcloud/docker/engine"
8
-	"github.com/dotcloud/docker/term"
8
+	"github.com/dotcloud/docker/pkg/term"
9 9
 	"github.com/dotcloud/docker/utils"
10 10
 	"io"
11 11
 	"io/ioutil"
12 12
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+Guillaume Charmes <guillaume@dotcloud.com> (@creack)
1
+Solomon Hykes <solomon@dotcloud.com> (@shykes)
0 2
new file mode 100644
... ...
@@ -0,0 +1,103 @@
0
+package term
1
+
2
+import (
3
+	"errors"
4
+	"os"
5
+	"os/signal"
6
+	"syscall"
7
+	"unsafe"
8
+)
9
+
10
+var (
11
+	ErrInvalidState = errors.New("Invalid terminal state")
12
+)
13
+
14
+type State struct {
15
+	termios Termios
16
+}
17
+
18
+type Winsize struct {
19
+	Height uint16
20
+	Width  uint16
21
+	x      uint16
22
+	y      uint16
23
+}
24
+
25
+func GetWinsize(fd uintptr) (*Winsize, error) {
26
+	ws := &Winsize{}
27
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
28
+	// Skipp errno = 0
29
+	if err == 0 {
30
+		return ws, nil
31
+	}
32
+	return ws, err
33
+}
34
+
35
+func SetWinsize(fd uintptr, ws *Winsize) error {
36
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
37
+	// Skipp errno = 0
38
+	if err == 0 {
39
+		return nil
40
+	}
41
+	return err
42
+}
43
+
44
+// IsTerminal returns true if the given file descriptor is a terminal.
45
+func IsTerminal(fd uintptr) bool {
46
+	var termios Termios
47
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&termios)))
48
+	return err == 0
49
+}
50
+
51
+// Restore restores the terminal connected to the given file descriptor to a
52
+// previous state.
53
+func RestoreTerminal(fd uintptr, state *State) error {
54
+	if state == nil {
55
+		return ErrInvalidState
56
+	}
57
+	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)))
58
+	if err != 0 {
59
+		return err
60
+	}
61
+	return nil
62
+}
63
+
64
+func SaveState(fd uintptr) (*State, error) {
65
+	var oldState State
66
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
67
+		return nil, err
68
+	}
69
+
70
+	return &oldState, nil
71
+}
72
+
73
+func DisableEcho(fd uintptr, state *State) error {
74
+	newState := state.termios
75
+	newState.Lflag &^= syscall.ECHO
76
+
77
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
78
+		return err
79
+	}
80
+	handleInterrupt(fd, state)
81
+	return nil
82
+}
83
+
84
+func SetRawTerminal(fd uintptr) (*State, error) {
85
+	oldState, err := MakeRaw(fd)
86
+	if err != nil {
87
+		return nil, err
88
+	}
89
+	handleInterrupt(fd, oldState)
90
+	return oldState, err
91
+}
92
+
93
+func handleInterrupt(fd uintptr, state *State) {
94
+	sigchan := make(chan os.Signal, 1)
95
+	signal.Notify(sigchan, os.Interrupt)
96
+
97
+	go func() {
98
+		_ = <-sigchan
99
+		RestoreTerminal(fd, state)
100
+		os.Exit(0)
101
+	}()
102
+}
0 103
new file mode 100644
... ...
@@ -0,0 +1,54 @@
0
+package term
1
+
2
+import (
3
+	"syscall"
4
+	"unsafe"
5
+)
6
+
7
+const (
8
+	getTermios = syscall.TIOCGETA
9
+	setTermios = syscall.TIOCSETA
10
+
11
+	ECHO   = 0x00000008
12
+	ONLCR  = 0x2
13
+	ISTRIP = 0x20
14
+	INLCR  = 0x40
15
+	ISIG   = 0x80
16
+	IGNCR  = 0x80
17
+	ICANON = 0x100
18
+	ICRNL  = 0x100
19
+	IXOFF  = 0x400
20
+	IXON   = 0x200
21
+)
22
+
23
+type Termios struct {
24
+	Iflag  uint64
25
+	Oflag  uint64
26
+	Cflag  uint64
27
+	Lflag  uint64
28
+	Cc     [20]byte
29
+	Ispeed uint64
30
+	Ospeed uint64
31
+}
32
+
33
+// MakeRaw put the terminal connected to the given file descriptor into raw
34
+// mode and returns the previous state of the terminal so that it can be
35
+// restored.
36
+func MakeRaw(fd uintptr) (*State, error) {
37
+	var oldState State
38
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
39
+		return nil, err
40
+	}
41
+
42
+	newState := oldState.termios
43
+	newState.Iflag &^= (ISTRIP | INLCR | IGNCR | IXON | IXOFF)
44
+	newState.Iflag |= ICRNL
45
+	newState.Oflag |= ONLCR
46
+	newState.Lflag &^= (ECHO | ICANON | ISIG)
47
+
48
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 {
49
+		return nil, err
50
+	}
51
+
52
+	return &oldState, nil
53
+}
0 54
new file mode 100644
... ...
@@ -0,0 +1,44 @@
0
+package term
1
+
2
+import (
3
+	"syscall"
4
+	"unsafe"
5
+)
6
+
7
+const (
8
+	getTermios = syscall.TCGETS
9
+	setTermios = syscall.TCSETS
10
+)
11
+
12
+type Termios struct {
13
+	Iflag  uint32
14
+	Oflag  uint32
15
+	Cflag  uint32
16
+	Lflag  uint32
17
+	Cc     [20]byte
18
+	Ispeed uint32
19
+	Ospeed uint32
20
+}
21
+
22
+// MakeRaw put the terminal connected to the given file descriptor into raw
23
+// mode and returns the previous state of the terminal so that it can be
24
+// restored.
25
+func MakeRaw(fd uintptr) (*State, error) {
26
+	var oldState State
27
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
28
+		return nil, err
29
+	}
30
+
31
+	newState := oldState.termios
32
+
33
+	newState.Iflag &^= (syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON)
34
+	newState.Oflag &^= syscall.OPOST
35
+	newState.Lflag &^= (syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN)
36
+	newState.Cflag &^= (syscall.CSIZE | syscall.PARENB)
37
+	newState.Cflag |= syscall.CS8
38
+
39
+	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
40
+		return nil, err
41
+	}
42
+	return &oldState, nil
43
+}
0 44
deleted file mode 100644
... ...
@@ -1,2 +0,0 @@
1
-Guillaume Charmes <guillaume@dotcloud.com> (@creack)
2
-Solomon Hykes <solomon@dotcloud.com> (@shykes)
3 1
deleted file mode 100644
... ...
@@ -1,103 +0,0 @@
1
-package term
2
-
3
-import (
4
-	"errors"
5
-	"os"
6
-	"os/signal"
7
-	"syscall"
8
-	"unsafe"
9
-)
10
-
11
-var (
12
-	ErrInvalidState = errors.New("Invalid terminal state")
13
-)
14
-
15
-type State struct {
16
-	termios Termios
17
-}
18
-
19
-type Winsize struct {
20
-	Height uint16
21
-	Width  uint16
22
-	x      uint16
23
-	y      uint16
24
-}
25
-
26
-func GetWinsize(fd uintptr) (*Winsize, error) {
27
-	ws := &Winsize{}
28
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
29
-	// Skipp errno = 0
30
-	if err == 0 {
31
-		return ws, nil
32
-	}
33
-	return ws, err
34
-}
35
-
36
-func SetWinsize(fd uintptr, ws *Winsize) error {
37
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
38
-	// Skipp errno = 0
39
-	if err == 0 {
40
-		return nil
41
-	}
42
-	return err
43
-}
44
-
45
-// IsTerminal returns true if the given file descriptor is a terminal.
46
-func IsTerminal(fd uintptr) bool {
47
-	var termios Termios
48
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&termios)))
49
-	return err == 0
50
-}
51
-
52
-// Restore restores the terminal connected to the given file descriptor to a
53
-// previous state.
54
-func RestoreTerminal(fd uintptr, state *State) error {
55
-	if state == nil {
56
-		return ErrInvalidState
57
-	}
58
-	_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)))
59
-	if err != 0 {
60
-		return err
61
-	}
62
-	return nil
63
-}
64
-
65
-func SaveState(fd uintptr) (*State, error) {
66
-	var oldState State
67
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
68
-		return nil, err
69
-	}
70
-
71
-	return &oldState, nil
72
-}
73
-
74
-func DisableEcho(fd uintptr, state *State) error {
75
-	newState := state.termios
76
-	newState.Lflag &^= syscall.ECHO
77
-
78
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
79
-		return err
80
-	}
81
-	handleInterrupt(fd, state)
82
-	return nil
83
-}
84
-
85
-func SetRawTerminal(fd uintptr) (*State, error) {
86
-	oldState, err := MakeRaw(fd)
87
-	if err != nil {
88
-		return nil, err
89
-	}
90
-	handleInterrupt(fd, oldState)
91
-	return oldState, err
92
-}
93
-
94
-func handleInterrupt(fd uintptr, state *State) {
95
-	sigchan := make(chan os.Signal, 1)
96
-	signal.Notify(sigchan, os.Interrupt)
97
-
98
-	go func() {
99
-		_ = <-sigchan
100
-		RestoreTerminal(fd, state)
101
-		os.Exit(0)
102
-	}()
103
-}
104 1
deleted file mode 100644
... ...
@@ -1,54 +0,0 @@
1
-package term
2
-
3
-import (
4
-	"syscall"
5
-	"unsafe"
6
-)
7
-
8
-const (
9
-	getTermios = syscall.TIOCGETA
10
-	setTermios = syscall.TIOCSETA
11
-
12
-	ECHO   = 0x00000008
13
-	ONLCR  = 0x2
14
-	ISTRIP = 0x20
15
-	INLCR  = 0x40
16
-	ISIG   = 0x80
17
-	IGNCR  = 0x80
18
-	ICANON = 0x100
19
-	ICRNL  = 0x100
20
-	IXOFF  = 0x400
21
-	IXON   = 0x200
22
-)
23
-
24
-type Termios struct {
25
-	Iflag  uint64
26
-	Oflag  uint64
27
-	Cflag  uint64
28
-	Lflag  uint64
29
-	Cc     [20]byte
30
-	Ispeed uint64
31
-	Ospeed uint64
32
-}
33
-
34
-// MakeRaw put the terminal connected to the given file descriptor into raw
35
-// mode and returns the previous state of the terminal so that it can be
36
-// restored.
37
-func MakeRaw(fd uintptr) (*State, error) {
38
-	var oldState State
39
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
40
-		return nil, err
41
-	}
42
-
43
-	newState := oldState.termios
44
-	newState.Iflag &^= (ISTRIP | INLCR | IGNCR | IXON | IXOFF)
45
-	newState.Iflag |= ICRNL
46
-	newState.Oflag |= ONLCR
47
-	newState.Lflag &^= (ECHO | ICANON | ISIG)
48
-
49
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(setTermios), uintptr(unsafe.Pointer(&newState))); err != 0 {
50
-		return nil, err
51
-	}
52
-
53
-	return &oldState, nil
54
-}
55 1
deleted file mode 100644
... ...
@@ -1,44 +0,0 @@
1
-package term
2
-
3
-import (
4
-	"syscall"
5
-	"unsafe"
6
-)
7
-
8
-const (
9
-	getTermios = syscall.TCGETS
10
-	setTermios = syscall.TCSETS
11
-)
12
-
13
-type Termios struct {
14
-	Iflag  uint32
15
-	Oflag  uint32
16
-	Cflag  uint32
17
-	Lflag  uint32
18
-	Cc     [20]byte
19
-	Ispeed uint32
20
-	Ospeed uint32
21
-}
22
-
23
-// MakeRaw put the terminal connected to the given file descriptor into raw
24
-// mode and returns the previous state of the terminal so that it can be
25
-// restored.
26
-func MakeRaw(fd uintptr) (*State, error) {
27
-	var oldState State
28
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
29
-		return nil, err
30
-	}
31
-
32
-	newState := oldState.termios
33
-
34
-	newState.Iflag &^= (syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON)
35
-	newState.Oflag &^= syscall.OPOST
36
-	newState.Lflag &^= (syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN)
37
-	newState.Cflag &^= (syscall.CSIZE | syscall.PARENB)
38
-	newState.Cflag |= syscall.CS8
39
-
40
-	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
41
-		return nil, err
42
-	}
43
-	return &oldState, nil
44
-}
... ...
@@ -3,7 +3,7 @@ package utils
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6
-	"github.com/dotcloud/docker/term"
6
+	"github.com/dotcloud/docker/pkg/term"
7 7
 	"io"
8 8
 	"strings"
9 9
 	"time"