0

How can I create an Augeas lens that will allow me to extract a value field from within a parenthesis?

Background:

I'm starting an Augeas lens for managing NASL files for OpenVAS. I need to be able to make a few arbitrary changes to a large number of these scripts.

NASLs contains a lot of key and value pairs which are all surrounded by parentheses as shown below, sometimes with multiple values.

This is a sample line I'm working with. I'm not using any line breaks, comments, etc in my test script yet. The line here is exactly what I'm using.

script_id(100197)

The basic format I'm trying to use for the extraction is:

let digits = store /\d+/
let oparen = Util.del_str "("
let cparen = Util.del_str ")"
(* script_id(100197); *)
let script_id = [  key "script_id" .  oparen  . digits . cparen  ]

And the resulting error from augparse is:

Error encountered at 1:0 (0 characters into string)
                           <|=|script_id(100197)>

Tree generated so far:
<blank>
Tim Brigham
  • 15,545
  • 10
  • 75
  • 115

1 Answers1

2

The first question you need to ask is which tree you want to produce from this string. How do you expect Augeas to parse script_id(100197).

Assuming you want to parse it as, e.g.:

{ "script_id" = "100197" }

you could parse this with the following code:

let script_id =
     let oparen = Util.del_str "("
  in let cparen = Util.del_str ")"
  in [ key "script_id"
     . oparen . store Rx.integer . cparen
     . Util.eol ]

You can add a unit test to check that it works:

test script_id get "script_id(100197)\n" = ?

and launching augparse on the file:

$ augparse openvas.aug 
Test result: openvas.aug:10.0-.44:
  { "script_id" = "100197" }

Note that Augeas only supports POSIX basic regular expressions, so \d is not recognized.

raphink
  • 11,987
  • 6
  • 37
  • 48