Implementation of SRFI-98 (An interface to access environment variables).
authorJulian Graham <julian@transmetropolitan.(none)>
Thu, 28 May 2009 22:15:05 +0000 (18:15 -0400)
committerJulian Graham <julian@transmetropolitan.(none)>
Sun, 31 May 2009 01:19:59 +0000 (21:19 -0400)
* 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
doc/ref/srfi-modules.texi
module/srfi/srfi-98.scm [new file with mode: 0644]
test-suite/tests/srfi-98.test [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 1785fe8..9aca5d9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -50,6 +50,10 @@ simplifies code and reduces both the storage and run-time overhead.
 \f
 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'
index 1fa50b2..7c107e7 100644 (file)
@@ -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 (file)
index 0000000..924a205
--- /dev/null
@@ -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 <julian.graham@aya.yale.edu>
+;;; 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 (file)
index 0000000..3fbb1ef
--- /dev/null
@@ -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")))
+