From b0e104bfd0d7794e01fabcfd3789c6efa0bbad18 Mon Sep 17 00:00:00 2001 From: PalsFreniers Date: Wed, 31 Jul 2024 14:45:04 +0200 Subject: [PATCH] Adding Termios library as std::libc::termios (posix libc functions) (#1272) Adding libc's termios to lib/std/libc --- lib/std/libc/os/posix.c3 | 135 +++++++++++++++++++++++++++++++++++++++ lib/std/libc/termios.c3 | 122 +++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 lib/std/libc/termios.c3 diff --git a/lib/std/libc/os/posix.c3 b/lib/std/libc/os/posix.c3 index 37f28e8c7..dd26b74d4 100644 --- a/lib/std/libc/os/posix.c3 +++ b/lib/std/libc/os/posix.c3 @@ -53,3 +53,138 @@ struct Stack_t extern fn CInt sigaltstack(Stack_t* ss, Stack_t* old_ss); extern fn CInt sigaction(CInt signum, Sigaction *action, Sigaction *oldaction); + +module libc::termios @if($and(env::LIBC, env::POSIX)); + +distinct Cc = char; +distinct Speed = CUInt; +distinct Tcflags = CUInt; + +const Tcflags TCOOFF = 0; +const Tcflags TCOON = 1; +const Tcflags TCIOFF = 2; +const Tcflags TCION = 3; +const Tcflags TCIFLUSH = 0; +const Tcflags TCOFLUSH = 1; +const Tcflags TCIOFLUSH = 2; +const Tcflags TCSANOW = 0; +const Tcflags TCSADRAIN = 1; +const Tcflags TCSAFLUSH = 2; +const Speed B0 = 0000000; +const Speed B50 = 0000001; +const Speed B75 = 0000002; +const Speed B110 = 0000003; +const Speed B134 = 0000004; +const Speed B150 = 0000005; +const Speed B200 = 0000006; +const Speed B300 = 0000007; +const Speed B600 = 0000010; +const Speed B1200 = 0000011; +const Speed B1800 = 0000012; +const Speed B2400 = 0000013; +const Speed B4800 = 0000014; +const Speed B9600 = 0000015; +const Speed B19200 = 0000016; +const Speed B38400 = 0000017; +const Speed B57600 = 0010001; +const Speed B115200 = 0010002; +const Speed B230400 = 0010003; +const Speed B460800 = 0010004; +const Speed B500000 = 0010005; +const Speed B576000 = 0010006; +const Speed B921600 = 0010007; +const Speed B1000000 = 0010010; +const Speed B1152000 = 0010011; +const Speed B1500000 = 0010012; +const Speed B2000000 = 0010013; +const Speed B2500000 = 0010014; +const Speed B3000000 = 0010015; +const Speed B3500000 = 0010016; +const Speed B4000000 = 0010017; +const Speed MAX_BAUD = B4000000; +const CInt VINTR = 0; +const CInt VQUIT = 1; +const CInt VERASE = 2; +const CInt VKILL = 3; +const CInt VEOF = 4; +const CInt VTIME = 5; +const CInt VMIN = 6; +const CInt VSWTC = 7; +const CInt VSTART = 8; +const CInt VSTOP = 9; +const CInt VSUSP = 10; +const CInt VEOL = 11; +const CInt VREPRINT = 12; +const CInt VDISCARD = 13; +const CInt VWERASE = 14; +const CInt VLNEXT = 15; +const CInt VEOL2 = 16; +const CInt ISIG = 0000001; +const CInt ICANON = 0000002; +const CInt ECHO = 0000010; +const CInt ECHOE = 0000020; +const CInt ECHOK = 0000040; +const CInt ECHONL = 0000100; +const CInt NOFLSH = 0000200; +const CInt TOSTOP = 0000400; +const CInt IEXTEN = 0100000; +const CInt CSIZE = 0000060; +const CInt CS5 = 0000000; +const CInt CS6 = 0000020; +const CInt CS7 = 0000040; +const CInt CS8 = 0000060; +const CInt CSTOPB = 0000100; +const CInt CREAD = 0000200; +const CInt PARENB = 0000400; +const CInt PARODD = 0001000; +const CInt HUPCL = 0002000; +const CInt CLOCAL = 0004000; +const CInt OPOST = 0000001; +const CInt OLCUC = 0000002; +const CInt ONLCR = 0000004; +const CInt OCRNL = 0000010; +const CInt ONOCR = 0000020; +const CInt ONLRET = 0000040; +const CInt OFILL = 0000100; +const CInt OFDEL = 0000200; +const CInt VTDLY = 0040000; +const CInt VT0 = 0000000; +const CInt VT1 = 0040000; +const CInt IGNBRK = 0000001; +const CInt BRKINT = 0000002; +const CInt IGNPAR = 0000004; +const CInt PARMRK = 0000010; +const CInt INPCK = 0000020; +const CInt ISTRIP = 0000040; +const CInt INLCR = 0000100; +const CInt IGNCR = 0000200; +const CInt ICRNL = 0000400; +const CInt IUCLC = 0001000; +const CInt IXON = 0002000; +const CInt IXANY = 0004000; +const CInt IXOFF = 0010000; +const CInt IMAXBEL = 0020000; +const CInt IUTF8 = 0040000; + +extern fn CInt tcgetattr(Fd fd, Termios* self); +extern fn CInt tcsetattr(Fd fd, CInt optional_actions, Termios* self); +extern fn CInt tcsendbreak(Fd fd, CInt duration); +extern fn CInt tcdrain(Fd fd); +extern fn CInt tcflush(Fd fd, CInt queue_selector); +extern fn CInt tcflow(Fd fd, CInt action); +extern fn Speed cfgetospeed(Termios* self); +extern fn Speed cfgetispeed(Termios* self); +extern fn CInt cfsetospeed(Termios* self, Speed speed); +extern fn CInt cfsetispeed(Termios* self, Speed speed); + +const CInt NCCS = 32; +struct Termios { + Tcflags c_iflag; + Tcflags c_oflag; + Tcflags c_cflag; + Tcflags c_lflag; + Cc c_line; + Cc[NCCS] c_cc; + Speed c_ispeed; + Speed c_ospeed; +} diff --git a/lib/std/libc/termios.c3 b/lib/std/libc/termios.c3 new file mode 100644 index 000000000..124f4d66e --- /dev/null +++ b/lib/std/libc/termios.c3 @@ -0,0 +1,122 @@ +module libc::termios @if($and(env::LIBC, env::POSIX)); + +fn int sendBreak(Fd fd, int duration) => tcsendbreak(fd, duration); +fn int drain(Fd fd) => tcdrain(fd); +fn int flush(Fd fd, int queue_selector) => tcflush(fd, queue_selector); +fn int flow(Fd fd, int action) => tcflow(fd, action); +fn Speed Termios.getOSpeed(Termios* self) => cfgetospeed(self); +fn Speed Termios.getISpeed(Termios* self) => cfgetispeed(self); +fn int Termios.setOSpeed(Termios* self, Speed speed) => cfsetospeed(self, speed); +fn int Termios.setISpeed(Termios* self, Speed speed) => cfsetispeed(self, speed); +fn int Termios.getAttr(Termios* self, Fd fd) => tcgetattr(fd, self); +fn int Termios.setAttr(Termios* self, Fd fd, int optional_actions) => tcsetattr(fd, optional_actions, self); + +module libc::termios @if($or(!env::LIBC, !env::POSIX)); + +distinct Cc = char; +distinct Speed = CUInt; +distinct Tcflags = CUInt; +struct Termios { + void* dummy; +} + +fn CInt tcgetattr(Fd fd, Termios* self) +{ + unreachable("tcgetattr unavailable"); +} + +fn CInt tcsetattr(Fd fd, CInt optional_actions, Termios* self) +{ + unreachable("tcsetattr unavailable"); +} + +fn CInt tcsendbreak(Fd fd, CInt duration) +{ + unreachable("tcsendbreak unavailable"); +} + +fn CInt tcdrain(Fd fd) +{ + unreachable("tcdrain unavailable"); +} + +fn CInt tcflush(Fd fd, CInt queue_selector) +{ + unreachable("tcflush unavailable"); +} + +fn CInt tcflow(Fd fd, CInt action) +{ + unreachable("tcflow unavailable"); +} + +fn Speed cfgetospeed(Termios* self) +{ + unreachable("cfgetospeed unavailable"); +} + +fn Speed cfgetispeed(Termios* self) +{ + unreachable("cfgetispeed unavailable"); +} + +fn CInt cfsetospeed(Termios* self, Speed speed) +{ + unreachable("cfsetospeed unavailable"); +} + +fn CInt cfsetispeed(Termios* self, Speed speed) +{ + unreachable("cfsetispeed unavailable"); +} + +fn int sendBreak(Fd fd, int duration) +{ + unreachable("sendBreak unavailable"); +} + +fn int drain(Fd fd) +{ + unreachable("drain unavailable"); +} + +fn int flush(Fd fd, int queue_selector) +{ + unreachable("flush unavailable"); +} + +fn int flow(Fd fd, int action) +{ + unreachable("flow unavailable"); +} + +fn Speed Termios.getOSpeed(Termios* self) +{ + unreachable("Termios.getOSpeed unavailable"); +} + +fn Speed Termios.getISpeed(Termios* self) +{ + unreachable("Termios.getISpeed unavailable"); +} + +fn int Termios.setOSpeed(Termios* self, Speed speed) +{ + unreachable("Termios.setOSpeed unavailable"); +} + +fn int Termios.setISpeed(Termios* self, Speed speed) +{ + unreachable("Termios.setISpeed unavailable"); +} + +fn int Termios.getAttr(Termios* self, Fd fd) +{ + unreachable("Termios.getAttr unavailable"); +} + +fn int Termios.setAttr(Termios* self, Fd fd, int optional_actions) +{ + unreachable("Termios.setAttr unavailable"); +} +