cosmopolitan/libc/calls/tcgetattr.c

114 lines
4.4 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
2020-06-15 14:18:57 +00:00
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/metatermios.internal.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/termios.internal.h"
#include "libc/calls/ttydefaults.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#define TCGETS 0x00005401 // linux
#define TIOCGETA 0x40487413 // bsd
int tcgetattr_nt(int, struct termios *);
static int tcgetattr_metal(int fd, struct termios *tio) {
bzero(tio, sizeof(*tio));
tio->c_iflag = TTYDEF_IFLAG;
tio->c_oflag = TTYDEF_OFLAG;
tio->c_lflag = TTYDEF_LFLAG;
tio->c_cflag = TTYDEF_CFLAG;
return 0;
}
static int tcgetattr_bsd(int fd, struct termios *tio) {
int rc;
union metatermios mt;
if ((rc = sys_ioctl(fd, TIOCGETA, &mt)) != -1) {
if (IsXnu()) {
COPY_TERMIOS(tio, &mt.xnu);
} else {
COPY_TERMIOS(tio, &mt.bsd);
}
}
return rc;
}
2020-06-15 14:18:57 +00:00
/**
* Obtains the termios struct.
*
* Here are the approximate defaults you can expect across platforms:
*
* c_iflag = ICRNL IXON
* c_oflag = OPOST ONLCR NL0 CR0 TAB0 BS0 VT0 FF0
* c_cflag = ISIG CREAD CS8
* c_lflag = ISIG ICANON ECHO ECHOE ECHOK IEXTEN ECHOCTL ECHOKE
* cfgetispeed() = B38400
* cfgetospeed() = B38400
* c_cc[VINTR] = CTRL-C
* c_cc[VQUIT] = CTRL-\ # ignore this comment
* c_cc[VERASE] = CTRL-?
* c_cc[VKILL] = CTRL-U
* c_cc[VEOF] = CTRL-D
* c_cc[VTIME] = CTRL-@
* c_cc[VMIN] = CTRL-A
* c_cc[VSTART] = CTRL-Q
* c_cc[VSTOP] = CTRL-S
* c_cc[VSUSP] = CTRL-Z
* c_cc[VEOL] = CTRL-@
* c_cc[VSWTC] = CTRL-@
* c_cc[VREPRINT] = CTRL-R
* c_cc[VDISCARD] = CTRL-O
* c_cc[VWERASE] = CTRL-W
* c_cc[VLNEXT] = CTRL-V
* c_cc[VEOL2] = CTRL-@
*
2020-06-15 14:18:57 +00:00
* @param fd open file descriptor that isatty()
* @param tio is where result is stored
* @return 0 on success, or -1 w/ errno
2020-06-15 14:18:57 +00:00
* @asyncsignalsafe
*/
int tcgetattr(int fd, struct termios *tio) {
int rc;
if (fd < 0) {
rc = einval();
} else if (!tio || (IsAsan() && !__asan_is_valid(tio, sizeof(*tio)))) {
rc = efault();
} else if (fd < g_fds.n && g_fds.p[fd].kind == kFdZip) {
rc = enotty();
} else if (IsLinux()) {
rc = sys_ioctl(fd, TCGETS, tio);
} else if (IsBsd()) {
rc = tcgetattr_bsd(fd, tio);
} else if (IsMetal()) {
rc = tcgetattr_metal(fd, tio);
} else if (IsWindows()) {
rc = tcgetattr_nt(fd, tio);
} else {
rc = enosys();
}
STRACE("tcgetattr(%d, %p) → %d% m", fd, tio, rc);
return rc;
Undiamond Python headers This change gets the Python codebase into a state where it conforms to the conventions of this codebase. It's now possible to include headers from Python, without worrying about ordering. Python has traditionally solved that problem by "diamonding" everything in Python.h, but that's problematic since it means any change to any Python header invalidates all the build artifacts. Lastly it makes tooling not work. Since it is hard to explain to Emacs when I press C-c C-h to add an import line it shouldn't add the header that actually defines the symbol, and instead do follow the nonstandard Python convention. Progress has been made on letting Python load source code from the zip executable structure via the standard C library APIs. System calss now recognizes zip!FILENAME alternative URIs as equivalent to zip:FILENAME since Python uses colon as its delimiter. Some progress has been made on embedding the notice license terms into the Python object code. This is easier said than done since Python has an extremely complicated ownership story. - Some termios APIs have been added - Implement rewinddir() dirstream API - GetCpuCount() API added to Cosmopolitan Libc - More bugs in Cosmopolitan Libc have been fixed - zipobj.com now has flags for mangling the path - Fixed bug a priori with sendfile() on certain BSDs - Polyfill F_DUPFD and F_DUPFD_CLOEXEC across platforms - FIOCLEX / FIONCLEX now polyfilled for fast O_CLOEXEC changes - APE now supports a hybrid solution to no-self-modify for builds - Many BSD-only magnums added, e.g. O_SEARCH, O_SHLOCK, SF_NODISKIO
2021-08-12 07:42:14 +00:00
}