fix codetest failure - trailing whitespace
[parrot.git] / src / pmc / env.pmc
blob5f49c9a8fbb308870281781b8e0ee4319866a284
1 /*
2 Copyright (C) 2001-2008, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/env.pmc - System Environment
9 =head1 DESCRIPTION
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
14 environment I<keys>.
16 =head2 Methods
18 =over 4
20 =cut
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.
29 #ifndef WIN32
30 #  ifdef __APPLE_CC__
31 #    include <crt_externs.h>
32 #    define environ (*_NSGetEnviron())
33 #  else /* !__APPLE_CC__ */
34 extern char **environ;
35 #  endif /* __APPLE_CC__ */
36 #endif /* !WIN32 */
38 static PMC *Env_PMC;
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>.
50 =cut
53     void class_init() {
54         Env_PMC = NULL;
55     }
57     VTABLE void *get_pointer() {
58         return Env_PMC;
59     }
61     VTABLE void set_pointer(void *ptr) {
62         Env_PMC = (PMC *)ptr;
63     }
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.
72 =cut
76     VTABLE PMC *get_iter() {
77         return Parrot_pmc_new_init(INTERP, enum_class_ArrayIterator, SELF);
78     }
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.
87 =cut
91     VTABLE INTVAL elements() {
92         INTVAL rv = 0;
94         while (environ[rv] != NULL)
95             rv++;
97         return rv;
98     }
102 =item C<INTVAL get_bool()>
104 Returns whether the environment has any elements.
106 =cut
109     VTABLE INTVAL get_bool() {
110         return SELF.elements() ? 1 : 0;
111     }
115 =item C<INTVAL get_integer()>
117 Returns the size of the hash.
119 =cut
123     VTABLE INTVAL get_integer() {
124         return SELF.elements();
125     }
129 =item C<FLOATVAL get_number()>
131 Returns the size of the hash.
133 =cut
136     VTABLE FLOATVAL get_number() {
137         return SELF.elements();
138     }
142 =item C<STRING *get_string_keyed(PMC *key)>
144 Returns the Parrot string value for the environment variable C<*key>.
146 =cut
150     VTABLE STRING *get_string_keyed_str(STRING *key) {
151         if (!STRING_IS_EMPTY(key)) {
152             char * const val = Parrot_getenv(interp, key);
154             if (val) {
155                 STRING * const retval = Parrot_str_new(interp, val, 0);
156                 return retval;
157             }
158         }
160         return string_from_literal(interp, "");
161     }
163     VTABLE STRING *get_string_keyed(PMC *key) {
164         return SELF.get_string_keyed_str(VTABLE_get_string(INTERP, key));
165     }
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.
175 =cut
179     VTABLE STRING *get_string_keyed_int(INTVAL pos) {
180         if (pos < 0 || pos >= SELF.elements()) {
181             return string_from_literal(interp, "");
182         }
183         else {
184             const char * const envp = environ[pos];
185             const char * const p    = strchr(envp, '=');
186             return Parrot_str_new(interp, envp, (UINTVAL)(p - envp));
187         }
188     }
192 =item C<STRING *get_pmc_keyed(PMC *key)>
194 Returns a String PMC for the environment variable C<*key>.
196 =cut
200     VTABLE PMC *get_pmc_keyed(PMC *key) {
201         STRING *keyname = VTABLE_get_string(INTERP, key);
203         char   *val     = NULL;
204         STRING *retval  = NULL;
205         PMC    *return_pmc;
207         if (!STRING_IS_EMPTY(keyname)) {
208             val         = Parrot_getenv(INTERP, keyname);
210             if (val) {
211                 retval = Parrot_str_new(INTERP, val, 0);
212             }
213         }
215         if (!retval)
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);
221         return return_pmc;
222     }
226 =item C<void set_string_keyed(PMC *key, STRING *value)>
228 Sets the environment variable C<*key> to C<*value>.
230 =cut
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);
239     }
243 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
245 Sets the environment variable C<*key> to C<*value>.
247 =cut
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);
257     }
261 =item C<INTVAL exists_keyed(PMC *key)>
263 Returns whether the environment variable for C<*key> exists.
265 =cut
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);
275             if (val) {
276                 return 1;
277             }
278         }
280         return 0;
281     }
285 =item C<void delete_keyed(PMC *key)>
287 Deletes the the environment variable for C<*key>.
289 =cut
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);
299             if (val) {
300                 Parrot_unsetenv(INTERP, keyname);
301             }
302         }
303     }
308 =back
310 =head1 SEE ALS0
312 PDD -
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/>
319 =cut
324  * Local variables:
325  *   c-file-style: "parrot"
326  * End:
327  * vim: expandtab shiftwidth=4:
328  */