File: Grammar.html

package info (click to toggle)
smokeping 2.3.6-5%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 3,964 kB
  • ctags: 2,378
  • sloc: perl: 14,815; sh: 523; makefile: 242
file content (523 lines) | stat: -rw-r--r-- 15,988 bytes parent folder | download | duplicates (3)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Grammar</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>

<body style="background-color: white">

<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<!--

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#grammar_definition">Grammar Definition</a></li>
		<ul>

			<li><a href="#special_section_keys">Special Section Keys</a></li>
			<li><a href="#special_variable_keys">Special Variable Keys</a></li>
			<li><a href="#special_table_keys">Special Table Keys</a></li>
			<li><a href="#special_text_keys">Special Text Keys</a></li>
		</ul>

		<li><a href="#configuration_syntax">Configuration Syntax</a></li>
		<ul>

			<li><a href="#general_syntax">General Syntax</a></li>
			<li><a href="#sections">Sections</a></li>
			<li><a href="#assignments">Assignments</a></li>
			<li><a href="#tabular_data">Tabular Data</a></li>
		</ul>

		<li><a href="#example">Example</a></li>
		<ul>

			<li><a href="#code">Code</a></li>
			<li><a href="#configuration">Configuration</a></li>
			<li><a href="#result">Result</a></li>
		</ul>

	</ul>

	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
	<li><a href="#license">LICENSE</a></li>
	<li><a href="#authors">AUTHORS</a></li>
</ul>
-->
<!-- INDEX END -->

<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Config::Grammar - A grammar-based, user-friendly config parser</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
 use Config::Grammar;</pre>
<pre>
 my $parser = Config::Grammar-&gt;new(\%grammar);
 my $cfg = $parser-&gt;parse('app.cfg') or die &quot;ERROR: $parser-&gt;{err}\n&quot;;
 my $pod = $parser-&gt;makepod();
 my $ex = $parser-&gt;maketmpl('TOP','SubNode');
 my $minex = $parser-&gt;maketmplmin('TOP','SubNode');</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Config::Grammar is a module to parse configuration files. The
configuration may consist of multiple-level sections with assignments
and tabular data. The parsed data will be returned as a hash
containing the whole configuration. Config::Grammar uses a grammar
that is supplied upon creation of a Config::Grammar object to parse
the configuration file and return helpful error messages in case of
syntax errors. Using the <strong>makepod</strong> method you can generate
documentation of the configuration file format.</p>
<p>The <strong>maketmpl</strong> method can generate a template configuration file.  If
your grammar contains regexp matches, the template will not be all
that helpful as Config::Grammar is not smart enough to give you sensible
template data based in regular expressions. The related function 
<strong>maketmplmin</strong> generates a minimal configuration template without 
examples, regexps or comments and thus allows an experienced user to
fill in the configuration data more efficiently.</p>
<p>
</p>
<h2><a name="grammar_definition">Grammar Definition</a></h2>
<p>The grammar is a multiple-level hash of hashes, which follows the structure of
the configuration. Each section or variable is represented by a hash with the
same structure.  Each hash contains special keys starting with an underscore
such as '_sections', '_vars', '_sub' or '_re' to denote meta data with information
about that section or variable. Other keys are used to structure the hash
according to the same nesting structure of the configuration itself. The
starting hash given as parameter to 'new' contains the ``root section''.</p>
<p>
</p>
<h3><a name="special_section_keys">Special Section Keys</a></h3>
<dl>
<dt><strong><a name="item__sections">_sections</a></strong>

<dd>
<p>Array containing the list of sub-sections of this section. Each sub-section
must then be represented by a sub-hash in this hash with the same name of the
sub-section.</p>

</dd><dd>
<p>The sub-section can also be a regular expression denoted by the syntax '/re/',
where re is the regular-expression. In case a regular expression is used, a
sub-hash named with the same '/re/' must be included in this hash.</p>

</li>
</dd><dt><strong><a name="item__vars">_vars</a></strong>

