Title

An interface to access environment variables.

Author

Taro Minowa(Higepon)

Status

This SRFI is currently in ``draft'' status. To see an explanation of each status that a SRFI can hold, see here. To provide input on this SRFI, please mail to <srfi minus 98 at srfi dot schemers dot org>. See instructions here to subscribe to the list. You can access previous messages via the archive of the mailing list.

Abstract

This SRFI specifies the procedure getenv which gets the value of the specified environment variable.

Issues

Rationale

Most operating systems provide the mechanism of passing auxiliary parameters implicitly to child processes, namely called enviornment variables. Some applications rely on environment variables to modify their behavior according to the local settings.

Most implementations of common gateway interface (CGI) uses environment variables to pass Meta-Variables from the server to the script[1]. Providing the means to access to the environment variables is indispensable to write practical programs in Scheme.

In fact, most widely-used Scheme implementations provide the means of getting the value of the specified environment variable, usually called getenv.

Additionally, some implementations provide the means to get all environment variables.

For obtaining the value of the environment value, getenv may use locale-setting information to encode the name, and decode the value of the environment variable.

Conceptually, environment variables are mappings from string names to values.

For example, CGI programs may obtain the values of the Meta-Variables such as "QUERY_STRING", "CONTENT_LENGTH" and "REQUEST_METHOD", as the following examples:

(getenv "QUERY_STRING") => "foo=bar&huga=hige"
(getenv "CONTENT_LENGTH") => "512"
(getenv "REQUEST_METHOD") => "post"

Therefore, it is quite useful to define the environment-independent way to get environment variables.

[1] The Common Gateway Interface (CGI) Version 1.1, RFC3875, http://www.ietf.org/rfc/rfc3875.

Specification

R6RS library name
Following two procedures belong to the R6RS library named (system getenv). This is a provisional library name until naming rules for SRFI library on R6RS are specified.
Function: getenv name
Returns the value of the named environment variable as a string, or #f if the named environment variable is not found.
(getenv "PATH") => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
Function: getenv
Returns names and values of all the environment variables as an a-list.
(getenv) => (("PATH" . "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin") ("USERNAME" . "taro"))

Implementation

Chicken, PLT Scheme and MzScheme

(let ((%getenv getenv))
  (set! getenv
    (lambda args
      (if (null? args)
        '()
        (%getenv (car args)))))))

Gauche

(define (getenv . args)
  (if (null? args)
      (sys-environ->alist)
      (apply sys-getenv args))))

Scheme48 and Scsh

(define (getenv . args)
  (if (null? args)
      (environment-alist)
      (apply lookup-environment-variable args))))

STklos

getenv is built into stklos.

Appendix: Existing implementations

Scheme implementationget environment variableget all the environment variables as an a-list
Bigloo(getenv name) => (or string? false) name:string? 
CHICKEN(getenv name) => (or string? false) name:string? 
Gambit(getenv name . <default>) ->(or string? <default> <Unbound OS environment variable error>) name:string? 
Gauche(sys-getenv name) => (or string? false) name:string?(sys-environ)
Guile(getenv name) => (or string? false) name:string? 
PLT(getenv name) => (or string? false) name:string? 
Scheme48(lookup-environment-variable name) => (or string? false) name:string?(environment-alist)
SLIB(getenv name) => (or string? false) name:string? 
STk(getenv name) => (or string? false) name:string? 
STklos(getenv name) => (or string? false) name:string?(getenv)

Acknowledgements

Thanks to Shiro Kawai, jmuk, Kokosabu, leque and all the members of the #Lisp_Scheme IRC channel on Freenode.

Copyright

Copyright (C) Taro Minowa(Higepon) (2008). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.