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 -c LINE, bjosh FILE and bjosh < lines: what lines write, and the
|
|
|
|
|
;; exit code.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo build && bjo test tests/run.bjo
|
|
|
|
|
|
|
|
|
|
(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)))
|
|
|
|
|
|
|
|
|
|
(def shell "src/main.exe")
|
|
|
|
|
|
|
|
|
|
(def scratch (path-combine (temp-dir) (str "bjosh-run-" (int->string process-id))))
|
|
|
|
|
|
|
|
|
|
;; Runs `form` with stderr into a file. Returns "out", then "err: " and
|
|
|
|
|
;; the error text when there is one, then "code: n" when it is not 0.
|
|
|
|
|
(: (-> Form string))
|
|
|
|
|
(defun (outcome form)
|
|
|
|
|
(def err-path (path-combine scratch "stderr"))
|
|
|
|
|
(match (run/output (FErrInto err-path form))
|
|
|
|
|
((Ok (Tuple code out))
|
|
|
|
|
(def err (match (file-read-text err-path) ((Ok t) t) ((Err _) "")))
|
|
|
|
|
(string-trim-right
|
|
|
|
|
(str out
|
|
|
|
|
(if (string-empty? err) "" (str "err: " err))
|
|
|
|
|
(if (= code 0) "" (str "code: " (int->string code))))))
|
|
|
|
|
((Err e) (str "did not run: " (run-error-message e)))))
|
|
|
|
|
|
|
|
|
|
(: (-> string string))
|
|
|
|
|
(defun (c line) (outcome (FArgv ["dotnet" shell "-c" line])))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(unless (file-exists? shell)
|
|
|
|
|
(println "FAILURE: src/main.exe is missing: run bjo build first"))
|
|
|
|
|
(ignore (directory-create scratch))
|
|
|
|
|
(def file (path-combine scratch "f.txt"))
|
|
|
|
|
|
|
|
|
|
;; Pipes, lists and status.
|
|
|
|
|
(expect "a pipe" (c "echo a b | tr a-z A-Z") "A B")
|
|
|
|
|
(expect "&& and ||" (c "false && echo no || echo yes") "yes")
|
|
|
|
|
(expect "; runs both" (c "echo a; echo b") "a\nb")
|
|
|
|
|
(expect "$? after a failure" (c "false; echo $?") "1")
|
|
|
|
|
(expect "without job control, a pipe's status is its last command's" (c "false | true; echo $?") "0")
|
|
|
|
|
(expect "the status of the last line is the exit code" (c "false") "code: 1")
|
|
|
|
|
(expect "exit" (c "exit 3; echo no") "code: 3")
|
|
|
|
|
|
|
|
|
|
;; Words and variables.
|
|
|
|
|
(expect "quotes" (c "echo 'a b' \"c d\" e\\ f") "a b c d e f")
|
|
|
|
|
(expect "a variable" (c "X=hi; echo $X ${X}x \"$X\"") "hi hix hi")
|
|
|
|
|
(expect "a value is not split" (c "X='a b'; printf '[%s]' $X") "[a b]")
|
|
|
|
|
(expect "an empty variable is no word" (c "printf '[%s]' $NOPE x") "[x]")
|
|
|
|
|
(expect "export" (c "export Y=1; env | grep '^Y='") "Y=1")
|
|
|
|
|
(expect "export of a shell variable" (c "Y=2; export Y; env | grep '^Y='") "Y=2")
|
|
|
|
|
(expect "unset" (c "Y=1; unset Y; echo [$Y]") "[]")
|
|
|
|
|
(expect "NAME=value for one command" (c "Z=2 env | grep '^Z='; echo [$Z]") "Z=2\n[]")
|
|
|
|
|
(expect "$0 and $1 of -c" (outcome (FArgv ["dotnet" shell "-c" "echo $0 $1" "one"])) "bjosh one")
|
|
|
|
|
|
|
|
|
|
;; Redirections.
|
|
|
|
|
(expect "> >> <" (c (str "echo x >" file "; echo y >>" file "; cat <" file)) "x\ny")
|
|
|
|
|
(expect "2>" (c (str "ls /no/such/dir 2>" file "; echo $?; grep -c . " file)) "2\n1")
|
|
|
|
|
(expect "> alone makes an empty file" (c (str ">" file "; wc -c <" file)) "0")
|
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 "2>&1 into a pipe" (c "ls /no/such/dir 2>&1 | grep -c such") "1")
|
|
|
|
|
(expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1")
|
|
|
|
|
(expect "2>&1 before >" (c (str "ls 2>&1 >" file))
|
|
|
|
|
"err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2")
|
2026-12-01 07:29:10 +01:00
|
|
|
(expect ">&2 is not supported yet" (c "printf x >&2")
|
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
|
|
|
"err: bjosh: >&2 is not supported yet\ncode: 2")
|
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
|
|
|
|
|
|
|
|
;; Shell commands.
|
|
|
|
|
(expect "cd and pwd" (c "cd /tmp; pwd") "/tmp")
|
|
|
|
|
(expect "cd -" (c "cd /tmp; cd /; cd -") "/tmp")
|
|
|
|
|
(expect "cd to nowhere" (string-contains? (c "cd /no/such/dir") "code: 1") #t)
|
|
|
|
|
(expect "type" (c "type cd") "cd is a shell command")
|
|
|
|
|
(expect "a shell command in a pipe" (c "cd | cat")
|
|
|
|
|
"err: bjosh: cd: a shell command cannot be in a pipe\ncode: 2")
|
|
|
|
|
|
|
|
|
|
;; Errors.
|
|
|
|
|
(expect "a program that is not found" (c "nosuchprogram")
|
|
|
|
|
"err: bjosh: nosuchprogram: command not found\ncode: 127")
|
|
|
|
|
(expect "syntax that is not supported" (string-contains? (c "if true; then ls; fi") "`if` is not supported") #t)
|
|
|
|
|
(expect "and its status" (string-ends-with? (c "if true; then ls; fi") "code: 2") #t)
|
|
|
|
|
(expect "& needs job control" (c "sleep 1 &")
|
|
|
|
|
"err: bjosh: & needs an interactive shell with job control\ncode: 2")
|
|
|
|
|
|
|
|
|
|
;; A file, and stdin.
|
|
|
|
|
(def script-path (path-combine scratch "script.sh"))
|
|
|
|
|
(ignore (file-write-text script-path "echo $0 $1\necho second line\n"))
|
|
|
|
|
(expect "a file, with its arguments" (outcome (FArgv ["dotnet" shell script-path "arg"]))
|
|
|
|
|
(str script-path " arg\nsecond line"))
|
|
|
|
|
(expect "lines on stdin" (outcome (FFrom script-path (FArgv ["dotnet" shell])))
|
|
|
|
|
"bjosh\nsecond line")
|
|
|
|
|
|
|
|
|
|
(ignore (directory-delete-tree scratch))
|
|
|
|
|
0)
|