[augeas-devel] [PATCH 5/5] Sysconfig: handle end of line comments and semicolons; strip quotes

lutter at redhat.com lutter at redhat.com
Thu Nov 1 23:50:28 UTC 2012


From: David Lutterkort <lutter at redhat.com>

Fixes RHBZ #761246
---
 lenses/sysconfig.aug            |   57 +++++++++++++++++++++++---------------
 lenses/tests/test_sysconfig.aug |   49 ++++++++++++++++++++++++++++-----
 2 files changed, 76 insertions(+), 30 deletions(-)

diff --git a/lenses/sysconfig.aug b/lenses/sysconfig.aug
index 2c7efaa..a39d7f3 100644
--- a/lenses/sysconfig.aug
+++ b/lenses/sysconfig.aug
@@ -4,51 +4,62 @@
 (* around values that need them                                        *)
 (* To keep things simple, we also do not support shell variable arrays *)
 module Sysconfig =
-  let eol = Util.eol
 
-  let key_re = /[A-Za-z0-9_]+(\[[0-9]+\])?/ - "unset" - "export"
+  let eol = Shellvars.eol
+  let semicol_eol = Shellvars.semicol_eol
+
+  let key_re = Shellvars.key_re
   let eq = Util.del_str "="
+
   let comment = Util.comment
-  let empty   = Util.empty
-  let xchgs   = Build.xchgs
-  let dels    = Util.del_str
+  let comment_or_eol = Shellvars.comment_or_eol
 
-  let nothing = del /(""|'')?/ "" . value ""
+  let empty   = Util.empty
 
-  (* Chars allowed in a bare string *)
-  let bchar = /[^ \t\n"'\\]|\\\\./
-  let qchar = /["']/  (* " *)
+  let bchar = /[^; \t\n"'\\]|\\\\./ (* " Emacs, relax *)
+  let qchar = /["']/  (* " Emacs, relax *)
 
   (* We split the handling of right hand sides into a few cases:
    *   bare  - strings that contain no spaces, optionally enclosed in
    *           single or double quotes
+   *   quot  - strings that must be enclosed in single or double quotes
    *   dquot - strings that contain at least one space or apostrophe,
    *           which must be enclosed in double quotes
    *   squot - strings that contain an unescaped double quote
    *)
-  let bare = del qchar? "" . store (bchar+) . del qchar? ""
+  let bare = Quote.do_quote_opt (store bchar+)
+
+  let quot =
+    let word = bchar* . /[; \t]/ . bchar* in
+    Quote.do_quote (store word+)
+
   let dquot =
-    del qchar "\"" . store (bchar* . /[ \t']/ . bchar*)+ . del qchar "\""
+    let char = /[^"\\]|\\\\./ in             (* " *)
+    let word = char* . "'" . char* in
+    Quote.do_dquote (store word+)
+
   let squot =
-    dels "'" . store ((bchar|/[ \t]/)* . "\"" . (bchar|/[ \t]/)*)+ . dels "'"
+    (* We do not allow escaped double quotes in single quoted strings, as  *)
+    (* that leads to a put ambiguity with bare, e.g. for the string '\"'.  *)
+    let char = /[^'\\]|\\\\[^"]/ in           (* " *)
+    let word = char* . "\"" . char* in
+    Quote.do_squote (store word+)
+
+  let export = Shellvars.export
+  let kv (value:lens) = [ export? . key key_re . eq . value . comment_or_eol ]
 
-  let export = [ key "export" . Util.del_ws_spc ]
-  let kv (value:lens) = [ export? . key key_re . eq . value . eol ]
-  let assign = kv nothing | kv bare | kv dquot | kv squot
+  let assign =
+    let nothing = del /(""|'')?/ "" . value "" in
+    kv nothing | kv bare | kv quot | kv dquot | kv squot
 
-  let var_action (name:string) =
-    [ xchgs name ("@" . name) . Util.del_ws_spc . store key_re . eol ]
+  let var_action = Shellvars.var_action
 
   let unset = var_action "unset"
   let bare_export = var_action "export"
 
-  let source =
-    [
-      del /\.|source/ "." . label ".source" .
-      Util.del_ws_spc . store /[^= \t\n]+/ . eol
-    ]
+  let source = Shellvars.source
 
-  let lns = (comment | empty | source | assign | unset | bare_export) *
+  let lns = empty* . (comment | source | assign | unset | bare_export)*
 
 (*
   Examples:
diff --git a/lenses/tests/test_sysconfig.aug b/lenses/tests/test_sysconfig.aug
index aad47cf..3e9356d 100644
--- a/lenses/tests/test_sysconfig.aug
+++ b/lenses/tests/test_sysconfig.aug
@@ -8,11 +8,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"
 
@@ -25,11 +25,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 lns put eth_static after
       set "BOOTPROTO" "dhcp" ;
@@ -42,7 +44,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 lns get empty_val =
     { "EMPTY" = "" } { "DEVICE" = "eth0" }
@@ -80,12 +82,45 @@ unset ONBOOT
   test lns get "var=ab#c\n" =
     { "var" = "ab#c" }
 
+  test lns get "var='ab#c'\n" =
+    { "var" = "ab#c" }
+
+  test lns get "var=\"ab#c\"\n" =
+    { "var" = "ab#c" }
+
+  test lns get "var=\"ab#c\"\n" =
+    { "var" = "ab#c" }
+
   (* We don't handle backticks *)
   test lns get
       "var=`grep nameserver /etc/resolv.conf | head -1`\n" = *
 
-  (* We don't handle comments at the end of a line yet *)
-  test lns get "var=ab #c\n" = *
+  test lns get "var=ab #c\n" =
+    { "var" = "ab"
+        { "#comment" = "c" } }
+
+  test lns put "var=ab #c\n"
+    after rm "/var/#comment" = "var=ab\n"
+
+  test lns put "var=ab\n"
+    after set "/var/#comment" "this is a var" =
+       "var=ab # this is a var\n"
+
+  (* Test semicolons *)
+  test lns get "VAR1=\"this;is;a;test\"\nVAR2=this;\n" =
+  { "VAR1" = "this;is;a;test" }
+  { "VAR2" = "this" }
+
+  (* BZ 761246 *)
+  test lns get "DEVICE=\"eth0\";\n" =
+    { "DEVICE" = "eth0" }
+
+  test lns put "DEVICE=\"eth0\";\n" after
+    set "/DEVICE" "em1" = "DEVICE=\"em1\";\n"
+
+  test lns get "DEVICE=\"eth0\"; # remark\n" =
+    { "DEVICE" = "eth0" }
+    { "#comment" = "remark" }
 
   (* Bug 109: allow a bare export *)
   test lns get "export FOO\n" =
-- 
1.7.7.6




More information about the augeas-devel mailing list