2 Copyright (C) 2001-2008, Parrot Foundation.
7 src/pmc/env.pmc - System Environment
11 C<Env> is a singleton class which provides access to the system environment.
13 XXX Think about returning Pair back. Currently there is no way to iterate over
24 /* array of environment variables,
25 speced in POSIX.1, but not in ISO-C
26 MS C compilers know about environ, as it is declared in stdlib.h.
27 OS X doesn't allow access to "environ" from within shared libraries.
31 # include <crt_externs.h>
32 # define environ (*_NSGetEnviron())
33 # else /* !__APPLE_CC__ */
34 extern char **environ;
35 # endif /* __APPLE_CC__ */
39 pmclass Env singleton provides hash {
43 =item C<void *get_pointer()>
45 =item C<void set_pointer(void *ptr)>
47 These two functions are part of the singleton creation interface. For more
48 information see F<src/pmc.c>.
57 VTABLE void *get_pointer() {
61 VTABLE void set_pointer(void *ptr) {
67 =item C<PMC *get_iter()>
69 Returns a new iterator for the environment.
70 This method is questionable, as environ is not in ISO-C.
76 VTABLE PMC *get_iter() {
77 return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
82 =item C<INTVAL elements()>
84 Returns the number of elements in the environment.
85 This method is questionable, as environ is not in ISO-C.
91 VTABLE INTVAL elements() {
94 while (environ[rv] != NULL)
102 =item C<INTVAL get_bool()>
104 Returns whether the environment has any elements.
109 VTABLE INTVAL get_bool() {
110 return SELF.elements() ? 1 : 0;
115 =item C<INTVAL get_integer()>
117 Returns the size of the hash.
123 VTABLE INTVAL get_integer() {
124 return SELF.elements();
129 =item C<FLOATVAL get_number()>
131 Returns the size of the hash.
136 VTABLE FLOATVAL get_number() {
137 return SELF.elements();
142 =item C<STRING *get_string_keyed(PMC *key)>
144 Returns the Parrot string value for the environment variable C<*key>.
150 VTABLE STRING *get_string_keyed_str(STRING *key) {
151 if (!STRING_IS_EMPTY(key)) {
152 char * const val = Parrot_getenv(interp, key);
155 STRING * const retval = Parrot_str_new(interp, val, 0);
160 return string_from_literal(interp, "");
163 VTABLE STRING *get_string_keyed(PMC *key) {
164 return SELF.get_string_keyed_str(VTABLE_get_string(INTERP, key));
169 =item C<STRING *get_string_keyed_int(PMC *key)>
171 Returns the Parrot string value for the environment variable at position C<pos>.
173 Used during iteration.
179 VTABLE STRING *get_string_keyed_int(INTVAL pos) {
180 if (pos < 0 || pos >= SELF.elements()) {
181 return string_from_literal(interp, "");
184 const char * const envp = environ[pos];
185 const char * const p = strchr(envp, '=');
186 return Parrot_str_new(interp, envp, (UINTVAL)(p - envp));
192 =item C<STRING *get_pmc_keyed(PMC *key)>
194 Returns a String PMC for the environment variable C<*key>.
200 VTABLE PMC *get_pmc_keyed(PMC *key) {
201 STRING *keyname = VTABLE_get_string(INTERP, key);
204 STRING *retval = NULL;
207 if (!STRING_IS_EMPTY(keyname)) {
208 val = Parrot_getenv(INTERP, keyname);
211 retval = Parrot_str_new(INTERP, val, 0);
216 retval = string_from_literal(INTERP, "");
218 return_pmc = Parrot_pmc_new(INTERP, enum_class_String);
220 VTABLE_set_string_native(INTERP, return_pmc, retval);
226 =item C<void set_string_keyed(PMC *key, STRING *value)>
228 Sets the environment variable C<*key> to C<*value>.
234 VTABLE void set_string_keyed(PMC *key, STRING *value) {
235 STRING *keyname = VTABLE_get_string(INTERP, key);
237 if (keyname && value)
238 Parrot_setenv(INTERP, keyname, value);
243 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
245 Sets the environment variable C<*key> to C<*value>.
251 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
252 STRING * keyname = VTABLE_get_string(INTERP, key);
253 STRING * str_value = VTABLE_get_string(INTERP, value);
255 if (keyname && str_value)
256 Parrot_setenv(INTERP, keyname, str_value);
261 =item C<INTVAL exists_keyed(PMC *key)>
263 Returns whether the environment variable for C<*key> exists.
269 VTABLE INTVAL exists_keyed(PMC *pmckey) {
270 STRING *keyname = VTABLE_get_string(INTERP, pmckey);
272 if (!STRING_IS_EMPTY(keyname)) {
273 char * const val = Parrot_getenv(interp, keyname);
285 =item C<void delete_keyed(PMC *key)>
287 Deletes the the environment variable for C<*key>.
293 VTABLE void delete_keyed(PMC *key) {
294 STRING *keyname = VTABLE_get_string(INTERP, key);
296 if (!STRING_IS_EMPTY(keyname)) {
297 char * const val = Parrot_getenv(INTERP, keyname);
300 Parrot_unsetenv(INTERP, keyname);
313 L<http://docs.parrot.org/parrot/latest/html/docs/pdds/pdd17_pmc.pod.html#Hash_types>
315 Environment in Perl 6 - L<http://dev.perl.org/perl6/rfc/318.html>
317 Module for Perl 5 - L<http://search.cpan.org/~stas/Env-C-0.06/>
325 * c-file-style: "parrot"
327 * vim: expandtab shiftwidth=4: