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

@ -2,8 +2,8 @@
;; 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 builtins): the shell commands of phase 1. They change the shell,
;; so they run in its process and must be alone in a pipe.
;; (bjosh builtins): the shell commands. They change the shell, so they run
;; in its process and must be alone in a pipe.
;;
;; - Each takes the shell and its arguments, and returns an exit status.
;; - Each writes to the current output and error ports.
@ -13,6 +13,7 @@
(import (std prelude))
(import (std run))
(import (bjoterm line))
(import (bjosh syntax))
(import (bjosh shell))
(import (bjosh commands))
(import (bjosh values))
@ -186,6 +187,48 @@
((Ok code) (max code job-status))
((Err e) (shell-error (run-error-message e)) 1))))))))
;; The names of the aliases, sorted.
(: (-> Shell (Vec string)))
(defun (alias-names sh)
(list->vec (list-sort (->list (map-keys (record-ref sh aliases))))))
;; alias NAME='TEXT', as `alias` writes it, which the shell reads back.
(: (-> string string string))
(defun (alias-line name text)
(str "alias " name "='" (string-replace text "'" "'\\''") "'"))
;; alias: lists the aliases. alias NAME: shows one. alias NAME=TEXT: sets
;; one; TEXT must be one pipe.
(: (-> Shell (Vec string) int))
(defun (alias sh args)
(if (vec-empty? args)
(begin
(for-each (fun (name)
(match (alias-ref sh name)
((Some text) (println (alias-line name text)))
(None unit)))
(alias-names sh))
0)
(each-status args
(fun (arg)
(match (string-split-once arg "=")
((Some (Tuple name text))
(match (parse-pipe text)
((Ok _) (alias-set! sh name text) 0)
((Err message) (shell-error (str "alias: " name ": " message)) 1)))
(None
(match (alias-ref sh arg)
((Some text) (println (alias-line arg text)) 0)
(None (shell-error (str "alias: " arg ": not found")) 1))))))))
(: (-> Shell (Vec string) int))
(defun (unalias sh args)
(each-status args
(fun (name)
(match (alias-ref sh name)
((Some _) (alias-remove! sh name) 0)
(None (shell-error (str "unalias: " name ": not found")) 1)))))
;; The path of a program on PATH, or a name with a / that exists.
(: (-> string (Option string)))
(defun (find-program name)
@ -205,6 +248,10 @@
(each-status args
(fun (name)
(cond
((some? (alias-ref sh name))
(println (str name " is an alias for "
(match (alias-ref sh name) ((Some t) t) (None ""))))
0)
((builtin? name) (println (str name " is a shell command")) 0)
(else
(match (value-command name (fun (n) (some? (find-program n))))
@ -229,6 +276,7 @@
(defun (help sh args)
(println "Shell commands:")
(for-each (fun (b) (println (builtin-help-line b))) (builtins))
(println (str " " (string-pad-right "command NAME [ARG]..." 28) "run the program NAME, not an alias or a value command"))
(println "")
(println "Value commands, by long and short name. A long name that is a program runs the program.")
(for-each (fun (c) (println (value-help-line c))) (value-commands))
@ -262,7 +310,9 @@
(Builtin (name "kill") (usage "kill [-SIGNAL] %JOB|PID...") (help "send a signal; TERM by default") (run kill))
(Builtin (name "type") (usage "type NAME...") (help "what a name runs") (run type-of))
(Builtin (name "help") (usage "help") (help "this list") (run help))
(Builtin (name "history") (usage "history") (help "the lines written before") (run history))])
(Builtin (name "history") (usage "history") (help "the lines written before") (run history))
(Builtin (name "alias") (usage "alias [NAME[=TEXT]]...") (help "list, show or set aliases; TEXT is one pipe") (run alias))
(Builtin (name "unalias") (usage "unalias NAME...") (help "remove aliases") (run unalias))])
;; ===========================================================================
;; Shell commands as values, in a pipe