Skip to content

Commit

Permalink
still use cgo for getchar
Browse files Browse the repository at this point in the history
  • Loading branch information
fdlsk2r committed May 29, 2019
1 parent 1b754e5 commit b38dae5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
29 changes: 4 additions & 25 deletions selector/getchar.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
package selector

import (
"os"
"syscall"
"unsafe"
)
// #include <stdio.h>
import "C"

// implement getChar
func getChar() {
fd, err := os.Open("/dev/tty")
if err != nil {
panic(err)
}

var term syscall.Termios
ptr := uintptr(unsafe.Pointer(&term))
syscall.Syscall6(syscall.SYS_IOCTL, fd.Fd(), syscall.TIOCGETA, ptr, 0, 0, 0)

// setup nobuffer
term.Lflag &^= syscall.ICANON
term.Cc[syscall.VMIN] = 1
term.Cc[syscall.VTIME] = 0
syscall.Syscall6(syscall.SYS_IOCTL, fd.Fd(), syscall.TIOCSETA, ptr, 0, 0, 0)

// read
b := make([]byte, 4)
syscall.Read(int(fd.Fd()), b)
func getchar() {
C.getchar()
}
2 changes: 1 addition & 1 deletion selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func execute(name string, args ...string) {
if err != nil {
println(os.Stdout, "> exec error: ", err)
println(os.Stdout, "> press any key to continue")
getChar()
getchar()
}
}
2 changes: 1 addition & 1 deletion selector/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func loadServers() (arr []server) {
fmt.Printf("> %v: %v \n", i+1, e)
}
fmt.Println("> press any key to continue")
getChar()
getchar()
}
return
}
Expand Down

0 comments on commit b38dae5

Please sign in to comment.