2 Copyright (C) 2001-2008, The Perl Foundation.
7 src/pmc/env.pmc - System Environment
11 C<Env> is a singleton class which provides access to the system environment.
21 #include "parrot/parrot.h"
23 /* array of environment variables,
24 speced in POSIX.1, but not in ISO-C
25 MS C compilers know about environ, as it is declared in stdlib.h.
26 OS X doesn't allow access to "environ" from within shared libraries.
30 # include <crt_externs.h>
31 # define environ (*_NSGetEnviron())
32 # else /* !__APPLE_CC__ */
33 extern char **environ;
34 # endif /* __APPLE_CC__ */
38 pmclass Env singleton provides hash {
42 =item C<void *get_pointer()>
44 =item C<void set_pointer(void *ptr)>
46 These two functions are part of the singleton creation interface. For more
47 information see F<src/pmc.c>.
56 VTABLE void *get_pointer() {
60 VTABLE void set_pointer(void *ptr) {
66 =item C<PMC *get_iter()>
68 Returns a new iterator for the environment.
69 This method is questionable, as environ is not in ISO-C.
75 VTABLE PMC *get_iter() {
76 STRING *name = CONST_STRING(interp, "set_key");
77 PMC *iter = pmc_new_init(interp, enum_class_Iterator, SELF);
78 PMC *key = pmc_new(interp, enum_class_Key);
80 Parrot_PCCINVOKE(interp, iter, name, "P->", key);
81 PObj_get_FLAGS(key) |= KEY_integer_FLAG;
85 PMC_int_val(key) = -1;
92 =item C<INTVAL elements()>
94 Returns the number of elements in the environment.
95 This method is questionable, as environ is not in ISO-C.
101 VTABLE INTVAL elements() {
104 while (environ[rv] != NULL)
112 =item C<INTVAL get_integer()>
114 Returns the size of the hash.
120 VTABLE INTVAL get_integer() {
121 return SELF.elements();
126 =item C<FLOATVAL get_number()>
128 Returns the size of the hash.
133 VTABLE FLOATVAL get_number() {
134 return SELF.elements();
139 =item C<STRING *get_string_keyed(PMC *key)>
141 Returns the Parrot string value for the environment variable C<*key>.
147 VTABLE STRING *get_string_keyed(PMC *key) {
148 if ((PObj_get_FLAGS(key) & KEY_type_FLAGS) == KEY_integer_FLAG) {
149 if (PMC_int_val(key) < 0 || PMC_int_val(key) >= SELF.elements()) {
150 return CONST_STRING(interp, "");
153 const char * const envp = environ[PMC_int_val(key)];
154 const char * const p = strchr(envp, '=');
155 return string_from_cstring(interp, envp, (INTVAL)(p - envp));
159 char * const keyname = string_to_cstring(interp,
160 VTABLE_get_string(interp, key));
165 val = Parrot_getenv(keyname, &free_it);
166 string_cstring_free(keyname);
169 STRING *retval = string_from_cstring(interp, val, 0);
179 return CONST_STRING(interp, "");
185 =item C<STRING *get_pmc_keyed(PMC *key)>
187 Returns a String PMC for the environment variable C<*key>.
193 VTABLE PMC *get_pmc_keyed(PMC *key) {
194 char * const keyname = string_to_cstring(INTERP,
195 VTABLE_get_string(INTERP, key));
198 STRING *retval = NULL;
203 val = Parrot_getenv(keyname, &free_it);
204 string_cstring_free(keyname);
207 retval = string_from_cstring(INTERP, val, 0);
214 retval = CONST_STRING(INTERP, "");
216 return_pmc = pmc_new(INTERP, enum_class_String);
218 VTABLE_set_string_native(INTERP, return_pmc, retval);
224 =item C<void set_string_keyed(PMC *key, STRING *value)>
226 Sets the environment variable C<*key> to C<*value>.
232 VTABLE void set_string_keyed(PMC *key, STRING *value) {
233 char * const keyname = string_to_cstring(INTERP,
234 VTABLE_get_string(INTERP, key));
235 char * const env_val = string_to_cstring(INTERP, value);
237 if (keyname && env_val)
238 Parrot_setenv(keyname, env_val);
241 string_cstring_free(keyname);
244 string_cstring_free(env_val);
249 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
251 Sets the environment variable C<*key> to C<*value>.
257 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
258 char * const keyname = string_to_cstring(INTERP,
259 VTABLE_get_string(INTERP, key));
261 const STRING * const str_value = VTABLE_get_string(INTERP, value);
262 char * const env_val = string_to_cstring(INTERP, str_value);
264 if (keyname && env_val)
265 Parrot_setenv(keyname, env_val);
268 string_cstring_free(keyname);
271 string_cstring_free(env_val);
276 =item C<INTVAL exists_keyed(PMC *key)>
278 Returns whether the environment variable for C<*key> exists.
284 VTABLE INTVAL exists_keyed(PMC *key) {
285 char * const keyname = string_to_cstring(INTERP,
286 VTABLE_get_string(INTERP, key));
290 char * const val = Parrot_getenv(keyname, &free_it);
291 string_cstring_free(keyname);
305 =item C<void delete_keyed(PMC *key)>
307 Deletes the the environment variable for C<*key>.
313 VTABLE void delete_keyed(PMC *key) {
314 char * const keyname = string_to_cstring(INTERP,
315 VTABLE_get_string(INTERP, key));
319 char * const val = Parrot_getenv(keyname, &free_it);
325 Parrot_unsetenv(keyname);
328 string_cstring_free(keyname);
340 L<http://www.parrotcode.org/docs/pdd/pdd17_pdd.html#Hash_types>
342 Environment in Perl 6 - L<http://dev.perl.org/perl6/rfc/318.html>
344 Module for Perl 5 - L<http://search.cpan.org/~stas/Env-C-0.06/>
352 * c-file-style: "parrot"
354 * vim: expandtab shiftwidth=4: