1 |
commit: fed16223f8539afc0a15fdd3496f625bc3f0b821 |
2 |
Author: André Aparício <aparicio99 <AT> gmail <DOT> com> |
3 |
AuthorDate: Fri Apr 6 21:08:10 2012 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Jul 3 01:16:10 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=fed16223 |
7 |
|
8 |
Builtin: implement read builtin |
9 |
|
10 |
--- |
11 |
Makefile.am | 3 + |
12 |
src/builtins/read_builtin.cpp | 94 +++++++++++++++++++++++++++++++++++++ |
13 |
src/builtins/read_builtin.h | 54 +++++++++++++++++++++ |
14 |
src/builtins/tests/read_tests.cpp | 76 ++++++++++++++++++++++++++++++ |
15 |
src/cppbash_builtin.cpp | 2 + |
16 |
5 files changed, 229 insertions(+), 0 deletions(-) |
17 |
|
18 |
diff --git a/Makefile.am b/Makefile.am |
19 |
index cbc9f3b..ca61091 100644 |
20 |
--- a/Makefile.am |
21 |
+++ b/Makefile.am |
22 |
@@ -105,6 +105,7 @@ cppunittests_SOURCES = test/run_tests.cpp \ |
23 |
src/builtins/tests/shift_tests.cpp \ |
24 |
src/builtins/tests/shopt_tests.cpp \ |
25 |
src/builtins/tests/return_tests.cpp \ |
26 |
+ src/builtins/tests/read_tests.cpp \ |
27 |
src/builtins/tests/printf_tests.cpp \ |
28 |
test/test.h \ |
29 |
test/test.cpp \ |
30 |
@@ -236,6 +237,8 @@ libbash_la_SOURCES = include/common.h \ |
31 |
src/builtins/inherit_builtin.cpp \ |
32 |
src/builtins/unset_builtin.h \ |
33 |
src/builtins/unset_builtin.cpp \ |
34 |
+ src/builtins/read_builtin.h \ |
35 |
+ src/builtins/read_builtin.cpp \ |
36 |
src/builtins/builtin_exceptions.h \ |
37 |
$(GENERATED_PARSER_C) \ |
38 |
$(GENERATED_PARSER_H) \ |
39 |
|
40 |
diff --git a/src/builtins/read_builtin.cpp b/src/builtins/read_builtin.cpp |
41 |
new file mode 100644 |
42 |
index 0000000..ca79094 |
43 |
--- /dev/null |
44 |
+++ b/src/builtins/read_builtin.cpp |
45 |
@@ -0,0 +1,94 @@ |
46 |
+/* |
47 |
+ Please use git log for copyright holder and year information |
48 |
+ |
49 |
+ This file is part of libbash. |
50 |
+ |
51 |
+ libbash is free software: you can redistribute it and/or modify |
52 |
+ it under the terms of the GNU General Public License as published by |
53 |
+ the Free Software Foundation, either version 2 of the License, or |
54 |
+ (at your option) any later version. |
55 |
+ |
56 |
+ libbash is distributed in the hope that it will be useful, |
57 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
58 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
59 |
+ GNU General Public License for more details. |
60 |
+ |
61 |
+ You should have received a copy of the GNU General Public License |
62 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
63 |
+*/ |
64 |
+/// |
65 |
+/// \file read_builtin.cpp |
66 |
+/// \brief class that implements the read builtin |
67 |
+/// |
68 |
+ |
69 |
+#include "builtins/read_builtin.h" |
70 |
+ |
71 |
+#include <string.h> |
72 |
+#include <boost/algorithm/string.hpp> |
73 |
+ |
74 |
+#include "core/interpreter.h" |
75 |
+#include "builtins/builtin_exceptions.h" |
76 |
+ |
77 |
+void read_builtin::process(const std::vector<std::string>& args, const std::string& input) |
78 |
+{ |
79 |
+ std::vector<std::string> split_input; |
80 |
+ boost::split(split_input, input, boost::is_any_of(" ")); |
81 |
+ |
82 |
+ auto vars = args.begin(); |
83 |
+ for(auto words = split_input.begin(); vars != args.end() && words != split_input.end(); ++vars, ++words) |
84 |
+ { |
85 |
+ if(vars != args.end() - 1) |
86 |
+ { |
87 |
+ _walker.set_value(*vars, *words); |
88 |
+ } |
89 |
+ else |
90 |
+ { |
91 |
+ std::string rest; |
92 |
+ for(; words != split_input.end() - 1; ++words) |
93 |
+ rest += *words + " "; |
94 |
+ rest += *words; |
95 |
+ _walker.set_value(*vars, rest); |
96 |
+ } |
97 |
+ } |
98 |
+ |
99 |
+ for(; vars != args.end(); ++vars) |
100 |
+ _walker.set_value(*vars, ""); |
101 |
+} |
102 |
+ |
103 |
+int read_builtin::exec(const std::vector<std::string>& bash_args) |
104 |
+{ |
105 |
+ int return_value = 0; |
106 |
+ std::string input; |
107 |
+ std::stringstream formated_input; |
108 |
+ |
109 |
+ getline(this->input_buffer(), input); |
110 |
+ |
111 |
+ if(this->input_buffer().eof()) |
112 |
+ return_value = 1; |
113 |
+ |
114 |
+ if(input.size() < 1) |
115 |
+ return return_value; |
116 |
+ |
117 |
+ while(input[input.length()-1] == '\\') { |
118 |
+ input.erase(input.end()-1); |
119 |
+ std::string input_line; |
120 |
+ getline(this->input_buffer(), input_line); |
121 |
+ |
122 |
+ if(this->input_buffer().eof()) |
123 |
+ return_value = 1; |
124 |
+ |
125 |
+ if(input.size() < 1) |
126 |
+ return return_value; |
127 |
+ |
128 |
+ input += input_line; |
129 |
+ } |
130 |
+ |
131 |
+ cppbash_builtin::transform_escapes(input, formated_input, false); |
132 |
+ |
133 |
+ if(bash_args.empty()) |
134 |
+ process({"REPLY"}, formated_input.str()); |
135 |
+ else |
136 |
+ process(bash_args, formated_input.str()); |
137 |
+ |
138 |
+ return return_value; |
139 |
+} |
140 |
|
141 |
diff --git a/src/builtins/read_builtin.h b/src/builtins/read_builtin.h |
142 |
new file mode 100644 |
143 |
index 0000000..caf8e86 |
144 |
--- /dev/null |
145 |
+++ b/src/builtins/read_builtin.h |
146 |
@@ -0,0 +1,54 @@ |
147 |
+/* |
148 |
+ Please use git log for copyright holder and year information |
149 |
+ |
150 |
+ This file is part of libbash. |
151 |
+ |
152 |
+ libbash is free software: you can redistribute it and/or modify |
153 |
+ it under the terms of the GNU General Public License as published by |
154 |
+ the Free Software Foundation, either version 2 of the License, or |
155 |
+ (at your option) any later version. |
156 |
+ |
157 |
+ libbash is distributed in the hope that it will be useful, |
158 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
159 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
160 |
+ GNU General Public License for more details. |
161 |
+ |
162 |
+ You should have received a copy of the GNU General Public License |
163 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
164 |
+*/ |
165 |
+/// |
166 |
+/// \file read_builtin.h |
167 |
+/// \brief class that implements the read builtin |
168 |
+/// |
169 |
+ |
170 |
+#ifndef LIBBASH_BUILTINS_READ_BUILTIN_H_ |
171 |
+#define LIBBASH_BUILTINS_READ_BUILTIN_H_ |
172 |
+ |
173 |
+#include "../cppbash_builtin.h" |
174 |
+ |
175 |
+/// |
176 |
+/// \class read_builtin |
177 |
+/// \brief the read builtin for bash |
178 |
+/// |
179 |
+class read_builtin: public virtual cppbash_builtin |
180 |
+{ |
181 |
+ public: |
182 |
+ BUILTIN_CONSTRUCTOR(read) |
183 |
+ |
184 |
+ /// |
185 |
+ /// \brief runs the read builtin on the supplied arguments |
186 |
+ /// \param bash_args the arguments to the read builtin |
187 |
+ /// \return exit status of read |
188 |
+ /// |
189 |
+ virtual int exec(const std::vector<std::string>& bash_args); |
190 |
+ |
191 |
+ private: |
192 |
+ /// |
193 |
+ /// \brief assigns words from input to the variables |
194 |
+ /// \param args the variables to be used |
195 |
+ /// \param input the input to be assigned to the variables |
196 |
+ /// |
197 |
+ virtual void process(const std::vector<std::string>& bash_args, const std::string& input); |
198 |
+}; |
199 |
+ |
200 |
+#endif |
201 |
|
202 |
diff --git a/src/builtins/tests/read_tests.cpp b/src/builtins/tests/read_tests.cpp |
203 |
new file mode 100644 |
204 |
index 0000000..9bc9683 |
205 |
--- /dev/null |
206 |
+++ b/src/builtins/tests/read_tests.cpp |
207 |
@@ -0,0 +1,76 @@ |
208 |
+/* |
209 |
+ Please use git log for copyright holder and year information |
210 |
+ |
211 |
+ This file is part of libbash. |
212 |
+ |
213 |
+ libbash is free software: you can redistribute it and/or modify |
214 |
+ it under the terms of the GNU General Public License as published by |
215 |
+ the Free Software Foundation, either version 2 of the License, or |
216 |
+ (at your option) any later version. |
217 |
+ |
218 |
+ libbash is distributed in the hope that it will be useful, |
219 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
220 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
221 |
+ GNU General Public License for more details. |
222 |
+ |
223 |
+ You should have received a copy of the GNU General Public License |
224 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
225 |
+*/ |
226 |
+/// |
227 |
+/// \file read_tests.cpp |
228 |
+/// \brief series of unit tests for read builtin |
229 |
+/// |
230 |
+#include <iostream> |
231 |
+#include <sstream> |
232 |
+#include <vector> |
233 |
+ |
234 |
+#include <gtest/gtest.h> |
235 |
+ |
236 |
+#include "core/interpreter.h" |
237 |
+#include "cppbash_builtin.h" |
238 |
+ |
239 |
+using namespace std; |
240 |
+ |
241 |
+static void test_read(interpreter& walker, const string& input, std::initializer_list<string> args) |
242 |
+{ |
243 |
+ stringstream test_input; |
244 |
+ test_input << input; |
245 |
+ cppbash_builtin::exec("read", args, std::cout, cerr, test_input, walker); |
246 |
+} |
247 |
+ |
248 |
+TEST(read_builtin_test, argument_assignment) |
249 |
+{ |
250 |
+ interpreter walker; |
251 |
+ |
252 |
+ test_read(walker, "foo bar", {}); |
253 |
+ EXPECT_STREQ("foo bar", walker.resolve<std::string>("REPLY").c_str()); |
254 |
+ |
255 |
+ test_read(walker, "foo bar", {"var"}); |
256 |
+ EXPECT_STREQ("foo bar", walker.resolve<std::string>("var").c_str()); |
257 |
+ |
258 |
+ test_read(walker, "foo bar", {"var1", "var2"}); |
259 |
+ EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str()); |
260 |
+ EXPECT_STREQ("bar", walker.resolve<std::string>("var2").c_str()); |
261 |
+ |
262 |
+ test_read(walker, "1 2 3 4", {"var1", "var2"}); |
263 |
+ EXPECT_STREQ("1", walker.resolve<std::string>("var1").c_str()); |
264 |
+ EXPECT_STREQ("2 3 4", walker.resolve<std::string>("var2").c_str()); |
265 |
+ |
266 |
+ test_read(walker, "foo", {"var1", "var2"}); |
267 |
+ EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str()); |
268 |
+ EXPECT_STREQ("", walker.resolve<std::string>("var2").c_str()); |
269 |
+ |
270 |
+ test_read(walker, "foo bar", {"var"}); |
271 |
+ EXPECT_STREQ("foo bar", walker.resolve<std::string>("var").c_str()); |
272 |
+} |
273 |
+ |
274 |
+TEST(read_builtin_test, line_continuation) |
275 |
+{ |
276 |
+ interpreter walker; |
277 |
+ |
278 |
+ test_read(walker, "foo\\\nbar", {}); |
279 |
+ EXPECT_STREQ("foobar", walker.resolve<std::string>("REPLY").c_str()); |
280 |
+ |
281 |
+ test_read(walker, "foo \\\n bar", {}); |
282 |
+ EXPECT_STREQ("foo bar", walker.resolve<std::string>("REPLY").c_str()); |
283 |
+} |
284 |
|
285 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
286 |
index 452fe64..c93b94a 100644 |
287 |
--- a/src/cppbash_builtin.cpp |
288 |
+++ b/src/cppbash_builtin.cpp |
289 |
@@ -44,6 +44,7 @@ |
290 |
#include "builtins/shopt_builtin.h" |
291 |
#include "builtins/source_builtin.h" |
292 |
#include "builtins/unset_builtin.h" |
293 |
+#include "builtins/read_builtin.h" |
294 |
|
295 |
namespace qi = boost::spirit::qi; |
296 |
namespace karma = boost::spirit::karma; |
297 |
@@ -73,6 +74,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() { |
298 |
{"printf", boost::factory<printf_builtin*>()}, |
299 |
{"let", boost::factory<let_builtin*>()}, |
300 |
{"unset", boost::factory<unset_builtin*>()}, |
301 |
+ {"read", boost::factory<read_builtin*>()}, |
302 |
}); |
303 |
return *p; |
304 |
} |