;; 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. ;; ;; 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`. (import (std prelude)) (import (std run)) (import (bjoterm terminal)) (import (bjoterm line)) (import (bjosh syntax)) (import (bjosh shell)) (import (bjosh builtins)) (import (bjosh exec)) (import (bjosh editor)) ;; =========================================================================== ;; 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 (fs-error-message 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))))