<dd>
<p>Array containing the list of variables (assignments) in this section.
Analogous to sections, regular expressions can be used.</p>

</li>
</dd><dt><strong><a name="item__mandatory">_mandatory</a></strong>

<dd>
<p>Array containing the list of mandatory sections and variables.</p>

</li>
</dd><dt><strong><a name="item__inherited">_inherited</a></strong>

<dd>
<p>Array containing the list of the variables that should be assigned the
same value as in the parent section if nothing is specified here.</p>

</li>
</dd><dt><strong><a name="item__table">_table</a></strong>

<dd>
<p>Hash containing the table grammar (see Special Table Keys). If not specified,
no table is allowed in this section. The grammar of the columns if specified
by sub-hashes named with the column number.</p>

</li>
</dd><dt><strong><a name="item__text">_text</a></strong>

<dd>
<p>Section contains free-form text. Only sections and @includes statements will
be interpreted, the rest will be added in the returned hash under '_text' as
string.</p>

</dd><dd>
<p><strong>_text</strong> is a hash reference which can contain a <strong>_re</strong> and a <strong>_re_error</strong> key
which will be used to scrutanize the text ... if the hash is empty, all text
will be accepted.</p>

</li>
</dd><dt><strong><a name="item__order">_order</a></strong>

<dd>
<p>If defined, a '_order' element will be put in every hash containing the
sections with a number that determines the order in which the sections were
defined.</p>

</li>
</dd><dt><strong><a name="item__doc">_doc</a></strong>

<dd>
<p>Describes what this section is about</p>

</li>
</dd><dt><strong><a name="item__sub">_sub</a></strong>

<dd>
<p>A function pointer. It is called for every instance of this section,
with the real name of the section passed as its first argument. This is
probably only useful for the regexp sections. If the function returns
a defined value it is assumed that the test was not successful and an
error is generated with the returned string as content.</p>

</li>
</dd></dl>
<p>
</p>
<h3><a name="special_variable_keys">Special Variable Keys</a></h3>
<dl>
<dt><strong><a name="item__re">_re</a></strong>

<dd>
<p>Regular expression upon which the value will be checked.</p>

</li>
</dd><dt><strong><a name="item__re_error">_re_error</a></strong>

<dd>
<p>String containing the returned error in case the regular expression doesn't
match (if not specified, a generic 'syntax error' message will be returned).</p>

</li>
</dd><dt><strong>_sub</strong>

<dd>
<p>A function pointer. It called for every value, with the value passed as its
first argument. If the function returns a defined value it is assumed that
the test was not successful and an error is generated with the returned
string as content.</p>

</dd><dd>
<p>If the '_varlist' key (see above) is defined in this section, the '_sub'
function will also receive an array reference as the second argument. The
array contains a list of those variables already defined in the same
section. This can be used to enforce the order of the variables.</p>

</li>
</dd><dt><strong><a name="item__default">_default</a></strong>

<dd>
<p>A default value that will be assigned to the variable if none is specified or inherited.</p>

</li>
</dd><dt><strong>_doc</strong>

<dd>
<p>Description of the variable.</p>

</li>
</dd><dt><strong><a name="item__example">_example</a></strong>

<dd>
<p>A one line example for the content of this variable.</p>

</li>
</dd></dl>
<p>
</p>
<h3><a name="special_table_keys">Special Table Keys</a></h3>
<dl>
<dt><strong><a name="item__columns">_columns</a></strong>

<dd>
<p>Number of columns. If not specified, it will not be enforced.</p>

</li>
</dd><dt><strong><a name="item__key">_key</a></strong>

<dd>
<p>If defined, the specified column number will be used as key in a hash in the
returned hash. If not defined, the returned hash will contain a '_table'
element with the contents of the table as array. The rows of the tables are
stored as arrays.</p>

</li>
</dd><dt><strong>_sub</strong>

<dd>
<p>they work analog to the description in the previous section.</p>

</li>
</dd><dt><strong>_doc</strong>

<dd>
<p>describes the content of the column.</p>

