~ubuntu-branches/debian/experimental/smokeping/experimental

« back to all changes in this revision

Viewing changes to doc/ParseConfig.pm.html

  • Committer: Bazaar Package Importer
  • Author(s): Jose Carlos Garcia Sogo
  • Date: 2002-04-02 15:02:08 UTC
  • Revision ID: james.westby@ubuntu.com-20020402150208-8eycqntc07q6gig5
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<HTML>
 
2
<HEAD>
 
3
<TITLE>ParseConfig.pm</TITLE>
 
4
<LINK REV="made" HREF="mailto:karrer@iis.ee.ethz.ch">
 
5
</HEAD>
 
6
 
 
7
<BODY>
 
8
 
 
9
<A NAME="__index__"></A>
 
10
<!-- INDEX BEGIN -->
 
11
<!--
 
12
 
 
13
<UL>
 
14
 
 
15
        <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
 
16
        <LI><A HREF="#description">DESCRIPTION</A></LI>
 
17
        <UL>
 
18
 
 
19
                <LI><A HREF="#grammar definition">Grammar Definition</A></LI>
 
20
                <UL>
 
21
 
 
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>
 
26
                </UL>
 
27
 
 
28
                <LI><A HREF="#configuration syntax">Configuration Syntax</A></LI>
 
29
                <UL>
 
30
 
 
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>
 
35
                </UL>
 
36
 
 
37
                <LI><A HREF="#example">Example</A></LI>
 
38
                <UL>
 
39
 
 
40
                        <LI><A HREF="#code">Code</A></LI>
 
41
                        <LI><A HREF="#configuration">Configuration</A></LI>
 
42
                        <LI><A HREF="#result">Result</A></LI>
 
43
                </UL>
 
44
 
 
45
        </UL>
 
46
 
 
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>
 
51
</UL>
 
52
-->
 
53
<!-- INDEX END -->
 
54
 
 
55
<P>
 
56
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
 
57
<PRE>
 
58
 use ISG::ParseConfig;</PRE>
 
59
<PRE>
 
60
 my $parser = ISG::ParseConfig-&gt;new(\%grammar);
 
61
 my $cfg = $parser-&gt;parse('app.cfg') or die &quot;ERROR: $parser-&gt;{err}\n&quot;;
 
62
 my $pod = $parser-&gt;makepod;</PRE>
 
63
<P>
 
64
<HR>
 
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>
 
74
<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>
 
83
<P>
 
84
<H3><A NAME="special section keys">Special Section Keys</A></H3>
 
85
<DL>
 
86
<DT><STRONG><A NAME="item__sections">_sections</A></STRONG><BR>
 
87
<DD>
 
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
 
90
sub-section.
 
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>
 
94
<P></P>
 
95
<DT><STRONG><A NAME="item__vars">_vars</A></STRONG><BR>
 
96
<DD>
 
97
Array containing the list of variables (assignments) in this section.
 
98
Analogous to sections, regular expressions can be used.
 
99
<P></P>
 
100
<DT><STRONG><A NAME="item__mandatory">_mandatory</A></STRONG><BR>
 
101
<DD>
 
102
Array containing the list of mandatory sections and variables.
 
103
<P></P>
 
104
<DT><STRONG><A NAME="item__table">_table</A></STRONG><BR>
 
105
<DD>
 
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.
 
109
<P></P>
 
110
<DT><STRONG><A NAME="item__text">_text</A></STRONG><BR>
 
111
<DD>
 
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
 
114
string.
 
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>
 
118
<P></P>
 
119
<DT><STRONG><A NAME="item__order">_order</A></STRONG><BR>
 
120
<DD>
 
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
 
123
defined.
 
124
<P></P>
 
125
<DT><STRONG><A NAME="item__doc">_doc</A></STRONG><BR>
 
126
<DD>
 
127
Describes what this section is about
 
128
<P></P></DL>
 
129
<P>
 
130
<H3><A NAME="special variable keys">Special Variable Keys</A></H3>
 
131
<DL>
 
132
<DT><STRONG><A NAME="item__re">_re</A></STRONG><BR>
 
133
<DD>
 
134
Regular expression upon which the value will be checked.
 
135
<P></P>
 
136
<DT><STRONG><A NAME="item__re_error">_re_error</A></STRONG><BR>
 
137
<DD>
 
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).
 
140
<P></P>
 
141
<DT><STRONG><A NAME="item__sub">_sub</A></STRONG><BR>
 
