mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os,term.termios: add termios.set_state/2, state.disable_echo/0, use them in os.input_password, to fix v -os wasm32_emscripten examples/2048/
This commit is contained in:
parent
37e7d5f5ae
commit
32114a679a
@ -12,7 +12,7 @@ import term
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
base_os = 'linux'
|
base_os = 'linux'
|
||||||
os_names = ['linux', 'macos', 'windows']
|
os_names = ['linux', 'macos', 'windows', 'freebsd', 'openbsd', 'solaris', 'termux']
|
||||||
skip_modules = [
|
skip_modules = [
|
||||||
'builtin.bare',
|
'builtin.bare',
|
||||||
'builtin.linux_bare.old',
|
'builtin.linux_bare.old',
|
||||||
|
@ -13,17 +13,15 @@ pub fn input_password(prompt string) !string {
|
|||||||
if termios.tcgetattr(0, mut old_state) != 0 {
|
if termios.tcgetattr(0, mut old_state) != 0 {
|
||||||
return last_error()
|
return last_error()
|
||||||
}
|
}
|
||||||
defer {
|
|
||||||
termios.tcsetattr(0, C.TCSANOW, mut old_state)
|
|
||||||
println('')
|
|
||||||
}
|
|
||||||
|
|
||||||
mut new_state := old_state
|
mut new_state := old_state
|
||||||
|
new_state.disable_echo()
|
||||||
new_state.c_lflag &= termios.invert(C.ECHO) // Disable echoing of characters
|
termios.set_state(0, new_state)
|
||||||
termios.tcsetattr(0, C.TCSANOW, mut new_state)
|
|
||||||
|
|
||||||
password := input_opt(prompt) or { return error('Failed to read password') }
|
password := input_opt(prompt) or { return error('Failed to read password') }
|
||||||
|
|
||||||
|
termios.set_state(0, old_state)
|
||||||
|
|
||||||
|
println('')
|
||||||
return password
|
return password
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ fn C.ioctl(fd int, request u64, arg voidptr) int
|
|||||||
// for the underlying C.termios structure
|
// for the underlying C.termios structure
|
||||||
[inline]
|
[inline]
|
||||||
pub fn flag(value int) TcFlag {
|
pub fn flag(value int) TcFlag {
|
||||||
return int(value)
|
return TcFlag(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
||||||
@ -86,3 +86,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -84,3 +84,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
//
|
|
||||||
// TODO Mac version needs to be implemented
|
|
||||||
// Will serve as more advanced input method
|
|
||||||
// based on the work of https://github.com/AmokHuginnsson/replxx
|
|
||||||
//
|
|
||||||
|
|
||||||
module termios
|
module termios
|
||||||
|
|
||||||
// not used but needed for function declarations
|
// not used but needed for function declarations
|
||||||
@ -14,45 +8,68 @@ type TcFlag = int
|
|||||||
type Speed = int
|
type Speed = int
|
||||||
type Cc = u8
|
type Cc = u8
|
||||||
|
|
||||||
// flag provides a termios flag of the correct size
|
// Termios definitions
|
||||||
// for the underlying C.termios structure
|
|
||||||
// It is only implemented for Unix like OSes
|
|
||||||
pub fn flag(value int) TcFlag {
|
|
||||||
$compile_error('feature not available')
|
|
||||||
}
|
|
||||||
|
|
||||||
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
|
||||||
// as its length varies across platforms
|
|
||||||
// It is only implemented for Unix like OSes
|
|
||||||
pub fn invert(value TcFlag) TcFlag {
|
|
||||||
$compile_error('feature not available')
|
|
||||||
}
|
|
||||||
|
|
||||||
// termios definitions
|
|
||||||
// Linux https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h
|
// Linux https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h
|
||||||
// OpenBSD https://github.com/openbsd/src/blob/master/sys/sys/termios.h
|
// OpenBSD https://github.com/openbsd/src/blob/master/sys/sys/termios.h
|
||||||
// FreeBSD https://web.mit.edu/freebsd/head/sys/sys/_termios.h
|
// FreeBSD https://web.mit.edu/freebsd/head/sys/sys/_termios.h
|
||||||
// Solaris https://github.com/omniti-labs/illumos-omnios/blob/master/usr/src/uts/common/sys/termios.h
|
// Solaris https://github.com/omniti-labs/illumos-omnios/blob/master/usr/src/uts/common/sys/termios.h
|
||||||
// DragonFly https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/sys/_termios.h
|
// DragonFly https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/sys/_termios.h
|
||||||
// QNX https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h
|
// QNX https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h
|
||||||
|
|
||||||
pub struct Termios {
|
pub struct Termios {
|
||||||
|
pub mut:
|
||||||
|
c_iflag TcFlag
|
||||||
|
c_oflag TcFlag
|
||||||
|
c_cflag TcFlag
|
||||||
|
c_lflag TcFlag
|
||||||
|
c_line Cc
|
||||||
|
c_cc [32]Cc
|
||||||
|
c_ispeed Speed
|
||||||
|
c_ospeed Speed
|
||||||
|
}
|
||||||
|
|
||||||
|
// flag provides a termios flag of the correct size
|
||||||
|
// for the underlying C.termios structure
|
||||||
|
pub fn flag(value int) TcFlag {
|
||||||
|
return TcFlag(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
||||||
|
// as its length varies across platforms
|
||||||
|
// It is only implemented for Unix like OSes
|
||||||
|
pub fn invert(value TcFlag) TcFlag {
|
||||||
|
return TcFlag(~int(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
|
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
|
||||||
// It is only implemented for Unix like OSes
|
// It is only implemented for Unix like OSes
|
||||||
pub fn tcgetattr(fd int, mut termios_p Termios) int {
|
pub fn tcgetattr(fd int, mut t Termios) int {
|
||||||
$compile_error('feature not available')
|
$compile_warn('termios.tcgetattr is not implemented on the platform')
|
||||||
|
eprintln('tcgetattr, fd: ${fd}, t: ${t}')
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
|
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
|
||||||
// It is only implemented for Unix like OSes
|
// It is only implemented for Unix like OSes
|
||||||
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
|
pub fn tcsetattr(fd int, optional_actions int, mut t Termios) int {
|
||||||
$compile_error('feature not available')
|
eprintln('tcsetattr, fd: ${fd}, optional_actions: ${optional_actions}, t: ${t}')
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
|
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
|
||||||
[inline]
|
// It is only implemented for Unix like OSes
|
||||||
pub fn ioctl(fd int, request u64, arg voidptr) int {
|
pub fn ioctl(fd int, request u64, arg voidptr) int {
|
||||||
$compile_error('feature not available')
|
eprintln('ioctl, fd: ${fd}, request: ${request}, arg: ${arg}')
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
eprintln('set_state, fd: ${fd} | new_state: ${new_state}')
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(8)
|
||||||
}
|
}
|
||||||
|
@ -84,3 +84,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -84,3 +84,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -41,7 +41,7 @@ fn C.ioctl(fd int, request u64, arg voidptr) int
|
|||||||
// for the underlying C.termios structure
|
// for the underlying C.termios structure
|
||||||
[inline]
|
[inline]
|
||||||
pub fn flag(value int) TcFlag {
|
pub fn flag(value int) TcFlag {
|
||||||
return int(value)
|
return TcFlag(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
// invert is a platform dependant way to bitwise NOT (~) TcFlag
|
||||||
@ -86,3 +86,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -84,3 +84,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -86,3 +86,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -80,3 +80,15 @@ pub fn ioctl(fd int, request u64, arg voidptr) int {
|
|||||||
return C.ioctl(fd, request, arg)
|
return C.ioctl(fd, request, arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
mut x := new_state
|
||||||
|
return tcsetattr(0, C.TCSANOW, mut x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
t.c_lflag &= invert(C.ECHO)
|
||||||
|
}
|
||||||
|
@ -13,8 +13,8 @@ fn test_termios() {
|
|||||||
tcgetattr(0, mut check_term)
|
tcgetattr(0, mut check_term)
|
||||||
assert check_term.c_lflag == silent_term.c_lflag
|
assert check_term.c_lflag == silent_term.c_lflag
|
||||||
|
|
||||||
tcsetattr(0, C.TCSANOW, mut orginal_term)
|
tcsetattr(0, C.TCSANOW, mut original_term)
|
||||||
|
|
||||||
tcgetattr(0, mut check_term)
|
tcgetattr(0, mut check_term)
|
||||||
assert check_term.c_lflag == orginal_term.c_lflag
|
assert check_term.c_lflag == original_term.c_lflag
|
||||||
}
|
}
|
@ -47,3 +47,13 @@ pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
|
|||||||
pub fn ioctl(fd int, request u64, arg voidptr) int {
|
pub fn ioctl(fd int, request u64, arg voidptr) int {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_state applies the flags in the `new_state` to the descriptor `fd`.
|
||||||
|
pub fn set_state(fd int, new_state Termios) int {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable_echo disables echoing characters as they are typed,
|
||||||
|
// when that Termios state is later set with termios.set_state(fd,t)
|
||||||
|
pub fn (mut t Termios) disable_echo() {
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user