Add 'unset' built-in

* geesh/environment.scm (delete-environment-vars!): New public function.
(delete-environment-functions!): New public function.
* geesh/built-ins/unset.scm: New file.
* Makefile.am: Add it.
* geesh/built-ins.scm: Enable 'unset'.
This commit is contained in:
Timothy Sample 2018-11-15 21:05:12 -05:00
parent 8fe509359d
commit 7e3bbda4cb
4 changed files with 51 additions and 1 deletions

View File

@ -44,6 +44,7 @@ MODULES = \
geesh/built-ins/echo.scm \
geesh/built-ins/false.scm \
geesh/built-ins/read.scm \
geesh/built-ins/unset.scm \
geesh/built-ins.scm \
geesh/environment.scm \
geesh/eval.scm \

View File

@ -46,7 +46,7 @@
("shift" . ,undefined)
("times" . ,undefined)
("trap" . ,undefined)
("unset" . ,undefined)))
("unset" . ,(@@ (geesh built-ins unset) main))))
;; Regular built-ins take precendence over utilities in the search
;; path, but not over functions.

37
geesh/built-ins/unset.scm Normal file
View File

@ -0,0 +1,37 @@
;;; 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 built-ins unset)
#:use-module (geesh environment)
#:use-module (ice-9 match))
;;; Commentary:
;;;
;;; The 'unset' utility.
;;;
;;; Code:
(define (main env . args)
(match args
(("-f" . names)
(delete-environment-functions! env names)
0)
((or ("-v" . names)
names)
(delete-environment-vars! env names)
0)))

View File

@ -27,12 +27,14 @@
var-ref
var-ref*
set-var!
delete-environment-vars!
environment->environ
environ->alist
environment-status
set-environment-status!
environment-function-ref
define-environment-function!
delete-environment-functions!
environment-arguments
with-environment-arguments))
@ -76,6 +78,11 @@
(set-environment-vars! env (acons name val
(environment-vars env))))
(define (delete-environment-vars! env names)
(set-environment-vars! env (remove (match-lambda
((key . _) (member key names)))
(environment-vars env))))
(define* (environment->environ env #:optional (bindings '()))
"Convert the environment variables from @var{env} into a list of
@code{\"name=value\"} strings (an @dfn{environ}). If @var{bindings}
@ -110,6 +117,11 @@ such function, return @code{#f}."
(set-environment-functions! env (acons name proc
(environment-functions env))))
(define (delete-environment-functions! env . names)
(set-environment-functions! env (remove (match-lambda
((key . _) (member key names)))
(environment-functions env))))
(define (with-environment-arguments env arguments thunk)
"Call @var{thunk} with the arguments in @var{env} set to
@var{arguments}."