</li>
</dd><dt><strong>_example</strong>

<dd>
<p>example for the content of this column</p>

</li>
</dd></dl>
<p>
</p>
<h3><a name="special_text_keys">Special Text Keys</a></h3>
<dl>
<dt><strong>_re</strong>

<dd>
<p>Regular expression upon which the text will be checked (everything as a single
line).</p>

</li>
</dd><dt><strong>_re_error</strong>

<dd>
<p>String containing the returned error in case the regular expression doesn't
match (if not specified, a generic 'syntax error' message will be returned).</p>

</li>
</dd><dt><strong>_sub</strong>

<dd>
<p>they work analog to the description in the previous section.</p>

</li>
</dd><dt><strong>_doc</strong>

<dd>
<p>Ditto.</p>

</li>
</dd><dt><strong>_example</strong>

<dd>
<p>Potential multi line example for the content of this text section</p>

</li>
</dd></dl>
<p>
</p>
<h2><a name="configuration_syntax">Configuration Syntax</a></h2>
<p>
</p>
<h3><a name="general_syntax">General Syntax</a></h3>
<p>'#' denotes a comment up to the end-of-line, empty lines are allowed and space
at the beginning and end of lines is trimmed.</p>
<p>'\' at the end of the line marks a continued line on the next line. A single
space will be inserted between the concatenated lines.</p>
<p>'@include filename' is used to include another file. Include works relative to the
directory where the parent file is in.</p>
<p>'@define a some value' will replace all occurences of 'a' in the following text
with 'some value'.</p>
<p>Fields in tables that contain white space can be enclosed in either <code>'</code> or <code>&quot;</code>.
Whitespace can also be escaped with <code>\</code>. Quotes inside quotes are allowed but must
be escaped with a backslash as well.</p>
<p>
</p>
<h3><a name="sections">Sections</a></h3>
<p>Config::Grammar supports hierarchical configurations through sections, whose
syntax is as follows:</p>
<dl>
<dt><strong><a name="item_level_1">Level 1</a></strong>

<dd>
<p>*** section name ***</p>

</li>
</dd><dt><strong><a name="item_level_2">Level 2</a></strong>

<dd>
<p>+ section name</p>

</li>
</dd><dt><strong><a name="item_level_3">Level 3</a></strong>

<dd>
<p>++ section name</p>

</li>
</dd><dt><strong><a name="item_level_n_2c_n_3e1">Level n, n&gt;1</a></strong>

<dd>
<p>+..+ section name (number of '+' determines level)</p>

</li>
</dd></dl>
<p>
</p>
<h3><a name="assignments">Assignments</a></h3>
<p>Assignements take the form: 'variable = value', where value can be any string
(can contain whitespaces and special characters). The spaces before and after
the equal sign are optional.</p>
<p>
</p>
<h3><a name="tabular_data">Tabular Data</a></h3>
<p>The data is interpreted as one or more columns separated by spaces.</p>
<p>
</p>
<h2><a name="example">Example</a></h2>
<p>
</p>
<h3><a name="code">Code</a></h3>
<pre>
 use Data::Dumper;
 use Config::Grammar;</pre>
<pre>
 my $RE_IP       = '\d+\.\d+\.\d+\.\d+';
 my $RE_MAC      = '[0-9a-f]{2}(?::[0-9a-f]{2}){5}';
 my $RE_HOST     = '\S+';</pre>
