bjosh/src/main.bjo

170 lines
6.1 KiB
Text
Raw Normal View History

;; 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`.
;;
;; - An interactive shell first runs the lines of the start-up file,
;; $XDG_CONFIG_HOME/bjosh/rc or ~/.config/bjosh/rc.
;; - The prompt is $PROMPT when it is set; see `custom-prompt`.
(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 start-up file.
(: (-> string))
(defun (rc-path)
(def config (match (getenv "XDG_CONFIG_HOME")
((Some c) c)
(None (path-combine (match (getenv "HOME") ((Some h) h) (None ".")) ".config"))))
(path-combine config "bjosh/rc"))
;; The directory, with ~ for home.
(: (-> string))
(defun (shown-directory)
(def dir (current-directory))
(def home (match (getenv "HOME") ((Some h) h) (None "")))
(if (and (not (string-empty? home)) (string-starts-with? dir home))
(str "~" (string-drop dir (string-length home)))
dir))
;; The prompt: `custom-prompt` of $PROMPT, else the directory, and $ green
;; after status 0, else red.
(: (-> Shell string))
(defun (prompt sh)
(match (var-ref sh "PROMPT")
((Some template) (custom-prompt sh template))
(None
(def color (if (= (record-ref sh status) 0) "32" "31"))
(str "\u{1B}[1;34m" (shown-directory) "\u{1B}[0m \u{1B}[" color "m$\u{1B}[0m "))))
(import/extern
(user-name (: System.Environment.UserName string #:get #:no-raise))
(machine-name (: System.Environment.MachineName string #:get #:no-raise)))
;; The text of a prompt escape: \w, \W, \u, \h, \$, \e, \n or \\.
(: (-> char (Option string)))
(defun (prompt-escape c)
(cond
((= c #\w) (Some (shown-directory)))
((= c #\W) (Some (let ((dir (shown-directory)))
(if (= dir "/") dir
(match (path-filename dir) ((Some n) n) (None dir))))))
((= c #\u) (Some user-name))
((= c #\h) (Some machine-name))
((= c #\$) (Some (if (= user-name "root") "#" "$")))
((= c #\e) (Some "\u{1B}"))
((= c #\n) (Some "\n"))
((= c #\\) (Some "\\"))
(else None)))
;; $PROMPT, with its escapes replaced (see `prompt-escape`), and the text
;; between them expanded as in double quotes. A part that does not parse
;; is shown as written.
(: (-> Shell string string))
(defun (custom-prompt sh template)
(def chars (string->vec template))
(def out (stringbuilder-empty))
(def pending (stringbuilder-empty))
(defun (flush!)
(def text (stringbuilder->string pending))
(stringbuilder-clear! pending)
(unless (string-empty? text)
(stringbuilder-add-string! out (match (expand-quoted sh text)
((Ok expanded) expanded)
((Err _) text)))))
(let go ((i 0))
(when (< i (vec-length chars))
(def c (vec-ref chars i))
(def escape (if (and (= c #\\) (< (+ i 1) (vec-length chars)))
(prompt-escape (vec-ref chars (+ i 1)))
None))
(match escape
((Some text)
(flush!)
(stringbuilder-add-string! out text)
(go (+ i 2)))
(None
(stringbuilder-add! pending c)
(go (+ i 1))))))
(flush!)
(stringbuilder->string out))
(: (-> 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"]))
(def rc (rc-path))
(when (file-exists? rc)
(match (file-read-text rc)
((Ok text) (ignore (run-text sh text)))
((Err e) (shell-error (str rc ": " (fs-error-message e))))))
(let go ()
(report-jobs sh)
(match (line-read ed term (prompt sh) #:notices (Some (fun () (job-changes 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))))