Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

@@ -0,0 +1,99 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h
const cclen = 32
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options on Linux.
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_line Cc
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
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
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_line Cc
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,97 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://github.com/apple/darwin-xnu/blob/main/bsd/sys/termios.h
const cclen = 20
type TcFlag = usize
type Speed = usize
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return usize(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~usize(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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(usize(C.ECHO))
}
@@ -0,0 +1,75 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module termios
// not used but needed for function declarations
type TcFlag = int
type Speed = int
type Cc = u8
// Termios represents platform dependent flags representing the terminal state.
// 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
// 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
// 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
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.
// It is only implemented for Unix like OSes
pub fn tcgetattr(fd int, mut t Termios) int {
$if wasm32_emscripten {
return 0
}
eprintln('term.termios: tcgetattr,tcsetattr,ioctl,set_state are not implemented for the platform')
eprintln('tcgetattr, fd: ${fd}, t: ${t}')
return 0
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic.
// It is only implemented for Unix like OSes.
pub fn tcsetattr(fd int, optional_actions int, mut t Termios) int {
eprintln('tcsetattr, fd: ${fd}, optional_actions: ${optional_actions}, t: ${t}')
return 0
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic.
// It is only implemented for Unix like OSes
pub fn ioctl(fd int, request u64, arg voidptr) int {
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)
}
@@ -0,0 +1,97 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/sys/_termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic.
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,97 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://web.mit.edu/freebsd/head/sys/sys/_termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,99 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h
const cclen = 32
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options on Linux.
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_line Cc
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
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
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_line Cc
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,97 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://web.mit.edu/freebsd/head/sys/sys/_termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,97 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://github.com/openbsd/src/blob/master/sys/sys/termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
+99
View File
@@ -0,0 +1,99 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
reserved [3]u32
c_ispeed Speed
c_ospeed Speed
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
reserved [3]u32
c_ispeed Speed
c_ospeed Speed
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,93 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// Serves as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
#include <termios.h>
#include <sys/ioctl.h>
// https://web.mit.edu/freebsd/head/sys/sys/_termios.h
const cclen = 20
type TcFlag = int
type Speed = int
type Cc = u8
// Termios stores the terminal options
pub struct C.termios {
mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
}
fn C.tcgetattr(fd i32, termios_p &C.termios) i32
fn C.tcsetattr(fd i32, optional_actions i32, const_termios_p &C.termios) i32
fn C.ioctl(fd i32, request u64, args ...voidptr) i32
// flag provides a termios flag of the correct size
// for the underlying C.termios structure
@[inline]
pub fn flag(value int) TcFlag {
return int(value)
}
// invert is a platform dependant way to bitwise NOT (~) TcFlag
// as its length varies across platforms
@[inline]
pub fn invert(value TcFlag) TcFlag {
return ~int(value)
}
pub struct Termios {
pub mut:
c_iflag TcFlag
c_oflag TcFlag
c_cflag TcFlag
c_lflag TcFlag
c_cc [cclen]Cc
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcgetattr(fd int, mut termios_p Termios) int {
unsafe {
return C.tcgetattr(fd, &C.termios(termios_p))
}
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic
@[inline]
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
unsafe {
return C.tcsetattr(fd, optional_actions, &C.termios(termios_p))
}
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
unsafe {
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)
}
@@ -0,0 +1,35 @@
module termios
fn test_portable() {
assert 123 == int(flag(123))
o := Termios{
c_lflag: flag(0xFFFF)
} // assume c_lflag exists everywhere
// dump( o.c_lflag )
mut n := o
n.c_lflag &= invert(1)
// dump( n.c_lflag )
assert n.c_lflag != o.c_lflag
n.disable_echo() // just assume it exists, and can be called everywhere
assert true
}
@[if !windows]
fn test_termios() {
mut original_term := Termios{}
tcgetattr(0, mut original_term)
println(original_term)
mut silent_term := original_term
silent_term.c_lflag &= invert(C.ECHO)
tcsetattr(0, C.TCSANOW, mut silent_term)
mut check_term := Termios{}
tcgetattr(0, mut check_term)
assert check_term.c_lflag == silent_term.c_lflag
tcsetattr(0, C.TCSANOW, mut original_term)
tcgetattr(0, mut check_term)
assert check_term.c_lflag == original_term.c_lflag
}
@@ -0,0 +1,65 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
//
// TODO: Windows version needs to be implemented.
// Will serve as more advanced input method
// based on the work of https://github.com/AmokHuginnsson/replxx
//
module termios
type TcFlag = int
type Speed = int
type Cc = u8
// flag provides a termios flag of the correct size for the underlying C.termios structure.
// It is only implemented for Unix like OSes.
pub fn flag(value int) TcFlag {
return int(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 ~int(value)
}
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
}
// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic.
// It is only implemented for Unix like OSes.
pub fn tcgetattr(fd int, mut termios_p Termios) int {
return -1
}
// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic.
// It is only implemented for Unix like OSes.
pub fn tcsetattr(fd int, optional_actions int, mut termios_p Termios) int {
return -1
}
// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic.
// It is only implemented for Unix like OSes.
@[inline]
pub fn ioctl(fd int, request u64, arg voidptr) int {
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() {
}