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 expand): variables, ~ and globs, with a fake environment and a
|
|
|
|
|
;; directory of files made for the test.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo test tests/expand.bjo
|
|
|
|
|
|
|
|
|
|
(import (std prelude))
|
|
|
|
|
(import (std simpletest))
|
|
|
|
|
(import (bjosh syntax))
|
|
|
|
|
(import (bjosh expand))
|
|
|
|
|
|
|
|
|
|
(import/extern
|
|
|
|
|
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
|
|
|
|
|
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
|
|
|
|
|
|
|
|
|
|
(: (-> string (Option string)))
|
|
|
|
|
(defun (lookup name)
|
|
|
|
|
(cond
|
|
|
|
|
((= name "HOME") (Some "/home/me"))
|
|
|
|
|
((= name "X") (Some "a b"))
|
|
|
|
|
((= name "STAR") (Some "*"))
|
|
|
|
|
((= name "EMPTY") (Some ""))
|
|
|
|
|
((= name "?") (Some "0"))
|
|
|
|
|
(else None)))
|
|
|
|
|
|
|
|
|
|
(: (-> string (Option string)))
|
|
|
|
|
(defun (home-of user)
|
|
|
|
|
(cond
|
|
|
|
|
((string-empty? user) (Some "/home/me"))
|
|
|
|
|
((= user "bob") (Some "/home/bob"))
|
|
|
|
|
(else None)))
|
|
|
|
|
|
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
|
|
|
;; $(...): the words of its first command after the first, as lines,
|
|
|
|
|
;; without quotes, and two empty lines.
|
|
|
|
|
(: (-> (Vec Item) string))
|
|
|
|
|
(defun (substitute items)
|
|
|
|
|
(def pipe (record-ref (record-ref (vec-ref items 0) and-or) first))
|
|
|
|
|
(def words (vec-map (fun (w) (string-replace (word->string w) "\"" ""))
|
|
|
|
|
(record-ref (vec-ref pipe 0) words)))
|
|
|
|
|
(str (string-join (vec-drop words 1) "\n") "\n\n"))
|
|
|
|
|
|
|
|
|
|
(def ex (Expander (lookup lookup) (home-of home-of) (substitute substitute)))
|
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
|
|
|
|
|
|
|
|
;; The words of the first command of `text`, joined by "|".
|
|
|
|
|
(: (-> string string))
|
|
|
|
|
(defun (words text)
|
|
|
|
|
(match (parse-line text)
|
|
|
|
|
((Ok items)
|
|
|
|
|
(def pipe (record-ref (record-ref (vec-ref items 0) and-or) first))
|
|
|
|
|
(string-join (expand-words ex (record-ref (vec-ref pipe 0) words)) "|"))
|
|
|
|
|
((Err _) "parse error")))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(expect "plain words" (words "ls -l x") "ls|-l|x")
|
|
|
|
|
(expect "a variable" (words "echo $HOME/src") "echo|/home/me/src")
|
|
|
|
|
(expect "a value with spaces is one word" (words "echo $X") "echo|a b")
|
|
|
|
|
(expect "a variable that is not set is empty" (words "echo a${NOPE}b") "echo|ab")
|
|
|
|
|
(expect "an empty unquoted variable is no word" (words "echo $EMPTY $NOPE x") "echo|x")
|
|
|
|
|
(expect "a quoted empty variable is a word" (words "echo \"$EMPTY\" x") "echo||x")
|
|
|
|
|
(expect "$?" (words "echo $?") "echo|0")
|
|
|
|
|
(expect "~" (words "cd ~ ~/src") "cd|/home/me|/home/me/src")
|
|
|
|
|
(expect "~user" (words "cd ~bob/x") "cd|/home/bob/x")
|
|
|
|
|
(expect "a user that does not exist" (words "cd ~nobody") "cd|~nobody")
|
|
|
|
|
|
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
|
|
|
;; $(...). `substitute` returns the words after `echo`, one per line.
|
|
|
|
|
(expect "a $(...) word is one word per line" (words "x $(echo a b)") "x|a|b")
|
|
|
|
|
(expect "its empty lines are no words" (words "x $(echo '' a)") "x|a")
|
|
|
|
|
(expect "nothing is no word" (words "x $(echo) y") "x|y")
|
|
|
|
|
(expect "in a word it is text, without the last line breaks" (words "x p$(echo a b)q") "x|pa\nbq")
|
|
|
|
|
(expect "in double quotes it is one word" (words "x \"$(echo a b)\"") "x|a\nb")
|
|
|
|
|
(expect "it is not a glob" (words "x $(echo '*')") "x|*")
|
|
|
|
|
|
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
|
|
|
;; Globs, in a directory of our own.
|
|
|
|
|
(def dir (path-combine (temp-dir) (str "bjosh-expand-" (int->string process-id))))
|
|
|
|
|
(ignore (directory-create dir))
|
|
|
|
|
(for-each (fun (name) (ignore (file-write-text (path-combine dir name) "")))
|
|
|
|
|
["b.txt" "a.txt" "c.log" ".hidden.txt" "*.txt"])
|
|
|
|
|
(def here (current-directory))
|
|
|
|
|
(set-current-directory! dir)
|
|
|
|
|
(expect "a glob, sorted" (words "ls *.txt") "ls|*.txt|a.txt|b.txt")
|
|
|
|
|
(expect "? and [ ]" (words "ls [ab].tx?") "ls|a.txt|b.txt")
|
|
|
|
|
(expect "a glob that matches nothing stays" (words "ls *.md") "ls|*.md")
|
|
|
|
|
(expect "a quoted * is not a glob" (words "ls '*'.txt") "ls|*.txt")
|
|
|
|
|
(expect "a \\* is not a glob" (words "ls \\*.txt") "ls|*.txt")
|
|
|
|
|
(expect "a * from a variable is not a glob" (words "ls $STAR") "ls|*")
|
|
|
|
|
(expect "a quoted part in a glob matches itself" (words "ls '*'*") "ls|*.txt")
|
|
|
|
|
(expect "names with a dot only for a pattern with a dot" (words "ls .h*") "ls|.hidden.txt")
|
|
|
|
|
(set-current-directory! here)
|
|
|
|
|
(for-each (fun (name) (ignore (file-delete (path-combine dir name))))
|
|
|
|
|
["b.txt" "a.txt" "c.log" ".hidden.txt" "*.txt"])
|
|
|
|
|
(ignore (directory-delete dir))
|
|
|
|
|
|
|
|
|
|
;; Redirections.
|
|
|
|
|
(expect "a target" (expand-target ex [(Lit "out" #f)]) (Ok "out"))
|
|
|
|
|
(expect "an empty target" (expand-target ex [(Param "EMPTY" #f)]) (Err "${EMPTY}: no file name"))
|
|
|
|
|
0)
|