From f54ceb35af46b89709c7ae03e909792b8b1f834f4123de93c435a38580c6bd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Tue, 1 Dec 2026 08:13:20 +0100 Subject: [PATCH] Ctrl-C of a value command that reads a terminal or a FIFO The file of < is opened with #:cancellable? #t, so Ctrl-C of li < /dev/tty takes nothing from the terminal, and the next line goes to the editor. tests/interactive.bjo checks it. Co-Authored-By: Claude Opus 5.5 --- plan.org | 8 ++++---- src/exec.bjo | 4 +++- tests/interactive.bjo | 4 ++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/plan.org b/plan.org index 4363573..29b6231 100644 --- a/plan.org +++ b/plan.org @@ -280,10 +280,10 @@ the text above:* after one with ~>~ is an error. ~2>~, ~2>&1~, assignments and ~&~ of value commands are errors. - A pipe of only value commands runs in the shell, with an empty input - or the file of ~<~. Ctrl-C cancels it with status 130, but only at a - point where a command waits for a value: a read of a terminal or a FIFO - (~li < /dev/tty~) is not cancelled, and goes on until it has a line. - There is no test of Ctrl-C: no value command waits long otherwise. + or the file of ~<~. Ctrl-C cancels it with status 130. The file of ~<~ + is opened with ~#:cancellable? #t~, so a cancelled read of a terminal or + a FIFO (~li < /dev/tty~) takes nothing from it, and the next line goes to + the editor (Linux only). tests/interactive.bjo checks this. - ~jobs~ and ~history~ are values in a pipe: records of number, current, state, command and id; and of number and line. Alone they write text. - ~2>&1~ is done (phase 3): ~>FILE 2>&1~ works, ~2>&1 >FILE~ is an error. diff --git a/src/exec.bjo b/src/exec.bjo index de41e15..97fe9dc 100644 --- a/src/exec.bjo +++ b/src/exec.bjo @@ -312,7 +312,9 @@ (else (match (record-ref seg from) ((Some path) - (match (open-input-file path) + ;; Cancellable, so Ctrl-C of a read of a terminal or a FIFO takes + ;; nothing from it. + (match (open-input-file path #:cancellable? #t) ((Ok in) (run-values-from sh seg in)) ((Err e) (shell-error (fs-error-message e)) 1))) (None (run-values-from sh seg (open-input-string ""))))))) diff --git a/tests/interactive.bjo b/tests/interactive.bjo index 028b2f9..37bbf93 100644 --- a/tests/interactive.bjo +++ b/tests/interactive.bjo @@ -54,6 +54,8 @@ "printf 'kill %%1\\r'" "sleep 0.5" "printf 'printf x | u\\r'" "sleep 1" "printf 'stty tostop; printf y | u; stty -tostop\\r'" "sleep 1" + "printf 'li < /dev/tty\\r'" "sleep 1" "printf '\\003'" "sleep 0.5" + "printf 'echo after $?\\r'" "sleep 0.5" "printf 'exit\\r'" "sleep 1"] "; ")) (def command (str "(" keys ") | XDG_STATE_HOME=" state " script -qec 'dotnet src/main.exe' /dev/null")) @@ -72,6 +74,8 @@ (string-contains? out "history | tl 1 | g line\nhistory | tl 1 | g line\n")) (expect-true "a value command at the end of a job" (string-contains? out "printf x | u\nx\n")) (expect-true "and with tostop set" (string-contains? out "stty -tostop\ny\n")) + (expect-true "Ctrl-C of a read of the terminal, and the next line runs" + (string-contains? out "\nafter 130\n")) (expect-true "the history is saved" (match (file-read-text (path-combine state "bjosh/history")) ((Ok text) (string-contains? text "kill %1"))