File: envvars.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (182 lines) | stat: -rw-r--r-- 4,996 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
   FALCON - The Falcon Programming Language
   FILE: envvars.cpp

   Short description
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: mer dic 27 2006

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Short description
*/

/*#
   @beginmodule core
*/

#include <falcon/setup.h>
#include <falcon/module.h>
#include <falcon/sys.h>
#include <falcon/vm.h>
#include "core_module.h"

/*#

   @funset core_environ Environment support
   @brief Functions used to access the process environment variables.

   Environment variables are an handful way to provide system wide
   configuration. Falcon RTL getenv(), setenv() and unsetenv() functions peek and
   manipulates environment variables.

   Variables set with "setenv()" will be available to child processes in case
   they are launched with the utilities in the Process module.

   @begingroup core_syssupport
   @beginset core_environ
*/

namespace Falcon {
namespace core {

/*#
   @function getenv
   @brief Get environment variable value.
   @param varName Environment variable name (as a string)
   @return The value of the environment variable or nil if it is not present.

   This function returns a string containing the value set for the given
   environment variable by the operating system before starting the Falcon process
   or or by a previous call to setenv(). If the given variable name is not
   declared, the function will return nil.

   On some systems (e.g. MS-Windows), setting a variable to an empty string is
   equivalent to unsetting it, so getenv() will never return an empty string. On
   other systems, environment variables may be set to empty strings, that may be
   returned by getenv().
*/

FALCON_FUNC  falcon_getenv( ::Falcon::VMachine *vm )
{
   Item *i_var = vm->param( 0 );
   if ( i_var == 0 || ! i_var->isString() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
      return;
   }

   String retVal;
   if ( Sys::_getEnv( *i_var->asString(), retVal ) )
   {
      vm->retval( retVal ); // will garbage this
   }
   else {
      vm->retnil();
   }
}


/*#
   @function setenv
   @brief Set environment variable value.
   @param varName Environment variable name (as a string)
   @param value a value for the given variable.
   @raise IoError on failure.

   This function sets the given value for the given environment variable. The
   varName parameter must be a string, while value may be any Falcon value. If the
   value is not a string, it will be converted
   using the toString() function.

   If the variable was previously set to a different value, its value is changed;
   if it doesn't existed, it is created.

   The function may fail if the system cannot perform the operation; this may
   happen if the space that the system reserves for environment variables is
   exhausted. In this case, the function raises an error.
*/

FALCON_FUNC  falcon_setenv( ::Falcon::VMachine *vm )
{
   Item *i_var = vm->param( 0 );
   Item *i_value = vm->param( 1 );
   if ( i_var == 0 || ! i_var->isString() || i_value == 0  )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
   }

   String *value;
   String localVal;

   if ( i_value->isString() )
      value = i_value->asString();
   else
   {
      value = &localVal;
      vm->itemToString( *value, i_value );
   }

   if ( ! Sys::_setEnv( *i_var->asString(), *value ) )
   {
      throw new IoError( ErrorParam( 1000, __LINE__ ).
         origin( e_orig_runtime ).desc( "Environment variable set failed." ).
         extra( *i_var->asString() ).
         sysError( (uint32) Sys::_lastError() ) );
   }
}


/*#
   @function unsetenv
   @brief Clear environment variable value.
   @param varName Environment variable name (as a string)

   This function removes a given variable setting, causing
   subsequents getenv( varName ) to return nil.
*/

FALCON_FUNC  falcon_unsetenv( ::Falcon::VMachine *vm )
{
   Item *i_var = vm->param( 0 );
   if ( i_var == 0 || ! i_var->isString() )
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ ).origin( e_orig_runtime ) );
   }

   Sys::_unsetEnv( *i_var->asString() );
}


static void _falcon_getEnviron_cb( const String& key, const String& value, void* data )
{
   LinearDict* ret = (LinearDict*) data;
   ret->put( new CoreString( key ), new CoreString( value ) );
}

/*#
   @function getEnviron()
   @brief Return a dictionary containing all the environment variables.
   @return A dictionary where each key is an environment variable.
*/

FALCON_FUNC  falcon_getEnviron( ::Falcon::VMachine *vm )
{
   LinearDict* ret = new LinearDict;
   Sys::_enumerateEnvironment( &_falcon_getEnviron_cb, ret );
   vm->retval( new CoreDict( ret ) );
}



}
}


/* end of envvars.cpp */