aboutsummaryrefslogtreecommitdiff
path: root/lib/Config/Parser.pm
blob: fed010068f0c264a213e4191cf306093380898a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package Config::Parser;
use strict;
use warnings;
use parent 'Config::AST';
use Carp;
use Cwd qw(abs_path);
use Text::ParseWords;
use Class::Inspector;

our $VERSION = "1.00";

sub new {
    my $class = shift;
    local %_ = @_;
    my $loaded = 0;

    my @parseargs;
    if (my $filename = delete $_{filename}) {
	push @parseargs, $filename;
	foreach my $k (qw(fh line)) {
	    if (my $v = delete $_{$k}) {
		push @parseargs, ($k, $v);
	    }
	}
    }
    
    unless ($_{lexicon}) {
	my $subs = Class::Inspector->subclasses(__PACKAGE__);
	if ($subs) {
	    $_{lexicon} = {};
	    foreach my $c (@$subs) {
#		print "LOADING FROM $c\n";
		if (my $s = loadsynt($c)) {
		    $_{lexicon} = { %{$_{lexicon}}, %$s };
		    $loaded++;
		}
		last if $c eq $class;
	    }
	    delete $_{lexicon} unless keys %{$_{lexicon}};
	}
    }

    my $self = $class->SUPER::new(%_);
    $self->init if $loaded;
    if (@parseargs) {
	$self->parse(@parseargs);
	$self->commit or croak "configuration failed";
    }
    
    return $self;    
}

sub init {
    my $self = shift;
    $self->{_inited} = 1;
}

sub inited { shift->{_inited} };

sub commit {
    my $self = shift;
    my $res = $self->SUPER::commit;
    if ($res && $self->inited && $self->can('mangle')) {
	$self->mangle;
    }
    return $res;
}

sub findsynt {
    my $class = shift;
    my $file = $class;
    $file =~ s{::}{/}g;
    $file .= '.pm';
    $file = abs_path($INC{$file})
	or croak "can't find module file for $class";
    local ($/, *FILE);
    open FILE, $file or croak "Can't open $file";
    my ($text, $data) = split /(?m)^__DATA__$/, <FILE>, 2;
    close FILE;

    return () unless $data;
    return ($file, 1+($text =~ tr/\n//), $data);
}

sub loadsynt {
    my ($class) = @_;
    if (my ($file, $line, $data) = findsynt($class)) {
	open(my $fh, '<', \$data)
	    or croak "can't open filehandle for data string";
	my $d = $class->new(filename => $file,
			    fh => $fh,
			    line => $line,
			    lexicon => { '*' => '*' })
	    or croak "Failed to parse template at $file:$line";
	close $fh;
	$d->as_hash(sub {
	    my ($what, $name, $val) = @_;
	    $name = '*' if $name eq 'ANY';
	    if ($what eq 'section') {
		$val->{section} = {};
		if ($name =~ s/:mandatory$//) {
		    $val->{mandatory} = 1;
		}
		($name, $val->{section});
	    } else {
		my @words = parse_line('\s+', 0, $val);
		my $ret = {};
		$val = shift @words;
		    
		if ($val eq 'STRING') {
		    # nothing
		} elsif ($val eq 'NUMBER') {
		    $ret->{re} = '\d+';
		} elsif ($val eq 'OCTAL') {
		    $ret->{re} = '[0-7]+';
		} elsif ($val eq 'HEX') {
		    $ret->{re} = '([0-9][A-Fa-f])+';
		} else {
		    unshift @words, $val;
		}

		while (($val = $words[0])
		       && $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/) {
		    $ret->{$+{kw}} = $+{val} // 1;
		    shift @words;
		}
		if (@words) {
		    if ($ret->{array}) {
			$ret->{default} = [@words];
		    } else {
			$ret->{default} = join(' ', @words);
		    }
		}
		($name, $ret);
	    }
	})->{section};
    }
}

1;

    

Return to:

Send suggestions and report system problems to the System administrator.