From 922d417bf4a7c4eb7d956d340161ad6407545ae7 Mon Sep 17 00:00:00 2001 From: Julian Graham Date: Thu, 28 May 2009 18:15:05 -0400 Subject: [PATCH] Implementation of SRFI-98 (An interface to access environment variables). * NEWS: Add SRFI-98 to 1.8.7 features. * doc/ref/srfi-modules.text (SRFI-98): Documentation for SRFI-98. * module/srfi/srfi-98.scm: New file. SRFI-98 implementation. * test-suite/tests/srfi-98.test: New file. SRFI-98 unit tests. --- NEWS | 4 ++++ doc/ref/srfi-modules.texi | 20 ++++++++++++++++ module/srfi/srfi-98.scm | 44 +++++++++++++++++++++++++++++++++++ test-suite/tests/srfi-98.test | 38 ++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 module/srfi/srfi-98.scm create mode 100644 test-suite/tests/srfi-98.test diff --git a/NEWS b/NEWS index 1785fe8d2..9aca5d912 100644 --- a/NEWS +++ b/NEWS @@ -50,6 +50,10 @@ simplifies code and reduces both the storage and run-time overhead. Changes in 1.8.7 (since 1.8.6) +* New modules (see the manual for details) + +** `(srfi srfi-98)', an interface to access environment variables + * Bugs fixed ** Fix compilation with `--disable-deprecated' diff --git a/doc/ref/srfi-modules.texi b/doc/ref/srfi-modules.texi index 1fa50b209..7c107e710 100644 --- a/doc/ref/srfi-modules.texi +++ b/doc/ref/srfi-modules.texi @@ -47,6 +47,7 @@ get the relevant SRFI documents from the SRFI home page * SRFI-61:: A more general `cond' clause * SRFI-69:: Basic hash tables. * SRFI-88:: Keyword objects. +* SRFI-98:: Accessing environment variables. @end menu @@ -3608,6 +3609,25 @@ Return the keyword object whose name is @var{str}. @end example @end deffn +@node SRFI-98 +@subsection SRFI-98 Accessing environment variables. +@cindex SRFI-98 +@cindex environment variables + +This is a portable wrapper around Guile's built-in support for +interacting with the current environment, @xref{Runtime Environment}. + +@deffn {Scheme Procedure} get-environment-variable name +Returns a string containing the value of the environment variable +given by the string @code{name}, or @code{#f} if the named +environment variable is not found. This is equivalent to +@code{(getenv name)}. +@end deffn + +@deffn {Scheme Procedure} get-environment-variables +Returns the names and values of all the environment variables as an +association list in which both the keys and the values are strings. +@end deffn @c srfi-modules.texi ends here diff --git a/module/srfi/srfi-98.scm b/module/srfi/srfi-98.scm new file mode 100644 index 000000000..924a20578 --- /dev/null +++ b/module/srfi/srfi-98.scm @@ -0,0 +1,44 @@ +;;; srfi-98.scm --- An interface to access environment variables + +;; Copyright (C) 2009 Free Software Foundation, Inc. +;; +;; This library is free software; you can redistribute it and/or +;; modify it under the terms of the GNU Lesser General Public +;; License as published by the Free Software Foundation; either +;; version 2.1 of the License, or (at your option) any later version. +;; +;; This library 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 +;; Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public +;; License along with this library; if not, write to the Free Software +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +;;; Author: Julian Graham +;;; Date: 2009-05-26 + +;;; Commentary: + +;; This is an implementation of SRFI-98 (An interface to access environment +;; variables). +;; +;; This module is fully documented in the Guile Reference Manual. + +;;; Code: + +(define-module (srfi srfi-98) + :use-module (srfi srfi-1) + :export (get-environment-variable + get-environment-variables)) + +(cond-expand-provide (current-module) '(srfi-98)) + +(define get-environment-variable getenv) +(define (get-environment-variables) + (define (string->alist-entry str) + (let ((pvt (string-index str #\=)) + (len (string-length str))) + (and pvt (cons (substring str 0 pvt) (substring str (+ pvt 1) len))))) + (filter-map string->alist-entry (environ))) diff --git a/test-suite/tests/srfi-98.test b/test-suite/tests/srfi-98.test new file mode 100644 index 000000000..3fbb1ef03 --- /dev/null +++ b/test-suite/tests/srfi-98.test @@ -0,0 +1,38 @@ +;;;; srfi-98.test --- Test suite for Guile's SRFI-98 functions. -*- scheme -*- +;;;; +;;;; Copyright 2009 Free Software Foundation, Inc. +;;;; +;;;; This program 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 2, or (at your option) +;;;; any later version. +;;;; +;;;; This program 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 this software; see the file COPYING. If not, write to +;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;;;; Boston, MA 02110-1301 USA + +(define-module (test-srfi-98) + #:use-module (srfi srfi-98) + #:use-module (test-suite lib)) + +(with-test-prefix "get-environment-variable" + (pass-if "get-environment-variable retrieves binding" + (putenv "foo=bar") + (equal? (get-environment-variable "foo") "bar")) + + (pass-if "get-environment-variable #f on unbound name" + (unsetenv "foo") + (not (get-environment-variable "foo")))) + +(with-test-prefix "get-environment-variables" + + (pass-if "get-environment-variables contains binding" + (putenv "foo=bar") + (equal? (assoc-ref (get-environment-variables) "foo") "bar"))) + -- 2.20.1