142
<DD>
 
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
 
146
string as content.
 
147
<P></P>
 
148
<DT><STRONG>_doc</STRONG><BR>
 
149
<DD>
 
150
Describtion of the variable.
 
151
<P></P></DL>
 
152
<P>
 
153
<H3><A NAME="special table keys">Special Table Keys</A></H3>
 
154
<DL>
 
155
<DT><STRONG><A NAME="item__columns">_columns</A></STRONG><BR>
 
156
<DD>
 
157
Number of columns. If not specified, it will not be enforced.
 
158
<P></P>
 
159
<DT><STRONG><A NAME="item__key">_key</A></STRONG><BR>
 
160
<DD>
 
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
 
164
stored as arrays.
 
165
<P></P>
 
166
<DT><STRONG>_sub</STRONG><BR>
 
167
<DD>
 
168
they work analog to the description in the previous section.
 
169
<P></P>
 
170
<DT><STRONG>_doc</STRONG><BR>
 
171
<DD>
 
172
describes the content of the column.
 
173
<P></P></DL>
 
174
<P>
 
175
<H3><A NAME="special text keys">Special Text Keys</A></H3>
 
176
<DL>
 
177
<DT><STRONG>_re</STRONG><BR>
 
178
<DD>
 
179
Regular expression upon which the text will be checked (everything as a single
 
180
line).
 
181
<P></P>
 
182
<DT><STRONG>_re_error</STRONG><BR>
 
183
<DD>
 
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).
 
186
<P></P>
 
187
<DT><STRONG>_sub</STRONG><BR>
 
188
<DD>
 
189
they work analog to the description in the previous section.
 
190
<P></P>
 
191
<DT><STRONG>_doc</STRONG><BR>
 
192
<DD>
 
193
Ditto.
 
194
<P></P></DL>
 
195
<P>
 
196
<H2><A NAME="configuration syntax">Configuration Syntax</A></H2>
 
197
<P>
 
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>&quot;</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>
 
209
<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>
 
213
<DL>
 
214
<DT><STRONG><A NAME="item_Level_1">Level 1</A></STRONG><BR>
 
215
<DD>
 
216
*** section name ***
 
217
<P></P>
 
218
<DT><STRONG><A NAME="item_Level_2">Level 2</A></STRONG><BR>
 
219
<DD>
 
220
+ section name
 
221
<P></P>
 
222
<DT><STRONG><A NAME="item_Level_3">Level 3</A></STRONG><BR>
 
223
<DD>
 
224
<P></P>
 
225
<DT><STRONG><A NAME="item_Level_n%2C_n%3E1">Level n, n&gt;1</A></STRONG><BR>
 
226
<DD>
 
227
+..+ section name (number of '+' determines level)
 
228
<P></P></DL>
 
229
<P>
 
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>
 
234
<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>
 
237
<P>
 
238
<H2><A NAME="example">Example</A></H2>
 
239
<P>
 
240
<H3><A NAME="code">Code</A></H3>
 
241
<PRE>
 
242
 my $parser = ISG::ParseConfig-&gt;new({
 
243
   _sections =&gt; [ 'network', 'hosts' ],
 
244
   network =&gt; {
 
245
      _vars     =&gt; [ 'dns' ],
 
246
      _sections =&gt; [ &quot;/$RE_IP/&quot; ],
 
247
      dns       =&gt; {
 
248
         _doc =&gt; &quot;address of the dns server&quot;,
 
249
         _re =&gt; $RE_HOST,
 
250
         _re_error =&gt;
 
251
            'dns must be an host name or ip address',
 
252
         },
 
253
      &quot;/$RE_IP/&quot; =&gt; {
 
254
         _doc    =&gt; &quot;Ip Adress&quot;,
 
255
         _vars   =&gt; [ 'dns', 'netmask', 'gateway' ],
 
256
         dns     =&gt; {
 
257
            _doc =&gt; &quot;address of the dns server&quot;,
 
258
            _re =&gt; $RE_HOST,
 
259
            _re_error =&gt;
 
260
               'dns must be an host name or ip address'
 
261
            },
 
262
         netmask =&gt; {
 
263
            _doc =&gt; &quot;Netmask&quot;,
 
264
            _re =&gt; $RE_IP,
 
265
            _re_error =&gt;
 
266
               'netmask must be a dotted ip address'
 
267
            },
 
268
         gateway =&gt; {
 
269
            _doc =&gt; &quot;Default Gateway address in IP notation&quot;,
 
270
            _re =&gt; $RE_IP,
 
271
            _re_error =&gt;
 
272
               'gateway must be a dotted ip address' },
 
273
         },
 
274
      },
 
275
   hosts =&gt; {
 
276
      _doc =&gt; &quot;Details about the hosts&quot;,
 
277
      _table  =&gt; {
 
278
          _doc =&gt; &quot;Description of all the Hosts&quot;,
 
279
         _key =&gt; 0,
 
280
         _columns =&gt; 3,
 
281
         0 =&gt; {
 
282
            _doc =&gt; &quot;Ethernet Address&quot;,
 
283
            _re =&gt; $RE_MAC,
 
284
            _re_error =&gt;
 
285
               'first column must be an ethernet mac address',
 
286
            },
 
287
         1 =&gt; {
 
288
            _doc =&gt; &quot;IP Address&quot;,
 
289
            _re =&gt; $RE_IP,
 
290
            _re_error =&gt;
 
291
               'second column must be a dotted ip address',
 
292
            },
 
293
         },
 
294
      },
 
295
   });</PRE>
 
