1 |
commit: 2a7c39aed496a03d4e0fe184bafbc34d50c87bd5 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Jul 21 15:41:08 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Aug 2 07:46:29 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=2a7c39ae |
7 |
|
8 |
Parser&Walker: reimplement export built-in |
9 |
|
10 |
Now export built-in will call back to parser grammar in order to support |
11 |
array definition. |
12 |
|
13 |
--- |
14 |
Makefile.am | 2 + |
15 |
bashast/bashast.g | 11 +++++++- |
16 |
bashast/gunit/array.gunit | 3 ++ |
17 |
bashast/gunit/pipeline.gunit | 2 +- |
18 |
bashast/libbashWalker.g | 5 ---- |
19 |
src/builtins/export_builtin.cpp | 46 +++++++++++++++++++++++++++++++++++++++ |
20 |
src/builtins/export_builtin.h | 46 +++++++++++++++++++++++++++++++++++++++ |
21 |
src/core/bash_ast.cpp | 5 ++++ |
22 |
src/core/bash_ast.h | 4 +++ |
23 |
src/cppbash_builtin.cpp | 2 + |
24 |
10 files changed, 118 insertions(+), 8 deletions(-) |
25 |
|
26 |
diff --git a/Makefile.am b/Makefile.am |
27 |
index 4ef9622..97652a8 100644 |
28 |
--- a/Makefile.am |
29 |
+++ b/Makefile.am |
30 |
@@ -196,6 +196,8 @@ libcppbash_la_SOURCES = src/common.h \ |
31 |
src/builtins/echo_builtin.h \ |
32 |
src/builtins/eval_builtin.cpp \ |
33 |
src/builtins/eval_builtin.h \ |
34 |
+ src/builtins/export_builtin.cpp \ |
35 |
+ src/builtins/export_builtin.h \ |
36 |
src/builtins/declare_builtin.cpp \ |
37 |
src/builtins/declare_builtin.h \ |
38 |
src/builtins/boolean_builtins.h \ |
39 |
|
40 |
diff --git a/bashast/bashast.g b/bashast/bashast.g |
41 |
index 2709fc5..3b9b5bd 100644 |
42 |
--- a/bashast/bashast.g |
43 |
+++ b/bashast/bashast.g |
44 |
@@ -313,11 +313,12 @@ command_atom |
45 |
: (FOR|SELECT|IF|WHILE|UNTIL|CASE|LPAREN|LBRACE|LLPAREN|LSQUARE|TEST_EXPR) => compound_command |
46 |
| FUNCTION BLANK string_expr_no_reserved_word ((BLANK? parens wspace?)|wspace) compound_command |
47 |
-> ^(FUNCTION string_expr_no_reserved_word compound_command) |
48 |
- | (name (LSQUARE|EQUALS|PLUS EQUALS)|LOCAL|EXPORT) => variable_definitions |
49 |
+ | (name (LSQUARE|EQUALS|PLUS EQUALS)|LOCAL) => variable_definitions |
50 |
( |
51 |
(BLANK bash_command) => BLANK bash_command -> bash_command variable_definitions |
52 |
| -> ^(VARIABLE_DEFINITIONS variable_definitions) |
53 |
) |
54 |
+ | (EXPORT) => EXPORT BLANK export_item -> ^(STRING EXPORT) ^(STRING ^(DOUBLE_QUOTED_STRING export_item)) |
55 |
| string_expr_no_reserved_word |
56 |
( |
57 |
(BLANK? parens) => BLANK? parens wspace? compound_command |
58 |
@@ -355,7 +356,6 @@ variable_definitions |
59 |
: ( |
60 |
variable_definition_atom ((BLANK name (LSQUARE|EQUALS|PLUS EQUALS)) => BLANK! variable_definition_atom)* |
61 |
| (LOCAL) => LOCAL BLANK! local_item ((BLANK name) => BLANK! local_item)* |
62 |
- | (EXPORT) => EXPORT! ((BLANK name) => BLANK! export_item)+ |
63 |
); |
64 |
|
65 |
variable_definition_atom |
66 |
@@ -406,6 +406,13 @@ local_item |
67 |
#endif |
68 |
} ->; |
69 |
export_item |
70 |
+ : ((~EOL) => (string_expr_part|BLANK|LPAREN|RPAREN))+; |
71 |
+ |
72 |
+builtin_variable_definitions |
73 |
+ : (builtin_variable_definition_atom) (BLANK builtin_variable_definition_atom)* |
74 |
+ -> ^(LIST ^(COMMAND ^(VARIABLE_DEFINITIONS builtin_variable_definition_atom +))); |
75 |
+ |
76 |
+builtin_variable_definition_atom |
77 |
: variable_definition_atom |
78 |
| name ->; |
79 |
|
80 |
|
81 |
diff --git a/bashast/gunit/array.gunit b/bashast/gunit/array.gunit |
82 |
index c304e7c..734343f 100644 |
83 |
--- a/bashast/gunit/array.gunit |
84 |
+++ b/bashast/gunit/array.gunit |
85 |
@@ -31,6 +31,9 @@ variable_definition_atom: |
86 |
"asdf+=()" -> (PLUS_ASSIGN asdf ARRAY) |
87 |
"asdf+=(a)" -> (PLUS_ASSIGN asdf (ARRAY (STRING a))) |
88 |
|
89 |
+builtin_variable_definitions: |
90 |
+"asdf=(a b c d) ade acd=bde" -> (LIST (COMMAND (VARIABLE_DEFINITIONS (= asdf (ARRAY (STRING a) (STRING b) (STRING c) (STRING d))) (= acd (STRING bde))))) |
91 |
+ |
92 |
variable_reference: |
93 |
"$asdf" -> (VAR_REF asdf) |
94 |
"${asdf[0]:-default}" -> (VAR_REF (USE_DEFAULT_WHEN_UNSET_OR_NULL (asdf 0) (STRING default))) |
95 |
|
96 |
diff --git a/bashast/gunit/pipeline.gunit b/bashast/gunit/pipeline.gunit |
97 |
index ec6deda..8f6dd43 100644 |
98 |
--- a/bashast/gunit/pipeline.gunit |
99 |
+++ b/bashast/gunit/pipeline.gunit |
100 |
@@ -20,7 +20,7 @@ gunit java_libbash; |
101 |
|
102 |
pipeline: |
103 |
"cat asdf" -> (COMMAND (STRING cat) (STRING asdf)) |
104 |
-"export VAR=bar LAA=laa foo" -> (COMMAND (VARIABLE_DEFINITIONS (= VAR (STRING bar)) (= LAA (STRING laa)))) |
105 |
+"export VAR=bar LAA=(1 2 3) foo" -> (COMMAND (STRING export) (STRING (DOUBLE_QUOTED_STRING VAR = bar LAA = ( 1 2 3 ) foo))) |
106 |
"LOCAL1=a LOCAL2=b export GLOBAL1=2 GLOBAL2 GLOBAL3" -> (COMMAND (STRING export) (STRING GLOBAL1 = 2) (STRING GLOBAL2) (STRING GLOBAL3) (= LOCAL1 (STRING a)) (= LOCAL2 (STRING b))) |
107 |
"time -p cat file" -> (COMMAND (STRING cat) (STRING file) (time p)) |
108 |
"time cat file | grep search" -> (| (COMMAND (STRING cat) (STRING file) time) (COMMAND (STRING grep) (STRING search))) |
109 |
|
110 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
111 |
index 2ee887c..25bfcb2 100644 |
112 |
--- a/bashast/libbashWalker.g |
113 |
+++ b/bashast/libbashWalker.g |
114 |
@@ -602,11 +602,6 @@ execute_command[const std::string& name, std::vector<std::string>& libbash_args] |
115 |
{ |
116 |
walker->set_status(walker->execute_builtin(name, libbash_args, out.get(), err.get(), in.get())); |
117 |
} |
118 |
- else if(name == "export") |
119 |
- { |
120 |
- std::cerr << "We do not support command env before the export builtin." << std::endl; |
121 |
- walker->set_status(1); |
122 |
- } |
123 |
else |
124 |
{ |
125 |
walker->set_status(1); |
126 |
|
127 |
diff --git a/src/builtins/export_builtin.cpp b/src/builtins/export_builtin.cpp |
128 |
new file mode 100644 |
129 |
index 0000000..2086cea |
130 |
--- /dev/null |
131 |
+++ b/src/builtins/export_builtin.cpp |
132 |
@@ -0,0 +1,46 @@ |
133 |
+/* |
134 |
+ Please use git log for copyright holder and year information |
135 |
+ |
136 |
+ This file is part of libbash. |
137 |
+ |
138 |
+ libbash is free software: you can redistribute it and/or modify |
139 |
+ it under the terms of the GNU General Public License as published by |
140 |
+ the Free Software Foundation, either version 2 of the License, or |
141 |
+ (at your option) any later version. |
142 |
+ |
143 |
+ libbash is distributed in the hope that it will be useful, |
144 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
145 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
146 |
+ GNU General Public License for more details. |
147 |
+ |
148 |
+ You should have received a copy of the GNU General Public License |
149 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
150 |
+*/ |
151 |
+/// |
152 |
+/// \file export_builtin.h |
153 |
+/// \brief class that implements the export builtin |
154 |
+/// |
155 |
+ |
156 |
+#include "builtins/export_builtin.h" |
157 |
+ |
158 |
+#include <sstream> |
159 |
+ |
160 |
+#include "core/bash_ast.h" |
161 |
+#include "core/interpreter.h" |
162 |
+ |
163 |
+int export_builtin::exec(const std::vector<std::string>& bash_args) |
164 |
+{ |
165 |
+ std::stringstream script; |
166 |
+ for(auto iter = bash_args.begin(); iter != bash_args.end(); ++iter) |
167 |
+ script << *iter; |
168 |
+ |
169 |
+ // Check if there is variable definition. If there isn't, parser_builtin_variable_definitions |
170 |
+ // will generate empty AST, which is not what we want. |
171 |
+ if(script.str().find("=") != std::string::npos) |
172 |
+ { |
173 |
+ bash_ast ast(script, &bash_ast::parser_builtin_variable_definitions); |
174 |
+ ast.interpret_with(_walker); |
175 |
+ } |
176 |
+ |
177 |
+ return 0; |
178 |
+} |
179 |
|
180 |
diff --git a/src/builtins/export_builtin.h b/src/builtins/export_builtin.h |
181 |
new file mode 100644 |
182 |
index 0000000..9ed5d70 |
183 |
--- /dev/null |
184 |
+++ b/src/builtins/export_builtin.h |
185 |
@@ -0,0 +1,46 @@ |
186 |
+/* |
187 |
+ Please use git log for copyright holder and year information |
188 |
+ |
189 |
+ This file is part of libbash. |
190 |
+ |
191 |
+ libbash is free software: you can redistribute it and/or modify |
192 |
+ it under the terms of the GNU General Public License as published by |
193 |
+ the Free Software Foundation, either version 2 of the License, or |
194 |
+ (at your option) any later version. |
195 |
+ |
196 |
+ libbash is distributed in the hope that it will be useful, |
197 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
198 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
199 |
+ GNU General Public License for more details. |
200 |
+ |
201 |
+ You should have received a copy of the GNU General Public License |
202 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
203 |
+*/ |
204 |
+/// |
205 |
+/// \file export_builtin.h |
206 |
+/// \brief class that implements the export builtin |
207 |
+/// |
208 |
+ |
209 |
+#ifndef LIBBASH_BUILTINS_export_BUILTIN_H_ |
210 |
+#define LIBBASH_BUILTINS_export_BUILTIN_H_ |
211 |
+ |
212 |
+#include "cppbash_builtin.h" |
213 |
+ |
214 |
+/// |
215 |
+/// \class export_builtin |
216 |
+/// \brief the export builtin for bash |
217 |
+/// |
218 |
+class export_builtin: public virtual cppbash_builtin |
219 |
+{ |
220 |
+ public: |
221 |
+ BUILTIN_CONSTRUCTOR(export) |
222 |
+ |
223 |
+ /// |
224 |
+ /// \brief runs the export builtin on the supplied arguments |
225 |
+ /// \param bash_args the arguments to the export builtin |
226 |
+ /// \return exit status of export |
227 |
+ /// |
228 |
+ virtual int exec(const std::vector<std::string>& bash_args); |
229 |
+}; |
230 |
+ |
231 |
+#endif |
232 |
|
233 |
diff --git a/src/core/bash_ast.cpp b/src/core/bash_ast.cpp |
234 |
index f3afc30..5b2ac68 100644 |
235 |
--- a/src/core/bash_ast.cpp |
236 |
+++ b/src/core/bash_ast.cpp |
237 |
@@ -237,6 +237,11 @@ pANTLR3_BASE_TREE bash_ast::parser_all_expansions(libbashParser_Ctx_struct* pars |
238 |
return parser->all_expansions(parser).tree; |
239 |
} |
240 |
|
241 |
+pANTLR3_BASE_TREE bash_ast::parser_builtin_variable_definitions(libbashParser_Ctx_struct* parser) |
242 |
+{ |
243 |
+ return parser->builtin_variable_definitions(parser).tree; |
244 |
+} |
245 |
+ |
246 |
void bash_ast::call_function(plibbashWalker ctx, |
247 |
ANTLR3_MARKER index) |
248 |
{ |
249 |
|
250 |
diff --git a/src/core/bash_ast.h b/src/core/bash_ast.h |
251 |
index fcf031c..bd7ecf8 100644 |
252 |
--- a/src/core/bash_ast.h |
253 |
+++ b/src/core/bash_ast.h |
254 |
@@ -115,6 +115,10 @@ public: |
255 |
/// \param parser the pointer to the parser |
256 |
static pANTLR3_BASE_TREE parser_all_expansions(libbashParser_Ctx_struct* parser); |
257 |
|
258 |
+ /// \brief the functor for parser builtin_variable_definitions rule |
259 |
+ /// \param parser the pointer to the parser |
260 |
+ static pANTLR3_BASE_TREE parser_builtin_variable_definitions(libbashParser_Ctx_struct* parser); |
261 |
+ |
262 |
/// |
263 |
/// \brief interpret the script with a given interpreter |
264 |
/// \param walker the interpreter object |
265 |
|
266 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
267 |
index c8eb5b9..03620d1 100644 |
268 |
--- a/src/cppbash_builtin.cpp |
269 |
+++ b/src/cppbash_builtin.cpp |
270 |
@@ -34,6 +34,7 @@ |
271 |
#include "builtins/declare_builtin.h" |
272 |
#include "builtins/echo_builtin.h" |
273 |
#include "builtins/eval_builtin.h" |
274 |
+#include "builtins/export_builtin.h" |
275 |
#include "builtins/inherit_builtin.h" |
276 |
#include "builtins/let_builtin.h" |
277 |
#include "builtins/return_builtin.h" |
278 |
@@ -57,6 +58,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() { |
279 |
{"continue", boost::factory<continue_builtin*>()}, |
280 |
{"echo", boost::factory<echo_builtin*>()}, |
281 |
{"eval", boost::factory<eval_builtin*>()}, |
282 |
+ {"export", boost::factory<export_builtin*>()}, |
283 |
{"declare", boost::factory<declare_builtin*>()}, |
284 |
{"source", boost::factory<source_builtin*>()}, |
285 |
{"shift", boost::factory<shift_builtin*>()}, |