1 |
commit: afe72fecbc6371c6d4cc2c784738459b5281db09 |
2 |
Author: André Aparício <aparicio99 <AT> gmail <DOT> com> |
3 |
AuthorDate: Tue Jun 19 17:36:58 2012 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Jul 20 01:20:22 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=afe72fec |
7 |
|
8 |
Builtin: Support more options for declare |
9 |
|
10 |
--- |
11 |
src/builtins/declare_builtin.cpp | 13 ++++++++++++- |
12 |
src/builtins/tests/declare_tests.cpp | 23 +++++++++++++++++++---- |
13 |
src/core/interpreter.h | 8 ++++++++ |
14 |
3 files changed, 39 insertions(+), 5 deletions(-) |
15 |
|
16 |
diff --git a/src/builtins/declare_builtin.cpp b/src/builtins/declare_builtin.cpp |
17 |
index 81877b5..34a78cd 100644 |
18 |
--- a/src/builtins/declare_builtin.cpp |
19 |
+++ b/src/builtins/declare_builtin.cpp |
20 |
@@ -41,6 +41,8 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args) |
21 |
} |
22 |
|
23 |
int result = 0; |
24 |
+ bool jump_option = false; |
25 |
+ bool option_global = false; |
26 |
|
27 |
std::vector<std::string> tokens; |
28 |
boost::split(tokens, bash_args[0], boost::is_any_of(" ")); |
29 |
@@ -50,6 +52,7 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args) |
30 |
if(tokens[0].size() > 2) |
31 |
throw libbash::unsupported_exception("declare: " + tokens[0] + " is not supported yet"); |
32 |
|
33 |
+ jump_option = true; |
34 |
switch(tokens[0][1]) |
35 |
{ |
36 |
case 'F': |
37 |
@@ -100,6 +103,10 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args) |
38 |
return result; |
39 |
case 'a': |
40 |
case 'i': |
41 |
+ break; |
42 |
+ case 'g': |
43 |
+ option_global = true; |
44 |
+ break; |
45 |
case 'A': |
46 |
case 'f': |
47 |
case 'l': |
48 |
@@ -119,7 +126,11 @@ int declare_builtin::exec(const std::vector<std::string>& bash_args) |
49 |
for(auto iter = bash_args.begin(); iter != bash_args.end(); ++iter) |
50 |
script << *iter + " "; |
51 |
|
52 |
- bash_ast ast(script, std::bind(&bash_ast::parser_builtin_variable_definitions, std::placeholders::_1, false)); |
53 |
+ if(jump_option) |
54 |
+ script.ignore(static_cast<std::streamsize>(tokens[0].size())); |
55 |
+ |
56 |
+ bool local = _walker.is_local_scope() && !option_global; |
57 |
+ bash_ast ast(script, std::bind(&bash_ast::parser_builtin_variable_definitions, std::placeholders::_1, local)); |
58 |
ast.interpret_with(_walker); |
59 |
|
60 |
return result; |
61 |
|
62 |
diff --git a/src/builtins/tests/declare_tests.cpp b/src/builtins/tests/declare_tests.cpp |
63 |
index eec5e41..1b83f7d 100644 |
64 |
--- a/src/builtins/tests/declare_tests.cpp |
65 |
+++ b/src/builtins/tests/declare_tests.cpp |
66 |
@@ -119,22 +119,37 @@ TEST(declare_built_test, _p) |
67 |
EXPECT_EQ("-bash: declare: bar: not found\n-bash: declare: test: not found\n", test_output2.str()); |
68 |
} |
69 |
|
70 |
+TEST(declare_built_test, _a) |
71 |
+{ |
72 |
+ interpreter walker; |
73 |
+ EXPECT_EQ(0, cppbash_builtin::exec("declare", {"-a", "foo"}, std::cout, cerr, cin, walker)); |
74 |
+ walker.set_value("foo", "bar", 3); |
75 |
+ EXPECT_STREQ("bar", walker.resolve<std::string>("foo", 3).c_str()); |
76 |
+} |
77 |
+ |
78 |
+TEST(declare_built_test, _g) |
79 |
+{ |
80 |
+ stringstream expression("function func() { declare -g var1=foo; declare var2=bar; }; func;"); |
81 |
+ interpreter walker; |
82 |
+ bash_ast ast(expression); |
83 |
+ ast.interpret_with(walker); |
84 |
+ |
85 |
+ EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str()); |
86 |
+ EXPECT_STREQ("", walker.resolve<std::string>("var2").c_str()); |
87 |
+} |
88 |
+ |
89 |
#define TEST_DECLARE(name, expected, ...) \ |
90 |
TEST(declare_builtin_test, name) { test_declare<libbash::unsupported_exception>(expected, {__VA_ARGS__}); } |
91 |
|
92 |
-TEST_DECLARE(_a, "declare -a is not supported yet", "-a", "world") |
93 |
TEST_DECLARE(_A, "declare -A is not supported yet", "-A", "world") |
94 |
TEST_DECLARE(_f, "declare -f is not supported yet", "-f", "world") |
95 |
-TEST_DECLARE(_i, "declare -i is not supported yet", "-i", "world") |
96 |
TEST_DECLARE(_l, "declare -l is not supported yet", "-l", "world") |
97 |
TEST_DECLARE(_r, "declare -r is not supported yet", "-r", "world") |
98 |
TEST_DECLARE(_t, "declare -t is not supported yet", "-t", "world") |
99 |
TEST_DECLARE(_u, "declare -u is not supported yet", "-u", "world") |
100 |
TEST_DECLARE(_x, "declare -x is not supported yet", "-x", "world") |
101 |
-TEST_DECLARE(pa, "declare +a is not supported yet", "+a", "world") |
102 |
TEST_DECLARE(pA, "declare +A is not supported yet", "+A", "world") |
103 |
TEST_DECLARE(pf, "declare +f is not supported yet", "+f", "world") |
104 |
-TEST_DECLARE(pi, "declare +i is not supported yet", "+i", "world") |
105 |
TEST_DECLARE(pl, "declare +l is not supported yet", "+l", "world") |
106 |
TEST_DECLARE(pr, "declare +r is not supported yet", "+r", "world") |
107 |
TEST_DECLARE(pt, "declare +t is not supported yet", "+t", "world") |
108 |
|
109 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
110 |
index 3f77f5d..2f6854b 100644 |
111 |
--- a/src/core/interpreter.h |
112 |
+++ b/src/core/interpreter.h |
113 |
@@ -176,6 +176,14 @@ public: |
114 |
return members.end(); |
115 |
} |
116 |
|
117 |
+ /// |
118 |
+ /// \brief checks whether the current scope is local or global |
119 |
+ /// \return whether current scope is local |
120 |
+ bool is_local_scope() const |
121 |
+ { |
122 |
+ return local_members.size() > 0; |
123 |
+ } |
124 |
+ |
125 |
/// \brief set current output stream |
126 |
/// \param stream the pointer to the output stream |
127 |
void set_output_stream(std::ostream* stream) |