<pre>
 my $parser = Config::Grammar-&gt;new({
   _sections =&gt; [ 'network', 'hosts' ],
   network =&gt; {
      _vars     =&gt; [ 'dns' ],
      _sections =&gt; [ &quot;/$RE_IP/&quot; ],
      dns       =&gt; {
         _doc =&gt; &quot;address of the dns server&quot;,
         _example =&gt; &quot;ns1.oetiker.xs&quot;,
         _re =&gt; $RE_HOST,
         _re_error =&gt;
            'dns must be an host name or ip address',
         },
      &quot;/$RE_IP/&quot; =&gt; {
         _doc    =&gt; &quot;Ip Adress&quot;,
         _example =&gt; '10.2.3.2',
         _vars   =&gt; [ 'netmask', 'gateway' ],
         netmask =&gt; {
            _doc =&gt; &quot;Netmask&quot;,
            _example =&gt; &quot;255.255.255.0&quot;,
            _re =&gt; $RE_IP,
            _re_error =&gt;
               'netmask must be a dotted ip address'
            },
         gateway =&gt; {
            _doc =&gt; &quot;Default Gateway address in IP notation&quot;,
            _example =&gt; &quot;10.22.12.1&quot;,
            _re =&gt; $RE_IP,
            _re_error =&gt;
               'gateway must be a dotted ip address' },
         },
      },
   hosts =&gt; {
      _doc =&gt; &quot;Details about the hosts&quot;,
      _table  =&gt; {
          _doc =&gt; &quot;Description of all the Hosts&quot;,
         _key =&gt; 0,
         _columns =&gt; 3,
         0 =&gt; {
            _doc =&gt; &quot;Ethernet Address&quot;,
            _example =&gt; &quot;0:3:3:d:a:3:dd:a:cd&quot;,
            _re =&gt; $RE_MAC,
            _re_error =&gt;
               'first column must be an ethernet mac address',
            },
         1 =&gt; {
            _doc =&gt; &quot;IP Address&quot;,
            _example =&gt; &quot;10.11.23.1&quot;,
            _re =&gt; $RE_IP,
            _re_error =&gt;
               'second column must be a dotted ip address',
            },
         2 =&gt; {
            _doc =&gt; &quot;Host Name&quot;,
            _example =&gt; &quot;tardis&quot;,
             },
         },
      },
   });</pre>
<pre>
 my $cfg = $parser-&gt;parse('test.cfg') or
   die &quot;ERROR: $parser-&gt;{err}\n&quot;;
 print Dumper($cfg);
 print $parser-&gt;makepod;</pre>
<p>
</p>
<h3><a name="configuration">Configuration</a></h3>
<pre>
 *** network ***
  
   dns      = 192.168.7.87
  
 + 192.168.7.64
 
   netmask  = 255.255.255.192
   gateway  = 192.168.7.65
  
 *** hosts ***
 
   00:50:fe:bc:65:11     192.168.7.97    plain.hades
   00:50:fe:bc:65:12     192.168.7.98    isg.ee.hades
   00:50:fe:bc:65:14     192.168.7.99    isg.ee.hades</pre>
<p>
</p>
<h3><a name="result">Result</a></h3>
<pre>
 {
   'hosts' =&gt; {
                '00:50:fe:bc:65:11' =&gt; [
                                         '00:50:fe:bc:65:11',
                                         '192.168.7.97',
                                         'plain.hades'
                                       ],
                '00:50:fe:bc:65:12' =&gt; [
                                         '00:50:fe:bc:65:12',
                                         '192.168.7.98',
                                         'isg.ee.hades'
                                       ],
                '00:50:fe:bc:65:14' =&gt; [
                                         '00:50:fe:bc:65:14',
                                         '192.168.7.99',
                                         'isg.ee.hades'
                                       ]
              },
   'network' =&gt; {
                  '192.168.7.64' =&gt; {
                                      'netmask' =&gt; '255.255.255.192',
                                      'gateway' =&gt; '192.168.7.65'
                                    },
                  'dns' =&gt; '192.168.7.87'
                }
 };</pre>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="./Config/Grammar/Dynamic.html">the Config::Grammar::Dynamic manpage</a></p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright (c) 2000-2005 by ETH Zurich. All rights reserved.
Copyright (c) 2007 by David Schweikert. All rights reserved.</p>
<p>
</p>
<hr />
<h1><a name="license">LICENSE</a></h1>
<p>This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>
</p>
<hr />
<h1><a name="authors">AUTHORS</a></h1>
<p>David Schweikert,
Tobias Oetiker,
Niko Tyni</p>

</body>

</html>