[augeas-devel] [PATCH] List handling shellvars_list lens added.

Frederik Wagner fnerdwq at googlemail.com
Thu May 6 14:29:52 UTC 2010


Lens shellvars_list has been added to treat variables in specific files
as lists of words, e.g. in /etc/sysconfig/ 'kernel' and 'bootloader'.

* lenses/shellvars.aug:
  removed include for /etc/sysconfig/kernel
* lenses/shellvars_array.aug:
  new lens
* lenses/tests/test_shellvars_array.aug:
  test for newlens
---
 lenses/shellvars.aug                 |    1 -
 lenses/shellvars_list.aug            |   54 +++++++++++++++
 lenses/tests/test_shellvars_list.aug |  122 ++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 1 deletions(-)
 create mode 100644 lenses/shellvars_list.aug
 create mode 100644 lenses/tests/test_shellvars_list.aug

diff --git a/lenses/shellvars.aug b/lenses/shellvars.aug
index 926a092..1605cbc 100644
--- a/lenses/shellvars.aug
+++ b/lenses/shellvars.aug
@@ -69,7 +69,6 @@ module Shellvars =
       sc_incl "irda" .
       sc_incl "irqbalance" .
       sc_incl "kdump" .
-      sc_incl "kernel" .
       sc_incl "keyboard" .
       sc_incl "kudzu" .
       sc_incl "libvirtd" .
