Fix bugs in termios library and cleanup code

This change fixes an issue with the tcflow() magic numbers that was
causing bash to freeze up on Linux. While auditing termios polyfills,
several other issues were identified with XNU/BSD compatibility.

Out of an abundance of caution this change undefines as much surface
area from libc/calls/struct/termios.h as possible, so that autoconf
scripts are less likely to detect non-POSIX teletypewriter APIs that
haven't been polyfilled by Cosmopolitan.

This is a *breaking change* for your static archives in /opt/cosmos if
you use the cosmocc toolchain. That's because this change disables the
ioctl() undiamonding trick for code outside the monorepo, specifically
because it'll lead to brittle ABI breakages like this. If you're using
the cosmocc toolchain, you'll need to rebuild libraries like ncurses,
readline, etc. Yes diamonds cause bloat. To work around that, consider
using tcgetwinsize() instead of ioctl(TIOCGWINSZ) since it'll help you
avoid pulling every single ioctl-related polyfill into the linkage.

The cosmocc script was specifying -DNDEBUG for some reason. It's fixed.
This commit is contained in:
Justine Tunney 2023-06-14 17:02:57 -07:00
parent 06b749ae03
commit 4778cd4d27
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
187 changed files with 1025 additions and 1848 deletions

View file

@ -17,18 +17,22 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
#define CBAUD 0x100f
#define CBAUDEX 0x1000
/**
* Returns input baud rate.
* @asyncsignalsafe
*/
uint32_t cfgetispeed(const struct termios *t) {
if (CBAUD) {
if (IsLinux()) {
return t->c_cflag & CBAUD;
} else {
return t->c_ispeed;
return t->_c_ispeed;
}
}
@ -37,43 +41,23 @@ uint32_t cfgetispeed(const struct termios *t) {
* @asyncsignalsafe
*/
uint32_t cfgetospeed(const struct termios *t) {
if (CBAUD) {
if (IsLinux()) {
return t->c_cflag & CBAUD;
} else {
return t->c_ospeed;
return t->_c_ospeed;
}
}
/**
* Sets input baud rate.
*
* @return 0 on success, or -1 w/ errno
* @asyncsignalsafe
*/
int cfsetispeed(struct termios *t, unsigned speed) {
if (speed) {
if (CBAUD) {
if (!(speed & ~CBAUD)) {
t->c_cflag &= ~CBAUD;
t->c_cflag |= speed;
} else {
return einval();
}
} else {
t->c_ispeed = speed;
}
}
return 0;
}
/**
* Sets output baud rate.
*
* @param speed can be `B0`, `B50`, `B38400`, `B4000000`, etc.
* @return 0 on success, or -1 w/ errno
* @raise EINVAL if `speed` isn't valid
* @asyncsignalsafe
*/
int cfsetospeed(struct termios *t, unsigned speed) {
if (CBAUD) {
int cfsetospeed(struct termios *t, uint32_t speed) {
if (IsLinux()) {
if (!(speed & ~CBAUD)) {
t->c_cflag &= ~CBAUD;
t->c_cflag |= speed;
@ -82,7 +66,28 @@ int cfsetospeed(struct termios *t, unsigned speed) {
return einval();
}
} else {
t->c_ospeed = speed;
t->_c_ospeed = speed;
return 0;
}
}
/**
* Sets input baud rate.
*
* @param speed can be `B0`, `B50`, `B38400`, `B4000000`, etc.
* @return 0 on success, or -1 w/ errno
* @raise EINVAL if `speed` isn't valid
* @asyncsignalsafe
*/
int cfsetispeed(struct termios *t, uint32_t speed) {
if (IsLinux()) {
if (speed) {
return cfsetospeed(t, speed);
} else {
return 0;
}
} else {
t->_c_ispeed = speed;
return 0;
}
}