OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [exp/] [terminal/] [util.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
//extern ioctl
64
func ioctl(int, int, unsafe.Pointer) int
65
 
66
// GetSize returns the dimensions of the given terminal.
67
func GetSize(fd int) (width, height int, err error) {
68
        var dimensions [4]uint16
69
 
70
        if ioctl(fd, syscall.TIOCGWINSZ, unsafe.Pointer(&dimensions)) < 0 {
71
                return -1, -1, syscall.GetErrno()
72
        }
73
        return int(dimensions[1]), int(dimensions[0]), nil
74
}
75
 
76
// ReadPassword reads a line of input from a terminal without local echo.  This
77
// is commonly used for inputting passwords and other sensitive data. The slice
78
// returned does not include the \n.
79
func ReadPassword(fd int) ([]byte, error) {
80
        var oldState syscall.Termios
81
        if err := syscall.Tcgetattr(fd, &oldState); err != nil {
82
                return nil, err
83
        }
84
 
85
        newState := oldState
86
        newState.Lflag &^= syscall.ECHO
87
        if err := syscall.Tcsetattr(fd, syscall.TCSANOW, &newState); err != nil {
88
                return nil, err
89
        }
90
 
91
        defer func() {
92
                syscall.Tcsetattr(fd, syscall.TCSANOW, &oldState)
93
        }()
94
 
95
        var buf [16]byte
96
        var ret []byte
97
        for {
98
                n, err := syscall.Read(fd, buf[:])
99
                if err != nil {
100
                        return nil, err
101
                }
102
                if n == 0 {
103
                        if len(ret) == 0 {
104
                                return nil, io.EOF
105
                        }
106
                        break
107
                }
108
                if buf[n-1] == '\n' {
109
                        n--
110
                }
111
                ret = append(ret, buf[:n]...)
112
                if n < len(buf) {
113
                        break
114
                }
115
        }
116
 
117
        return ret, nil
118
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.