;; 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 expand): words of (bjosh syntax) to the strings a command gets. ;; ;; - $NAME is the value of the variable, or "" when there is none. ;; - ~ is $HOME, and ~user the home of that user in /etc/passwd. A user ;; that does not exist leaves ~user as written. ;; - A word with *, ? or [ in text that was not quoted is a glob, matched ;; with (std glob) from the current directory. Its matches are the words, ;; sorted. A glob that matches nothing stays as written, as in POSIX. ;; A file name that is not UTF-8 has U+FFFD for its bad bytes: a ;; 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. (import (std prelude)) (import (std glob)) (import (bjosh syntax)) (export Expander expand-word expand-words expand-text expand-target default-home-of) ;; What expansion reads. ;; - `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))) (: 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))) (defun (default-home-of user) (if (string-empty? user) (getenv "HOME") (match (file-read-lines "/etc/passwd") ((Ok lines) (let go ((i 0)) (if (= i (vec-length lines)) None (let ((fields (string-split (vec-ref lines i) ":"))) (if (and (>= (vec-length fields) 6) (= (vec-ref fields 0) user)) (Some (vec-ref fields 5)) (go (+ i 1))))))) ((Err _) None)))) ;; `s` with each glob character after a \, so that it matches itself. (: (-> string string)) (defun (glob-escape s) (string-join (vec-map (fun (c) (def text (char->string c)) (if (string-contains? "\\*?[]{}" text) (str "\\" text) text)) (string->vec s)) "")) (: (-> string bool)) (defun (glob-chars? s) (or (string-contains? s "*") (string-contains? s "?") (string-contains? s "["))) ;; A word expanded once: its text, the same as a glob pattern, whether it ;; is a glob, and whether it is only unquoted parameters. (type (: Pieces (Record (: text string) (: pattern string) (: glob? bool) (: params-only? bool)))) (: (-> Expander (Vec WordPart) Pieces)) (defun (pieces ex w) (let go ((i 0) (text "") (pattern "") (glob? #f) (params-only? #t)) (if (= i (vec-length w)) (Pieces (text text) (pattern pattern) (glob? glob?) (params-only? params-only?)) (match (vec-ref w i) ((Lit s #f) (go (+ i 1) (str text s) (str pattern s) (or glob? (glob-chars? s)) #f)) ((Lit s #t) (go (+ i 1) (str text s) (str pattern (glob-escape s)) glob? #f)) ((Param n quoted?) (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) (None (str "~" user)))) (go (+ i 1) (str text home) (str pattern (glob-escape home)) glob? #f)))))) ;; The words of `w`: one, none for empty unquoted parameters, or the ;; 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 ((and (record-ref p params-only?) (string-empty? text)) []) ((record-ref p glob?) (match (glob-parse (record-ref p pattern)) ((Ok g) (def matches (vec-map path->string/lossy (->vec (glob g)))) (if (vec-empty? matches) [text] matches)) ((Err _) [text]))) (else [text]))) ;; The words of all of `words`, in order. (: (-> Expander (Vec (Vec WordPart)) (Vec string))) (defun (expand-words ex words) (let go ((i 0) (out [])) (if (= i (vec-length words)) out (go (+ i 1) (vec-append out (expand-word ex (vec-ref words i))))))) ;; `w` as one string, with no glob: for the value of an assignment. (: (-> Expander (Vec WordPart) string)) (defun (expand-text ex w) (record-ref (pieces ex w) text)) ;; The file of a redirection: `w` expanded, which must be one word. ;; Returns the message "ambiguous redirect" for a glob of several matches, ;; and "no file name" for none. (: (-> Expander (Vec WordPart) (Result string string))) (defun (expand-target ex w) (def words (expand-word ex w)) (cond ((= (vec-length words) 1) (Ok (vec-ref words 0))) ((vec-empty? words) (Err (str (word->string w) ": no file name"))) (else (Err (str (word->string w) ": ambiguous redirect")))))