#! /usr/bin/perl5 # file : current_parser.pl # # parses current.conf file # basically def file for parse function... # usage : parse_conf(config_file, hash_reference) # # Jeff Bloom # WRD Austin # jabloom@usgs.gov # # &parse_conf("./current.conf"); sub parse_conf { my($config_file) = @_; my $key, $value, $multiline_string_flag, $rval, $string_flag; # local vars my(%ENV) = %ENV; sub true {1}; sub false {0}; if (! open(CONF, $config_file)){ print STDERR "Unable to open configuration file:\n". "\t$config_file\n"; exit(255); } $em = '^#'; while (){ $store_line = $_; ++$LINE_NUMBER; chomp; # we are not in the midst of a quotation ... if (! $multiline_string_flag) { next if (/^\s*$/); next if (/$em/o); # strip out any comments that dont start lines s/^([$em]*)#.*$/$1/o; next if (/^\s*$/); $rval = ($key, $value) = split('='); # not key/value pair ... if ($rval != 2) { print STDERR "Unable to parse configuration file line number $LINE_NUMBER:\n". "$store_line\n"; exit(255); } $key =~ s/\s+$//; # strip trailing whitespace if (exists $ENV{$key}){ print STDERR "Replacing previously defined variable: $key\n"; } $value =~ s/^\s+//; # strip leading whitespace if ($value =~ /^\"/){ # " is this a string definition? $string_flag = true; $START_STRING_LINE_NUMBER = $LINE_NUMBER; if ($value =~ s/^\"([^\"]*)\"(.*)$/$1/) { #" if ($2 =~ /[^\s]+/) { print STDERR "Unrecognized text after close quote on line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $value =~ s/^\s+//; } else { # multiline string... $string_flag = true; $multiline_string_flag = true; $value =~ s/^\"//; # " strip lead quote mark... $value =~ s/\s+$//; # and trailing whitespace $value .= "\n"; # and append a newline next; } } $value =~ s/\s+$//; # remove trailing whitespace if (! $string_flag && $value =~ /\s+/){ print STDERR "spaces found in unquoted string, line number $LINE_NUMBER:\n". "$store_line\n"; exit; } while ($value =~ /\[([^\s^\[^\]]*)\]/){ if (! exists($ENV{$1})){ print STDERR "Undefined variable used in '[variable]' construct,". " line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $value =~ s/\[([^\s^\[^\]]*)\]/$ENV{$1}/; } if ($value =~ /\[|\]/){ print STDERR "Incomplete bracket pair used on line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $string_flag = false; } # if we are in the midst of a string, look for the end of the string else { if (s/([^\"]*)\"(.*)$/$1/){ # " if ($2 =~ /[^\s]+/){ print STDERR "Unrecognized text after close quote on line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $value .= $1; # Sub in variablename while ($value =~ /\[([^\s^\[^\]]*)\]/){ if (! exists($ENV{$1})){ print STDERR "Undefined variable used in '[variable]' construct,". " line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $value =~ s/\[([^\s^\[^\]]*)\]/$ENV{$1}/; } if ($value =~ /\[|\]/){ print STDERR "Incomplete bracket pair used on line $LINE_NUMBER\n". "$store_line\n"; exit(255); } $string_flag = false; $multiline_string_flag = false; } # if the end is not found, just add this segment to the value. else { $value .= "$_\n"; next; } } # Assign the variable $$key = $value; $ENV{$key} = $value; # print STDOUT "\$$key = \'$value\'\n"; } ######################################## if ($multiline_string_flag){ print STDERR "Incomplete string found in file, ". "possibly starting on line $START_STRING_LINE_NUMBER\n"; exit(255); } return true; } 1; __END__