v2 / vlib / term / termios / termios_default.c.v
75 lines · 66 sloc · 2.64 KB · 37255767290c243b71f0e78d77c3bd5e875748e6
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module termios
5
6// not used but needed for function declarations
7type TcFlag = int
8type Speed = int
9type Cc = u8
10
11// Termios represents platform dependent flags representing the terminal state.
12// Linux https://github.com/lattera/glibc/blob/master/sysdeps/unix/sysv/linux/bits/termios.h
13// OpenBSD https://github.com/openbsd/src/blob/master/sys/sys/termios.h
14// FreeBSD https://web.mit.edu/freebsd/head/sys/sys/_termios.h
15// Solaris https://github.com/omniti-labs/illumos-omnios/blob/master/usr/src/uts/common/sys/termios.h
16// DragonFly https://gitweb.dragonflybsd.org/dragonfly.git/blob_plain/HEAD:/sys/sys/_termios.h
17// QNX https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h
18pub struct Termios {
19pub mut:
20 c_iflag TcFlag
21 c_oflag TcFlag
22 c_cflag TcFlag
23 c_lflag TcFlag
24 c_line Cc
25 c_cc [32]Cc
26 c_ispeed Speed
27 c_ospeed Speed
28}
29
30// flag provides a termios flag of the correct size for the underlying C.termios structure.
31pub fn flag(value int) TcFlag {
32 return TcFlag(value)
33}
34
35// invert is a platform dependant way to bitwise NOT (~) TcFlag as its length varies across platforms.
36// It is only implemented for Unix like OSes.
37pub fn invert(value TcFlag) TcFlag {
38 return TcFlag(~int(value))
39}
40
41// tcgetattr is an unsafe wrapper around C.termios and keeps its semantic.
42// It is only implemented for Unix like OSes
43pub fn tcgetattr(fd int, mut t Termios) int {
44 $if wasm32_emscripten {
45 return 0
46 }
47 eprintln('term.termios: tcgetattr,tcsetattr,ioctl,set_state are not implemented for the platform')
48 eprintln('tcgetattr, fd: ${fd}, t: ${t}')
49 return 0
50}
51
52// tcsetattr is an unsafe wrapper around C.termios and keeps its semantic.
53// It is only implemented for Unix like OSes.
54pub fn tcsetattr(fd int, optional_actions int, mut t Termios) int {
55 eprintln('tcsetattr, fd: ${fd}, optional_actions: ${optional_actions}, t: ${t}')
56 return 0
57}
58
59// ioctl is an unsafe wrapper around C.ioctl and keeps its semantic.
60// It is only implemented for Unix like OSes
61pub fn ioctl(fd int, request u64, arg voidptr) int {
62 eprintln('ioctl, fd: ${fd}, request: ${request}, arg: ${arg}')
63 return 0
64}
65
66// set_state applies the flags in the `new_state` to the descriptor `fd`.
67pub fn set_state(fd int, new_state Termios) int {
68 eprintln('set_state, fd: ${fd} | new_state: ${new_state}')
69 return 0
70}
71
72// disable_echo disables echoing characters as they are typed, when that Termios state is later set with termios.set_state(fd,t).
73pub fn (mut t Termios) disable_echo() {
74 t.c_lflag &= invert(8)
75}
76