本文整理匯總了Golang中C.isatty函數的典型用法代碼示例。如果您正苦於以下問題:Golang isatty函數的具體用法?Golang isatty怎麽用?Golang isatty使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了isatty函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: openSerial
/**
* Open the serial port setting the baud etc.
*/
func openSerial() (io.ReadWriteCloser, error) {
file, err :=
os.OpenFile(
"/dev/ttyUSB0",
syscall.O_RDWR|syscall.O_NOCTTY,
0600)
if err != nil {
return nil, err
}
fd := C.int(file.Fd())
if C.isatty(fd) == 0 {
err := errors.New("File is not a serial port")
return nil, err
}
var termios C.struct_termios
_, err = C.tcgetattr(fd, &termios)
if err != nil {
return nil, err
}
var baud C.speed_t
baud = C.B115200
_, err = C.cfsetispeed(&termios, baud)
if err != nil {
return nil, err
}
_, err = C.cfsetospeed(&termios, baud)
if err != nil {
return nil, err
}
return file, nil
}
示例2: CheckIsatty
// Determines if the device is a terminal. Return an error, if any.
//
// int isatty(int fd);
func CheckIsatty(fd int) error {
exitCode, errno := C.isatty(C.int(fd))
if exitCode == 1 {
return nil
}
return errors.New("it is not a tty: " + errno.Error())
}
示例3: Isatty
// Determines if the device is a terminal.
//
// int isatty(int fd);
func Isatty(fd int) bool {
exitCode, _ := C.isatty(C.int(fd))
if exitCode == 1 {
return true
}
return false
}
示例4: getFileDescriptor
func (c *Connection) getFileDescriptor() (fd C.int, err error) {
fd = C.int(c.file.Fd())
if C.isatty(fd) != 1 {
err = errors.New("File is not a tty")
}
return fd, err
}
示例5: IsTerminal
// May move to util/file? For now is here because it's the only
// one used by colors.
func IsTerminal(file *os.File) bool {
rv, err := C.isatty(C.int(file.Fd()))
if err != nil {
return false
}
if rv != 0 {
return true
}
return false
}
示例6: openPort
func openPort(name string, c *Config) (rwc io.ReadWriteCloser, err error) {
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return
}
defer func() {
if err != nil {
f.Close()
}
}()
fd := C.int(f.Fd())
if C.isatty(fd) != 1 {
return nil, errors.New("File is not a tty")
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
return nil, err
}
var speed C.speed_t
switch c.Baud {
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
default:
return nil, fmt.Errorf("Unknown baud rate %v", c.Baud)
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
return nil, err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
return nil, err
}
// Select local mode
st.c_cflag |= C.CLOCAL | C.CREAD
// Select stop bits
switch c.StopBits {
case StopBits1:
st.c_cflag &^= C.CSTOPB
case StopBits2:
st.c_cflag |= C.CSTOPB
default:
panic(c.StopBits)
}
// Select character size
st.c_cflag &^= C.CSIZE
switch c.Size {
case Byte5:
st.c_cflag |= C.CS5
case Byte6:
st.c_cflag |= C.CS6
case Byte7:
st.c_cflag |= C.CS7
case Byte8:
st.c_cflag |= C.CS8
default:
panic(c.Size)
}
// Select parity mode
switch c.Parity {
case ParityNone:
st.c_cflag &^= C.PARENB
case ParityEven:
st.c_cflag |= C.PARENB
st.c_cflag &^= C.PARODD
case ParityOdd:
st.c_cflag |= C.PARENB
st.c_cflag |= C.PARODD
default:
panic(c.Parity)
}
// Select CRLF translation
if c.CRLFTranslate {
st.c_iflag |= C.ICRNL
} else {
st.c_iflag &^= C.ICRNL
}
// Select raw mode
st.c_lflag &^= C.ICANON | C.ECHO | C.ECHOE | C.ISIG
st.c_oflag &^= C.OPOST
//.........這裏部分代碼省略.........
示例7: OpenPort
func OpenPort(name string, baud int) (rwc io.ReadWriteCloser, err error) {
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return
}
fd := C.int(f.Fd())
if C.isatty(fd) != 1 {
f.Close()
return nil, fmt.Errorf("[ERROR] not a tty/cu device")
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
f.Close()
return nil, err
}
var speed C.speed_t
switch baud {
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
default:
f.Close()
return nil, fmt.Errorf("Unknown baud rate %v", baud)
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
// Select local mode
st.c_cflag |= (C.CLOCAL | C.CREAD)
// Select raw mode
st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
st.c_oflag &= ^C.tcflag_t(C.OPOST)
_, err = C.tcsetattr(fd, C.TCSANOW, &st)
if err != nil {
f.Close()
return nil, err
}
r1, _, e := syscall.Syscall(syscall.SYS_FCNTL,
uintptr(f.Fd()),
uintptr(syscall.F_SETFL),
uintptr(0))
if e != 0 || r1 != 0 {
s := fmt.Sprint("[WARN] clearing NONBLOCK syscall error:", e, r1)
f.Close()
return nil, fmt.Errorf("%s", s)
}
return f, nil
}
示例8: isatty
func isatty(fd uintptr) bool {
return int(C.isatty(C.int(fd))) != 0
}
示例9: IsTty
// IsTty returns true is stdin is a terminal
func IsTty() bool {
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
}
示例10: isatty
// isatty is a wrapper around isatty(3).
// Returns true if the specified fd is associated with a tty.
func isatty(fd int) bool {
return C.isatty(C.int(fd)) == 1
}
示例11: openPort
func openPort(name string, baud int, spec []byte, flow []bool) (rwc io.ReadWriteCloser, err error) {
port := new(serialPort)
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return
}
fd := C.int(f.Fd())
if C.isatty(fd) != 1 {
f.Close()
return nil, errors.New("File is not a tty")
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
f.Close()
return nil, err
}
var speed C.speed_t
switch baud {
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
default:
f.Close()
return nil, fmt.Errorf("Unknown baud rate %v", baud)
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
// Select local mode
st.c_cflag |= (C.CLOCAL | C.CREAD)
// Select raw mode
st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
st.c_oflag &= ^C.tcflag_t(C.OPOST)
// Flow control
if flow[RTS_FLAG] {
st.c_cflag |= C.tcflag_t(C.CRTSCTS)
}
if flow[XON_FLAG] {
st.c_cflag |= C.tcflag_t(C.IXON | C.IXOFF | C.IXANY)
}
// Defaults to 8N1 if nothing valid is given
byteSize := spec[0]
parity := spec[1]
stopBits := spec[2]
switch byteSize {
case byte(5):
st.c_cflag |= C.tcflag_t(C.CS5)
break
case byte(6):
st.c_cflag |= C.tcflag_t(C.CS6)
break
case byte(7):
st.c_cflag |= C.tcflag_t(C.CS7)
break
case byte(8):
default:
st.c_cflag |= C.tcflag_t(C.CS8)
break
}
switch parity {
case PARITY_EVEN:
st.c_cflag |= C.tcflag_t(C.PARENB)
st.c_cflag &= ^C.tcflag_t(C.PARODD)
break
case PARITY_ODD:
st.c_cflag |= C.tcflag_t(C.PARENB)
st.c_cflag |= C.tcflag_t(C.PARODD)
break
case PARITY_NONE:
default:
st.c_cflag &= ^C.tcflag_t(C.PARENB)
break
}
switch stopBits {
case byte(2):
st.c_cflag |= C.tcflag_t(C.CSTOPB)
//.........這裏部分代碼省略.........
示例12: openPort
func openPort(name string, baud int) (rwc io.ReadWriteCloser, err error) {
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return
}
fd := C.int(f.Fd())
if C.isatty(fd) != 1 {
f.Close()
return nil, errors.New("File is not a tty")
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
f.Close()
return nil, err
}
var speed C.speed_t
switch baud {
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
case 4800:
speed = C.B4800
case 2400:
speed = C.B2400
default:
f.Close()
return nil, fmt.Errorf("Unknown baud rate %v", baud)
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
// Select local mode
st.c_cflag |= (C.CLOCAL | C.CREAD)
// Select raw mode
st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
st.c_oflag &= ^C.tcflag_t(C.OPOST)
_, err = C.tcsetattr(fd, C.TCSANOW, &st)
if err != nil {
f.Close()
return nil, err
}
//fmt.Println("Tweaking", name)
r1, _, e := syscall.Syscall(syscall.SYS_FCNTL,
uintptr(f.Fd()),
uintptr(syscall.F_SETFL),
uintptr(0))
if e != 0 || r1 != 0 {
s := fmt.Sprint("Clearing NONBLOCK syscall error:", e, r1)
f.Close()
return nil, errors.New(s)
}
/*
r1, _, e = syscall.Syscall(syscall.SYS_IOCTL,
uintptr(f.Fd()),
uintptr(0x80045402), // IOSSIOSPEED
uintptr(unsafe.Pointer(&baud)));
if e != 0 || r1 != 0 {
s := fmt.Sprint("Baudrate syscall error:", e, r1)
f.Close()
return nil, os.NewError(s)
}
*/
return f, nil
}
示例13: isTerminal
func isTerminal(file *os.File) bool {
return int(C.isatty(C.int(file.Fd()))) != 0
}
示例14: IsATTY
func IsATTY(fd int) bool {
return C.isatty(C.int(fd)) != 0
}
示例15: openPort
func openPort(name string, baud int) (rwc io.ReadWriteCloser, err error) {
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY, 0666)
if err != nil {
return
}
fd := C.int(f.Fd())
if C.isatty(fd) != 1 {
f.Close()
return nil, errors.New("File is not a tty")
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
f.Close()
return nil, err
}
var speed C.speed_t
switch baud {
case 115200:
speed = C.B115200
case 57600:
speed = C.B57600
case 38400:
speed = C.B38400
case 19200:
speed = C.B19200
case 9600:
speed = C.B9600
case 4800:
speed = C.B4800
case 2400:
speed = C.B2400
default:
f.Close()
return nil, fmt.Errorf("Unknown baud rate %v", baud)
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
f.Close()
return nil, err
}
// Select local mode
st.c_cflag |= (C.CLOCAL | C.CREAD)
// Select raw mode
st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
st.c_oflag &= ^C.tcflag_t(C.OPOST)
_, err = C.tcsetattr(fd, C.TCSANOW, &st)
if err != nil {
f.Close()
return nil, err
}
return f, nil
}