1 ###########################################################
2 ### SMB Build System ###
3 ### - config.mk parsing functions ###
5 ### Copyright (C) Stefan (metze) Metzmacher 2004 ###
6 ### Released under the GNU GPL ###
7 ###########################################################
14 ###########################################################
15 # The parsing function which parses the file
17 # $result = _parse_config_mk($filename)
19 # $filename - the path of the config.mk file
20 # which should be parsed
22 # $result - the resulting structure
24 # $result->{ERROR_CODE} - the error_code, '0' means success
25 # $result->{ERROR_STR} - the error string
27 # $result->{$key}{KEY} - the key == the variable which was parsed
28 # $result->{$key}{VAL} - the value of the variable
29 sub _parse_config_mk($)
35 my $section = "GLOBAL";
38 $result->{ERROR_CODE} = -1;
40 open(CONFIG_MK, "< $filename") || die ("Can't open $filename\n");
49 # lines beginnig with '#' are ignored
51 if ($line =~ /^\#.*$/) {
58 if (($waiting == 0) && ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/)) {
64 # 1.) lines with an alphanumeric character indicate
66 # 2.) followed by zero or more whitespaces or tabs
67 # 3.) then one '=' character
68 # 4.) followed by the value of the variable
69 # 5.) a newline ('\n') can be escaped by a '\' before the newline
70 # and the next line needs to start with a tab ('\t')
72 if (($waiting == 0) && ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/)) {
77 # when we have a '\' before the newline
78 # then skip it and wait for the next line.
80 if ($val =~ /(.*)(\\)$/) {
87 $result->{$section}{$key}{KEY} = $key;
88 $result->{$section}{$key}{VAL} = $val;
93 # when we are waiting for a value to continue then
94 # check if it has a leading tab.
96 if (($waiting == 1) && ($line =~ /^\t(.*)$/)) {
100 # when we have a '\' before the newline
101 # then skip it and wait for the next line.
103 if ($val =~ /(.*)( \\)$/) {
110 $result->{$section}{$key}{VAL} .= " ";
111 $result->{$section}{$key}{VAL} .= $val;
116 # catch empty lines they're ignored
117 # and we're no longer waiting for the value to continue
126 $result->{ERROR_STR} = "Bad line while parsing $filename\n$filename:$linenum: $line";
133 $result->{ERROR_CODE} = 0;
138 ###########################################################
139 # A caching function to avoid to parse
140 # a file twice or more
142 # $result = _get_parse_results($filename)
144 # $filename - the path of the config.mk file
145 # which should be parsed
147 # $result - the resulting structure
149 # $result->{ERROR_CODE} - the error_code, '0' means success
150 # $result->{ERROR_STR} - the error string
152 # $result->{$key}{KEY} - the key == the variable which was parsed
153 # $result->{$key}{VAL} - the value of the variable
154 my $_get_parse_results_cache;
155 sub _get_parse_results($)
157 my $filename = shift;
159 if ((!defined($_get_parse_results_cache->{$filename}{ERROR_CODE}))
160 ||($_get_parse_results_cache->{$filename}{ERROR_CODE} != 0)) {
161 $_get_parse_results_cache->{$filename} = _parse_config_mk($filename);
164 return $_get_parse_results_cache->{$filename};
167 ###########################################################
168 # The fetching function to fetch the value of a variable
171 # $value = _fetch_var_from_config_mk($filename,$section,$variable)
173 # $filename - the path of the config.mk file
174 # which should be parsed
176 # $section - the section name of the variable
178 # $variable - the variable name of which we want the value
180 # $value - the value of the variable
181 sub _fetch_var_from_config_mk($$$)
183 my $filename = shift;
189 $result = _get_parse_results($filename);
191 if ($result->{ERROR_CODE} != 0) {
192 die ($result->{ERROR_STR});
195 if (defined($result->{$section}{$key})) {
196 $val = input::strtrim($result->{$section}{$key}{VAL});
197 } elsif (defined($result->{DEFAULT}{$key})) {
198 $val = input::strtrim($result->{DEFAULT}{$key}{VAL});
204 ###########################################################
205 # The fetching function to fetch the array of values of a variable
208 # $array = _fetch_array_from_config_mk($filename,$section,$variable)
210 # $filename - the path of the config.mk file
211 # which should be parsed
213 # $section - the section name of the variable
215 # $variable - the variable name of which we want the value
217 # $array - the array of values of the variable
218 sub _fetch_array_from_config_mk($$$)
220 my $filename = shift;
226 $result = _get_parse_results($filename);
228 if ($result->{ERROR_CODE} != 0) {
229 die ($result->{ERROR_STR});
232 if (defined($result->{$section}{$key})) {
233 $val = $result->{$section}{$key}{VAL};
234 } elsif (defined($result->{DEFAULT}{$key})) {
235 $val = $result->{DEFAULT}{$key}{VAL};
238 return input::str2array($val);
241 ###########################################################
242 # A function for fetching MODULE_<module>_<parameter>
243 # variables out of a config.mk file
245 # $value = module_get_var($filename,$module,$parameter)
247 # $filename - the path of the config.mk file
248 # which should be parsed
250 # $module - the middle part of the variable name of which we want the value
252 # $parameter - the last part of the variable name of which we want the value
254 # $value - the value of the variable
255 sub module_get_var($$$)
257 my $filename = shift;
261 my $section = "MODULE::".$module;
263 return _fetch_var_from_config_mk($filename,$section,$var);
266 ###########################################################
267 # A function for fetching MODULE_<module>_<parameter>
268 # variables out of a config.mk file
270 # $array = module_get_array($filename,$module,$parameter)
272 # $filename - the path of the config.mk file
273 # which should be parsed
275 # $module - the middle part of the variable name of which we want the value
277 # $parameter - the last part of the variable name of which we want the value
279 # $array - the array of values of the variable
280 sub module_get_array($$$)
282 my $filename = shift;
286 my $section = "MODULE::".$module;
288 return _fetch_array_from_config_mk($filename,$section,$var);
291 ###########################################################
292 # A function for fetching SUBSYSTEM_<subsystem>_<parameter>
293 # variables out of a config.mk file
295 # $value = subsystem_get_var($filename,$subsystem,$parameter)
297 # $filename - the path of the config.mk file
298 # which should be parsed
300 # $subsystem - the middle part of the variable name of which we want the value
302 # $parameter - the last part of the variable name of which we want the value
304 # $value - the value of the variable
305 sub subsystem_get_var($$$)
307 my $filename = shift;
308 my $subsystem = shift;
311 my $section = "SUBSYSTEM::".$subsystem;
313 return _fetch_var_from_config_mk($filename,$section,$var);
316 ###########################################################
317 # A function for fetching SUBSYSTEM_<subsystem>_<parameter>
318 # variables out of a config.mk file
320 # $array = subsystem_get_array($filename,$subsystem,$parameter)
322 # $filename - the path of the config.mk file
323 # which should be parsed
325 # $subsystem - the middle part of the variable name of which we want the value
327 # $parameter - the last part of the variable name of which we want the value
329 # $array - the array of values of the variable
330 sub subsystem_get_array($$$)
332 my $filename = shift;
333 my $subsystem = shift;
336 my $section = "SUBSYSTEM::".$subsystem;
338 return _fetch_array_from_config_mk($filename,$section,$var);
341 ###########################################################
342 # A function for fetching LIBRARY_<library>_<parameter>
343 # variables out of a config.mk file
345 # $value = library_get_var($filename,$library,$parameter)
347 # $filename - the path of the config.mk file
348 # which should be parsed
350 # $library - the middle part of the variable name of which we want the value
352 # $parameter - the last part of the variable name of which we want the value
354 # $value - the value of the variable
355 sub library_get_var($$$)
357 my $filename = shift;
361 my $section = "LIBRARY::".$library;
363 return _fetch_var_from_config_mk($filename,$section,$var);
366 ###########################################################
367 # A function for fetching LIBRARY_<library>_<parameter>
368 # variables out of a config.mk file
370 # $array = library_get_array($filename,$library,$parameter)
372 # $filename - the path of the config.mk file
373 # which should be parsed
375 # $library - the middle part of the variable name of which we want the value
377 # $parameter - the last part of the variable name of which we want the value
379 # $array - the array of values of the variable
380 sub library_get_array($$$)
382 my $filename = shift;
386 my $section = "LIBRARY::".$library;
388 return _fetch_array_from_config_mk($filename,$section,$var);
391 ###########################################################
392 # A function for fetching BINARY_<binary>_<parameter>
393 # variables out of a config.mk file
395 # $value = binary_get_var($filename,$binary,$parameter)
397 # $filename - the path of the config.mk file
398 # which should be parsed
400 # $binary - the middle part of the variable name of which we want the value
402 # $parameter - the last part of the variable name of which we want the value
404 # $value - the value of the variable
405 sub binary_get_var($$$)
407 my $filename = shift;
411 my $section = "BINARY::".$binary;
413 return _fetch_var_from_config_mk($filename,$section,$var);
416 ###########################################################
417 # A function for fetching BINARY_<binary>_<parameter>
418 # variables out of a config.mk file
420 # $array = binary_get_array($filename,$binary,$parameter)
422 # $filename - the path of the config.mk file
423 # which should be parsed
425 # $binary - the middle part of the variable name of which we want the value
427 # $parameter - the last part of the variable name of which we want the value
429 # $array - the array of values of the variable
430 sub binary_get_array($$$)
432 my $filename = shift;
436 my $section = "BINARY::".$binary;
438 return _fetch_array_from_config_mk($filename,$section,$var);
444 my $filename = shift;
446 my $result = _parse_config_mk($filename);
448 die ($result->{ERROR_STR}) unless $result->{ERROR_CODE} == 0;
450 foreach my $section (keys %{$result}) {
451 next if ($section eq "ERROR_CODE");
452 my ($type, $name) = split(/::/, $section, 2);
454 $input->{$name}{NAME} = $name;
455 $input->{$name}{TYPE} = $type;
457 foreach my $key (values %{$result->{$section}}) {
458 $input->{$name}{$key->{KEY}} = [input::str2array($key->{VAL})];