]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/variable.c
2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * Copyright 2001-2004 David Abrahams.
9 * Copyright 2005 Reece H. Dunn.
10 * Copyright 2005 Rene Rivera.
11 * Distributed under the Boost Software License, Version 1.0.
12 * (See accompanying file LICENSE_1_0.txt or copy at
13 * http://www.boost.org/LICENSE_1_0.txt)
17 * variable.c - handle Jam multi-element variables.
21 * var_defines() - load a bunch of variable=value settings
22 * var_get() - get value of a user defined symbol
23 * var_set() - set a variable in jam's user defined symbol table.
24 * var_swap() - swap a variable's value with the given one
25 * var_done() - free variable tables
29 * var_enter() - make new var symbol table entry, returning var ptr
30 * var_dump() - dump a variable to stdout
49 * VARIABLE - a user defined multi-value variable
52 typedef struct _variable VARIABLE
;
60 static LIST
* * var_enter( struct module_t
*, OBJECT
* symbol
);
61 static void var_dump( OBJECT
* symbol
, LIST
* value
, char * what
);
65 * var_defines() - load a bunch of variable=value settings
67 * If preprocess is false, take the value verbatim.
69 * Otherwise, if the variable value is enclosed in quotes, strip the quotes.
70 * Otherwise, if variable name ends in PATH, split value at :'s.
71 * Otherwise, split the value at blanks.
74 void var_defines( struct module_t
* module
, char * const * e
, int preprocess
)
84 if ( ( val
= strchr( *e
, '=' ) )
86 /* On the mac (MPW), the var=val is actually var\0val */
87 /* Think different. */
88 || ( val
= *e
+ strlen( *e
) )
93 size_t const len
= strlen( val
+ 1 );
94 int const quoted
= ( val
[ 1 ] == '"' ) && ( val
[ len
] == '"' ) &&
97 if ( quoted
&& preprocess
)
99 string_append_range( buf
, val
+ 2, val
+ len
);
100 l
= list_push_back( l
, object_new( buf
->value
) );
101 string_truncate( buf
, 0 );
108 #if defined( OPT_NO_EXTERNAL_VARIABLE_SPLIT )
110 #elif defined( OS_MAC )
117 /* Split *PATH at :'s, not spaces. */
120 if ( !strncmp( val
- 4, "PATH", 4 ) ||
121 !strncmp( val
- 4, "Path", 4 ) ||
122 !strncmp( val
- 4, "path", 4 ) )
130 preprocess
&& ( ( p
= strchr( pp
, split
) ) != 0 );
134 string_append_range( buf
, pp
, p
);
135 l
= list_push_back( l
, object_new( buf
->value
) );
136 string_truncate( buf
, 0 );
139 l
= list_push_back( l
, object_new( pp
) );
143 string_append_range( buf
, *e
, val
);
145 OBJECT
* const varname
= object_new( buf
->value
);
146 var_set( module
, varname
, l
, VAR_SET
);
147 object_free( varname
);
149 string_truncate( buf
, 0 );
156 /* Last returned variable value saved so we may clear it in var_done(). */
157 static LIST
* saved_var
= L0
;
161 * var_get() - get value of a user defined symbol
163 * Returns NULL if symbol unset.
166 LIST
* var_get( struct module_t
* module
, OBJECT
* symbol
)
170 /* Some "fixed" variables... */
171 if ( object_equal( symbol
, constant_TMPDIR
) )
173 list_free( saved_var
);
174 result
= saved_var
= list_new( object_new( path_tmpdir()->value
) );
176 else if ( object_equal( symbol
, constant_TMPNAME
) )
178 list_free( saved_var
);
179 result
= saved_var
= list_new( path_tmpnam() );
181 else if ( object_equal( symbol
, constant_TMPFILE
) )
183 list_free( saved_var
);
184 result
= saved_var
= list_new( path_tmpfile() );
186 else if ( object_equal( symbol
, constant_STDOUT
) )
188 list_free( saved_var
);
189 result
= saved_var
= list_new( object_copy( constant_STDOUT
) );
191 else if ( object_equal( symbol
, constant_STDERR
) )
193 list_free( saved_var
);
194 result
= saved_var
= list_new( object_copy( constant_STDERR
) );
202 if ( ( n
= module_get_fixed_var( module
, symbol
) ) != -1 )
205 var_dump( symbol
, module
->fixed_variables
[ n
], "get" );
206 result
= module
->fixed_variables
[ n
];
208 else if ( module
->variables
&& ( v
= (VARIABLE
*)hash_find(
209 module
->variables
, symbol
) ) )
212 var_dump( v
->symbol
, v
->value
, "get" );
217 else if ( ( module
->name
&& object_equal( module
->name
, constant_ENVIRON
) )
218 || root_module() == module
)
220 /* On VMS, when a variable from root or ENVIRON module is not found,
221 * explicitly request it from the process.
222 * By design, process variables (and logicals) are not made available
223 * to C main(), and thus will not get loaded in bulk to root/ENVRON.
224 * So we get around it by getting any such variable on first request.
226 const char * val
= getenv( object_str( symbol
) );
230 struct module_t
* environ_module
= module
;
231 char * environ
[ 2 ] = { 0 }; /* NULL-terminated */
234 if ( root_module() == module
)
236 environ_module
= bindmodule( constant_ENVIRON
);
239 string_copy( buf
, object_str( symbol
) );
240 string_append( buf
, "=" );
241 string_append( buf
, val
);
243 environ
[ 0 ] = buf
->value
;
245 /* Load variable to global module, with splitting, for backward
246 * compatibility. Then to .ENVIRON, without splitting.
248 var_defines( root_module(), environ
, 1 );
249 var_defines( environ_module
, environ
, 0 );
252 if ( module
->variables
&& ( v
= (VARIABLE
*)hash_find(
253 module
->variables
, symbol
) ) )
256 var_dump( v
->symbol
, v
->value
, "get" );
267 LIST
* var_get_and_clear_raw( module_t
* module
, OBJECT
* symbol
)
272 if ( module
->variables
&& ( v
= (VARIABLE
*)hash_find( module
->variables
,
284 * var_set() - set a variable in Jam's user defined symbol table
286 * 'flag' controls the relationship between new and old values of the variable:
287 * SET replaces the old with the new; APPEND appends the new to the old; DEFAULT
288 * only uses the new if the variable was previously unset.
290 * Copies symbol. Takes ownership of value.
293 void var_set( struct module_t
* module
, OBJECT
* symbol
, LIST
* value
, int flag
296 LIST
* * v
= var_enter( module
, symbol
);
299 var_dump( symbol
, value
, "set" );
303 case VAR_SET
: /* Replace value */
308 case VAR_APPEND
: /* Append value */
309 *v
= list_append( *v
, value
);
312 case VAR_DEFAULT
: /* Set only if unset */
313 if ( list_empty( *v
) )
323 * var_swap() - swap a variable's value with the given one
326 LIST
* var_swap( struct module_t
* module
, OBJECT
* symbol
, LIST
* value
)
328 LIST
* * v
= var_enter( module
, symbol
);
329 LIST
* oldvalue
= *v
;
331 var_dump( symbol
, value
, "set" );
338 * var_enter() - make new var symbol table entry, returning var ptr
341 static LIST
* * var_enter( struct module_t
* module
, OBJECT
* symbol
)
347 if ( ( n
= module_get_fixed_var( module
, symbol
) ) != -1 )
348 return &module
->fixed_variables
[ n
];
350 if ( !module
->variables
)
351 module
->variables
= hashinit( sizeof( VARIABLE
), "variables" );
353 v
= (VARIABLE
*)hash_insert( module
->variables
, symbol
, &found
);
356 v
->symbol
= object_copy( symbol
);
365 * var_dump() - dump a variable to stdout
368 static void var_dump( OBJECT
* symbol
, LIST
* value
, char * what
)
370 out_printf( "%s %s = ", what
, object_str( symbol
) );
377 * var_done() - free variable tables
380 static void delete_var_( void * xvar
, void * data
)
382 VARIABLE
* const v
= (VARIABLE
*)xvar
;
383 object_free( v
->symbol
);
384 list_free( v
->value
);
387 void var_done( struct module_t
* module
)
389 list_free( saved_var
);
391 hashenumerate( module
->variables
, delete_var_
, 0 );
392 hash_free( module
->variables
);