bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// The terminal on fd 0 and fd 1: raw mode, the size, signals, and reading
|
2026-11-30 21:05:59 +01:00
|
|
|
// bytes. (bjoterm terminal) is the Bjolang side: it decodes the bytes, and
|
|
|
|
|
// waits for the rest of a sequence.
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Runtime.Versioning;
|
|
|
|
|
|
|
|
|
|
namespace Bjoterm;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The terminal of this process.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <list type="bullet">
|
|
|
|
|
/// <item>One per process: <see cref="Open"/> returns the same object each
|
|
|
|
|
/// time. Linux only.</item>
|
|
|
|
|
/// <item>Modes: <see cref="EnterRaw"/> saves the current modes and sets raw
|
|
|
|
|
/// mode; <see cref="LeaveRaw"/> restores the saved modes. They are also
|
|
|
|
|
/// restored at process exit.</item>
|
|
|
|
|
/// <item>Signals, registered at the first open:
|
2026-11-30 21:05:59 +01:00
|
|
|
/// - SIGWINCH: <see cref="TakeResized"/> returns true once, and a read
|
|
|
|
|
/// that waits returns <see cref="Resized"/>.
|
|
|
|
|
/// - SIGCONT in raw mode: raw mode is set again, it counts as a resize,
|
|
|
|
|
/// and .NET's own handling (which sets .NET's modes) is cancelled.
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/// - SIGTSTP in raw mode (only with ISIG on): the saved modes are restored
|
|
|
|
|
/// and the process stops with SIGSTOP. SIGCONT then sets raw mode
|
|
|
|
|
/// again.</item>
|
|
|
|
|
/// <item>SIGTERM, SIGHUP and SIGQUIT in raw mode: the saved modes are
|
|
|
|
|
/// restored, and the signal is not cancelled. A program that cancels one of
|
|
|
|
|
/// them to go on running is in cooked mode after it.</item>
|
2026-11-30 21:05:59 +01:00
|
|
|
/// <item>Reads run one at a time.</item>
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/// </list>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
[SupportedOSPlatform("linux")]
|
|
|
|
|
public sealed unsafe class Tty {
|
|
|
|
|
// Linux termios, the same on x86-64 and arm64.
|
|
|
|
|
private const int TermiosSize = 60;
|
|
|
|
|
private const int IFlag = 0, CFlag = 8, LFlag = 12, CcOffset = 17;
|
|
|
|
|
private const int VTIME = 5, VMIN = 6;
|
|
|
|
|
private const uint BRKINT = 0x2, INPCK = 0x10, ISTRIP = 0x20, ICRNL = 0x100, IXON = 0x400;
|
|
|
|
|
private const uint CS8 = 0x30;
|
|
|
|
|
private const uint ISIG = 0x1, ICANON = 0x2, ECHO = 0x8, IEXTEN = 0x8000;
|
|
|
|
|
private const int TCSADRAIN = 1;
|
|
|
|
|
private const ulong TIOCGWINSZ = 0x5413;
|
|
|
|
|
private const short POLLIN = 0x1, POLLHUP = 0x10, POLLERR = 0x8;
|
|
|
|
|
private const int O_NONBLOCK = 0x800, O_CLOEXEC = 0x80000;
|
|
|
|
|
private const int EINTR = 4, EAGAIN = 11;
|
|
|
|
|
private const int SIGTSTP = 20, SIGSTOP = 19;
|
|
|
|
|
|
2026-11-30 21:05:59 +01:00
|
|
|
/// What <see cref="ReadAsync"/> returns at end of input.
|
|
|
|
|
public const int EndOfInput = -1;
|
|
|
|
|
|
|
|
|
|
/// What <see cref="ReadAsync"/> returns after a resize.
|
|
|
|
|
public const int Resized = -2;
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
|
|
|
|
|
private static readonly object openGate = new();
|
|
|
|
|
private static Tty? opened;
|
|
|
|
|
|
|
|
|
|
private readonly object gate = new();
|
|
|
|
|
private readonly byte[] saved = new byte[TermiosSize];
|
|
|
|
|
private readonly byte[] raw = new byte[TermiosSize];
|
|
|
|
|
private readonly List<PosixSignalRegistration> registrations = new();
|
|
|
|
|
private readonly int wakeRead, wakeWrite;
|
|
|
|
|
// Inside EnterRaw ... LeaveRaw.
|
|
|
|
|
private bool rawWanted;
|
|
|
|
|
// The raw modes are set on the terminal now.
|
|
|
|
|
private bool rawSet;
|
|
|
|
|
private int resized;
|
2026-11-30 21:05:59 +01:00
|
|
|
private readonly object readGate = new();
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
|
|
|
|
|
private Tty(int wakeRead, int wakeWrite) {
|
|
|
|
|
this.wakeRead = wakeRead;
|
|
|
|
|
this.wakeWrite = wakeWrite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Opens the terminal. Raises <see cref="PlatformNotSupportedException"/>
|
|
|
|
|
/// on a system other than Linux, and <see cref="IOException"/> when fd 0
|
|
|
|
|
/// or fd 1 is not a terminal.
|
|
|
|
|
public static Tty Open() {
|
|
|
|
|
lock (openGate) {
|
|
|
|
|
if (opened is not null) return opened;
|
|
|
|
|
if (!OperatingSystem.IsLinux()) {
|
|
|
|
|
throw new PlatformNotSupportedException("bjoterm supports only Linux for now.");
|
|
|
|
|
}
|
|
|
|
|
if (isatty(0) != 1) throw new IOException("Standard input is not a terminal.");
|
|
|
|
|
if (isatty(1) != 1) throw new IOException("Standard output is not a terminal.");
|
|
|
|
|
int* fds = stackalloc int[2];
|
|
|
|
|
if (pipe2(fds, O_NONBLOCK | O_CLOEXEC) != 0) throw LastError();
|
|
|
|
|
var tty = new Tty(fds[0], fds[1]);
|
|
|
|
|
tty.registrations.Add(PosixSignalRegistration.Create(PosixSignal.SIGWINCH, tty.OnResize));
|
|
|
|
|
tty.registrations.Add(PosixSignalRegistration.Create(PosixSignal.SIGCONT, tty.OnContinue));
|
|
|
|
|
tty.registrations.Add(PosixSignalRegistration.Create(PosixSignal.SIGTSTP, tty.OnStop));
|
|
|
|
|
// These end the process with no finally block and no
|
|
|
|
|
// ProcessExit. They are not cancelled here.
|
|
|
|
|
foreach (var signal in new[] { PosixSignal.SIGTERM, PosixSignal.SIGHUP, PosixSignal.SIGQUIT }) {
|
|
|
|
|
tty.registrations.Add(PosixSignalRegistration.Create(signal, tty.OnEnd));
|
|
|
|
|
}
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (_, _) => tty.RestoreAtExit();
|
|
|
|
|
opened = tty;
|
|
|
|
|
return tty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The size as [columns, rows]. [80, 24] when the terminal does not say.
|
|
|
|
|
public int[] Size() {
|
|
|
|
|
ushort* ws = stackalloc ushort[4];
|
|
|
|
|
if (ioctl(1, TIOCGWINSZ, ws) != 0 && ioctl(0, TIOCGWINSZ, ws) != 0) return [80, 24];
|
|
|
|
|
int rows = ws[0], cols = ws[1];
|
|
|
|
|
return [cols > 0 ? cols : 80, rows > 0 ? rows : 24];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsRaw {
|
|
|
|
|
get { lock (gate) { return rawWanted; } }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Saves the current modes and sets raw mode. With
|
|
|
|
|
/// <paramref name="signals"/>, Ctrl-C, Ctrl-Z and Ctrl-\ still send
|
|
|
|
|
/// their signals. Raises <see cref="InvalidOperationException"/> when
|
|
|
|
|
/// raw mode is set already.
|
|
|
|
|
public void EnterRaw(bool signals) {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (rawWanted) throw new InvalidOperationException("The terminal is in raw mode already.");
|
|
|
|
|
fixed (byte* s = saved) {
|
|
|
|
|
if (tcgetattr(0, s) != 0) throw LastError();
|
|
|
|
|
}
|
|
|
|
|
saved.CopyTo(raw, 0);
|
|
|
|
|
Clear(raw, IFlag, BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
|
|
|
|
Set(raw, CFlag, CS8);
|
|
|
|
|
Clear(raw, LFlag, ECHO | ICANON | IEXTEN | (signals ? 0 : ISIG));
|
|
|
|
|
raw[CcOffset + VMIN] = 1;
|
|
|
|
|
raw[CcOffset + VTIME] = 0;
|
|
|
|
|
ApplyRaw();
|
|
|
|
|
rawWanted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Restores the modes saved by <see cref="EnterRaw"/>. Does nothing
|
|
|
|
|
/// outside raw mode.
|
|
|
|
|
public void LeaveRaw() {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (!rawWanted) return;
|
|
|
|
|
rawWanted = false;
|
|
|
|
|
RestoreSaved();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stops the process as Ctrl-Z does in cooked mode: restores the saved
|
|
|
|
|
/// modes, and sends SIGTSTP to the process group. When the process
|
|
|
|
|
/// continues, raw mode is set again.
|
|
|
|
|
public void Suspend() {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (rawSet) RestoreSaved();
|
|
|
|
|
}
|
|
|
|
|
kill(0, SIGTSTP);
|
|
|
|
|
// SIGTSTP may be ignored or cancelled (a shell with job control).
|
|
|
|
|
// Then nothing stopped, and raw mode comes back here.
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (rawWanted && !rawSet) ApplyRaw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 21:05:59 +01:00
|
|
|
/// Whether the terminal was resized, or the process continued, since
|
|
|
|
|
/// the last call.
|
|
|
|
|
public bool TakeResized() => Interlocked.Exchange(ref resized, 0) != 0;
|
|
|
|
|
|
|
|
|
|
/// Reads bytes into <paramref name="buffer"/>.
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/// <list type="bullet">
|
2026-11-30 21:05:59 +01:00
|
|
|
/// <item>Returns the count read; 0 after <paramref name="timeoutMs"/>
|
|
|
|
|
/// (below 0 waits for ever); <see cref="EndOfInput"/>; or
|
|
|
|
|
/// <see cref="Resized"/> when a resize came first.</item>
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/// <item>Cancelling the token raises
|
2026-11-30 21:05:59 +01:00
|
|
|
/// <see cref="OperationCanceledException"/>. Nothing has been read
|
|
|
|
|
/// then.</item>
|
|
|
|
|
/// <item>Reads run one at a time: a read waits for the one before it.
|
|
|
|
|
/// A cancelled read ends soon after, as its wait is woken.</item>
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
/// </list>
|
2026-11-30 21:05:59 +01:00
|
|
|
public Task<int> ReadAsync(byte[] buffer, int timeoutMs, CancellationToken cancel) {
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
// poll blocks the thread, so the read runs on a thread of its own.
|
|
|
|
|
return Task.Factory.StartNew(() => {
|
2026-11-30 21:05:59 +01:00
|
|
|
lock (readGate) {
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
using var wake = cancel.Register(Wake);
|
2026-11-30 21:05:59 +01:00
|
|
|
return Read(buffer, timeoutMs, cancel);
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
}
|
2026-11-30 21:05:59 +01:00
|
|
|
// Not the caller's token: the task always runs, and raises from
|
|
|
|
|
// Read when the token is cancelled.
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 21:05:59 +01:00
|
|
|
private int Read(byte[] buffer, int timeoutMs, CancellationToken cancel) {
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
long deadline = timeoutMs < 0 ? long.MaxValue : Environment.TickCount64 + timeoutMs;
|
|
|
|
|
pollfd* fds = stackalloc pollfd[2];
|
|
|
|
|
while (true) {
|
|
|
|
|
cancel.ThrowIfCancellationRequested();
|
2026-11-30 21:05:59 +01:00
|
|
|
if (Volatile.Read(ref resized) != 0) return Resized;
|
|
|
|
|
int wait = deadline == long.MaxValue ? -1 : (int)Math.Max(0, deadline - Environment.TickCount64);
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
fds[0] = new pollfd { fd = 0, events = POLLIN };
|
|
|
|
|
fds[1] = new pollfd { fd = wakeRead, events = POLLIN };
|
|
|
|
|
int n = poll(fds, 2, wait);
|
|
|
|
|
if (n < 0) {
|
|
|
|
|
if (Marshal.GetLastPInvokeError() == EINTR) continue;
|
|
|
|
|
throw LastError();
|
|
|
|
|
}
|
2026-11-30 21:05:59 +01:00
|
|
|
if ((fds[1].revents & POLLIN) != 0) {
|
|
|
|
|
DrainWake();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
if ((fds[0].revents & (POLLIN | POLLHUP | POLLERR)) != 0) {
|
|
|
|
|
long got;
|
|
|
|
|
fixed (byte* b = buffer) {
|
|
|
|
|
got = read(0, b, buffer.Length);
|
|
|
|
|
}
|
2026-11-30 21:05:59 +01:00
|
|
|
if (got > 0) return (int)got;
|
|
|
|
|
if (got == 0) return EndOfInput;
|
|
|
|
|
int errno = Marshal.GetLastPInvokeError();
|
|
|
|
|
if (errno == EINTR || errno == EAGAIN) continue;
|
|
|
|
|
throw LastError(errno);
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
}
|
2026-11-30 21:05:59 +01:00
|
|
|
if (n == 0) return 0;
|
bjoterm: raw mode, key events and escape sequences
- (bjoterm terminal): terminal-open, with-raw-mode, terminal-size,
terminal-suspend, read-event and read-event/timeout, and escape
sequences for the cursor, clearing, the alternate screen, bracketed
paste and styles. Linux only.
- glue/Tty.cs: termios, poll on fd 0, SIGWINCH, SIGCONT, SIGTSTP, and
restoring the modes at SIGTERM, SIGHUP, SIGQUIT and process exit.
- glue/Keys.cs: the decoder of CSI and SS3 keys with modifiers, Alt,
UTF-8 and bracketed paste.
- Tests: the decoder from bytes, and raw mode on a pseudo-terminal under
script. plan.org has the decisions and the phases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:31:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnResize(PosixSignalContext ctx) {
|
|
|
|
|
Interlocked.Exchange(ref resized, 1);
|
|
|
|
|
Wake();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnContinue(PosixSignalContext ctx) {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (!rawWanted) return;
|
|
|
|
|
ctx.Cancel = true;
|
|
|
|
|
// In the background, setting modes would stop the process with
|
|
|
|
|
// SIGTTOU. The next SIGCONT, when it is in the foreground, sets
|
|
|
|
|
// them.
|
|
|
|
|
if (tcgetpgrp(0) == getpgrp()) ApplyRaw();
|
|
|
|
|
}
|
|
|
|
|
Interlocked.Exchange(ref resized, 1);
|
|
|
|
|
Wake();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStop(PosixSignalContext ctx) {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (!rawSet) return;
|
|
|
|
|
ctx.Cancel = true;
|
|
|
|
|
RestoreSaved();
|
|
|
|
|
}
|
|
|
|
|
// SIGTSTP is cancelled here, so stop with SIGSTOP. A shell reports
|
|
|
|
|
// both as stopped.
|
|
|
|
|
kill(Environment.ProcessId, SIGSTOP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEnd(PosixSignalContext ctx) => RestoreAtExit();
|
|
|
|
|
|
|
|
|
|
private void RestoreAtExit() {
|
|
|
|
|
lock (gate) {
|
|
|
|
|
if (rawSet) RestoreSaved();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The callers hold the gate.
|
|
|
|
|
private void ApplyRaw() {
|
|
|
|
|
fixed (byte* r = raw) {
|
|
|
|
|
if (tcsetattr(0, TCSADRAIN, r) != 0) throw LastError();
|
|
|
|
|
}
|
|
|
|
|
rawSet = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RestoreSaved() {
|
|
|
|
|
fixed (byte* s = saved) {
|
|
|
|
|
tcsetattr(0, TCSADRAIN, s);
|
|
|
|
|
}
|
|
|
|
|
rawSet = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Wake() {
|
|
|
|
|
byte one = 1;
|
|
|
|
|
write(wakeWrite, &one, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrainWake() {
|
|
|
|
|
byte* b = stackalloc byte[64];
|
|
|
|
|
while (read(wakeRead, b, 64) > 0) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Clear(byte[] t, int offset, uint bits) =>
|
|
|
|
|
BitConverter.TryWriteBytes(t.AsSpan(offset), BitConverter.ToUInt32(t, offset) & ~bits);
|
|
|
|
|
|
|
|
|
|
private static void Set(byte[] t, int offset, uint bits) =>
|
|
|
|
|
BitConverter.TryWriteBytes(t.AsSpan(offset), BitConverter.ToUInt32(t, offset) | bits);
|
|
|
|
|
|
|
|
|
|
private static Win32Exception LastError() => LastError(Marshal.GetLastPInvokeError());
|
|
|
|
|
|
|
|
|
|
private static Win32Exception LastError(int errno) => new(errno);
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
private struct pollfd {
|
|
|
|
|
public int fd;
|
|
|
|
|
public short events;
|
|
|
|
|
public short revents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int isatty(int fd);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int tcgetattr(int fd, byte* termios);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int tcsetattr(int fd, int when, byte* termios);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int tcgetpgrp(int fd);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int getpgrp();
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int ioctl(int fd, ulong request, ushort* arg);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int pipe2(int* fds, int flags);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int poll(pollfd* fds, ulong n, int timeout);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern long read(int fd, byte* buffer, long count);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern long write(int fd, byte* buffer, long count);
|
|
|
|
|
[DllImport("libc", SetLastError = true)] private static extern int kill(int pid, int signal);
|
|
|
|
|
}
|