Skip to content

Commit

Permalink
Merge pull request #932 from no92/termios-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke authored Nov 30, 2023
2 parents f23f7bc + c6cb748 commit 96e57af
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 57 deletions.
58 changes: 43 additions & 15 deletions abis/linux/termios.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,33 @@ typedef unsigned int tcflag_t;
#define VEOL2 16

// bitwise flags for c_iflag in struct termios
#define BRKINT 0000002
#define ICRNL 0000400
#define IGNBRK 0000001
#define IGNCR 0000200
#define BRKINT 0000002
#define IGNPAR 0000004
#define INLCR 0000100
#define PARMRK 0000010
#define INPCK 0000020
#define ISTRIP 0000040
#define INLCR 0000100
#define IGNCR 0000200
#define ICRNL 0000400
#define IUCLC 0001000
#define IXON 0002000
#define IXANY 0004000
#define IXOFF 0010000
#define IXON 0002000
#define PARMRK 0000010
#define IMAXBEL 0020000
#define IUTF8 0040000

// bitwise flags for c_oflag in struct termios
#define OPOST 0000001
#define OLCUC 0000002
#define ONLCR 0000004
#define OCRNL 0000010
#define ONOCR 0000020
#define ONLRET 0000040
#define OFDEL 0000200
#define OFILL 0000100
#define OFDEL 0000200

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_XOPEN_SOURCE)

#define NLDLY 0000400
#define NL0 0000000
Expand All @@ -64,19 +70,20 @@ typedef unsigned int tcflag_t;
#define TAB2 0010000
#define TAB3 0014000

#define XTABS 0014000
#define BSDLY 0020000
#define BS0 0000000
#define BS1 0020000

#define VTDLY 0040000
#define VT0 0000000
#define VT1 0040000

#define FFDLY 0100000
#define FF0 0000000
#define FF1 0100000

#endif

#define VTDLY 0040000
#define VT0 0000000
#define VT1 0040000

// bitwise constants for c_cflag in struct termios
#define CSIZE 0000060
#define CS5 0000000
Expand All @@ -92,16 +99,37 @@ typedef unsigned int tcflag_t;
#define CLOCAL 0004000

// bitwise constants for c_lflag in struct termios
#define ISIG 0000001
#define ICANON 0000002
#define ECHO 0000010
#define ECHOE 0000020
#define ECHOK 0000040
#define ECHONL 0000100
#define ICANON 0000002
#define IEXTEN 0100000
#define ISIG 0000001
#define NOFLSH 0000200
#define TOSTOP 0000400
#define IEXTEN 0100000

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)

#define EXTA 0000016
#define EXTB 0000017
#define CBAUD 0010017
#define CBAUDEX 0010000
#define CIBAUD 002003600000
#define CMSPAR 010000000000
#define CRTSCTS 020000000000

#define XCASE 0000004
#define ECHOCTL 0001000
#define ECHOPRT 0002000
#define ECHOKE 0004000
#define FLUSHO 0010000
#define PENDIN 0040000
#define EXTPROC 0200000

#define XTABS 0014000

#endif

