3
<TITLE>ParseConfig.pm</TITLE>
4
<LINK REV="made" HREF="mailto:karrer@iis.ee.ethz.ch">
9
<A NAME="__index__"></A>
15
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
16
<LI><A HREF="#description">DESCRIPTION</A></LI>
19
<LI><A HREF="#grammar definition">Grammar Definition</A></LI>
22
<LI><A HREF="#special section keys">Special Section Keys</A></LI>
23
<LI><A HREF="#special variable keys">Special Variable Keys</A></LI>
24
<LI><A HREF="#special table keys">Special Table Keys</A></LI>
25
<LI><A HREF="#special text keys">Special Text Keys</A></LI>
28
<LI><A HREF="#configuration syntax">Configuration Syntax</A></LI>
31
<LI><A HREF="#general syntax">General Syntax</A></LI>
32
<LI><A HREF="#sections">Sections</A></LI>
33
<LI><A HREF="#assignments">Assignments</A></LI>
34
<LI><A HREF="#tabular data">Tabular Data</A></LI>
37
<LI><A HREF="#example">Example</A></LI>
40
<LI><A HREF="#code">Code</A></LI>
41
<LI><A HREF="#configuration">Configuration</A></LI>
42
<LI><A HREF="#result">Result</A></LI>
47
<LI><A HREF="#copyright">COPYRIGHT</A></LI>
48
<LI><A HREF="#license">LICENSE</A></LI>
49
<LI><A HREF="#author">AUTHOR</A></LI>
50
<LI><A HREF="#history">HISTORY</A></LI>
56
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
58
use ISG::ParseConfig;</PRE>
60
my $parser = ISG::ParseConfig->new(\%grammar);
61
my $cfg = $parser->parse('app.cfg') or die "ERROR: $parser->{err}\n";
62
my $pod = $parser->makepod;</PRE>
65
<H1><A NAME="description">DESCRIPTION</A></H1>
66
<P>ISG::ParseConfig is a module to parse configuration files. The
67
configuration may consist of multiple-level sections with assignments
68
and tabular data. The parsed data will be returned as a hash
69
containing the whole configuration. ISG::ParseConfig uses a grammar
70
that is supplied upon creation of a ISG::ParseConfig object to parse
71
the configuration file and return helpful error messages in case of
72
syntax errors. Using the <STRONG>makepod</STRONG> methode you can generate
73
documentation of the configuration file format.</P>
75
<H2><A NAME="grammar definition">Grammar Definition</A></H2>
76
<P>The grammar is a multiple-level hash of hashes, which follows the structure of
77
the configuration. Each section or variable is represented by a hash with the
78
same structure. Each hash contains special keys starting with an underscore
79
such as '_sections', '_vars', '_sub' or '_re' to denote meta data with information
80
about that section or variable. Other keys are used to structure the hash
81
according to the same nesting structure of the configuration itself. The
82
starting hash given as parameter to 'new' contains the ``root section''.</P>
84
<H3><A NAME="special section keys">Special Section Keys</A></H3>
86
<DT><STRONG><A NAME="item__sections">_sections</A></STRONG><BR>
88
Array containing the list of sub-sections of this section. Each sub-section
89
must then be represented by a sub-hash in this hash with the same name of the
91
<P>The sub-section can also be a regular expression denoted by the syntax '/re/',
92
where re is the regular-expression. In case a regular expression is used, a
93
sub-hash named with the same '/re/' must be included in this hash.</P>
95
<DT><STRONG><A NAME="item__vars">_vars</A></STRONG><BR>
97
Array containing the list of variables (assignments) in this section.
98
Analogous to sections, regular expressions can be used.
100
<DT><STRONG><A NAME="item__mandatory">_mandatory</A></STRONG><BR>
102
Array containing the list of mandatory sections and variables.
104
<DT><STRONG><A NAME="item__table">_table</A></STRONG><BR>
106
Hash containing the table grammar (see Special Table Keys). If not specified,
107
no table is allowed in this section. The grammar of the columns if specified
108
by sub-hashes named with the column number.
110
<DT><STRONG><A NAME="item__text">_text</A></STRONG><BR>
112
Section contains free-form text. Only sections and @includes statements will
113
be interpreted, the rest will be added in the returned hash under '_text' as
115
<P><STRONG>_text</STRONG> is a hash reference which can contain a <STRONG>_re</STRONG> and a <STRONG>_re_error</STRONG> key
116
which will be used to scrutanize the text ... if the hash is empty, all text
117
will be accepted.</P>
119
<DT><STRONG><A NAME="item__order">_order</A></STRONG><BR>
121
If defined, a '_order' element will be put in every hash containing the
122
sections with a number that determines the order in which the sections were
125
<DT><STRONG><A NAME="item__doc">_doc</A></STRONG><BR>
127
Describes what this section is about
130
<H3><A NAME="special variable keys">Special Variable Keys</A></H3>
132
<DT><STRONG><A NAME="item__re">_re</A></STRONG><BR>
134
Regular expression upon which the value will be checked.
136
<DT><STRONG><A NAME="item__re_error">_re_error</A></STRONG><BR>
138
String containing the returned error in case the regular expression doesn't
139
match (if not specified, a generic 'syntax error' message will be returned).
141
<DT><STRONG><A NAME="item__sub">_sub</A></STRONG><BR>
143
A function pointer. It called for every value, with the value passed as its
144
first argument. If the function returns a defined value it is assumed that
145
the test was not successful and an error is generated with the returned
148
<DT><STRONG>_doc</STRONG><BR>
150
Describtion of the variable.
153
<H3><A NAME="special table keys">Special Table Keys</A></H3>
155
<DT><STRONG><A NAME="item__columns">_columns</A></STRONG><BR>
157
Number of columns. If not specified, it will not be enforced.
159
<DT><STRONG><A NAME="item__key">_key</A></STRONG><BR>
161
If defined, the specified column number will be used as key in a hash in the
162
returned hash. If not defined, the returned hash will contain a '_table'
163
element with the contents of the table as array. The rows of the tables are
166
<DT><STRONG>_sub</STRONG><BR>
168
they work analog to the description in the previous section.
170
<DT><STRONG>_doc</STRONG><BR>
172
describes the content of the column.
175
<H3><A NAME="special text keys">Special Text Keys</A></H3>
177
<DT><STRONG>_re</STRONG><BR>
179
Regular expression upon which the text will be checked (everything as a single
182
<DT><STRONG>_re_error</STRONG><BR>
184
String containing the returned error in case the regular expression doesn't
185
match (if not specified, a generic 'syntax error' message will be returned).
187
<DT><STRONG>_sub</STRONG><BR>
189
they work analog to the description in the previous section.
191
<DT><STRONG>_doc</STRONG><BR>
196
<H2><A NAME="configuration syntax">Configuration Syntax</A></H2>
198
<H3><A NAME="general syntax">General Syntax</A></H3>
199
<P>'#' denotes a comment up to the end-of-line, empty lines are allowed and space
200
at the beginning and end of lines is trimmed.</P>
201
<P>'\' at the end of the line marks a continued line on the next line. A single
202
space will be inserted between the concatenated lines.</P>
203
<P>'@include filename' is used to include another file.</P>
204
<P>'@define a some value' will replace all occurences of 'a' in the following text
205
with 'some value'.</P>
206
<P>Fields in tables that contain white space can be enclosed in either <CODE>'</CODE> or <CODE>"</CODE>.
207
Whitespace can also be escaped with <CODE>\</CODE>. Quotes inside quotes are allowed but must
208
be escaped with a backslash as well.</P>
210
<H3><A NAME="sections">Sections</A></H3>
211
<P>ISG::ParseConfig supports hierarchical configurations through sections, whose
212
syntax is as follows:</P>
214
<DT><STRONG><A NAME="item_Level_1">Level 1</A></STRONG><BR>
218
<DT><STRONG><A NAME="item_Level_2">Level 2</A></STRONG><BR>
222
<DT><STRONG><A NAME="item_Level_3">Level 3</A></STRONG><BR>
225
<DT><STRONG><A NAME="item_Level_n%2C_n%3E1">Level n, n>1</A></STRONG><BR>
227
+..+ section name (number of '+' determines level)
230
<H3><A NAME="assignments">Assignments</A></H3>
231
<P>Assignements take the form: 'variable = value', where value can be any string
232
(can contain whitespaces and special characters). The spaces before and after
233
the equal sign are optional.</P>
235
<H3><A NAME="tabular data">Tabular Data</A></H3>
236
<P>The data is interpreted as one or more columns separated by spaces.</P>
238
<H2><A NAME="example">Example</A></H2>
240
<H3><A NAME="code">Code</A></H3>
242
my $parser = ISG::ParseConfig->new({
243
_sections => [ 'network', 'hosts' ],
245
_vars => [ 'dns' ],
246
_sections => [ "/$RE_IP/" ],
248
_doc => "address of the dns server",
251
'dns must be an host name or ip address',
253
"/$RE_IP/" => {
254
_doc => "Ip Adress",
255
_vars => [ 'dns', 'netmask', 'gateway' ],
257
_doc => "address of the dns server",
260
'dns must be an host name or ip address'
263
_doc => "Netmask",
266
'netmask must be a dotted ip address'
269
_doc => "Default Gateway address in IP notation",
272
'gateway must be a dotted ip address' },
276
_doc => "Details about the hosts",
278
_doc => "Description of all the Hosts",
282
_doc => "Ethernet Address",
285
'first column must be an ethernet mac address',
288
_doc => "IP Address",
291
'second column must be a dotted ip address',
297
my $cfg = $parser->parse('test.cfg') or
298
die "ERROR: $parser->{err}\n";
300
print $praser->makepod;</PRE>
302
<H3><A NAME="configuration">Configuration</A></H3>
308
dns = 129.132.7.87</PRE>
314
netmask = 255.255.255.192
315
gateway = 129.132.7.65</PRE>
321
00:50:fe:bc:65:11 129.132.7.97 plain.hades
322
00:50:fe:bc:65:12 129.132.7.98 isg.ee.hades
323
00:50:fe:bc:65:14 129.132.7.99 isg.ee.hades</PRE>
325
<H3><A NAME="result">Result</A></H3>
329
'00:50:fe:bc:65:11' => [
334
'00:50:fe:bc:65:12' => [
339
'00:50:fe:bc:65:14' => [
346
'129.132.7.64' => {
347
'netmask' => '255.255.255.192',
348
'gateway' => '129.132.7.65'
350
'dns' => '129.132.7.87'
355
<H1><A NAME="copyright">COPYRIGHT</A></H1>
356
<P>Copyright (c) 2000, 2001 by ETH Zurich. All rights reserved.</P>
359
<H1><A NAME="license">LICENSE</A></H1>
360
<P>This program is free software; you can redistribute it and/or modify
361
it under the terms of the GNU General Public License as published by
362
the Free Software Foundation; either version 2 of the License, or
363
(at your option) any later version.</P>
364
<P>This program is distributed in the hope that it will be useful,
365
but WITHOUT ANY WARRANTY; without even the implied warranty of
366
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
367
GNU General Public License for more details.</P>
368
<P>You should have received a copy of the GNU General Public License
369
along with this program; if not, write to the Free Software
370
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</P>
373
<H1><A NAME="author">AUTHOR</A></H1>
374
<P>David Schweikert <<A HREF="mailto:dws@ee.ethz.ch">dws@ee.ethz.ch</A>>
375
Tobias Oetiker <<A HREF="mailto:oetiker@ee.ethz.ch">oetiker@ee.ethz.ch</A>></P>
378
<H1><A NAME="history">HISTORY</A></H1>
380
2001-05-11 ds 1.2 Initial Version for policy 0.3
381
2001-09-04 ds 1.3 Remove space before comments, more strict variable definition
382
2001-09-19 to 1.4 Added _sub error parsing and _doc self documentation
383
2001-10-20 to Improved Rendering of _doc information
384
2002-01-09 to Added Documentation to the _text section documentation
385
2002-01-28 to Fixed quote parsing in tables
386
2002-03-12 ds 1.5 Implemented @define, make makepod return a string and not an array</PRE>