2018-07-17 18:28:46 +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 eval)
|
2018-07-19 04:49:55 +01:00
|
|
|
#:use-module (geesh environment)
|
2018-07-17 18:28:46 +01:00
|
|
|
#:use-module (geesh shell)
|
|
|
|
#:use-module (geesh word)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:export (eval-sh))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; This module provides an interpreter for the Shell language.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2018-11-21 01:44:33 +00:00
|
|
|
(define* (eval-word env word #:key (output 'fields) (rhs-tildes? #f))
|
2018-07-18 20:45:05 +01:00
|
|
|
(parameterize ((eval-cmd-sub (lambda (exps)
|
|
|
|
(sh:substitute-command env
|
|
|
|
(lambda ()
|
|
|
|
(for-each (cut eval-sh env <>) exps))))))
|
2018-11-21 01:44:33 +00:00
|
|
|
(expand-word env word #:output output #:rhs-tildes? rhs-tildes?)))
|
2018-07-17 18:28:46 +01:00
|
|
|
|
2018-07-18 05:54:38 +01:00
|
|
|
(define (eval-redir env redir)
|
|
|
|
"Evaluate the redirect @var{redir} in environment @var{env}."
|
|
|
|
(match-let* (((op fd word) redir)
|
2018-11-21 01:44:33 +00:00
|
|
|
(field (eval-word env word #:output 'string)))
|
2018-07-18 05:54:38 +01:00
|
|
|
(match op
|
|
|
|
((or '>& '<&)
|
|
|
|
(let ((n (string->number field)))
|
|
|
|
(cond
|
|
|
|
((and n (exact-integer? n)) `(,op ,fd ,n))
|
|
|
|
((string=? field "-") `(,op ,fd -))
|
|
|
|
(else (throw 'bad-dup)))))
|
|
|
|
(_ `(,op ,fd ,field)))))
|
|
|
|
|
|
|
|
(define (exp->thunk env exp)
|
|
|
|
(lambda () (eval-sh env exp)))
|
|
|
|
|
2018-07-18 20:09:11 +01:00
|
|
|
(define (exps->thunk env exps)
|
|
|
|
(lambda () (eval-sh env `(<sh-begin> ,@exps))))
|
|
|
|
|
2018-07-17 18:28:46 +01:00
|
|
|
(define (eval-sh env exp)
|
|
|
|
"Evaluate the Shell expression @var{exp} in the context of the Shell
|
|
|
|
environment @var{env}."
|
|
|
|
(match exp
|
2018-10-16 19:16:01 +01:00
|
|
|
(('<sh-and> exp1 exp2)
|
|
|
|
(sh:and env (exp->thunk env exp1) (exp->thunk env exp2)))
|
2018-07-18 20:03:11 +01:00
|
|
|
(('<sh-begin> . sub-exps)
|
|
|
|
(for-each (cut eval-sh env <>) sub-exps))
|
2018-11-21 16:37:48 +00:00
|
|
|
(('<sh-case> word (pattern-lists . sub-exp-lists) ...)
|
|
|
|
(let ((value (eval-word env word #:output 'string)))
|
|
|
|
(apply sh:case env value
|
|
|
|
(map (lambda (patterns sub-exps)
|
|
|
|
`(,(map (cut eval-word env <> #:output 'pattern)
|
|
|
|
patterns)
|
|
|
|
,(exps->thunk env sub-exps)))
|
|
|
|
pattern-lists
|
|
|
|
sub-exp-lists))))
|
|
|
|
(('<sh-cond> (test-exps . sub-exp-lists) ..1)
|
|
|
|
(apply sh:cond env
|
|
|
|
(map (lambda (test-exp sub-exps)
|
|
|
|
`(,(match test-exp
|
|
|
|
('<sh-else> #t)
|
|
|
|
(exp (exp->thunk env exp)))
|
|
|
|
,(exps->thunk env sub-exps)))
|
|
|
|
test-exps
|
|
|
|
sub-exp-lists)))
|
2018-11-09 03:22:23 +00:00
|
|
|
(('<sh-defun> name . sub-exps)
|
|
|
|
(let ((proc (lambda (env . args)
|
|
|
|
(eval-sh env `(<sh-begin> ,@sub-exps)))))
|
|
|
|
(define-environment-function! env name proc)))
|
2018-07-17 18:28:46 +01:00
|
|
|
(('<sh-exec> words ..1)
|
|
|
|
(let ((args (append-map (cut eval-word env <>) words)))
|
|
|
|
(match args
|
|
|
|
((name . args) (apply sh:exec env name args))
|
2018-07-18 05:54:38 +01:00
|
|
|
(() #f))))
|
2018-11-15 19:08:43 +00:00
|
|
|
(('<sh-exec-let> ((names var-words) ..1) cmd-words ..1)
|
|
|
|
(let* ((args (append-map (cut eval-word env <>) cmd-words))
|
|
|
|
(bindings (map (lambda (name word)
|
|
|
|
`(,name . ,(eval-word env word
|
2018-11-21 01:44:33 +00:00
|
|
|
#:output 'string
|
2018-11-15 19:08:43 +00:00
|
|
|
#:rhs-tildes? #t)))
|
|
|
|
names var-words)))
|
|
|
|
(match args
|
|
|
|
((name . args) (apply sh:exec-let env bindings name args))
|
|
|
|
(() (for-each (match-lambda
|
|
|
|
((name . value) (set-var! env name value)))
|
|
|
|
bindings)))))
|
2018-10-16 20:28:19 +01:00
|
|
|
(('<sh-for> (name (words ...)) . sub-exps)
|
|
|
|
(sh:for env `(,name ,(append-map (cut eval-word env <>) words))
|
|
|
|
(exps->thunk env sub-exps)))
|
2018-10-16 19:16:01 +01:00
|
|
|
(('<sh-not> exp)
|
|
|
|
(sh:not env (exp->thunk env exp)))
|
|
|
|
(('<sh-or> exp1 exp2)
|
|
|
|
(sh:or env (exp->thunk env exp1) (exp->thunk env exp2)))
|
2018-10-16 18:58:03 +01:00
|
|
|
(('<sh-pipeline> cmd*s ..1)
|
|
|
|
(apply sh:pipeline env (map (cut exp->thunk env <>) cmd*s)))
|
2018-07-19 04:49:55 +01:00
|
|
|
(('<sh-set!> (names words) ..1)
|
|
|
|
(for-each (lambda (name word)
|
|
|
|
(set-var! env name (eval-word env word
|
2018-11-21 01:44:33 +00:00
|
|
|
#:output 'string
|
2018-07-19 04:49:55 +01:00
|
|
|
#:rhs-tildes? #t)))
|
|
|
|
names words))
|
2018-07-18 20:09:11 +01:00
|
|
|
(('<sh-subshell> . sub-exps)
|
|
|
|
(sh:subshell env (exps->thunk env sub-exps)))
|
2018-11-21 16:35:13 +00:00
|
|
|
(('<sh-while> test-exp sub-exps ..1)
|
|
|
|
(sh:while env (exp->thunk env test-exp) (exps->thunk env sub-exps)))
|
2018-07-18 05:54:38 +01:00
|
|
|
(('<sh-with-redirects> (redirs ..1) sub-exp)
|
|
|
|
(match sub-exp
|
|
|
|
;; For "simple commands" we have to observe a special order of
|
|
|
|
;; evaluation: first command words, then redirects, and finally
|
|
|
|
;; assignment words.
|
|
|
|
(('<sh-exec> words ..1)
|
2018-11-15 19:07:03 +00:00
|
|
|
(let ((args (append-map (cut eval-word env <>) words)))
|
|
|
|
(match (false-if-exception
|
|
|
|
(map (cut eval-redir env <>) redirs))
|
|
|
|
(#f (set-environment-status! env 1))
|
|
|
|
(redirs
|
|
|
|
(match args
|
2018-11-15 19:18:16 +00:00
|
|
|
;; This built-in, called with no arguments, is a very
|
|
|
|
;; special case. We need to treat the redirects
|
|
|
|
;; directly rather than pass them to
|
|
|
|
;; 'sh:with-redirects'.
|
|
|
|
(("exec") (sh:set-redirects env redirs))
|
2018-11-15 19:07:03 +00:00
|
|
|
((name . args)
|
|
|
|
(sh:with-redirects env redirs
|
|
|
|
(lambda ()
|
|
|
|
(apply sh:exec env name args))))
|
|
|
|
(() #f))))))
|
2018-11-15 19:08:43 +00:00
|
|
|
(('<sh-exec-let> ((names var-words) ..1) cmd-words ..1)
|
|
|
|
(let ((args (append-map (cut eval-word env <>) cmd-words)))
|
|
|
|
(match (false-if-exception
|
|
|
|
(map (cut eval-redir env <>) redirs))
|
|
|
|
(#f (set-environment-status! env 1))
|
|
|
|
(redirs
|
|
|
|
(let ((bindings (map (lambda (name word)
|
|
|
|
`(,name . ,(eval-word env word
|
2018-11-21 01:44:33 +00:00
|
|
|
#:output 'string
|
2018-11-15 19:08:43 +00:00
|
|
|
#:rhs-tildes? #t)))
|
|
|
|
names var-words)))
|
|
|
|
(match args
|
2018-11-15 19:18:16 +00:00
|
|
|
;; See the '<sh-exec>' case for why this built-in is
|
|
|
|
;; treated specially.
|
|
|
|
(("exec") (sh:set-redirects env redirs))
|
2018-11-15 19:08:43 +00:00
|
|
|
((name . args)
|
|
|
|
(sh:with-redirects env redirs
|
|
|
|
(lambda ()
|
|
|
|
(apply sh:exec-let env bindings name args))))
|
|
|
|
(() (for-each (match-lambda
|
|
|
|
((name . value) (set-var! env name value)))
|
|
|
|
bindings))))))))
|
2018-11-15 19:07:03 +00:00
|
|
|
(_ (match (false-if-exception
|
|
|
|
(map (cut eval-redir env <>) redirs))
|
|
|
|
(#f (set-environment-status! env 1))
|
|
|
|
(redirs
|
2018-07-18 05:54:38 +01:00
|
|
|
(sh:with-redirects env redirs
|
2018-11-21 16:35:13 +00:00
|
|
|
(exp->thunk env sub-exp)))))))
|
|
|
|
(('<sh-until> test-exp sub-exps ..1)
|
|
|
|
(sh:until env (exp->thunk env test-exp) (exps->thunk env sub-exps)))))
|