2018-07-12 03:05:15 +01:00
|
|
|
;;; The Geesh Shell Interpreter
|
|
|
|
;;; Copyright 2018 Timothy Sample <samplet@ngyro.com>
|
|
|
|
;;;
|
|
|
|
;;; This file is part of Geesh.
|
|
|
|
;;;
|
|
|
|
;;; Geesh is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;;; (at your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; Geesh is distributed in the hope that it will be useful,
|
|
|
|
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with Geesh. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (geesh word)
|
|
|
|
#:use-module (geesh environment)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:export (eval-cmd-sub
|
|
|
|
expand-word))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This module contains functions for manipulating shell words. This
|
|
|
|
;;; includes tilde expansion, parameter expansions, field splitting,
|
|
|
|
;;; globbing, etc.
|
|
|
|
;;;
|
|
|
|
;;; In the code below, the term "qword" is used to refer to words that
|
|
|
|
;;; only contain quotations (i.e., no substitutions).
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(define (normalize-word word)
|
|
|
|
"Normalize @var{word} (which may be a word or a qword) so that it is
|
|
|
|
guaranteed to be a list."
|
|
|
|
(match word
|
|
|
|
((? string?) (list word))
|
|
|
|
(((? symbol?) _) (list word))
|
|
|
|
(_ word)))
|
|
|
|
|
|
|
|
(define (infix x xs)
|
|
|
|
"Place @var{x} between each element of the list @var{xs}."
|
|
|
|
(if (null? xs)
|
|
|
|
xs
|
|
|
|
(let loop ((xs (cdr xs)) (acc (list (car xs))))
|
|
|
|
(if (null? xs)
|
|
|
|
(reverse acc)
|
|
|
|
(loop (cdr xs) (cons* (car xs) x acc))))))
|
|
|
|
|
|
|
|
(define (list-split xs sym)
|
|
|
|
"Split the list @var{xs} into sublists delimited by the symbol
|
|
|
|
@var{sym}."
|
|
|
|
(let loop ((xs xs) (small-acc '()) (big-acc '()))
|
|
|
|
(cond
|
|
|
|
((null? xs)
|
|
|
|
(reverse (cons (reverse small-acc) big-acc)))
|
|
|
|
((eq? (car xs) sym)
|
|
|
|
(loop (cdr xs) '() (cons (reverse small-acc) big-acc)))
|
|
|
|
(else
|
|
|
|
(loop (cdr xs) (cons (car xs) small-acc) big-acc)))))
|
|
|
|
|
|
|
|
(define (split-fields qword ifs)
|
|
|
|
"Split @var{qword} into a list of qwords delimited by the character
|
|
|
|
set @var{ifs}."
|
|
|
|
|
|
|
|
(define (wedge-apart qword-part ifs)
|
|
|
|
(match qword-part
|
|
|
|
(('<sh-quote> _) (list qword-part))
|
|
|
|
("" '(""))
|
|
|
|
(str (let ((str-parts (string-split str ifs)))
|
|
|
|
(if (every string-null? str-parts)
|
|
|
|
'(wedge)
|
2018-07-18 07:33:08 +01:00
|
|
|
(filter (lambda (x)
|
|
|
|
(or (eq? x 'wedge) (not (string-null? x))))
|
|
|
|
(infix 'wedge str-parts)))))))
|
2018-07-12 03:05:15 +01:00
|
|
|
|
|
|
|
(let ((wedged (append-map (cut wedge-apart <> ifs)
|
|
|
|
(normalize-word qword))))
|
2018-07-18 07:33:08 +01:00
|
|
|
(filter pair? (list-split wedged 'wedge))))
|
2018-07-12 03:05:15 +01:00
|
|
|
|
|
|
|
(define (remove-quotes qword)
|
|
|
|
"Remove quote forms from @var{qword} and concatenate the result into a
|
|
|
|
single field (string)."
|
|
|
|
(let loop ((qword (normalize-word qword)) (acc '()))
|
|
|
|
(match qword
|
|
|
|
(() (string-concatenate-reverse acc))
|
|
|
|
((('<sh-quote> qword*) . t) (loop t (cons (remove-quotes qword*) acc)))
|
|
|
|
(((? string? h) . t) (loop t (cons h acc))))))
|
|
|
|
|
|
|
|
(define eval-cmd-sub
|
|
|
|
;; A procedure for evaluating (expanding) a command substitution.
|
|
|
|
;; This is parameterized to avoid a circular dependency.
|
|
|
|
(make-parameter (lambda (exps) (throw 'eval-cmd-sub-unset))))
|
|
|
|
|
|
|
|
(define (string-not-null? str)
|
|
|
|
"Check if @var{str} is a non-null string."
|
|
|
|
(and (string? str) (not (string-null? str))))
|
|
|
|
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(define (parameter-ref env name)
|
|
|
|
"Get the value of the variable or special parameter @var{name} in
|
|
|
|
@var{env}. If @var{name} is unset, return @code{#f}."
|
|
|
|
(match name
|
|
|
|
("?" (number->string (environment-status env)))
|
|
|
|
(_ (var-ref env name))))
|
|
|
|
|
|
|
|
(define (parameter-ref* env name)
|
|
|
|
"Get the value of the variable or special parameter @var{name} in
|
|
|
|
@var{env}. If @var{name} is unset, return @code{\"\"}."
|
|
|
|
(or (parameter-ref env name) ""))
|
|
|
|
|
2018-07-12 03:05:15 +01:00
|
|
|
(define (word->qword env word)
|
|
|
|
"Convert @var{word} into a qword by resolving all parameter, command,
|
|
|
|
and arithmetic substitions using the environment @var{env}."
|
|
|
|
(match word
|
|
|
|
((? string?)
|
|
|
|
word)
|
|
|
|
(('<sh-quote> quoted-word)
|
|
|
|
`(<sh-quote> ,(word->qword env quoted-word)))
|
|
|
|
(('<sh-cmd-sub> . exps)
|
|
|
|
((eval-cmd-sub) exps))
|
|
|
|
(('<sh-ref> name)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(parameter-ref* env name))
|
2018-07-12 03:05:15 +01:00
|
|
|
(('<sh-ref-or> name default)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(or (parameter-ref env name)
|
2018-07-12 03:05:15 +01:00
|
|
|
(word->qword env (or default ""))))
|
|
|
|
(('<sh-ref-or*> name default)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(let ((value (parameter-ref env name)))
|
2018-07-12 03:05:15 +01:00
|
|
|
(if (string-not-null? value)
|
|
|
|
value
|
|
|
|
(word->qword env (or default "")))))
|
|
|
|
(('<sh-ref-or!> name default)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(or (parameter-ref env name)
|
2018-07-12 03:05:15 +01:00
|
|
|
(let ((new-value (expand-word env (or default "")
|
|
|
|
#:split? #f #:rhs-tildes? #t)))
|
|
|
|
(set-var! env name new-value)
|
|
|
|
new-value)))
|
|
|
|
(('<sh-ref-or!*> name default)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(let ((value (parameter-ref env name)))
|
2018-07-12 03:05:15 +01:00
|
|
|
(if (string-not-null? value)
|
|
|
|
value
|
|
|
|
(let ((new-value (expand-word env (or default "")
|
|
|
|
#:split? #f #:rhs-tildes? #t)))
|
|
|
|
(set-var! env name new-value)
|
|
|
|
new-value))))
|
|
|
|
(('<sh-ref-assert> name message) (error "Not implemented"))
|
|
|
|
(('<sh-ref-assert*> name message) (error "Not implemented"))
|
|
|
|
(('<sh-ref-and> name value)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(if (string-not-null? (parameter-ref env name))
|
2018-07-12 03:05:15 +01:00
|
|
|
(word->qword env (or value ""))
|
|
|
|
""))
|
|
|
|
(('<sh-ref-and*> name value)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(or (and (parameter-ref env name)
|
2018-07-12 03:05:15 +01:00
|
|
|
(word->qword env (or value "")))
|
|
|
|
""))
|
|
|
|
(('<sh-ref-except-min> name pattern) (error "Not implemented"))
|
|
|
|
(('<sh-ref-except-max> name pattern) (error "Not implemented"))
|
|
|
|
(('<sh-ref-skip-min> name pattern) (error "Not implemented"))
|
|
|
|
(('<sh-ref-skip-max> name pattern) (error "Not implemented"))
|
|
|
|
(('<sh-ref-length> name)
|
Add 'status' field to environment
Instead of using an environment variable named '?', we will use a
special environment field called 'status'. This lets us get rid of a
lot of number-string conversions (since an environment variable has to
have a string value).
* geesh/environment.scm (<environment>): Add a 'status' field.
(make-environment): Set it to 0 by default.
* geesh/repl.scm (run-repl): Use new field in place of '?' variable.
* geesh/shell.scm (exec-utility, sh:and, sh:exec-let, sh:for, sh:not,
sh:or, sh:pipeline, sh:subshell, sh:substitute-command): Ditto.
* geesh/word.scm (parameter-ref): New function that handles both
special parameters (e.g., '?') and variables.
(parameter-ref*): Like 'var-ref*', but for 'parameter-ref'.
(word->qword): Replace 'var-ref' and 'var-ref*' with 'parameter-ref'
and 'parameter-ref*' respectively.
2018-11-09 02:29:19 +00:00
|
|
|
(number->string (string-length (parameter-ref* env name))))
|
2018-07-12 03:05:15 +01:00
|
|
|
(_ (map (cut word->qword env <>) word))))
|
|
|
|
|
|
|
|
(define* (expand-word env word #:key (split? #t) (rhs-tildes? #f))
|
|
|
|
"Expand @var{word} into a list of fields using the environment
|
|
|
|
@var{env}."
|
|
|
|
(let ((qword (word->qword env word)))
|
|
|
|
(if split?
|
|
|
|
(map remove-quotes
|
|
|
|
(split-fields qword (char-set #\newline #\tab #\space)))
|
|
|
|
(remove-quotes qword))))
|