296
<PRE>
 
297
 my $cfg = $parser-&gt;parse('test.cfg') or
 
298
   die &quot;ERROR: $parser-&gt;{err}\n&quot;;
 
299
 print Dumper($cfg);
 
300
 print $praser-&gt;makepod;</PRE>
 
301
<P>
 
302
<H3><A NAME="configuration">Configuration</A></H3>
 
303
<PRE>
 
304
 *** network ***
 
305
</PRE>
 
306
<PRE>
 
307
 
 
308
   dns      = 129.132.7.87</PRE>
 
309
<PRE>
 
310
 
 
311
 + 129.132.7.64</PRE>
 
312
<PRE>
 
313
 
 
314
   netmask  = 255.255.255.192
 
315
   gateway  = 129.132.7.65</PRE>
 
316
<PRE>
 
317
 
 
318
 *** hosts ***</PRE>
 
319
<PRE>
 
320
 
 
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>
 
324
<P>
 
325
<H3><A NAME="result">Result</A></H3>
 
326
<PRE>
 
327
 {
 
328
   'hosts' =&gt; {
 
329
                '00:50:fe:bc:65:11' =&gt; [
 
330
                                         '00:50:fe:bc:65:11',
 
331
                                         '129.132.7.97',
 
332
                                         'plain.hades'
 
333
                                       ],
 
334
                '00:50:fe:bc:65:12' =&gt; [
 
335
                                         '00:50:fe:bc:65:12',
 
336
                                         '129.132.7.98',
 
337
                                         'isg.ee.hades'
 
338
                                       ],
 
339
                '00:50:fe:bc:65:14' =&gt; [
 
340
                                         '00:50:fe:bc:65:14',
 
341
                                         '129.132.7.99',
 
342
                                         'isg.ee.hades'
 
343
                                       ]
 
344
              },
 
345
   'network' =&gt; {
 
346
                  '129.132.7.64' =&gt; {
 
347
                                      'netmask' =&gt; '255.255.255.192',
 
348
                                      'gateway' =&gt; '129.132.7.65'
 
349
                                    },
 
350
                  'dns' =&gt; '129.132.7.87'
 
351
                }
 
352
 };</PRE>
 
353
<P>
 
354
<HR>
 
355
<H1><A NAME="copyright">COPYRIGHT</A></H1>
 
356
<P>Copyright (c) 2000, 2001 by ETH Zurich. All rights reserved.</P>
 
357
<P>
 
358
<HR>
 
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>
 
371
<P>
 
372
<HR>
 
373
<H1><A NAME="author">AUTHOR</A></H1>
 
374
<P>David&nbsp;Schweikert&nbsp;&lt;<A&nbsp;HREF="mailto:dws@ee.ethz.ch">dws@ee.ethz.ch</A>&gt;
 
375
Tobias&nbsp;Oetiker&nbsp;&lt;<A&nbsp;HREF="mailto:oetiker@ee.ethz.ch">oetiker@ee.ethz.ch</A>&gt;</P>
 
376
<P>
 
377
<HR>
 
378
<H1><A NAME="history">HISTORY</A></H1>
 
379
<PRE>
 
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>
 
387
 
 
388
</BODY>
 
389
 
 
390
</HTML>