Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_environment_variables_object.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Implementation of afw_object interface for environment variables.
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw.h"
16 #include "afw_object_impl.h"
17 
18 
19 /* Declares and rti/inf defines for interface afw_object */
20 #define AFW_IMPLEMENTATION_ID "fcgi"
22 
23 typedef struct impl_self_s {
24  afw_object_t pub;
25  const afw_object_t *properties;
26  afw_boolean_t all_variables_loaded;
27 } impl_self_t;
28 
29 
30 /* Called first time impl_afw_object_get_next_property() is called. */
31 static void
32 impl_load_all_variables(impl_self_t *self, afw_xctx_t *xctx)
33 {
34  char ** envp;
35  const afw_utf8_octet_t *s;
36  const afw_utf8_octet_t *c;
37  afw_utf8_t property_name;
38  const afw_value_t *value;
39 
40  /* Get point to environment variables special way for Windows. */
41 #if defined(WIN) && (_MSC_VER >= 1900)
42  envp = *__p__environ();
43 #else
44  extern char ** environ;
45  envp = environ;
46 #endif
47 
48  /* Loop adding missing variables. */
49  for (; *envp != 0; envp++) {
50  property_name.s = *envp;
51  for (s = c = *envp; *c && *c != '='; c++);
52  property_name.len = c - s;
53 
54  if (!afw_object_has_property(self->properties, &property_name, xctx)) {
55  if (!*c) {
56  value = afw_value_empty_string;
57  }
58  else {
60  self->pub.p, xctx);
61  }
62  afw_object_set_property(self->properties,
63  afw_utf8_clone(&property_name, self->pub.p, xctx),
64  value, xctx);
65  }
66  }
67 
68  /* Indicate loaded. */
69  self->all_variables_loaded = true;
70 
71 }
72 
73 
74 
75 AFW_DEFINE(const afw_object_t *)
77  afw_boolean_t preload_variables,
78  afw_xctx_t *xctx)
79 {
80  impl_self_t *self;
81  const afw_pool_t *p;
82 
83  static const afw_utf8_t impl_path =
84  AFW_UTF8_LITERAL("/afw/_AdaptiveEnvironmentVariables_/current");
85 
86  /* Allocate memory for self and initialize. */
87  p = afw_pool_create(xctx->p, xctx);
88  self = afw_pool_calloc_type(p, impl_self_t, xctx);
89  self->pub.inf = &impl_afw_object_inf;
90  self->pub.p = p;
91  self->pub.meta.id = &afw_s_current;
92  self->pub.meta.object_type_uri = &afw_s__AdaptiveEnvironmentVariables_;
93  self->pub.meta.object_uri = &impl_path;
94 
95  /* Create request properties object. */
96  self->properties = afw_object_create_and_cede_p(p, xctx);
97 
98  if (preload_variables) {
99  impl_load_all_variables(self, xctx);
100  }
101 
102  return (const afw_object_t *)self;
103 }
104 
105 
106 
107 /*
108  * Implementation of method release of interface afw_object.
109  */
110 void
111 impl_afw_object_release(
112  const afw_object_t * instance,
113  afw_xctx_t *xctx)
114 {
115  impl_self_t *self = (impl_self_t *)instance;
116 
117  afw_object_release(self->properties, xctx);
118 }
119 
120 
121 
122 /*
123  * Implementation of method add_reference of interface afw_object.
124  */
125 void
126 impl_afw_object_add_reference (
127  const afw_object_t * instance,
128  afw_xctx_t *xctx)
129 {
130  impl_self_t *self = (impl_self_t *)instance;
131 
132  afw_object_add_reference(self->properties, xctx);
133 }
134 
135 /*
136  * Implementation of method get_count for interface afw_object.
137  */
140  const afw_object_t * instance,
141  afw_xctx_t * xctx)
142 {
143  impl_self_t *self = (impl_self_t *)instance;
144 
145  /* If all variable not loaded yet, load them. */
146  if (!self->all_variables_loaded) {
147  impl_load_all_variables(self, xctx);
148  }
149 
150  return afw_object_get_count(self->properties, xctx);
151 }
152 
153 /*
154  * Implementation of method get_meta for interface afw_object.
155  */
156 const afw_value_t *
157 impl_afw_object_get_meta(
158  const afw_object_t *instance,
159  const afw_pool_t *p,
160  afw_xctx_t *xctx)
161 {
163  instance, p, xctx);
164 }
165 
166 
167 
168 /*
169  * Implementation of method get_property of interface afw_object.
170  */
171 const afw_value_t *
172 impl_afw_object_get_property(
173  const afw_object_t * instance,
174  const afw_utf8_t * property_name,
175  afw_xctx_t *xctx)
176 {
177  impl_self_t *self = (impl_self_t *)instance;
178  const void *s;
179  const afw_value_t *value;
180  const afw_utf8_z_t *property_name_z;
181 
182  /* Look for property in memory first. */
183  value = afw_object_get_property(self->properties, property_name,
184  xctx);
185  if (value) {
186  return value;
187  }
188 
189  /* If not in memory, try FCGX_GetParam(). */
190  value = NULL;
191  property_name_z = afw_utf8_z_create(property_name->s, property_name->len,
192  xctx->p, xctx);
193  s = getenv(property_name_z);
194  if (s) {
195  value = afw_value_make_string_copy(s, AFW_UTF8_Z_LEN, xctx->p, xctx);
196  }
197 
198  return value;
199 }
200 
201 
202 
203 /*
204  * Implementation of method get_property_meta for interface afw_object.
205  */
206 const afw_value_t *
207 impl_afw_object_get_property_meta(
208  const afw_object_t *instance,
209  const afw_utf8_t *property_name,
210  const afw_pool_t *p,
211  afw_xctx_t *xctx)
212 {
214  instance, property_name, p, xctx);
215 }
216 
217 
218 
219 /*
220  * Implementation of method get_next_property of interface afw_object.
221  */
222 const afw_value_t *
223 impl_afw_object_get_next_property(
224  const afw_object_t * instance,
225  const afw_iterator_t * * iterator,
226  const afw_utf8_t * * property_name,
227  afw_xctx_t *xctx)
228 {
229  impl_self_t *self = (impl_self_t *)instance;
230 
231  /* If all variable not loaded yet, load them. */
232  if (!self->all_variables_loaded) {
233  impl_load_all_variables(self, xctx);
234  }
235 
236  /* Call method of properties object. */
237  return afw_object_get_next_property(self->properties,
238  iterator, property_name, xctx);
239 }
240 
241 
242 
243 /*
244  * Implementation of method get_next_property_meta for interface afw_object.
245  */
246 const afw_value_t *
247 impl_afw_object_get_next_property_meta(
248  const afw_object_t *instance,
249  const afw_iterator_t **iterator,
250  const afw_utf8_t **property_name,
251  const afw_pool_t *p,
252  afw_xctx_t *xctx)
253 {
255  instance, iterator, property_name, p, xctx);
256 }
257 
258 
259 
260 /*
261  * Implementation of method has_property of interface afw_object.
262  */
264 impl_afw_object_has_property(
265  const afw_object_t * instance,
266  const afw_utf8_t * property_name,
267  afw_xctx_t *xctx)
268 {
269  const afw_value_t *value;
270 
271  /* Call impl_get_own_property and throw away value. */
272  value = impl_afw_object_get_property(
273  instance, property_name, xctx);
274 
275  return (value != NULL);
276 }
277 
278 
279 
280 /*
281  * Implementation of method get_setter of interface afw_object.
282  */
283 const afw_object_setter_t *
284 impl_afw_object_get_setter (
285  const afw_object_t * instance,
286  afw_xctx_t *xctx)
287 {
288  /* Is readonly. */
289  return NULL;
290 }
Adaptive Framework Core API.
AFW_DEFINE(const afw_object_t *)
Helpers for object implementation development.
Interface afw_interface implementation declares.
#define AFW_UTF8_Z_LEN
String is NUL (0) terminate.
Definition: afw_common.h:266
#define AFW_UTF8_LITERAL(A_STRING)
String literal initializer.
Definition: afw_common.h:582
struct afw_iterator_s afw_iterator_t
_Bool afw_boolean_t
Definition: afw_common.h:373
afw_utf8_octet_t afw_utf8_z_t
NFC normalized UTF-8 null terminated string.
Definition: afw_common.h:523
char afw_utf8_octet_t
8 bits of utf-8 codepoint.
Definition: afw_common.h:236
apr_size_t afw_size_t
size_t.
Definition: afw_common.h:151
afw_environment_create_environment_variables_object(afw_boolean_t preload_variables, afw_xctx_t *xctx)
Create a readonly object for accessing environment variables.
afw_size_t impl_afw_object_get_count(const afw_object_t *instance, afw_xctx_t *xctx)
const afw_value_t * afw_object_impl_get_property_meta(const afw_object_t *instance, const afw_utf8_t *property_name, const afw_pool_t *p, afw_xctx_t *xctx)
A general impl of method get_property_meta for interface afw_object that can be accessed externally.
const afw_value_t * afw_object_impl_get_meta(const afw_object_t *instance, const afw_pool_t *p, afw_xctx_t *xctx)
A general impl of method get_meta for interface afw_object that can be accessed externally.
const afw_value_t * afw_object_impl_get_next_property_meta(const afw_object_t *instance, const afw_iterator_t **iterator, const afw_utf8_t **property_name, const afw_pool_t *p, afw_xctx_t *xctx)
A general impl of method get_next_property_meta for interface afw_object that can be accessed externa...
#define afw_object_get_property(instance, property_name, xctx)
Call method get_property of interface afw_object.
#define afw_object_add_reference(instance, xctx)
Call method add_reference of interface afw_object.
#define afw_object_get_next_property(instance, iterator, property_name, xctx)
Call method get_next_property of interface afw_object.
#define afw_object_has_property(instance, property_name, xctx)
Call method has_property of interface afw_object.
#define afw_object_release(instance, xctx)
Call method release of interface afw_object.
#define afw_object_get_count(instance, xctx)
Call method get_count of interface afw_object.
afw_object_set_property(const afw_object_t *instance, const afw_utf8_t *property_name, const afw_value_t *value, afw_xctx_t *xctx)
Set the value of an object's property.
Definition: afw_object.c:46
#define afw_object_create_and_cede_p(p, xctx)
Create an empty entity object in memory in specified pool and cede control of the pool to the object.
Definition: afw_object.h:898
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
const afw_pool_t * afw_pool_create(const afw_pool_t *parent, afw_xctx_t *xctx)
Create a new pool.
afw_utf8_z_create(const afw_utf8_octet_t *s, afw_size_t len, const afw_pool_t *p, afw_xctx_t *xctx)
Create a NFC Normalized zero terminated UTF-8 string in specified pool.
Definition: afw_utf8.c:366
const afw_utf8_t * afw_utf8_clone(const afw_utf8_t *string, const afw_pool_t *p, afw_xctx_t *xctx)
Clone a utf-8 string into a specific pool.
Definition: afw_utf8.h:347
afw_value_empty_string
Adaptive value empty string.
Definition: afw_value.h:342
afw_value_make_string_copy(const afw_utf8_octet_t *s, afw_size_t len, const afw_pool_t *p, afw_xctx_t *xctx)
Definition: afw_value.c:496
afw_value_create_string_from_u8z(const afw_utf8_z_t *string_z, const afw_pool_t *p, afw_xctx_t *xctx)
Definition: afw_value.c:519
Interface afw_object public struct.
Interface afw_object_setter public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
Interface afw_xctx public struct.