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 = (
15 "NOPROTO" => "string",
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",
29 ###########################################################
30 # The parsing function which parses the file
32 # $result = _parse_config_mk($filename)
34 # $filename - the path of the config.mk file
35 # which should be parsed
37 # $result - the resulting structure
39 # $result->{ERROR_CODE} - the error_code, '0' means success
40 # $result->{ERROR_STR} - the error string
42 # $result->{$key}{KEY} - the key == the variable which was parsed
43 # $result->{$key}{VAL} - the value of the variable
44 sub _parse_config_mk($)
50 my $section = "GLOBAL";
53 $result->{ERROR_CODE} = -1;
55 open(CONFIG_MK, "< $filename") || die ("Can't open $filename\n");
64 # lines beginnig with '#' are ignored
66 if ($line =~ /^\#.*$/) {
73 if (($waiting == 0) && ($line =~ /^\[([a-zA-Z0-9_:]+)\][\t ]*$/)) {
79 # 1.) lines with an alphanumeric character indicate
81 # 2.) followed by zero or more whitespaces or tabs
82 # 3.) then one '=' character
83 # 4.) followed by the value of the variable
84 # 5.) a newline ('\n') can be escaped by a '\' before the newline
85 # and the next line needs to start with a tab ('\t')
87 if (($waiting == 0) && ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/)) {
92 # when we have a '\' before the newline
93 # then skip it and wait for the next line.
95 if ($val =~ /(.*)(\\)$/) {
102 $result->{$section}{$key}{KEY} = $key;
103 $result->{$section}{$key}{VAL} = $val;
108 # when we are waiting for a value to continue then
109 # check if it has a leading tab.
111 if (($waiting == 1) && ($line =~ /^\t(.*)$/)) {
115 # when we have a '\' before the newline
116 # then skip it and wait for the next line.
118 if ($val =~ /(.*)( \\)$/) {
125 $result->{$section}{$key}{VAL} .= " ";
126 $result->{$section}{$key}{VAL} .= $val;
131 # catch empty lines they're ignored
132 # and we're no longer waiting for the value to continue
141 $result->{ERROR_STR} = "Bad line while parsing $filename\n$filename:$linenum: $line";
148 $result->{ERROR_CODE} = 0;
156 my $filename = shift;
158 my $result = _parse_config_mk($filename);
160 die ($result->{ERROR_STR}) unless $result->{ERROR_CODE} == 0;
162 foreach my $section (keys %{$result}) {
163 next if ($section eq "ERROR_CODE");
164 my ($type, $name) = split(/::/, $section, 2);
166 $input->{$name}{NAME} = $name;
167 $input->{$name}{TYPE} = $type;
169 foreach my $key (values %{$result->{$section}}) {
170 $key->{VAL} = input::strtrim($key->{VAL});
171 my $vartype = $attribute_types{$key->{KEY}};
172 if (not defined($vartype)) {
173 die("Unknown attribute $key->{KEY}");
175 if ($vartype eq "string") {
176 $input->{$name}{$key->{KEY}} = $key->{VAL};
177 } elsif ($vartype eq "list") {
178 $input->{$name}{$key->{KEY}} = [input::str2array($key->{VAL})];
179 } elsif ($vartype eq "bool") {
180 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
181 die("Invalid value for bool attribute $key->{KEY}: $key->{VAL}");
183 $input->{$name}{$key->{KEY}} = $key->{VAL};