LibC/Kernel: Cleanup termios code

This is still not correct, but much better than it used to be
This commit is contained in:
2024-07-30 11:10:43 +03:00
parent 9bc02c81f8
commit 681d8327f5
9 changed files with 99 additions and 49 deletions

View File

@@ -15,8 +15,8 @@ __BEGIN_DECLS
O(SYS_OPENAT, openat) \
O(SYS_SEEK, seek) \
O(SYS_TELL, tell) \
O(SYS_GET_TERMIOS, gettermios) \
O(SYS_SET_TERMIOS, settermios) \
O(SYS_TCGETATTR, tcgetattr) \
O(SYS_TCSETATTR, tcsetattr) \
O(SYS_FORK, fork) \
O(SYS_EXEC, exec) \
O(SYS_SLEEP, sleep) \

View File

@@ -35,6 +35,8 @@ struct termios
tcflag_t c_cflag; /* Control modes. */
tcflag_t c_lflag; /* Local modes. */
cc_t c_cc[NCCS]; /* Control characters. */
speed_t c_ospeed;
speed_t c_ispeed;
};
#define BRKINT 0x001

View File

@@ -1,14 +1,65 @@
#include <errno.h>
#include <sys/syscall.h>
#include <termios.h>
#include <unistd.h>
speed_t cfgetispeed(const struct termios*);
speed_t cfgetispeed(const struct termios* termios)
{
return termios->c_ispeed;
}
speed_t cfgetospeed(const struct termios*);
speed_t cfgetospeed(const struct termios* termios)
{
return termios->c_ospeed;
}
int cfsetispeed(struct termios*, speed_t);
static bool is_valid_speed(speed_t speed)
{
switch (speed)
{
case B0:
case B50:
case B75:
case B110:
case B134:
case B150:
case B200:
case B300:
case B600:
case B1200:
case B1800:
case B2400:
case B4800:
case B9600:
case B19200:
case B38400:
return true;
default:
return false;
}
}
int cfsetospeed(struct termios*, speed_t);
int cfsetispeed(struct termios* termios, speed_t speed)
{
if (!is_valid_speed(speed))
{
errno = EINVAL;
return -1;
}
termios->c_ispeed = speed;
return 0;
}
int cfsetospeed(struct termios* termios, speed_t speed)
{
if (!is_valid_speed(speed))
{
errno = EINVAL;
return -1;
}
termios->c_ospeed = speed;
return 0;
}
int tcdrain(int);
@@ -16,16 +67,16 @@ int tcflow(int, int);
int tcflush(int, int);
int tcgetattr(int, struct termios* termios)
int tcgetattr(int fildes, struct termios* termios)
{
return syscall(SYS_GET_TERMIOS, termios);
return syscall(SYS_TCGETATTR, fildes, termios);
}
pid_t tcgetsid(int);
int tcsendbreak(int, int);
int tcsetattr(int, int, const struct termios* termios)
int tcsetattr(int fildes, int optional_actions, const struct termios* termios)
{
return syscall(SYS_SET_TERMIOS, termios);
return syscall(SYS_TCSETATTR, fildes, optional_actions, termios);
}