diff --git a/lenses/shellvars_list.aug b/lenses/shellvars_list.aug
new file mode 100644
index 0000000..25cbfc2
--- /dev/null
+++ b/lenses/shellvars_list.aug
@@ -0,0 +1,54 @@
+(* Generic lens for shell-script config files like the ones found *)
+(* in /etc/sysconfig, where a string needs to be split into       *)
+(* single words.                                                  *)
+module Shellvars_list =
+  autoload xfm
+
+  let eol = Util.eol
+
+  let key_re = /[A-Za-z0-9_]+/
+  let eq      = Util.del_str "="
+  let comment = Util.comment
+  let empty   = Util.empty
+  let indent  = Util.indent
+
+  let sqchar = /[^ '\t\n]/
+  let dqchar = /[^ "\t\n]/
+  let char   = /[^ "'\t\n]/
+
+  (* lists values of the form ...  val1 val2 val3  ... *)
+  let list(ch:regexp) =
+    let list_value = store ch+ in
+      indent . counter "values" .
+      [ seq "values" . list_value ] .
+      [ del /[ \t\n]+/ " "  . seq "values" . list_value ]* . indent
+
+
+  (* handle single quoted lists *)
+  let squote_arr = [ label "quote" . store /'/ ] . (list sqchar)? .
del /'/ "'"
+
+  (* similarly handle double qouted lists *)
+  let dquote_arr = [ label "quote" . store /"/ ] . (list dqchar)? .
del /"/ "\""
+
+  (* handle unquoted single value *)
+  let unquot_val = [ label "quote" . store "" ] . counter "vals" .
[seq "vals"  . store char+]?
+
+
+  (* lens for key value pairs *)
+  let kv = [ key key_re . eq . ( squote_arr | dquote_arr | unquot_val
) .  eol ]
+
+  let lns = ( comment | empty | kv )*
+
+  let sc_incl (n:string) = (incl ("/etc/sysconfig/" . n))
+  let filter_sysconfig =
+      sc_incl "bootloader" .
+      sc_incl "kernel"
+
+  let filter = filter_sysconfig
+             . Util.stdexcl
+
+  let xfm = transform lns filter
+
+(* Local Variables: *)
+(* mode: caml       *)
+(* End:             *)
diff --git a/lenses/tests/test_shellvars_list.aug
b/lenses/tests/test_shellvars_list.aug
new file mode 100644
index 0000000..aa6a2fa
--- /dev/null
+++ b/lenses/tests/test_shellvars_list.aug
@@ -0,0 +1,122 @@
+(* Test for shell list handling lens *)
+module Test_shellvars_list =
+
+  let list_vals = "# Some comment
+MODULES_LOADED_ON_BOOT=\"ipv6 sunrpc\"
+
+DEFAULT_APPEND=\"showopts noresume console=tty0 console=ttyS0,115200n8 ro\"
+
+LOADER_TYPE=\"grub\"
+"
+
+  test Shellvars_list.lns get list_vals =
+    { "#comment" = "Some comment" }
+    { "MODULES_LOADED_ON_BOOT"
+      { "quote" = "\"" }
+      { "1" = "ipv6" }
+      { "2" = "sunrpc" }
+    }
+    {  }
+    { "DEFAULT_APPEND"
+      { "quote" = "\"" }
+      { "1" = "showopts" }
+      { "2" = "noresume" }
+      { "3" = "console=tty0" }
+      { "4" = "console=ttyS0,115200n8" }
+      { "5" = "ro" }
+    }
+    {  }
+    { "LOADER_TYPE"
+      { "quote" = "\"" }
+      { "1" = "grub" }
+    }
+
+
+  (* append a value *)
+  test Shellvars_list.lns put "VAR=\"test1\t  \ntest2\"\n" after
+    set "VAR/01" "test3"
+    = "VAR=\"test1\t  \ntest2 test3\"\n"
+
+  (* in double quoted lists, single quotes are allowed *)
+  test Shellvars_list.lns get "VAR=\"test'1 test2\"\n" =
+    { "VAR"
+      { "quote" = "\"" }
+      { "1" = "test'1" }
+      { "2" = "test2" }
+    }
+
+  (* add new value, delete one and append something *)
+  (* !!! order of adding 'quote' key and values seeems to be relevant!!! *)
+  (* ---> first the quote *)
+  test Shellvars_list.lns put list_vals after
+    set "FAILSAVE_APPEND/quote" "\"" ;
+    set "FAILSAVE_APPEND/01" "console=ttyS0" ;
+    rm "LOADER_TYPE" ;
+    rm "MODULES_LOADED_ON_BOOT/1" ;
+    set "DEFAULT_APPEND/01" "teststring"
+    = "# Some comment
+MODULES_LOADED_ON_BOOT=\"sunrpc\"
+
+DEFAULT_APPEND=\"showopts noresume console=tty0
console=ttyS0,115200n8 ro teststring\"
+
+FAILSAVE_APPEND=\"console=ttyS0\"
+"
+
+  (* test of single quotes (leading/trailing whitespaces are kept *)
+  (* leading/trailing) *)
+  test Shellvars_list.lns put "VAR=' \t test1\t  \ntest2  '\n" after
+    set "VAR/01" "test3"
+    = "VAR=' \t test1\t  \ntest2 test3  '\n"
+
+  (* change quotes (leading/trailing whitespaces are lost *)
+  test Shellvars_list.lns put "VAR=' \t test1\t  \ntest2  '\n" after
+    set "VAR/quote" "\""
+    = "VAR=\"test1\t  \ntest2\"\n"
+
+  (* double quotes are allowed in single quoted lists *)
+  test Shellvars_list.lns get "VAR='test\"1 test2'\n" =
+    { "VAR"
+      { "quote" = "'" }
+      { "1" = "test\"1" }
+      { "2" = "test2" }
+    }
+
+  (* emtpy list with quotes *)
+  test Shellvars_list.lns get "VAR=''\n" =
+    { "VAR"
+      { "quote" = "'" }
+    }
+
+  (* unquoted value *)
+  test Shellvars_list.lns get "VAR=test\n" =
+    { "VAR"
+      { "quote" = "" }
+      { "1" = "test" }
+    }
+
+  (* append to unquoted value *)
+  test Shellvars_list.lns put "VAR=test1\n" after
+    set "VAR/quote" "\"";
+    set "VAR/01" "test2"
+    = "VAR=\"test1 test2\"\n"
+
+  (* empty entry *)
+  test Shellvars_list.lns get "VAR=\n" =
+    { "VAR"
+      { "quote" = "" }
+    }
+
+  (* set value w/o quotes to empty value... *)
+  test Shellvars_list.lns put "VAR=\n" after
+    set "VAR/01" "test"
+    = "VAR=test\n"
+
+  (* ... or no value *)
+  test Shellvars_list.lns put "" after
+    set "VAR/quote" "";
+    set "VAR/1" "test"
+    = "VAR=test\n"
+
+(* Local Variables: *)
+(* mode: caml       *)
+(* End:             *)
-- 
1.7.0




More information about the augeas-devel mailing list