OSDN Git Service

libgo: Update to weekly.2011-12-22.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / terminal / util.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build linux
6
7 // Package terminal provides support functions for dealing with terminals, as
8 // commonly found on UNIX systems.
9 //
10 // Putting a terminal into raw mode is the most common requirement:
11 //
12 //      oldState, err := terminal.MakeRaw(0)
13 //      if err != nil {
14 //              panic(err)
15 //      }
16 //      defer terminal.Restore(0, oldState)
17 package terminal
18
19 import (
20         "io"
21         "syscall"
22         "unsafe"
23 )
24
25 // State contains the state of a terminal.
26 type State struct {
27         termios syscall.Termios
28 }
29
30 // IsTerminal returns true if the given file descriptor is a terminal.
31 func IsTerminal(fd int) bool {
32         var termios syscall.Termios
33         err := syscall.Tcgetattr(fd, &termios)
34         return err == nil
35 }
36
37 // MakeRaw put the terminal connected to the given file descriptor into raw
38 // mode and returns the previous state of the terminal so that it can be
39 // restored.
40 func MakeRaw(fd int) (*State, error) {
41         var oldState State
42         if err := syscall.Tcgetattr(fd, &oldState.termios); err != nil {
43                 return nil, err
44         }
45
46         newState := oldState.termios
47         newState.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXON | syscall.IXOFF
48         newState.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG
49         if err := syscall.Tcsetattr(fd, syscall.TCSANOW, &newState); err != nil {
50                 return nil, err
51         }
52
53         return &oldState, nil
54 }
55
56 // Restore restores the terminal connected to the given file descriptor to a
57 // previous state.
58 func Restore(fd int, state *State) error {
59         err := syscall.Tcsetattr(fd, syscall.TCSANOW, &state.termios)
60         return err
61 }
62
63 func ioctl(int, int, unsafe.Pointer) int __asm__("ioctl")
64
65 // GetSize returns the dimensions of the given terminal.
66 func GetSize(fd int) (width, height int, err error) {
67         var dimensions [4]uint16
68
69         if ioctl(fd, syscall.TIOCGWINSZ, unsafe.Pointer(&dimensions)) < 0 {
70                 return -1, -1, syscall.GetErrno()
71         }
72         return int(dimensions[1]), int(dimensions[0]), nil
73 }
74
75 // ReadPassword reads a line of input from a terminal without local echo.  This
76 // is commonly used for inputting passwords and other sensitive data. The slice
77 // returned does not include the \n.
78 func ReadPassword(fd int) ([]byte, error) {
79         var oldState syscall.Termios
80         if err := syscall.Tcgetattr(fd, &oldState); err != nil {
81                 return nil, err
82         }
83
84         newState := oldState
85         newState.Lflag &^= syscall.ECHO
86         if err := syscall.Tcsetattr(fd, syscall.TCSANOW, &newState); err != nil {
87                 return nil, err
88         }
89
90         defer func() {
91                 syscall.Tcsetattr(fd, syscall.TCSANOW, &oldState)
92         }()
93
94         var buf [16]byte
95         var ret []byte
96         for {
97                 n, err := syscall.Read(fd, buf[:])
98                 if err != nil {
99                         return nil, err
100                 }
101                 if n == 0 {
102                         if len(ret) == 0 {
103                                 return nil, io.EOF
104                         }
105                         break
106                 }
107                 if buf[n-1] == '\n' {
108                         n--
109                 }
110                 ret = append(ret, buf[:n]...)
111                 if n < len(buf) {
112                         break
113                 }
114         }
115
116         return ret, nil
117 }