diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2018-01-20 11:10:57 +0100 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2018-01-20 17:22:40 +0100 |
commit | e406653af5ae596d2d6f212f6ebaf66bcfe5dbf1 (patch) | |
tree | d02955cecd38c297b70feaad168cad91c748a4f7 /lib/Config | |
download | config-parser-e406653af5ae596d2d6f212f6ebaf66bcfe5dbf1.tar.gz config-parser-e406653af5ae596d2d6f212f6ebaf66bcfe5dbf1.tar.bz2 |
Initial commit
Diffstat (limited to 'lib/Config')
-rw-r--r-- | lib/Config/Parser.pm | 116 | ||||
-rw-r--r-- | lib/Config/Parser/Ini.pm | 96 |
2 files changed, 212 insertions, 0 deletions
diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm new file mode 100644 index 0000000..c0a2179 --- /dev/null +++ b/lib/Config/Parser.pm | |||
@@ -0,0 +1,116 @@ | |||
1 | package Config::Parser; | ||
2 | use strict; | ||
3 | use warnings; | ||
4 | use parent 'Config::Tree'; | ||
5 | use Carp; | ||
6 | use Cwd qw(abs_path); | ||
7 | use Text::ParseWords; | ||
8 | use Class::Inspector; | ||
9 | |||
10 | our $VERSION = "1.00"; | ||
11 | |||
12 | sub new { | ||
13 | my $class = shift; | ||
14 | local %_ = @_; | ||
15 | |||
16 | my @parseargs; | ||
17 | if (my $filename = delete $_{filename}) { | ||
18 | push @parseargs, $filename; | ||
19 | foreach my $k (qw(fh line)) { | ||
20 | if (my $v = delete $_{$k}) { | ||
21 | push @parseargs, ($k, $v); | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | unless ($_{parameters}) { | ||
27 | my $subs = Class::Inspector->subclasses(__PACKAGE__); | ||
28 | if ($subs) { | ||
29 | $_{parameters} = {}; | ||
30 | foreach my $c (@$subs) { | ||
31 | # print "LOADING FROM $c\n"; | ||
32 | if (my $s = loadsynt($c)) { | ||
33 | $_{parameters} = { %{$_{parameters}}, %$s }; | ||
34 | } | ||
35 | last if $c eq $class; | ||
36 | } | ||
37 | delete $_{parameters} unless keys %{$_{parameters}}; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | my $self = $class->SUPER::new(%_); | ||
42 | |||
43 | if (@parseargs) { | ||
44 | $self->parse(@parseargs); | ||
45 | $self->commit or croak "configuration failed"; | ||
46 | } | ||
47 | |||
48 | return $self; | ||
49 | } | ||
50 | |||
51 | sub findsynt { | ||
52 | my $class = shift; | ||
53 | my $file = $class; | ||
54 | $file =~ s{::}{/}g; | ||
55 | $file .= '.pm'; | ||
56 | $file = abs_path($INC{$file}) | ||
57 | or croak "can't find module file for $class"; | ||
58 | local ($/, *FILE); | ||
59 | open FILE, $file or croak "Can't open $file"; | ||
60 | my ($text, $data) = split /(?m)^__DATA__$/, <FILE>, 2; | ||
61 | close FILE; | ||
62 | |||
63 | return () unless $data; | ||
64 | return ($file, 1+($text =~ tr/\n//), $data); | ||
65 | } | ||
66 | |||
67 | sub loadsynt { | ||
68 | my ($class) = @_; | ||
69 | if (my ($file, $line, $data) = findsynt($class)) { | ||
70 | open(my $fh, '<', \$data); | ||
71 | my $d = $class->new(filename => $file, | ||
72 | fh => $fh, | ||
73 | line => $line, | ||
74 | parameters => { '*' => '*' }) | ||
75 | or croak "Failed to parse template at $file:$line"; | ||
76 | close $fh; | ||
77 | $d->as_hash(sub { | ||
78 | my ($what, $name, $val) = @_; | ||
79 | $name = '*' if $name eq 'ANY'; | ||
80 | if ($what eq 'section') { | ||
81 | $val->{section} = {}; | ||
82 | if ($name =~ s/:mandatory$//) { | ||
83 | $val->{mandatory} = 1; | ||
84 | } | ||
85 | ($name, $val->{section}); | ||
86 | } else { | ||
87 | my @words = parse_line('\s+', 0, $val); | ||
88 | my $ret = {}; | ||
89 | $val = shift @words; | ||
90 | |||
91 | if ($val eq 'STRING') { | ||
92 | # nothing | ||
93 | } elsif ($val eq 'NUMBER') { | ||
94 | $ret->{re} = '\d+'; | ||
95 | } elsif ($val eq 'OCTAL') { | ||
96 | $ret->{re} = '[0-7]+'; | ||
97 | } elsif ($val eq 'HEX') { | ||
98 | $ret->{re} = '([0-9][A-Fa-f])+'; | ||
99 | } else { | ||
100 | unshift @words, $val; | ||
101 | } | ||
102 | |||
103 | while (($val = shift @words) | ||
104 | && $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/) { | ||
105 | $ret->{$+{kw}} = $+{val} // 1; | ||
106 | } | ||
107 | $ret->{default} = $val if $val; | ||
108 | ($name, $ret); | ||
109 | } | ||
110 | })->{section}; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | 1; | ||
115 | |||
116 | |||
diff --git a/lib/Config/Parser/Ini.pm b/lib/Config/Parser/Ini.pm new file mode 100644 index 0000000..b3a1ef9 --- /dev/null +++ b/lib/Config/Parser/Ini.pm | |||
@@ -0,0 +1,96 @@ | |||
1 | package Config::Parser::Ini; | ||
2 | use strict; | ||
3 | use warnings; | ||
4 | use parent 'Config::Parser'; | ||
5 | use Carp; | ||
6 | use Text::ParseWords; | ||
7 | |||
8 | sub parse { | ||
9 | my $self = shift; | ||
10 | $self->{_filename} = shift // confess "No filename given"; | ||
11 | local %_ = @_; | ||
12 | $self->debug(1, "parsing $self->{_filename}"); | ||
13 | $self->_readconfig($self->{_filename}, %_); | ||
14 | return $self; | ||
15 | } | ||
16 | |||
17 | sub filename { shift->{_filename} } | ||
18 | |||
19 | # _readconfig(FILE) | ||
20 | sub _readconfig { | ||
21 | my $self = shift; | ||
22 | my $file = shift; | ||
23 | local %_ = @_; | ||
24 | my $fh = delete $_{fh}; | ||
25 | my $need_close; | ||
26 | |||
27 | $self->debug(1, "reading file $file"); | ||
28 | unless ($fh) { | ||
29 | open($fh, "<", $file) | ||
30 | or do { | ||
31 | $self->error("can't open configuration file $file: $!"); | ||
32 | $self->{_error_count}++; | ||
33 | return 0; | ||
34 | }; | ||
35 | $need_close = 1; | ||
36 | } | ||
37 | |||
38 | my $line = delete $_{line} // 0; | ||
39 | my @path; | ||
40 | my $include; | ||
41 | |||
42 | while (<$fh>) { | ||
43 | ++$line; | ||
44 | chomp; | ||
45 | if (/\\$/) { | ||
46 | chop; | ||
47 | $_ .= <$fh>; | ||
48 | redo; | ||
49 | } | ||
50 | |||
51 | s/^\s+//; | ||
52 | s/\s+$//; | ||
53 | s/#.*//; | ||
54 | next if ($_ eq ""); | ||
55 | |||
56 | my $locus = new Config::Tree::Locus($file, $line); | ||
57 | |||
58 | if (/^\[(.+?)\]$/) { | ||
59 | @path = parse_line('\s+', 0, $1); | ||
60 | if (@path == 1 && $path[0] eq 'include') { | ||
61 | $include = 1; | ||
62 | } else { | ||
63 | $include = 0; | ||
64 | $self->add_node(\@path, | ||
65 | new Config::Tree::Node::Section(locus => $locus)); | ||
66 | } | ||
67 | } elsif (/([\w_-]+)\s*=\s*(.*)/) { | ||
68 | my ($k, $v) = ($1, $2); | ||
69 | $k = lc($k) if $self->{_ci}; #FIXME:private member | ||
70 | |||
71 | if ($include) { | ||
72 | if ($k eq 'path') { | ||
73 | $self->_readconfig($v); | ||
74 | } elsif ($k eq 'pathopt') { | ||
75 | $self->_readconfig($v) if -f $v; | ||
76 | } elsif ($k eq 'glob') { | ||
77 | foreach my $file (bsd_glob($v, 0)) { | ||
78 | $self->_readconfig($file); | ||
79 | } | ||
80 | } else { | ||
81 | $self->error("keyword \"$k\" is unknown", locus => $locus); | ||
82 | $self->{_error_count}++; | ||
83 | } | ||
84 | } else { | ||
85 | $self->add_value([@path, $k], $v, $locus); | ||
86 | } | ||
87 | } else { | ||
88 | $self->error("malformed line", locus => $locus); | ||
89 | $self->{_error_count}++; | ||
90 | } | ||
91 | } | ||
92 | close $fh if $need_close; | ||
93 | } | ||
94 | |||
95 | 1; | ||
96 | |||