struct termios {
tcflag_t c_iflag;
Expand Down
4 changes: 4 additions & 0 deletions abis/mlibc/termios.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ typedef unsigned int tcflag_t;
#define HUPCL 0x0040
#define CLOCAL 0x0080

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#define CBAUD 0x100F
#endif

// bitwise constants for c_lflag in struct termios
#define ECHO 0x0001
#define ECHOE 0x0002
Expand Down
1 change: 1 addition & 0 deletions options/glibc/include/sys/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if __MLIBC_LINUX_OPTION
# include <asm/ioctls.h>
# include <bits/winsize.h>
# include <sys/ttydefaults.h>
#endif

#ifdef __cplusplus
Expand Down
25 changes: 17 additions & 8 deletions options/posix/generic/termios-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
#include <mlibc/posix-sysdeps.hpp>

speed_t cfgetispeed(const struct termios *tios) {
return tios->ibaud;
return tios->c_cflag & CBAUD;
}

speed_t cfgetospeed(const struct termios *tios) {
return tios->obaud;
return tios->c_cflag & CBAUD;
}
int cfsetispeed(struct termios *, speed_t) {
__ensure(!"Not implemented");
__builtin_unreachable();

int cfsetispeed(struct termios *termios, speed_t speed) {
return speed ? cfsetospeed(termios, speed) : 0;
}
int cfsetospeed(struct termios *, speed_t) {
__ensure(!"Not implemented");
__builtin_unreachable();

int cfsetospeed(struct termios *termios, speed_t speed) {
if(speed & ~CBAUD) {
errno = EINVAL;
return -1;
}

termios->c_cflag &= ~CBAUD;
termios->c_cflag |= speed;

return 0;
}

void cfmakeraw(struct termios *t) {
Expand Down
19 changes: 18 additions & 1 deletion options/posix/include/sys/ttydefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
#define _SYS_TTYDEFAULTS_H

// Values taken from musl

#define TTYDEF_IFLAG (BRKINT | ISTRIP | ICRNL | IMAXBEL | IXON | IXANY)
#define TTYDEF_OFLAG (OPOST | ONLCR | XTABS)
#define TTYDEF_LFLAG (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
#define TTYDEF_CFLAG (CREAD | CS7 | PARENB | HUPCL)
#define TTYDEF_SPEED (B9600)

#define CTRL(x) ((x) & 037)
#define CEOF CTRL('d')

#define CEOL '\0'
#define CEOL2 '\0'
#define CSTATUS '\0'

#define CERASE 0177
#define CINTR CTRL('c')
#define CKILL CTRL('u')
#define CMIN 1
#define CQUIT 034
#define CSUSP CTRL('z')
#define CTIME 0
#define CDSUSP CTRL('y')
#define CSTART CTRL('q')
#define CSTOP CTRL('s')
#define CLNEXT CTRL('v')
#define CDISCARD CTRL('o')
#define CWERASE CTRL('w')
#define CREPRINT CTRL('r')
#define CDISCARD CTRL('o')
#define CEOT CEOF
#define CBRK CEOL
#define CRPRNT CREPRINT
#define CFLUSH CDISCARD

#endif // _SYS_TTYDEFAULTS_H
60 changes: 34 additions & 26 deletions options/posix/include/termios.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ extern "C" {

#include <bits/winsize.h>

#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#include <sys/ttydefaults.h>
#endif

// baud rate constants for speed_t
#define B0 0
#define B50 1
Expand All @@ -22,31 +26,43 @@ extern "C" {
#define B300 7
#define B600 8
#define B1200 9
#define B1800 10
#define B2400 11
#define B4800 12
#define B9600 13
#define B19200 14
#define B38400 15
#define B57600 16
#define B115200 17
#define B230400 18
#define B1800 10
#define B2400 11
#define B4800 12
#define B9600 13
#define B19200 14
#define B38400 15
#define B57600 0010001
#define B115200 0010002
#define B230400 0010003
#define B460800 0010004
#define B500000 0010005
#define B576000 0010006
#define B921600 0010007
#define B1000000 0010010
#define B1152000 0010011
#define B1500000 0010012
#define B2000000 0010013
#define B2500000 0010014
#define B3000000 0010015
#define B3500000 0010016
#define B4000000 0010017

// constants for tcsetattr()
#define TCSANOW 1
#define TCSADRAIN 2
#define TCSAFLUSH 3
#define TCSANOW 0
#define TCSADRAIN 1
#define TCSAFLUSH 2

// constants for tcflush()
#define TCIFLUSH 1
#define TCIFLUSH 0
#define TCOFLUSH 1
#define TCIOFLUSH 2
#define TCOFLUSH 3

// constants for tcflow()
#define TCIOFF 1
#define TCION 2
#define TCOOFF 3
#define TCOON 4
#define TCOOFF 0
#define TCOON 1
#define TCIOFF 2
#define TCION 3

#define TIOCM_DTR 0x002
#define TIOCM_RTS 0x004
Expand Down Expand Up @@ -76,14 +92,6 @@ int tcsetattr(int, int, const struct termios *);
#define TIOCSWINSZ 0x5414
#define TIOCGSID 0x5429

#define ECHOCTL 0001000
#define FLUSHO 0010000
#define IMAXBEL 0020000
#define ECHOKE 0040000

#define IUCLC 0001000
#define IUTF8 0040000

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion options/posix/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ extern "C" {
#define STDIN_FILENO 0
#define STDOUT_FILENO 1

#define _POSIX_VDISABLE (-1)
#define _POSIX_VDISABLE '\0'

#define L_ctermid 20

Expand Down
9 changes: 4 additions & 5 deletions sysdeps/managarm/generic/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,10 @@ int sys_tcgetattr(int fd, struct termios *attr) {
}

int sys_tcsetattr(int fd, int when, const struct termios *attr) {
if(when != TCSANOW)
mlibc::infoLogger() << "\e[35mmlibc: tcsetattr() when argument ignored\e[39m"
<< frg::endlog;
int result;
if(int e = sys_ioctl(fd, TCSETS, const_cast<struct termios *>(attr), &result); e)
if(when < TCSANOW || when > TCSAFLUSH)
return EINVAL;

if(int e = sys_ioctl(fd, TCSETS, const_cast<struct termios *>(attr), nullptr); e)
return e;
return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion sysdeps/managarm/generic/ioctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
managarm::fs::GenericIoctlReply<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
*result = resp.result();
if(result)
*result = resp.result();
return 0;
}
case TIOCSCTTY: {
Expand Down

0 comments on commit 96e57af

Please sign in to comment.