From c1f8a870edd6db27d025dacebab098ffe1186c18 Mon Sep 17 00:00:00 2001 From: Timothy Sample Date: Sun, 25 Nov 2018 22:42:05 -0500 Subject: [PATCH] Add the dot and eval built-ins * geesh/built-ins/utils.scm (get-evaluator): New public function. * geesh/built-ins/dot.scm: New file. * geesh/built-ins/eval.scm: New file. * geesh/Makefile.am: Add them. * geesh/built-ins.scm (*special-built-ins*): Add dot and eval. --- Makefile.am | 2 ++ geesh/built-ins.scm | 4 +-- geesh/built-ins/dot.scm | 53 +++++++++++++++++++++++++++++++++++++++ geesh/built-ins/eval.scm | 38 ++++++++++++++++++++++++++++ geesh/built-ins/utils.scm | 9 ++++++- 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 geesh/built-ins/dot.scm create mode 100644 geesh/built-ins/eval.scm diff --git a/Makefile.am b/Makefile.am index ba60bd8..6269967 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,7 +45,9 @@ MODULES = \ geesh/built-ins/cd.scm \ geesh/built-ins/colon.scm \ geesh/built-ins/continue.scm \ + geesh/built-ins/dot.scm \ geesh/built-ins/echo.scm \ + geesh/built-ins/eval.scm \ geesh/built-ins/export.scm \ geesh/built-ins/false.scm \ geesh/built-ins/pwd.scm \ diff --git a/geesh/built-ins.scm b/geesh/built-ins.scm index de21c79..7dd14b5 100644 --- a/geesh/built-ins.scm +++ b/geesh/built-ins.scm @@ -32,11 +32,11 @@ ;; Special built-ins take precedence over any other command. (define *special-built-ins* - `(("." . ,undefined) + `(("." . ,(@@ (geesh built-ins dot) main)) (":" . ,(@@ (geesh built-ins colon) main)) ("break" . ,(@@ (geesh built-ins break) main)) ("continue" . ,(@@ (geesh built-ins continue) main)) - ("eval" . ,undefined) + ("eval" . ,(@@ (geesh built-ins eval) main)) ("exec" . ,undefined) ("exit" . ,undefined) ("export" . ,(@@ (geesh built-ins export) main)) diff --git a/geesh/built-ins/dot.scm b/geesh/built-ins/dot.scm new file mode 100644 index 0000000..6405a6f --- /dev/null +++ b/geesh/built-ins/dot.scm @@ -0,0 +1,53 @@ +;;; The Geesh Shell Interpreter +;;; Copyright 2018 Timothy Sample +;;; +;;; 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 . + +(define-module (geesh built-ins dot) + #:use-module (geesh built-ins utils) + #:use-module (geesh environment) + #:use-module (geesh parser) + #:use-module (ice-9 match)) + +;;; Commentary: +;;; +;;; The 'dot' utility. +;;; +;;; Code: + +(define (main . args) + (match args + ((file) + (catch 'system-error + (lambda () + (call-with-input-file file + (lambda (port) + (set-status! 0) + (let loop () + (match (read-sh port) + ((? eof-object?) (get-status)) + (exp ((get-evaluator) exp) + (loop))))))) + (lambda args + (format (current-error-port) + "~a: .: ~a: ~a.~%" + (car (program-arguments)) file + (strerror (system-error-errno args))) + EXIT_FAILURE))) + (_ (format (current-error-port) + "~a: .: Invalid options ~s.~%" + (car (program-arguments)) args) + EXIT_FAILURE))) diff --git a/geesh/built-ins/eval.scm b/geesh/built-ins/eval.scm new file mode 100644 index 0000000..9c487ae --- /dev/null +++ b/geesh/built-ins/eval.scm @@ -0,0 +1,38 @@ +;;; The Geesh Shell Interpreter +;;; Copyright 2018 Timothy Sample +;;; +;;; 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 . + +(define-module (geesh built-ins eval) + #:use-module (geesh built-ins utils) + #:use-module (geesh environment) + #:use-module (geesh parser) + #:use-module (ice-9 match)) + +;;; Commentary: +;;; +;;; The 'eval' utility. +;;; +;;; Code: + +(define (main . args) + (match (string-trim-both (string-join args " ") #\space) + ("" 0) + (code + (call-with-input-string code + (lambda (port) + ((get-evaluator) `( ,@(read-sh-all port))))) + (get-status)))) diff --git a/geesh/built-ins/utils.scm b/geesh/built-ins/utils.scm index 41e5bdd..e263d0e 100644 --- a/geesh/built-ins/utils.scm +++ b/geesh/built-ins/utils.scm @@ -18,7 +18,8 @@ (define-module (geesh built-ins utils) #:use-module (ice-9 match) - #:export (split-assignment)) + #:export (get-evaluator + split-assignment)) ;;; Commentary: ;;; @@ -26,6 +27,12 @@ ;;; ;;; Code: +;; We need to be able to run Shell code from the 'dot' and 'eval' +;; built-ins. This is a bit of trickery to avoid a module dependency +;; loop. +(define (get-evaluator) + (module-ref (resolve-interface '(geesh eval)) 'eval-sh)) + (define (split-assignment assignment) (match (string-index assignment #\=) (#f (values assignment #f))