[augeas-devel] matching optional whitespace/newline

Aaron Knister aaron.knister at gmail.com
Tue Sep 9 03:53:37 UTC 2014


I'm attempting to write a lens for the LVM config file. One of the fun things about the file is that within a given section (contained within { }) is that the closing curly brace is not necessarily preceded by a newline. e.g.:

config { myoption = "test"}

is perfectly valid as is

config { myoption = "test"
}

I'm having a really hard time getting augeas to properly deal with that optional newline. Here's a simplified version of my lens so far. For now I'm ignoring the section name and just focusing on the block itself excluding the curly brackets:

module Lvm =
  let eol = Util.eol
  let empty = Util.empty
  let comment = Util.comment
  let indent = del /[ \t]*/ ""
  let eq = del /=/ "="
  let opt_nl = del /\n?/ "\n"
  let lns1 =
     [ indent . key /[a-zA-Z][a-zA-Z0-9_]+/ . indent . eq . indent . store /[0-9]+/ . opt_nl]

  let empty_generic_re = /\n[ \t]*#?[ \t]*/
  let empty2 =
    [ del empty_generic_re "\n" . Util.del_str "\n" ]

  let lns =
     empty* . (( lns1 | comment ) . (lns1 | comment | empty2 )* )?

  test lns get "foo=0" = ?
  test lns get "foo=0\n" = ?

  test lns get "foo=0bar=9" = ?
  test lns get "foo=0bar=9\n" = ?

  test lns put "\n\n#baz\nfoo=0\n" after set "/foo2" "8" = ?

  test lns get "foo=0\n#hello\nbar=9" = ?
  test lns get "foo=0\n#hello\nbar=9\n" = ?

  test lns get "foo=0\n\n\n\nbar=9" = ?
  test lns get "foo=0\n\n\nbar=9\n" = ?

This works OK -- it matches comments and it matches directives smushed on the same line (valid lvm.conf syntax) and handles optional trailing newlines without a problem. I've also half way got it handling empty lines, however as the last 2 tests demonstrate it only handles empty lines in groups of 2. In the empty2 lens I was attempting to match any \n followed by another \n the idea being that this would catch a newline that appears after a configuration line since it's newline is optional. If I remove the optional newline match from the lns1 lens then the put methods don't generate output that contains a newline for new trees. 

Any help here would be greatly appreciated!

Thanks,
Aaron




More information about the augeas-devel mailing list