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 my %attribute_types = (
16 "REQUIRED_SUBSYSTEMS" => "list",
17 "OUTPUT_TYPE" => "string",
18 "INIT_OBJ_FILES" => "list",
19 "ADD_OBJ_FILES" => "list",
20 "OBJ_FILES" => "list",
21 "SUBSYSTEM" => "string",
22 "INIT_FUNCTION" => "string",
23 "MAJOR_VERSION" => "string",
24 "MINOR_VERSION" => "string",
25 "RELEASE_VERSION" => "string",
27 "TARGET_CFLAGS" => "list",
31 ###########################################################
32 # The parsing function which parses the file
34 # $result = _parse_config_mk($filename)
36 # $filename - the path of the config.mk file
37 # which should be parsed
39 # $result - the resulting structure
41 # $result->{ERROR_CODE} - the error_code, '0' means success
42 # $result->{ERROR_STR} - the error string
44 # $result->{$key}{KEY} - the key == the variable which was parsed
45 # $result->{$key}{VAL} - the value of the variable
46 sub _parse_config_mk($)
52 my $section = "GLOBAL";
55 $result->{ERROR_CODE} = -1;
57 open(CONFIG_MK, "< $filename") || die ("Can't open $filename\n");
66 # lines beginnig with '#' are ignored
68 if ($line =~ /^\#.*$/) {
75 if (($waiting == 0) && ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/)) {
81 # 1.) lines with an alphanumeric character indicate
83 # 2.) followed by zero or more whitespaces or tabs
84 # 3.) then one '=' character
85 # 4.) followed by the value of the variable
86 # 5.) a newline ('\n') can be escaped by a '\' before the newline
87 # and the next line needs to start with a tab ('\t')
89 if (($waiting == 0) && ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/)) {
94 # when we have a '\' before the newline
95 # then skip it and wait for the next line.
97 if ($val =~ /(.*)(\\)$/) {
104 $result->{$section}{$key}{KEY} = $key;
105 $result->{$section}{$key}{VAL} = $val;
110 # when we are waiting for a value to continue then
111 # check if it has a leading tab.
113 if (($waiting == 1) && ($line =~ /^\t(.*)$/)) {
117 # when we have a '\' before the newline
118 # then skip it and wait for the next line.
120 if ($val =~ /(.*)( \\)$/) {
127 $result->{$section}{$key}{VAL} .= " ";
128 $result->{$section}{$key}{VAL} .= $val;
133 # catch empty lines they're ignored
134 # and we're no longer waiting for the value to continue
143 $result->{ERROR_STR} = "Bad line while parsing $filename\n$filename:$linenum: $line";
150 $result->{ERROR_CODE} = 0;
158 my $filename = shift;
160 my $result = _parse_config_mk($filename);
162 die ($result->{ERROR_STR}) unless $result->{ERROR_CODE} == 0;
164 foreach my $section (keys %{$result}) {
165 next if ($section eq "ERROR_CODE");
166 my ($type, $name) = split(/::/, $section, 2);
168 $input->{$name}{NAME} = $name;
169 $input->{$name}{TYPE} = $type;
171 foreach my $key (values %{$result->{$section}}) {
172 $key->{VAL} = input::strtrim($key->{VAL});
173 my $vartype = $attribute_types{$key->{KEY}};
174 if (not defined($vartype)) {
175 die("Unknown attribute $key->{KEY}");
177 if ($vartype eq "string") {
178 $input->{$name}{$key->{KEY}} = $key->{VAL};
179 } elsif ($vartype eq "list") {
180 $input->{$name}{$key->{KEY}} = [input::str2array($key->{VAL})];
181 } elsif ($vartype eq "bool") {
182 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
183 die("Invalid value for bool attribute $key->{KEY}: $key->{VAL}");
185 $input->{$name}{$key->{KEY}} = $key->{VAL};