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>
This commit is contained in:
Linus Björnstam 2026-12-01 09:50:15 +01:00
parent f54ceb35af
commit f8c4ad64bc
13 changed files with 680 additions and 128 deletions

View file

@ -14,6 +14,10 @@
;; command's arguments are strings.
;; - Values of variables are never split, and never globs. This is not
;; POSIX: `rm $FILES` with spaces in FILES removes one file.
;; - $(...) is the output of its lines, without the line breaks at its
;; end. A word that is only an unquoted $(...) is one word per line, and
;; empty lines are no words. Elsewhere it is text in the word, and never
;; a glob.
;; - A word of only unquoted parameters that are all empty is no word, as
;; in POSIX. "$X" is always one word.
@ -28,8 +32,18 @@
;; - `lookup`: the value of a name: a shell variable, then the
;; environment, and the special names ?, $ and 0 to 9.
;; - `home-of`: the home of a user, or of the current user for "".
;; - `substitute`: runs the items of $(...), and returns their output.
(type (: Expander (Record (: lookup (-> string (Option string)))
(: home-of (-> string (Option string))))))
(: home-of (-> string (Option string)))
(: substitute (-> (Vec Item) string)))))
;; `text` without the line breaks at its end.
(: (-> string string))
(defun (trim-line-breaks text)
(cond
((string-ends-with? text "\r\n") (trim-line-breaks (string-drop-right text 2)))
((string-ends-with? text "\n") (trim-line-breaks (string-drop-right text 1)))
(else text)))
;; The home of `user` from /etc/passwd, or $HOME for "".
(: (-> string (Option string)))
@ -76,6 +90,9 @@
(def v (match ((record-ref ex lookup) n) ((Some v) v) (None "")))
(go (+ i 1) (str text v) (str pattern (glob-escape v)) glob?
(and params-only? (not quoted?))))
((Subst items _)
(def v (trim-line-breaks ((record-ref ex substitute) items)))
(go (+ i 1) (str text v) (str pattern (glob-escape v)) glob? #f))
((Tilde user)
(def home (match ((record-ref ex home-of) user)
((Some h) h)
@ -86,6 +103,14 @@
;; matches of a glob.
(: (-> Expander (Vec WordPart) (Vec string)))
(defun (expand-word ex w)
(match w
([(Subst items #f)]
(vec-filter (fun (line) (not (string-empty? line)))
(string-split (string-replace ((record-ref ex substitute) items) "\r\n" "\n") "\n")))
(_ (expand-pieces ex w))))
(: (-> Expander (Vec WordPart) (Vec string)))
(defun (expand-pieces ex w)
(def p (pieces ex w))
(def text (record-ref p text))
(cond