1 |
commit: 059d8f2c5263bd3e82406d2890f063782e7ca76b |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Apr 21 11:02:46 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Apr 28 02:52:42 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=059d8f2c |
7 |
|
8 |
Builtin: add interpreter object as a protected member |
9 |
|
10 |
Many builtins use the internal interpreter class so we add it to |
11 |
the cppbash_builtin class. |
12 |
|
13 |
--- |
14 |
src/builtins/tests/boolean_tests.cpp | 11 ++++++++--- |
15 |
src/builtins/tests/echo_tests.cpp | 8 ++++++-- |
16 |
src/core/interpreter.h | 5 +++-- |
17 |
src/cppbash_builtin.cpp | 2 +- |
18 |
src/cppbash_builtin.h | 19 +++++++++++++------ |
19 |
5 files changed, 31 insertions(+), 14 deletions(-) |
20 |
|
21 |
diff --git a/src/builtins/tests/boolean_tests.cpp b/src/builtins/tests/boolean_tests.cpp |
22 |
index 5ba3d34..4c9616b 100644 |
23 |
--- a/src/builtins/tests/boolean_tests.cpp |
24 |
+++ b/src/builtins/tests/boolean_tests.cpp |
25 |
@@ -21,19 +21,24 @@ |
26 |
/// \brief series of unit tests for echo built in |
27 |
/// |
28 |
#include <iostream> |
29 |
-#include "../../cppbash_builtin.h" |
30 |
+ |
31 |
#include <gtest/gtest.h> |
32 |
|
33 |
+#include "core/interpreter.h" |
34 |
+#include "cppbash_builtin.h" |
35 |
+ |
36 |
using namespace std; |
37 |
|
38 |
TEST(boolean_builtin_test, true) |
39 |
{ |
40 |
- int result = cppbash_builtin::exec("true", {}, std::cout, std::cerr, std::cin); |
41 |
+ interpreter walker; |
42 |
+ int result = cppbash_builtin::exec("true", {}, std::cout, std::cerr, std::cin, walker); |
43 |
ASSERT_EQ(0, result); |
44 |
} |
45 |
|
46 |
TEST(boolean_builtin_test, false) |
47 |
{ |
48 |
- int result = cppbash_builtin::exec("false", {}, std::cout, std::cerr, std::cin); |
49 |
+ interpreter walker; |
50 |
+ int result = cppbash_builtin::exec("false", {}, std::cout, std::cerr, std::cin, walker); |
51 |
ASSERT_EQ(1, result); |
52 |
} |
53 |
|
54 |
diff --git a/src/builtins/tests/echo_tests.cpp b/src/builtins/tests/echo_tests.cpp |
55 |
index 2a606d4..bee9be5 100644 |
56 |
--- a/src/builtins/tests/echo_tests.cpp |
57 |
+++ b/src/builtins/tests/echo_tests.cpp |
58 |
@@ -22,17 +22,21 @@ |
59 |
/// \author Nathan Eloe |
60 |
/// |
61 |
#include <iostream> |
62 |
-#include "../../cppbash_builtin.h" |
63 |
#include <sstream> |
64 |
#include <vector> |
65 |
+ |
66 |
#include <gtest/gtest.h> |
67 |
|
68 |
+#include "core/interpreter.h" |
69 |
+#include "cppbash_builtin.h" |
70 |
+ |
71 |
using namespace std; |
72 |
|
73 |
static void test_echo(const string& expected, std::initializer_list<string> args) |
74 |
{ |
75 |
stringstream test_output; |
76 |
- cppbash_builtin::exec("echo",args,test_output,cerr,cin); |
77 |
+ interpreter walker; |
78 |
+ cppbash_builtin::exec("echo",args,test_output,cerr,cin,walker); |
79 |
ASSERT_EQ(expected, test_output.str()); |
80 |
} |
81 |
|
82 |
|
83 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
84 |
index f2dfbeb..e471b94 100644 |
85 |
--- a/src/core/interpreter.h |
86 |
+++ b/src/core/interpreter.h |
87 |
@@ -46,7 +46,8 @@ typedef struct libbashWalker_Ctx_struct * plibbashWalker; |
88 |
/// \class interpreter |
89 |
/// \brief implementation for bash interpreter |
90 |
/// |
91 |
-class interpreter{ |
92 |
+class interpreter |
93 |
+{ |
94 |
|
95 |
/// \var private::members |
96 |
/// \brief global symbol table for variables |
97 |
@@ -547,7 +548,7 @@ public: |
98 |
int execute_builtin(const std::string& name, |
99 |
const std::vector<std::string>& args) |
100 |
{ |
101 |
- return cppbash_builtin::exec(name, args, *out, *err, *in); |
102 |
+ return cppbash_builtin::exec(name, args, *out, *err, *in, *this); |
103 |
} |
104 |
|
105 |
/// \brief perform ${parameter:−word} expansion |
106 |
|
107 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
108 |
index 8778173..38a6111 100644 |
109 |
--- a/src/cppbash_builtin.cpp |
110 |
+++ b/src/cppbash_builtin.cpp |
111 |
@@ -26,7 +26,7 @@ |
112 |
#include "builtins/echo_builtin.h" |
113 |
#include "builtins/boolean_builtins.h" |
114 |
|
115 |
-cppbash_builtin::cppbash_builtin(std::ostream &outstream, std::ostream &errstream, std::istream &instream): _out_stream(&outstream), _err_stream(&errstream), _inp_stream(&instream) |
116 |
+cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker) |
117 |
{ |
118 |
} |
119 |
|
120 |
|
121 |
diff --git a/src/cppbash_builtin.h b/src/cppbash_builtin.h |
122 |
index 3bd3c46..e7b68ea 100644 |
123 |
--- a/src/cppbash_builtin.h |
124 |
+++ b/src/cppbash_builtin.h |
125 |
@@ -34,8 +34,9 @@ |
126 |
#include <boost/function.hpp> |
127 |
#include <boost/scoped_ptr.hpp> |
128 |
|
129 |
-#define STREAM_ARGS std::ostream &out, std::ostream &err, std::istream &in |
130 |
+#define BUILTIN_ARGS std::ostream &out, std::ostream &err, std::istream &in, interpreter &walker |
131 |
|
132 |
+class interpreter; |
133 |
/// |
134 |
/// \class cppbash_builtin |
135 |
/// \brief a virtual class to inherit builtin functions from |
136 |
@@ -49,7 +50,7 @@ class cppbash_builtin |
137 |
/// \param errstream where to send standard error. Default: cerr |
138 |
/// \param instream where to get standard input from. Default: stdin |
139 |
/// |
140 |
- explicit cppbash_builtin(STREAM_ARGS); |
141 |
+ explicit cppbash_builtin(BUILTIN_ARGS); |
142 |
/// prevent copying |
143 |
cppbash_builtin(const cppbash_builtin& ) = delete; |
144 |
const cppbash_builtin& operator=( const cppbash_builtin& ) = delete; |
145 |
@@ -77,9 +78,11 @@ class cppbash_builtin |
146 |
/// |
147 |
std::istream& input_buffer() {return *_inp_stream;} |
148 |
|
149 |
- static int exec(const std::string& builtin, const std::vector<std::string>& args, STREAM_ARGS) |
150 |
+ static int exec(const std::string& builtin, |
151 |
+ const std::vector<std::string>& args, |
152 |
+ BUILTIN_ARGS) |
153 |
{ |
154 |
- boost::scoped_ptr<cppbash_builtin> p(builtins()[builtin](out,err,in)); |
155 |
+ boost::scoped_ptr<cppbash_builtin> p(builtins()[builtin](out,err,in,walker)); |
156 |
return p->exec(args); |
157 |
} |
158 |
|
159 |
@@ -110,15 +113,19 @@ class cppbash_builtin |
160 |
/// \brief current standard input stream |
161 |
/// |
162 |
std::istream *_inp_stream; |
163 |
+ |
164 |
+ interpreter& _walker; |
165 |
+ |
166 |
/// |
167 |
/// \var builtins |
168 |
/// \brief holds factories for creating instances of child classes |
169 |
/// |
170 |
- typedef std::map<std::string, boost::function< cppbash_builtin*(STREAM_ARGS) >> builtins_type; |
171 |
+ typedef std::map<std::string, boost::function< cppbash_builtin*(BUILTIN_ARGS) >> builtins_type; |
172 |
static builtins_type& builtins(); |
173 |
+ |
174 |
}; |
175 |
|
176 |
#define BUILTIN_CONSTRUCTOR(name) \ |
177 |
- name ## _builtin(STREAM_ARGS) : cppbash_builtin(out, err, in) {} |
178 |
+ name ## _builtin(BUILTIN_ARGS) : cppbash_builtin(out, err, in, walker) {} |
179 |
|
180 |
#endif |