Thread overview
acquiring the contents of environment variables
Aug 17, 2005
jicman
Aug 17, 2005
AJG
Aug 17, 2005
jicman
Aug 17, 2005
Chris Sauls
Aug 17, 2005
jicman
Aug 17, 2005
Derek Parnell
Aug 17, 2005
jicman
August 17, 2005
Greetings!

Does D has a library that can acquire the content of the environment variables of a system?  I know I can do a

set > c:\envvars.txt

and read that file and get "some" of the environment vars.  However, is there a proper library that I can do this from D?  I searched through the internet and most of the stuff I encountered were compiler variables, etc.

Any ideas?

thanks,

josé


August 17, 2005
Hi,

>Does D has a library that can acquire the content of the environment variables of a system?

I don't know if there's a phobos function for that.

>Any ideas?

What I do is:

http://www.opengroup.org/onlinepubs/009695399/functions/getenv.html

In addition, the external variable

extern char** environ;

Might be useful to you, to get them all at once.

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html

Cheers,
--AJG.


August 17, 2005
Thanks!  Yep, this will help me lots.

jic

AJG says...
>
>Hi,
>
>>Does D has a library that can acquire the content of the environment variables of a system?
>
>I don't know if there's a phobos function for that.
>
>>Any ideas?
>
>What I do is:
>
>http://www.opengroup.org/onlinepubs/009695399/functions/getenv.html
>
>In addition, the external variable
>
>extern char** environ;
>
>Might be useful to you, to get them all at once.
>
>http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
>
>Cheers,
>--AJG.
>
>


August 17, 2005
jicman wrote:
> Greetings!
> 
> Does D has a library that can acquire the content of the environment variables of a system?  I know I can do a
> 
> set > c:\envvars.txt
> 
> and read that file and get "some" of the environment vars.  However, is there a proper library that I can do this from D?  I searched through the internet and most of the stuff I encountered were compiler variables, etc.

It isn't perfect for what you want (yet) because it doesn't yet enumerate the full environment automatically, but it might give you an idea.  (What is it?)  Its an Environment singleton I threw together for an unannounced project of mine, code-name Mallard.  See what you think.  (Its the attachment.)

-- Chris Sauls


August 17, 2005
On Wed, 17 Aug 2005 14:53:20 +0000 (UTC), jicman wrote:

> Greetings!
> 
> Does D has a library that can acquire the content of the environment variables of a system?  I know I can do a
> 
> set > c:\envvars.txt
> 
> and read that file and get "some" of the environment vars.  However, is there a proper library that I can do this from D?  I searched through the internet and most of the stuff I encountered were compiler variables, etc.
> 
> Any ideas?
> 

I've used these snippets ...

extern (C)
{
        char*   getenv  (char *);
        int     putenv  (char *);
}

//-------------------------------------------------------
char[] GetEnv(char[] pSymbol)
//-------------------------------------------------------
{
    return std.string.toString(getenv(pSymbol));
}


putenv(envname ~ "=" ~ whatever);

-- 
Derek Parnell
Melbourne, Australia
18/08/2005 7:01:53 AM
August 17, 2005
Thanks.

Derek Parnell says...
>
>On Wed, 17 Aug 2005 14:53:20 +0000 (UTC), jicman wrote:
>
>> Greetings!
>> 
>> Does D has a library that can acquire the content of the environment variables of a system?  I know I can do a
>> 
>> set > c:\envvars.txt
>> 
>> and read that file and get "some" of the environment vars.  However, is there a proper library that I can do this from D?  I searched through the internet and most of the stuff I encountered were compiler variables, etc.
>> 
>> Any ideas?
>> 
>
>I've used these snippets ...
>
>extern (C)
>{
>        char*   getenv  (char *);
>        int     putenv  (char *);
>}
>
>//-------------------------------------------------------
>char[] GetEnv(char[] pSymbol)
>//-------------------------------------------------------
>{
>    return std.string.toString(getenv(pSymbol));
>}
>
>
>putenv(envname ~ "=" ~ whatever);
>
>-- 
>Derek Parnell
>Melbourne, Australia
>18/08/2005 7:01:53 AM


August 17, 2005
Thanks.

