line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Collectd::Config; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
3820
|
use Test::Collectd::Config::Parse; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
196
|
|
4
|
6
|
|
|
6
|
|
2960
|
use Parse::Lex; |
|
6
|
|
|
|
|
97248
|
|
|
6
|
|
|
|
|
193
|
|
5
|
6
|
|
|
6
|
|
3542
|
use IO::File; |
|
6
|
|
|
|
|
21300
|
|
|
6
|
|
|
|
|
628
|
|
6
|
6
|
|
|
6
|
|
32
|
use strict; |
|
6
|
|
|
|
|
6
|
|
|
6
|
|
|
|
|
109
|
|
7
|
6
|
|
|
6
|
|
23
|
use warnings; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
278
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Test::Collectd::Config - Reimplementation of L in perl. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.1001 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSOS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Test::Collectd::Config; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $config = parse ( "/etc/collectd.conf" ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This module reimplements the config parser of collectd in perl. It's being used by L. The only exported function is L. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
require Exporter; |
28
|
6
|
|
|
6
|
|
23
|
use vars qw($VERSION @ISA @EXPORT_OK @EXPORT $BFALSE $IGNORE_mlWHITE_SPACE $CLOSEBRAC $IGNORE_COL $NUMBER $EOL $IGNORE_CONT_QUOTED_STRING $SLASH $ERROR $IGNORE_WHITE_SPACE $OPENBRAC $BTRUE $IGNORE_COMMENT $IGNORE_START_QUOTED_STRING $QUOTED_STRING $DUPE_0_QUOTED_STRING $DUPE_0_UNQUOTED_STRING $UNQUOTED_STRING); |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
4913
|
|
29
|
|
|
|
|
|
|
push @ISA, qw(Exporter); |
30
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
31
|
|
|
|
|
|
|
parse |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
@EXPORT = qw( |
34
|
|
|
|
|
|
|
parse |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$VERSION = "0.1001"; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $EOL = '\r?\n'; |
40
|
|
|
|
|
|
|
my $IP_BYTE = '(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])'; |
41
|
|
|
|
|
|
|
my $PORT = '(6(5(5(3[0-5]|[0-2][0-9])|[0-4][0-9][0-9])|[0-4][0-9][0-9][0-9])|[1-5][0-9][0-9][0-9][0-9]|[1-9][0-9]?[0-9]?[0-9]?)'; |
42
|
|
|
|
|
|
|
my $IPV4_ADDR = "$IP_BYTE\\.$IP_BYTE\\.$IP_BYTE\\.$IP_BYTE(:$PORT)?"; |
43
|
|
|
|
|
|
|
my $HEX_NUMBER = '0[xX][0-9a-fA-F]+'; |
44
|
|
|
|
|
|
|
my $OCT_NUMBER = '0[0-7]+'; |
45
|
|
|
|
|
|
|
my $DEC_NUMBER = '[\+\-]?[0-9]+'; |
46
|
|
|
|
|
|
|
my $FLOAT_NUMBER = '[\+\-]?[0-9]*\.[0-9]+([eE][\+\-][0-9]+)?'; |
47
|
|
|
|
|
|
|
my $NUMBER = "($FLOAT_NUMBER|$HEX_NUMBER|$OCT_NUMBER|$DEC_NUMBER)"; |
48
|
|
|
|
|
|
|
my $QUOTED_STRING = '([^\\"]+|\\.)*'; |
49
|
|
|
|
|
|
|
#$QUOTED_STRING = '((?
|
50
|
|
|
|
|
|
|
my $UNQUOTED_STRING = '[0-9A-Za-z_]+'; |
51
|
|
|
|
|
|
|
my $WHITE_SPACE = '[\ \t\b]'; |
52
|
|
|
|
|
|
|
my $NON_WHITE_SPACE = '[^\ \t\b]'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $ml_buffer; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my @lex = ( |
57
|
|
|
|
|
|
|
qw(IGNORE_WHITE_SPACE), $WHITE_SPACE, |
58
|
|
|
|
|
|
|
qw(IGNORE_COMMENT), '#.*', |
59
|
|
|
|
|
|
|
qw(IGNORE_COL), "\\$EOL", |
60
|
|
|
|
|
|
|
qw(EOL), $EOL, |
61
|
|
|
|
|
|
|
qw(SLASH /), |
62
|
|
|
|
|
|
|
qw(OPENBRAC <), |
63
|
|
|
|
|
|
|
qw(CLOSEBRAC >), |
64
|
|
|
|
|
|
|
qw(BTRUE), '(true|yes|on)$', sub { 1 }, |
65
|
|
|
|
|
|
|
qw(BFALSE), '(false|no|off)$', sub { 0 }, |
66
|
|
|
|
|
|
|
qw(UNQUOTED_STRING), ${IPV4_ADDR}, |
67
|
|
|
|
|
|
|
qw(NUMBER), $NUMBER, |
68
|
|
|
|
|
|
|
qw(QUOTED_STRING), "\"$QUOTED_STRING\"", |
69
|
|
|
|
|
|
|
qw(DUPE_0_UNQUOTED_STRING), ${UNQUOTED_STRING}, |
70
|
|
|
|
|
|
|
qw(IGNORE_START_QUOTED_STRING), "\"$QUOTED_STRING\\$EOL", \&_start_string, |
71
|
|
|
|
|
|
|
qw(ML:IGNORE_mlWHITE_SPACE), "^${WHITE_SPACE}+", |
72
|
|
|
|
|
|
|
qw(ML:IGNORE_CONT_QUOTED_STRING), "${NON_WHITE_SPACE}${QUOTED_STRING}\\${EOL}", \&_cont_string, |
73
|
|
|
|
|
|
|
qw(ML:DUPE_0_QUOTED_STRING), "${NON_WHITE_SPACE}${QUOTED_STRING}\"", \&_end_string, |
74
|
|
|
|
|
|
|
qw(ERROR .*), \&_error, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _error { |
78
|
0
|
|
|
0
|
|
0
|
die qq!can\'t analyze: "$_[1]"!; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
sub _start_string { |
81
|
1
|
|
|
1
|
|
63
|
$ml_buffer = ""; |
82
|
1
|
|
|
|
|
5
|
($ml_buffer.= $_[1]) =~ s/\\\r?\n$//; |
83
|
1
|
|
|
|
|
4
|
$_[0] -> lexer -> start ("ML"); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
sub _cont_string { |
86
|
1
|
|
|
1
|
|
3
|
($ml_buffer.= $_[1]) =~ s/\\$//; |
87
|
1
|
|
|
|
|
2
|
return @_; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
sub _end_string { |
90
|
1
|
|
|
1
|
|
58
|
_cont_string (@_); |
91
|
1
|
|
|
|
|
2
|
$_[0] -> lexer -> start ("INITIAL"); |
92
|
1
|
|
|
|
|
25
|
return $ml_buffer; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Parse::Lex->exclusive('ML'); |
96
|
|
|
|
|
|
|
my $lexer = Parse::Lex->new(@lex); |
97
|
|
|
|
|
|
|
#$lexer -> trace; |
98
|
|
|
|
|
|
|
$lexer -> skip(''); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _lex { |
101
|
258
|
|
|
258
|
|
403
|
while (my $token = $lexer -> next) { |
102
|
553
|
100
|
|
|
|
23653
|
return ('', undef) if $lexer->eoi; |
103
|
546
|
100
|
|
|
|
1752
|
next if $token -> name =~ /^IGNORE_/; |
104
|
251
|
|
|
|
|
713
|
(my $name = $token->name) =~ s/^DUPE_[^_]+_//; |
105
|
251
|
|
|
|
|
704
|
return ($name, $token->text); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
0
|
|
0
|
sub _parse_error {die "parser failed ", join ", ", map { "$_: ".${$_[0] -> {$_}}} qw(TOKEN VALUE)} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 parse ( $config_in ) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Parses $config_in and returns the compiled configuration in the form of a nested structure identical to the one returned to the plugin's config callback. The |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub parse { |
118
|
7
|
|
50
|
7
|
1
|
3903
|
my $in = shift || die "usage: __PACKAGE__->parse(\$config_in)"; |
119
|
7
|
|
|
|
|
10
|
my $config; |
120
|
7
|
50
|
33
|
|
|
29
|
if (ref $in && $in->isa("GLOB")) { |
|
|
50
|
|
|
|
|
|
121
|
0
|
|
|
|
|
0
|
$config = $in; |
122
|
|
|
|
|
|
|
} elsif (!ref $in) { |
123
|
7
|
50
|
|
|
|
51
|
my $fh = IO::File -> new ( $in, 'r' ) or die $!; |
124
|
7
|
|
|
|
|
681
|
local $/; |
125
|
7
|
|
|
|
|
15
|
$config = $fh; |
126
|
|
|
|
|
|
|
} else { |
127
|
0
|
|
|
|
|
0
|
die 'parse($config_in) must be either a string (filename) or a GLOB (handle).'; |
128
|
|
|
|
|
|
|
} |
129
|
7
|
|
|
|
|
50
|
$lexer->from($config); |
130
|
7
|
|
|
|
|
23784
|
my $parser = Test::Collectd::Config::Parse -> new; |
131
|
7
|
|
|
|
|
34
|
my $value = $parser -> YYParse(yylex => \&_lex, yyerror => \&_parse_error, yydebug => 0x00); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|