Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +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/.
|
|
|
|
|
|
|
|
|
|
;; bjosh on a terminal: job control and the prompt.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo build && bjo test tests/interactive.bjo
|
|
|
|
|
;;
|
|
|
|
|
;; The test runs bjosh under `script`, and writes keys to it with pauses
|
Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is
one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:15 +01:00
|
|
|
;; between them. The history and the start-up file are in a directory of
|
|
|
|
|
;; the test.
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
|
|
|
|
|
(import (std prelude))
|
|
|
|
|
(import (std simpletest))
|
|
|
|
|
(import (std run))
|
|
|
|
|
|
|
|
|
|
(import/extern
|
|
|
|
|
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
|
|
|
|
|
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
|
|
|
|
|
|
|
|
|
|
;; The output without escape sequences and carriage returns.
|
|
|
|
|
(: (-> string string))
|
|
|
|
|
(defun (plain text)
|
|
|
|
|
(def chars (string->vec (string-replace text "\r" "")))
|
|
|
|
|
(let go ((i 0) (out (stringbuilder-empty)))
|
|
|
|
|
(cond
|
|
|
|
|
((= i (vec-length chars)) (stringbuilder->string out))
|
|
|
|
|
((and (= (char->int (vec-ref chars i)) 27) (< (+ i 1) (vec-length chars)) (= (vec-ref chars (+ i 1)) #\[))
|
|
|
|
|
(let skip ((j (+ i 2)))
|
|
|
|
|
(if (and (< j (vec-length chars))
|
|
|
|
|
(let ((code (char->int (vec-ref chars j)))) (and (>= code 0x20) (<= code 0x3f))))
|
|
|
|
|
(skip (+ j 1))
|
|
|
|
|
(go (+ j 1) out))))
|
|
|
|
|
(else (stringbuilder-add! out (vec-ref chars i)) (go (+ i 1) out)))))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(cond
|
|
|
|
|
((not (file-exists? "/usr/bin/script")) (println "skipped: no script command"))
|
|
|
|
|
((not (file-exists? "src/main.exe")) (println "FAILURE: src/main.exe is missing: run bjo build first"))
|
|
|
|
|
(else
|
|
|
|
|
(def state (path-combine (temp-dir) (str "bjosh-interactive-" (int->string process-id))))
|
2026-12-01 10:57:19 +01:00
|
|
|
;; An empty commands directory: no package of user commands is written.
|
|
|
|
|
(ignore (directory-create (path-combine state "bjosh/commands")))
|
Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is
one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:15 +01:00
|
|
|
(ignore (file-write-text (path-combine state "bjosh/rc") "alias fromrc='echo set in rc'\n"))
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
(def keys (string-join ["sleep 2.5"
|
|
|
|
|
"printf 'sleep 30\\r'" "sleep 1" "printf '\\032'" "sleep 0.5"
|
|
|
|
|
"printf 'jobs\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'bg\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'kill %%1\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'jobs\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'sleep 30\\r'" "sleep 1" "printf '\\003'" "sleep 0.5"
|
|
|
|
|
"printf 'echo status $?\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'ech'" "sleep 0.3" "printf '\\032'" "sleep 0.5"
|
|
|
|
|
"printf 'o alive\\r'" "sleep 0.5"
|
2026-12-01 07:21:04 +01:00
|
|
|
"printf 'sleep 30 &\\r'" "sleep 1"
|
|
|
|
|
"printf 'jobs | g command\\r'" "sleep 1"
|
|
|
|
|
"printf 'history | tl 1 | g line\\r'" "sleep 1"
|
|
|
|
|
"printf 'kill %%1\\r'" "sleep 0.5"
|
Phase 2, first part: values and value commands
- Values are Json of (text json); a record is JsonFields.
- Value commands: files, procs, open, where, select, get, sort-by,
first, last, count, reverse, uniq, lines, from-json, to-json,
from-csv, to-csv, each with a short name.
- Value commands next to each other run as one unit: a (std run)
filter in a job, or in the shell when a line has no program.
- Records are shown as a table; values go to programs as lines.
- A name is a shell command, a short name, a program on PATH, then a
long name.
- 2>&1, with >FILE 2>&1.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:05:09 +01:00
|
|
|
"printf 'printf x | u\\r'" "sleep 1"
|
|
|
|
|
"printf 'stty tostop; printf y | u; stty -tostop\\r'" "sleep 1"
|
2026-12-01 08:13:20 +01:00
|
|
|
"printf 'li < /dev/tty\\r'" "sleep 1" "printf '\\003'" "sleep 0.5"
|
|
|
|
|
"printf 'echo after $?\\r'" "sleep 0.5"
|
Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is
one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:15 +01:00
|
|
|
"printf 'fromrc\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'sleep 0.5 &\\r'" "sleep 0.3" "printf 'ec'" "sleep 1.5" "printf 'ho typed\\r'" "sleep 0.5"
|
|
|
|
|
"printf 'cd /tmp\\r'" "sleep 0.3"
|
|
|
|
|
"printf \"PROMPT='<\\\\W \\$(echo s)>\\\\$ '\\r\"" "sleep 0.5"
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
"printf 'exit\\r'" "sleep 1"]
|
|
|
|
|
"; "))
|
Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is
one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:15 +01:00
|
|
|
(def command (str "(" keys ") | XDG_STATE_HOME=" state " XDG_CONFIG_HOME=" state " script -qec 'dotnet src/main.exe' /dev/null"))
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
(def out (match (run/output '(sh "-c" ,command))
|
|
|
|
|
((Ok (Tuple _ text)) (plain text))
|
|
|
|
|
((Err e) (run-error-message e))))
|
|
|
|
|
(expect-true "Ctrl-Z stops the job" (string-contains? out "^Z\n[1]+ Stopped sleep 30"))
|
|
|
|
|
(expect-true "jobs lists it" (string-contains? out "jobs\n[1]+ Stopped sleep 30"))
|
|
|
|
|
(expect-true "bg continues it" (string-contains? out "bg\n[1]+ sleep 30 &"))
|
|
|
|
|
(expect-true "kill ends it" (string-contains? out "[1]+ Terminated sleep 30"))
|
|
|
|
|
(expect-true "Ctrl-C ends a job, and the prompt starts a new line" (string-contains? out "^C\n"))
|
|
|
|
|
(expect-true "with status 130" (string-contains? out "status 130"))
|
|
|
|
|
(expect-true "Ctrl-Z at the prompt does not stop the shell" (string-contains? out "\nalive\n"))
|
2026-12-01 07:21:04 +01:00
|
|
|
(expect-true "jobs in a pipe" (string-contains? out "jobs | g command\nsleep 30\n"))
|
|
|
|
|
(expect-true "history in a pipe, with this line last"
|
|
|
|
|
(string-contains? out "history | tl 1 | g line\nhistory | tl 1 | g line\n"))
|
Phase 2, first part: values and value commands
- Values are Json of (text json); a record is JsonFields.
- Value commands: files, procs, open, where, select, get, sort-by,
first, last, count, reverse, uniq, lines, from-json, to-json,
from-csv, to-csv, each with a short name.
- Value commands next to each other run as one unit: a (std run)
filter in a job, or in the shell when a line has no program.
- Records are shown as a table; values go to programs as lines.
- A name is a shell command, a short name, a program on PATH, then a
long name.
- 2>&1, with >FILE 2>&1.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:05:09 +01:00
|
|
|
(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"))
|
2026-12-01 08:13:20 +01:00
|
|
|
(expect-true "Ctrl-C of a read of the terminal, and the next line runs"
|
|
|
|
|
(string-contains? out "\nafter 130\n"))
|
Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is
one word per line. X=$(cmd) sets $? to its status.
- alias, unalias, and command NAME. An alias is one pipe.
- ~/.config/bjosh/rc runs before the first prompt.
- PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n.
- "Done" lines are written above the input while the prompt waits.
- >&2, for programs and value commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:50:15 +01:00
|
|
|
(expect-true "the start-up file runs" (string-contains? out "fromrc\nset in rc\n"))
|
|
|
|
|
(expect-true "a job that ends is reported while the prompt waits"
|
|
|
|
|
(match (string-split-once out "Done sleep 0.5")
|
|
|
|
|
((Some (Tuple _ after)) (string-contains? after "echo typed\ntyped\n"))
|
|
|
|
|
(None #f)))
|
|
|
|
|
(expect-true "PROMPT" (string-contains? out "<tmp s>$ "))
|
Phase 1: POSIX command lines, job control and the prompt
- (bjosh syntax): a part of POSIX sh, parsed with (text
parser-combinators). Syntax that is not supported raises an error that
names it.
- (bjosh expand): variables, ~ and globs. Values are not split.
- (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and
NAME=value through env.
- (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit,
export, unset, jobs, fg, bg, kill, type, help, history.
- (bjosh editor): Enter waits for open quotes and trailing operators;
completion of commands and files.
- -c LINE, FILE, and stdin. A Native AOT build too.
- Tests: the parser, expansion, the editor, lines through -c, and job
control under script.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 06:19:45 +01:00
|
|
|
(expect-true "the history is saved"
|
|
|
|
|
(match (file-read-text (path-combine state "bjosh/history"))
|
|
|
|
|
((Ok text) (string-contains? text "kill %1"))
|
|
|
|
|
((Err _) #f)))
|
|
|
|
|
(unless (string-contains? out "status 130")
|
|
|
|
|
(println out))
|
|
|
|
|
(ignore (directory-delete-tree state))))
|
|
|
|
|
0)
|