Skip to content

Commit

Permalink
cmd/retro: minor tweak to support raw mod on all posix platforms (hop…
Browse files Browse the repository at this point in the history
…efully)
  • Loading branch information
db47h committed Aug 5, 2016
1 parent 16fe1fa commit bccff4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
34 changes: 30 additions & 4 deletions cmd/retro/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//+build !linux
//+build !windows

package main

// setRawIO() attempts to set stdin to raw IO and returns a function
// to restore IO settings as they were before
import (
"syscall"

"github.com/pkg/term/termios"
)

// switch terminal to raw IO. we do not use the higher level functions
// of the term package because it doesn't allow the use of existing file
// descriptors, nor does it allow custom termios settings.
func setRawIO() (func(), error) {
return nil, nil
var tios syscall.Termios
err := termios.Tcgetattr(0, &tios)
if err != nil {
return nil, err
}
a := tios
a.Iflag &^= syscall.BRKINT | syscall.ISTRIP | syscall.IXON | syscall.IXOFF
a.Iflag |= syscall.IGNBRK | syscall.IGNPAR
a.Lflag &^= syscall.ICANON | syscall.ISIG | syscall.IEXTEN | syscall.ECHO
a.Cc[syscall.VMIN] = 1
a.Cc[syscall.VTIME] = 0
err = termios.Tcsetattr(0, termios.TCSANOW, &a)
if err != nil {
// well, try to restore as it was if it errors
termios.Tcsetattr(0, termios.TCSANOW, &tios)
return nil, err
}
return func() {
termios.Tcsetattr(0, termios.TCSANOW, &tios)
}, nil
}
30 changes: 4 additions & 26 deletions cmd/retro/term_linux.go → cmd/retro/term_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,10 @@

package main

import (
"syscall"
import "errors"

"github.com/pkg/term/termios"
)

// switch terminal to raw IO.
// setRawIO() attempts to set stdin to raw IO and returns a function
// to restore IO settings as they were before
func setRawIO() (func(), error) {
var tios syscall.Termios
err := termios.Tcgetattr(0, &tios)
if err != nil {
return nil, err
}
a := tios
a.Iflag &^= syscall.BRKINT | syscall.ISTRIP | syscall.IXON | syscall.IXOFF
a.Iflag |= syscall.IGNBRK | syscall.IGNPAR
a.Lflag &^= syscall.ICANON | syscall.ISIG | syscall.IEXTEN | syscall.ECHO
a.Cc[syscall.VMIN] = 1
a.Cc[syscall.VTIME] = 0
err = termios.Tcsetattr(0, termios.TCSANOW, &a)
if err != nil {
// well, try to restore as it was if it errors
termios.Tcsetattr(0, termios.TCSANOW, &tios)
return nil, err
}
return func() {
termios.Tcsetattr(0, termios.TCSANOW, &tios)
}, nil
return nil, errors.New("raw IO not supported")
}

0 comments on commit bccff4f

Please sign in to comment.