aboutsummaryrefslogtreecommitdiffstats
path: root/main/augeas/0016-Shellvars-allow-in-commands.patch
blob: f88c3cce2a7e97d335defa039456d7da44fec7d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
From 1e02e1f746ade63007fbe9caa03e89511a1da66b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= <raphael.pinson@camptocamp.com>
Date: Tue, 25 Aug 2015 11:28:41 +0200
Subject: [PATCH 16/19] Shellvars: allow &&/|| in commands

---
 lenses/shellvars.aug            | 80 ++++++++++++++++++++++++-----------------
 lenses/tests/test_shellvars.aug | 42 ++++++++++++++++++++--
 2 files changed, 87 insertions(+), 35 deletions(-)

diff --git a/lenses/shellvars.aug b/lenses/shellvars.aug
index f68f5bb..f7f4c47 100644
--- a/lenses/shellvars.aug
+++ b/lenses/shellvars.aug
@@ -106,28 +106,66 @@ module Shellvars =
     . Util.del_str "return"
     . ( Util.del_ws_spc . store Rx.integer )?
 
+  let action (operator:string) (lbl:string) (sto:lens) =
+    [ del (Rx.opt_space . operator . Rx.opt_space) (" " . operator . " ")
+    . label ("@".lbl) . sto ]
+
+  let action_pipe = action "|" "pipe"
+  let action_and = action "&&" "and"
+  let action_or = action "||" "or"
+
   let condition =
-       let action (operator:string) (lbl:string) =
-         [ Sep.opt_space . Util.del_str operator . Sep.opt_space
-         . label lbl . sto_to_semicol ]
-    in let cond (start:string) (end:string) = [ label "type" . store start ]
-                                            . Util.del_ws_spc . sto_to_semicol
-                                            . Util.del_ws_spc . Util.del_str end
-                                            . ( action "&&" "@and" | action "||" "@or" )*
+    let cond (start:string) (end:string) = [ label "type" . store start ]
+                                         . Util.del_ws_spc . sto_to_semicol
+                                         . Util.del_ws_spc . Util.del_str end
+                                         . ( action_and sto_to_semicol | action_or sto_to_semicol )*
     in Util.indent . label "@condition" . (cond "[" "]" | cond "[[" "]]")
 
   (* Entry types *)
   let entry_eol_item (item:lens) = [ item . comment_or_eol ]
   let entry_item (item:lens) = [ item ]
 
+  let entry_eol_nocommand =
+      entry_eol_item source
+        | entry_eol_item kv
+        | entry_eol_item unset
+        | entry_eol_item bare_export
+        | entry_eol_item builtin
+        | entry_eol_item return
+        | entry_eol_item condition
+        | entry_eol_item eval
+        | entry_eol_item alias
+
+  let entry_noeol_nocommand =
+      entry_item source
+        | entry_item kv
+        | entry_item unset
+        | entry_item bare_export
+        | entry_item builtin
+        | entry_item return
+        | entry_item condition
+        | entry_item eval
+        | entry_item alias
+
   (* Command *)
   let rec command =
        let reserved_key = /exit|shift|return|ulimit|unset|export|source|\.|if|for|select|while|until|then|else|fi|done|case|eval|alias/
     in let word = /[A-Za-z0-9_.-\/]+/
-    in let pipe = del /[ \t]*\|[ \t]*/ " | "
+    in let entry_eol = entry_eol_nocommand | entry_eol_item command
+    in let entry_noeol = entry_noeol_nocommand | entry_item command
+    in let entry = entry_eol | entry_noeol
+    in let pipe = action_pipe (entry_eol_item command | entry_item command)
+    in let and = action_and entry
+    in let or = action_or entry
     in Util.indent . label "@command" . store (word - reserved_key)
      . [ Sep.space . label "@arg" . sto_to_semicol]?
-     . (pipe . (entry_eol_item command | entry_item command) )*
+     . ( pipe | and | or )?
+
+  let entry_eol = entry_eol_nocommand
+                | entry_eol_item command
+
+  let entry_noeol = entry_noeol_nocommand
+                  | entry_item command
 
 (************************************************************************
  * Group:                 CONDITIONALS AND LOOPS
@@ -185,30 +223,6 @@ module Shellvars =
       . entry+
       . Util.indent . Util.del_str "}" . eol ]
 
-  let entry_eol =
-      entry_eol_item source
-        | entry_eol_item kv
-        | entry_eol_item unset
-        | entry_eol_item bare_export
-        | entry_eol_item builtin
-        | entry_eol_item return
-        | entry_eol_item condition
-        | entry_eol_item eval
-        | entry_eol_item alias
-        | entry_eol_item command
-
-  let entry_noeol =
-      entry_item source
-        | entry_item kv
-        | entry_item unset
-        | entry_item bare_export
-        | entry_item builtin
-        | entry_item return
-        | entry_item condition
-        | entry_item eval
-        | entry_item alias
-        | entry_item command
-
   let rec rec_entry =
     let entry = comment | entry_eol | rec_entry in
         cond_if entry
diff --git a/lenses/tests/test_shellvars.aug b/lenses/tests/test_shellvars.aug
index e0bf7fb..2c810cb 100644
--- a/lenses/tests/test_shellvars.aug
+++ b/lenses/tests/test_shellvars.aug
@@ -627,8 +627,46 @@ test Shellvars.lns get "echo foobar 'and this is baz'
 test Shellvars.lns get "echo \"$STRING\" | grep foo\n" =
   { "@command" = "echo"
     { "@arg" = "\"$STRING\"" }
-    { "@command" = "grep"
-      { "@arg" = "foo" } } }
+    { "@pipe"
+      { "@command" = "grep"
+        { "@arg" = "foo" } } } }
+
+(* Test: Shellvars.lns
+     Support && and || after command
+     GH #215 *)
+test Shellvars.lns get "grep -q \"Debian\" /etc/issue && echo moo\n" =
+  { "@command" = "grep"
+    { "@arg" = "-q \"Debian\" /etc/issue" }
+    { "@and"
+      { "@command" = "echo"
+        { "@arg" = "moo" } } } }
+
+test Shellvars.lns get "grep -q \"Debian\" /etc/issue || echo baa\n" =
+  { "@command" = "grep"
+    { "@arg" = "-q \"Debian\" /etc/issue" }
+    { "@or"
+      { "@command" = "echo"
+        { "@arg" = "baa" } } } }
+
+test Shellvars.lns get "grep -q \"Debian\" /etc/issue && DEBIAN=1\n" =
+  { "@command" = "grep"
+    { "@arg" = "-q \"Debian\" /etc/issue" }
+    { "@and"
+      { "DEBIAN" = "1" } } }
+
+test Shellvars.lns get "cat /etc/issue | grep -q \"Debian\" && echo moo || echo baa\n" =
+  { "@command" = "cat"
+    { "@arg" = "/etc/issue" }
+    { "@pipe"
+      { "@command" = "grep"
+        { "@arg" = "-q \"Debian\"" }
+        { "@and"
+          { "@command" = "echo"
+            { "@arg" = "moo" }
+            { "@or"
+              { "@command" = "echo"
+                { "@arg" = "baa" } } } } } } } }
+
 
 (* Local Variables: *)
 (* mode: caml       *)
-- 
2.5.0