Chris Sauls says...
>
>This is a multi-part message in MIME format.
>--------------020006040509030700090304
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit
>
>jicman wrote:
>> Greetings!
>> 
>> Does D has a library that can acquire the content of the environment variables of a system?  I know I can do a
>> 
>> set > c:\envvars.txt
>> 
>> and read that file and get "some" of the environment vars.  However, is there a proper library that I can do this from D?  I searched through the internet and most of the stuff I encountered were compiler variables, etc.
>
>It isn't perfect for what you want (yet) because it doesn't yet enumerate the full environment automatically, but it might give you an idea.  (What is it?)  Its an Environment singleton I threw together for an unannounced project of mine, code-name Mallard.  See what you think.  (Its the attachment.)
>
>-- Chris Sauls
>
>--------------020006040509030700090304
>Content-Type: text/plain;
> name="environment.d"
>Content-Transfer-Encoding: 7bit
>Content-Disposition: inline;
> filename="environment.d"
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Mallard 0.1
>  An object-oriented CGI platform.
>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Getting/setting environment variables via the Environment singleton.
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/ module mallard.environment;
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Imports
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>// standard imports
>private import std.string   ;
>private import std.c.stdlib ; // for getenv()
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Aliases
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/ alias std.string.toString stdToString;
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Externs
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>private extern(C) int putenv(char*);
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Constants
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>// CGI environment variable names - useful to make typos compile-time errors
>public const char[]
>  DOCUMENT_ROOT         = `DOCUMENT_ROOT`         ,
>  GATEWAY_INTERFACE     = `GATEWAY_INTERFACE`     ,
>  HTTP_ACCEPT           = `HTTP_ACCEPT`           ,
>  HTTP_ACCEPT_CHARSET   = `HTTP_ACCEPT_CHARSET`   ,
>  HTTP_ACCEPT_ENCODING  = `HTTP_ACCEPT_ENCODING`  ,
>  HTTP_ACCEPT_LANGUAGE  = `HTTP_ACCEPT_LANGUAGE`  ,
>  HTTP_CONNECTION       = `HTTP_CONNECTION`       ,
>  HTTP_HOST             = `HTTP_HOST`             ,
>  HTTP_KEEP_ALIVE       = `HTTP_KEEP_ALIVE`       ,
>  HTTP_USER_AGENT       = `HTTP_USER_AGENT`       ,
>  PATH_INFO             = `PATH_INFO`             ,
>  PATH_TRANSLATED       = `PATH_TRANSLATED`       ,
>  QUERY_STRING          = `QUERY_STRING`          ,
>  REDIRECT_QUERY_STRING = `REDIRECT_QUERY_STRING` ,
>  REDIRECT_STATUS       = `REDIRECT_STATUS`       ,
>  REDIRECT_URL          = `REDIRECT_URL`          ,
>  REMOTE_ADDR           = `REMOTE_ADDR`           ,
>  REMOTE_PORT           = `REMOTE_PORT`           ,
>  REQUEST_METHOD        = `REQUEST_METHOD`        ,
>  REQUEST_URI           = `REQUEST_URI`           ,
>  SCRIPT_FILENAME       = `SCRIPT_FILENAME`       ,
>  SCRIPT_NAME           = `SCRIPT_NAME`           ,
>  SERVER_ADDR           = `SERVER_ADDR`           ,
>  SERVER_ADMIN          = `SERVER_ADMIN`          ,
>  SERVER_NAME           = `SERVER_NAME`           ,
>  SERVER_PORT           = `SERVER_PORT`           ,
>  SERVER_PROTOCOL       = `SERVER_PROTOCOL`       ,
>  SERVER_SIGNATURE      = `SERVER_SIGNATURE`      ,
>  SERVER_SOFTWARE       = `SERVER_SOFTWARE`       ;
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  Singleton Environment
>  TODO:
>    Implement a method to enumerate the entire environment (or a least its keys).
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/ public abstract class Environment { static:
>
>  /+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>    Getting environment variable values.
>    NOTE: Defaults to an empty string.
>    USAGE:
>      char[] var;
>      var = Environment.get("some key");
>      var = Environment["some key"];
>  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>  public char[] get (in char[] key) {
>    return stdToString(getenv(toStringz(key)));
>  }
>
>  public alias get opIndex;
>
>  /+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>    Setting environment variable values.
>    USAGE:
>      char[] key = "some key", value = "some value";
>      Environment.set(key, value);
>      Environment[key] = value;
>  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>  public int set (in char[] key, in char[] value) {
>    return putenv(toStringz(format("%s=%s", key, value)));
>  }
>
>  // can't use alias because the params are backward to support variable-length keysets
>  public int opIndexAssign (in char[] value, in char[] key) {
>    return set(key, value);
>  }
>
>  /+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>    An array of the CGI keys.  Can be useful for stepping through them all easily.
>    For an example, see the opApply overload below.
>  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>  public const char[][] STD_CGI_KEYS = [
>    DOCUMENT_ROOT         ,
>    GATEWAY_INTERFACE     ,
>    HTTP_ACCEPT           ,
>    HTTP_ACCEPT_CHARSET   ,
>    HTTP_ACCEPT_ENCODING  ,
>    HTTP_ACCEPT_LANGUAGE  ,
>    HTTP_CONNECTION       ,
>    HTTP_HOST             ,
>    HTTP_KEEP_ALIVE       ,
>    HTTP_USER_AGENT       ,
>    PATH_INFO             ,
>    PATH_TRANSLATED       ,
>    QUERY_STRING          ,
>    REDIRECT_QUERY_STRING ,
>    REDIRECT_STATUS       ,
>    REDIRECT_URL          ,
>    REMOTE_ADDR           ,
>    REMOTE_PORT           ,
>    REQUEST_METHOD        ,
>    REQUEST_URI           ,
>    SCRIPT_FILENAME       ,
>    SCRIPT_NAME           ,
>    SERVER_ADDR           ,
>    SERVER_ADMIN          ,
>    SERVER_NAME           ,
>    SERVER_PORT           ,
>    SERVER_PROTOCOL       ,
>    SERVER_SIGNATURE      ,
>    SERVER_SOFTWARE
>  ];
>
>  // For array-like syntax.  Some people like that.
>  public alias STD_CGI_KEYS keys;
>
>  /+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>    Make the Environment foreach'able.
>    Just steps through the standard CGI keys.
>  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>  // for just keys
>  public int opApply(int delegate(inout char[]) dg) {
>    int result ;
>
>    foreach (char[] key; STD_CGI_KEYS) {
>      result = dg(key);
>      if (result)
>        break;
>    }
>    return result;
>  }
>
>  // for key->value pairs
>  public int opApply(int delegate(inout char[], inout char[]) dg) {
>    int    result ;
>    char[] key    ,
>           value  ;
>
>    foreach (char[] key; STD_CGI_KEYS) {
>      value  = get (key       ) ;
>      result = dg  (key, value) ;
>      if (result)
>        break;
>    }
>    return result;
>  }
>
>} // end singleton Environment
>
>/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>--------------020006040509030700090304--