2026-11-30 22:22:18 +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): the shell. See plan.org.
|
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
|
|
|
;;
|
|
|
|
|
;; bjosh interactive, with job control, when stdin is a
|
|
|
|
|
;; terminal; else runs the lines of stdin
|
|
|
|
|
;; bjosh -c LINE runs LINE
|
|
|
|
|
;; bjosh FILE ARG... runs the lines of FILE, with $0 FILE, $1 ARG...
|
|
|
|
|
;;
|
|
|
|
|
;; The exit code is the status of the last line, or the code of `exit`.
|
2026-11-30 22:22:18 +01:00
|
|
|
|
|
|
|
|
(import (std prelude))
|
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 run))
|
2026-11-30 22:22:18 +01:00
|
|
|
(import (bjoterm terminal))
|
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 (bjoterm line))
|
|
|
|
|
(import (bjosh syntax))
|
|
|
|
|
(import (bjosh shell))
|
|
|
|
|
(import (bjosh builtins))
|
|
|
|
|
(import (bjosh exec))
|
|
|
|
|
(import (bjosh editor))
|
2026-11-30 22:22:18 +01:00
|
|
|
|
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 loop
|
|
|
|
|
;; ===========================================================================
|
|
|
|
|
|
|
|
|
|
(: (-> string))
|
|
|
|
|
(defun (history-path)
|
|
|
|
|
(def state (match (getenv "XDG_STATE_HOME")
|
|
|
|
|
((Some s) s)
|
|
|
|
|
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".local/state"))))
|
|
|
|
|
(path-combine state "bjosh/history"))
|
|
|
|
|
|
|
|
|
|
;; The directory, with ~ for home, and $ green after status 0, else red.
|
|
|
|
|
(: (-> Shell string))
|
|
|
|
|
(defun (prompt sh)
|
|
|
|
|
(def dir (current-directory))
|
|
|
|
|
(def home (match (getenv "HOME") ((Some h) h) (None "")))
|
|
|
|
|
(def shown (if (and (not (string-empty? home)) (string-starts-with? dir home))
|
|
|
|
|
(str "~" (string-drop dir (string-length home)))
|
|
|
|
|
dir))
|
|
|
|
|
(def color (if (= (record-ref sh status) 0) "32" "31"))
|
|
|
|
|
(str "\u{1B}[1;34m" shown "\u{1B}[0m \u{1B}[" color "m$\u{1B}[0m "))
|
|
|
|
|
|
|
|
|
|
(: (-> Terminal int))
|
|
|
|
|
(defun (interactive term)
|
|
|
|
|
(def jc (match (job-control)
|
|
|
|
|
((Ok jc) (Some jc))
|
|
|
|
|
((Err _) (shell-error "no job control: & and Ctrl-Z do not work") None)))
|
|
|
|
|
(def ed (make-line-editor #:completer (Some complete) #:input-done? (Some input-done?)
|
|
|
|
|
#:suspend? #f))
|
|
|
|
|
(def path (history-path))
|
|
|
|
|
(ignore (line-history-load! ed path))
|
|
|
|
|
(ignore (try (directory-create (path-combine path "..")) #:catch (System.Exception)))
|
|
|
|
|
(def sh (make-shell jc (Some ed) ["bjosh"]))
|
|
|
|
|
(let go ()
|
|
|
|
|
(report-jobs sh)
|
|
|
|
|
(match (line-read ed term (prompt sh))
|
|
|
|
|
((LineText text)
|
|
|
|
|
(ignore (line-history-save ed path))
|
|
|
|
|
(ignore (run-text sh text)))
|
|
|
|
|
(LineCancelled (set-status! sh 130))
|
|
|
|
|
(LineEof (ignore (run-text sh "exit"))))
|
|
|
|
|
(match (record-ref sh exit-code)
|
|
|
|
|
((Some code) code)
|
|
|
|
|
(None (go)))))
|
|
|
|
|
|
|
|
|
|
(: (-> Shell string int))
|
|
|
|
|
(defun (run-script sh text)
|
|
|
|
|
(ignore (run-text sh text))
|
|
|
|
|
(match (record-ref sh exit-code)
|
|
|
|
|
((Some code) code)
|
|
|
|
|
(None (record-ref sh status))))
|
|
|
|
|
|
|
|
|
|
(: (-> (Vec string) int))
|
|
|
|
|
(defun (run-file args)
|
|
|
|
|
(match (file-read-text (vec-ref args 0))
|
|
|
|
|
((Ok text) (run-script (make-shell None None args) text))
|
|
|
|
|
((Err e) (shell-error (str (vec-ref args 0) ": " (written e))) 127)))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(cond
|
|
|
|
|
((and (>= (vec-length args) 2) (= (vec-ref args 0) "-c"))
|
|
|
|
|
(run-script (make-shell None None (vec-add-first (vec-slice args 2 (- (vec-length args) 2)) "bjosh"))
|
|
|
|
|
(vec-ref args 1)))
|
|
|
|
|
((vec-empty? args)
|
|
|
|
|
(match (terminal-open)
|
|
|
|
|
((Ok term) (interactive term))
|
|
|
|
|
((Err _)
|
|
|
|
|
(run-script (make-shell None None ["bjosh"]) (read-all (parameter-ref current-input-port))))))
|
|
|
|
|
(else (run-file args))))
|