[augeas-devel] [PATCH] Support eol comments in shellvars.aug (close ticket 114); Adapt test_shellvars.aug accordinly; Add comment_generic and comment_eol to util.aug to help with eol comments.

Raphael Pinson raphink at gmail.com
Wed Nov 3 00:05:21 UTC 2010


This patch aims at fixing ticket 114 about supporting eol comments in shellvars.aug.
This feature was planned to be added in test_shellvars.aug.
The patch adds the feature to shellvars.aug and tests it in test_shellvars.aug.

In order to make this feature easier to use, the patch adds two entries in util.aug:
  - comment_generic, which manages the current comment lens in a generic way,
    by allowing to set a default comment sign. This can actually be used for other needs;
  - comment_eol, which is a default comment, but the default comment sign is " # "
    instead of "# " because we want a space between the value and the comment by default.

Both Util.comment and Util.comment_eol now use comment_generic. In order to achieve this,
the way comments are parsed had to be modified a little. The change has been done in
a way that doesn't affect at all the tree either way. All tests using Util.comment still work.
However, if you see a problem with this change, please state so.

Note: It is still possible to use "#" in strings that are in double or simple quotes.
If a "#" is met without being encompassed in double or simple quotes, it is considered a comment.
For some reason, I could not make it so that "#" are ignored in backquotes (``) statements.
I don't think this is a big issue for now, but eventually it would be good to find a way to do so.
The patch to the test file shows clearly which statements support "#" outside of comments.

---
 lenses/shellvars.aug            |   10 ++++++----
 lenses/tests/test_shellvars.aug |   34 ++++++++++++++++++++++++++--------
 lenses/util.aug                 |   20 ++++++++++++++++----
 3 files changed, 48 insertions(+), 16 deletions(-)

diff --git a/lenses/shellvars.aug b/lenses/shellvars.aug
index 86423bd..6676f91 100644
--- a/lenses/shellvars.aug
+++ b/lenses/shellvars.aug
@@ -8,13 +8,15 @@ module Shellvars =
   let key_re = /[A-Za-z0-9_]+(\[[0-9]+\])?/ - "unset" - "export"
   let eq = Util.del_str "="
   let comment = Util.comment
+  let comment_eol = Util.comment_eol
   let empty   = Util.empty
   let xchgs   = Build.xchgs
 
-  let char  = /[^() '"\t\n]|\\\\"/
+  let char  = /[^#() '"\t\n]|\\\\"/
   let dquot = /"([^"\\\n]|\\\\.)*"/                    (* " Emacs, relax *)
   let squot = /'[^'\n]*'/
-  let bquot = /`[^`\n]*`/
+  (* For some reason, `` conflicts with comment_eol *)
+  let bquot = /`[^#`\n]*`/
 
   (* Array values of the form '(val1 val2 val3)'. We do not handle empty *)
   (* arrays here because of typechecking headaches. Instead, they are    *)
@@ -33,10 +35,10 @@ module Shellvars =
       store (char* | dquot | squot | bquot | empty_array)
 
   let export = [ key "export" . Util.del_ws_spc ]
-  let kv = [ export? . key key_re . eq . (simple_value | array) . eol ]
+  let kv = [ export? . key key_re . eq . (simple_value | array) . (eol|comment_eol) ]
 
   let var_action (name:string) =
-    [ xchgs name ("@" . name) . Util.del_ws_spc . store key_re . eol ]
+    [ xchgs name ("@" . name) . Util.del_ws_spc . store key_re . (eol|comment_eol) ]
 
   let unset = var_action "unset"
   let bare_export = var_action "export"
diff --git a/lenses/tests/test_shellvars.aug b/lenses/tests/test_shellvars.aug
index 32db1a2..b862910 100644
--- a/lenses/tests/test_shellvars.aug
+++ b/lenses/tests/test_shellvars.aug
@@ -6,11 +6,11 @@ DEVICE=eth0
 BOOTPROTO=static
 BROADCAST=172.31.0.255
 HWADDR=ab:cd:ef:12:34:56
-export IPADDR=172.31.0.31
+export IPADDR=172.31.0.31 # this is our IP
 #DHCP_HOSTNAME=host.example.com
 NETMASK=255.255.255.0
 NETWORK=172.31.0.0
-unset ONBOOT
+unset ONBOOT    #   We do not want this var
 "
   let empty_val = "EMPTY=\nDEVICE=eth0\n"
 
@@ -23,11 +23,13 @@ unset ONBOOT
     { "BROADCAST" = "172.31.0.255" }
     { "HWADDR" = "ab:cd:ef:12:34:56" }
     { "IPADDR" = "172.31.0.31"
-        { "export" } }
+        { "export" }
+        { "#comment" = "this is our IP" } }
     { "#comment" = "DHCP_HOSTNAME=host.example.com" }
     { "NETMASK" = "255.255.255.0" }
     { "NETWORK" = "172.31.0.0" }
