libc library

The libc calls twk's packages share on Linux and macOS, bound through dart:ffi: poll and PollFd, read, write, open, ttyname, signal, getppid, pipe, close, ioctl (with fionread), tcgetattr and tcsetattr, calloc and free, and errno. The core reads the process's terminal with them, twk_term's Pty drives a pseudo-terminal with them beside calls of its own, and twk's file watch takes its memory from them.

For the VM on Linux and macOS: a symbol is looked up in the process on its first call, so importing it costs nothing elsewhere, but calling one on Windows or the web fails. It is no part of package:twk_core/twk_core.dart, which compiles for the web too.

How many bytes the terminal on stdin holds, unread:

import 'dart:ffi';

int unread() {
  final n = calloc(1, sizeOf<Int>()).cast<Int>();
  try {
    return ioctl(0, fionread, n.cast()) == 0 ? n.value : 0;
  } finally {
    free(n.cast());
  }
}

Classes

PollFd
struct pollfd, the same on Linux and macOS.

Constants

eintr → const int
EINTR: a call interrupted by a signal.
oWronly → const int
O_WRONLY, for open: for writing.
pollErr → const int
POLLERR: the descriptor has an error.
pollHup → const int
POLLHUP: the other end hung up.
pollIn → const int
POLLIN: there is data to read.
pollNval → const int
POLLNVAL: the descriptor is not open.
pollOut → const int
POLLOUT: a write would not block.
tcsanow → const int
TCSANOW, for tcsetattr: the attributes change at once.

Properties

eagain → int
EAGAIN: a non-blocking call would block.
final
errno → int
errno, read right after the leaf call that set it: after one that is not a leaf call the VM may have changed it.
no setter
fionread → int
FIONREAD, for ioctl: how many bytes a descriptor holds to be read (an int).
final
oNoctty → int
O_NOCTTY, for open: a terminal does not become the controlling one.
final
oNonblock → int
O_NONBLOCK, for open: a call on the descriptor does not block.
final
sigtstp → int
SIGTSTP, for signal: what stops a job.
final

Functions

calloc(int count, int size) → Pointer<Void>
calloc(3): count zeroed elements of size bytes, or nullptr.
close(int fd) → int
close(2): closes fd; 0, or -1.
free(Pointer<Void> p) → void
free(3): frees p, taken from calloc.
getppid() → int
getppid(2), which can't fail and is safe in a signal handler.
ioctl(int fd, int request, Pointer<Void> arg) → int
ioctl(2): the request (fionread, …) on fd with arg; -1 on an error.
open(Pointer<Char> path, int flags, int mode) → int
open(2): opens path as flags say, a new file of mode; a descriptor, or -1. A leaf call, like read.
pipe(Pointer<Int> fds) → int
pipe(2): a pipe, its read end in fds[0], its write end in fds[1]; 0, or -1.
poll(Pointer<PollFd> fds, int count, int timeout) → int
poll(2): waits for the events of count descriptors of fds, at most timeout milliseconds (-1 for ever); how many have some, 0 when the time ran out, or -1 (a signal came: EINTR).
read(int fd, Pointer<Void> buf, int count) → int
read(2): reads at most count bytes of fd into buf; how many, 0 at the end, or -1. A leaf call: read only what is there (after poll, or as many as fionread says), or it blocks the isolate's thread.
sigaction(int number, Pointer<Void> action, Pointer<Void> old) → int
sigaction(2): number's disposition set from action unless that is nullptr, and the one before it put in old unless that is; 0, or -1. A leaf call, like read. The structs are the caller's to lay out.
signal(int number, Pointer<Void> handler) → Pointer<Void>
signal(2): handler called on number from now on; the one before it, or SIG_ERR. A leaf call, like read. Only for a signal Dart can't watch: it replaces what ProcessSignal.watch set. The handler runs outside Dart, so it is a native function. The process's terminal holds SIGTSTP while raw (PosixTty).
tcgetattr(int fd, Pointer<Void> termios) → int
tcgetattr(3): reads the attributes of the terminal fd into termios; 0, or -1.
tcsetattr(int fd, int when, Pointer<Void> termios) → int
tcsetattr(3): sets the attributes of the terminal fd to termios, when (tcsanow, …) says when; 0, or -1.
ttyname(int fd) → Pointer<Char>
ttyname(3): the path of the terminal fd is, in memory of libc's that the next call overwrites; null where it is none.
write(int fd, Pointer<Void> buf, int count) → int
write(2): writes at most count bytes of buf to fd; how many, or -1. A leaf call, like read.