Re: parsing braces question
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: parsing braces question

From: Adrian Ho <tcl@03s.net>
Date: Mon Mar 06 2006 - 04:04:55 CET

On 2006-03-06, Charles Heizer <heizer1@llnl.gov> wrote:
> I'm trying to write a tcl script to parse a BIND DNS config file and
> I'm haing some trouble trying to parse the braces.
>
> example --
>
> zone "." IN {
> type hint;
> file "named.ca";
> };
>
> zone "localhost" IN {
> type master;
> file "localhost.zone";
> allow-update { none; };
> };
>
> I would like to parse this so I can read the configuation and write it
> back out to make changes. Any suggestions are welcome.

If your changes actually require parsing the config file (as opposed to
simple search-n-replace text substitution), then you can take advantage
of the fact that BIND config syntax is very close to Tcl's and [source]
the config file instead. You'd have to implement acl/options/zone/etc.
as Tcl procs, of course. Running the [source] in a safe interp
<http://wiki.tcl.tk/1496> may be a good thing, depending on your
deployment usage scenario.

A few things to watch out for:

* Avoid /* C-style comments */ in the config file. The #-style comment
  is handled natively by Tcl, and the // C++-style comment is easily
  handled with:

    proc // {args} {}

  although each comment must itself be in proper Tcl syntax (eg. no
  unbalanced braces or semicolons).

* Semicolons and quotes in the original file will be "lost" in the [source],
  so you'll have to insert them in the correct places upon output.

* BIND config directives ($ORIGIN, $INCLUDE, etc.) are handled by
  defining the appropriate variables and procedures, and leveraging on
  Tcl's own substitution mechanism. For example:

    set ORIGIN BIND_origin
    proc BIND_origin {domain args} {
      ...
    }

- Adrian
Received on Sun Apr 30 02:24:15 2006