-    { "@unset"   = "ONBOOT" }
+    { "@unset"   = "ONBOOT"
+        { "#comment" = "We do not want this var" } }
 
   test Shellvars.lns put eth_static after
       set "BOOTPROTO" "dhcp" ;
@@ -40,7 +42,7 @@ DEVICE=eth0
 BOOTPROTO=dhcp
 HWADDR=ab:cd:ef:12:34:56
 #DHCP_HOSTNAME=host.example.com
-unset ONBOOT
+unset ONBOOT    #   We do not want this var
 "
   test Shellvars.lns get empty_val =
     { "EMPTY" = "" } { "DEVICE" = "eth0" }
@@ -70,13 +72,29 @@ unset ONBOOT
     { "var" = "\\\"" }
 
   test Shellvars.lns get "var=ab#c\n" =
-    { "var" = "ab#c" }
+    { "var" = "ab"
+        { "#comment" = "c" } }
+
+  test Shellvars.lns get "var='ab#c'\n" =
+    { "var" = "'ab#c'" }
+
+  test Shellvars.lns get "var=\"ab#c\"\n" =
+    { "var" = "\"ab#c\"" }
+
+  (* For some reason, `` conflicts with comment_eol *)
+  test Shellvars.lns get "var=`ab#c`\n" =
+    { "var" = "`ab"
+       { "#comment" = "c`" }
 
   test Shellvars.lns get "var=`grep nameserver /etc/resolv.conf | head -1`\n" =
     { "var" = "`grep nameserver /etc/resolv.conf | head -1`" }
 
-  (* We don't handle comments at the end of a line yet *)
-  test Shellvars.lns get "var=ab #c\n" = *
+  test Shellvars.lns put "var=ab #c\n"
+    after rm "/var/#comment" = "var=ab\n"
+
+  test Shellvars.lns put "var=ab\n"
+    after set "/var/#comment" "this is a var" =
+       "var=ab # this is a var\n"
 
   (* Handling of arrays *)
   test Shellvars.lns get "var=(val1 \"val\\\"2\\\"\" val3)\n" =
diff --git a/lenses/util.aug b/lenses/util.aug
index a01d8c8..e82967b 100644
--- a/lenses/util.aug
+++ b/lenses/util.aug
@@ -65,13 +65,25 @@ Variable: indent
      of comments and stores them in nodes, except for empty comments which are
      ignored together with empty lines
 
-View: comment
-  Map comments into "#comment" nodes
+View: comment_generic
+  Map comments and set default comment sign
 *)
-  let comment =
-    [ indent . label "#comment" . del /#[ \t]*/ "# "
+
+  let comment_generic (d:string) =
+    [ label "#comment" . del /[ \t]*#[ \t]*/ d
         . store /([^ \t\n].*[^ \t\n]|[^ \t\n])/ . eol ]
 
+(* View: comment
+  Map comments into "#comment" nodes
+*)
+  let comment = comment_generic "# "
+
+(* View: comment_eol
+  Map comments into "#comment" nodes
+  Add a space before # for end of line comments
+*)
+  let comment_eol = comment_generic " # "
+
 (*
 View: empty
   Map empty lines, including empty comments
-- 
1.7.0.4




More information about the augeas-devel mailing list