[augeas-devel] hosts.aug revisited

Kjetil Torgrim Homme kjetilho at linpro.no
Mon Jul 21 22:23:34 UTC 2008


I thought I'd try to enhance hosts.aug to recognise all syntactically
correct /etc/hosts as an introduction to Augeas lenses.  This turned
out to be harder than I thought it would be, even with the help of
Raphaël Pinson and David Lutterkort on IRC :-)

The problem with the version in 0.2.2 (and Hg) is that it neither
accepts leading white space nor comments after records, ie. this is
valid, but Augeas fails silently if fed the file:

# This is a comment
  127.0.0.1  localhost loghost # another comment
192.168.0.1  my-gw.local  


Here's the current code:

module Hosts = autoload xfm

  let sep_tab = Util.del_ws_tab
  let sep_spc = Util.del_ws_spc
  let eol = Util.del_str "\n"

  let comment = [ del /(#.*|[ \t]*)\n/ "\n" ]
  let word = /[^# \n\t]+/
  let record = [ seq "host" . [ label "ipaddr" . store  word ] . sep_tab .
                              [ label "canonical" . store word ] .
                              [ label "alias" . sep_spc . store word ]*
                 . eol ]

  let lns = ( comment | record ) *
  let xfm = transform lns (incl "/etc/hosts")


First I tried to allow trailing comments by changing:

  let record = [ seq "host" . [ label "ipaddr" . store  word ] . sep_tab .
                              [ label "canonical" . store word ] .
                              [ label "alias" . sep_spc . store word ]*
                 . comment ]

comment is a true superset of eol, after all.  This sort of works,
since it allows "127.0.0.1 localhost# a comment", but will fail if you
put whitespace in front of the comment.  So let's try:

  let record = [ seq "host" . [ label "ipaddr" . store  word ] . sep_tab .
                              [ label "canonical" . store word ] .
                              [ label "alias" . sep_spc . store word ]*
                 . sep_spc? . comment ]

No -- it doesn't work, since the whitespace could be part of sep_spc
or of comment, and ambiguity isn't allowed.  Let's revert the
definition of record back to the original and change comment instead:

  let comment = [ del /[ \t]*(#.*)?\n/ "\n" ]

It can parse the file!  Nice.  OK, next task, allow leading whitespace:

  let record = [ sep_spc?
                 . seq "host" . [ label "ipaddr" . store  word ] . sep_tab .
                                [ label "canonical" . store word ] .
                                [ label "alias" . sep_spc . store word ]*
                 . comment ]

Yes, so far so good.  Now let's try to manipulate the file:

  $ augtool -c
  augtool> set /files/etc/hosts/10000/ipaddr 192.168.2.17
  augtool> set /files/etc/hosts/10000/canonical yetanother
  augtool> save
  /usr/share/augeas/lenses/hosts.aug:16.17-19.26:Short split for concat
  Saving failed

(16.17-19.26 is the definition of record)

This happens if I only change the comment definition, too.  That's as
far as I got -- I'd appreciate any help.

-- 
regards,
Kjetil T.




More information about the augeas-devel mailing list