1 |
commit: 5619617d2f23057f07fee8766998d8e01bc1a644 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Jun 1 14:55:14 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Jun 3 12:53:53 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=5619617d |
7 |
|
8 |
Walker: support output redirection |
9 |
|
10 |
We only support redirections for bash built-ins for now. |
11 |
|
12 |
--- |
13 |
bashast/libbashWalker.g | 49 +++++++++++++++++++++++++++------ |
14 |
scripts/command_execution.bash | 2 +- |
15 |
scripts/command_execution.bash.result | 1 - |
16 |
src/core/interpreter.h | 14 +++++++-- |
17 |
4 files changed, 52 insertions(+), 14 deletions(-) |
18 |
|
19 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
20 |
index 58de4b7..1e6f94b 100644 |
21 |
--- a/bashast/libbashWalker.g |
22 |
+++ b/bashast/libbashWalker.g |
23 |
@@ -40,6 +40,7 @@ options |
24 |
|
25 |
@postinclude{ |
26 |
|
27 |
+ #include <fstream> |
28 |
#include <iostream> |
29 |
#include <sstream> |
30 |
|
31 |
@@ -466,10 +467,16 @@ simple_command |
32 |
execute_command[const std::string& name, std::vector<std::string>& libbash_args] |
33 |
@declarations { |
34 |
interpreter::local_scope current_scope(*walker); |
35 |
+ std::unique_ptr<std::ostream> out; |
36 |
+ std::unique_ptr<std::ostream> err; |
37 |
+ std::unique_ptr<std::istream> in; |
38 |
+ bool redirection = false; |
39 |
} |
40 |
- :var_def[true]* redirect* { |
41 |
+ :var_def[true]* (redirect[out, err, in]{ redirection = true; })* { |
42 |
if(walker->has_function(name)) |
43 |
{ |
44 |
+ if(redirection) |
45 |
+ std::cerr << "We do not support redirection for function calls." << std::endl; |
46 |
ANTLR3_MARKER command_index = INDEX(); |
47 |
try |
48 |
{ |
49 |
@@ -485,7 +492,7 @@ execute_command[const std::string& name, std::vector<std::string>& libbash_args] |
50 |
} |
51 |
else if(cppbash_builtin::is_builtin(name)) |
52 |
{ |
53 |
- walker->set_status(walker->execute_builtin(name, libbash_args)); |
54 |
+ walker->set_status(walker->execute_builtin(name, libbash_args, out.get(), err.get(), in.get())); |
55 |
} |
56 |
else if(name == "export") |
57 |
{ |
58 |
@@ -500,21 +507,45 @@ execute_command[const std::string& name, std::vector<std::string>& libbash_args] |
59 |
} |
60 |
(BANG { walker->set_status(!walker->get_status()); })?; |
61 |
|
62 |
-redirect |
63 |
- :^(REDIR redirect_operator redirect_destination) { |
64 |
+redirect[std::unique_ptr<std::ostream>& out, std::unique_ptr<std::ostream>& err, std::unique_ptr<std::istream>& in] |
65 |
+ :^(REDIR LESS_THAN redirect_destination_input[in]) { |
66 |
+ std::cerr << "Redirection is not supported yet" << std::endl; |
67 |
+ } |
68 |
+ |^(REDIR GREATER_THAN redirect_destination_output[out]) |
69 |
+ |^(REDIR DIGIT LESS_THAN redirect_destination_input[in]) { |
70 |
+ std::cerr << "Redirection is not supported yet" << std::endl; |
71 |
+ } |
72 |
+ |^(REDIR DIGIT GREATER_THAN redirect_destination_output[out]) { |
73 |
std::cerr << "Redirection is not supported yet" << std::endl; |
74 |
}; |
75 |
|
76 |
-redirect_destination |
77 |
- :string_expr //path to a file |
78 |
- |FILE_DESCRIPTOR DIGIT |
79 |
- |FILE_DESCRIPTOR_MOVE DIGIT; |
80 |
- |
81 |
redirect_operator |
82 |
:LESS_THAN |
83 |
|GREATER_THAN |
84 |
|FILE_DESCRIPTOR DIGIT redirect_operator; |
85 |
|
86 |
+redirect_destination_output[std::unique_ptr<std::ostream>& out] |
87 |
+ :string_expr { |
88 |
+ out.reset(new std::ofstream($string_expr.libbash_value, std::ofstream::trunc)); |
89 |
+ } |
90 |
+ |FILE_DESCRIPTOR DIGIT { |
91 |
+ std::cerr << "FILE_DESCRIPTOR redirection is not supported yet" << std::endl; |
92 |
+ } |
93 |
+ |FILE_DESCRIPTOR_MOVE DIGIT { |
94 |
+ std::cerr << "FILE_DESCRIPTOR_MOVE redirection is not supported yet" << std::endl; |
95 |
+ }; |
96 |
+ |
97 |
+redirect_destination_input[std::unique_ptr<std::istream>& in] |
98 |
+ :string_expr { |
99 |
+ std::cerr << "Input redirection for file is not supported yet" << std::endl; |
100 |
+ } |
101 |
+ |FILE_DESCRIPTOR DIGIT { |
102 |
+ std::cerr << "FILE_DESCRIPTOR redirection is not supported yet" << std::endl; |
103 |
+ } |
104 |
+ |FILE_DESCRIPTOR_MOVE DIGIT { |
105 |
+ std::cerr << "FILE_DESCRIPTOR_MOVE redirection is not supported yet" << std::endl; |
106 |
+ }; |
107 |
+ |
108 |
argument[std::vector<std::string>& args] |
109 |
: string_expr { |
110 |
if($string_expr.quoted) |
111 |
|
112 |
diff --git a/scripts/command_execution.bash b/scripts/command_execution.bash |
113 |
index 8005a23..a500b63 100644 |
114 |
--- a/scripts/command_execution.bash |
115 |
+++ b/scripts/command_execution.bash |
116 |
@@ -19,7 +19,7 @@ FOO="abc" echo "command environment" |
117 |
export FOO003=1 FOO004=abc FOO005=(1 2 3) FOO002 |
118 |
export foo |
119 |
abc=1 export foo |
120 |
-true > /dev/null |
121 |
+echo "hi" > /dev/null |
122 |
|
123 |
function unset_inner() |
124 |
{ |
125 |
|
126 |
diff --git a/scripts/command_execution.bash.result b/scripts/command_execution.bash.result |
127 |
index d47650d..e0bf724 100644 |
128 |
--- a/scripts/command_execution.bash.result |
129 |
+++ b/scripts/command_execution.bash.result |
130 |
@@ -5,7 +5,6 @@ right |
131 |
end |
132 |
command environment |
133 |
We do not support command env before the export builtin. |
134 |
-Redirection is not supported yet |
135 |
FOO006=1 in unset_outer |
136 |
FOO007= in unset_outer |
137 |
FOO006= in unset_outer |
138 |
|
139 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
140 |
index 3420ee4..2c5f383 100644 |
141 |
--- a/src/core/interpreter.h |
142 |
+++ b/src/core/interpreter.h |
143 |
@@ -574,9 +574,17 @@ public: |
144 |
/// \param builtin arguments |
145 |
/// \return the return value of the builtin |
146 |
int execute_builtin(const std::string& name, |
147 |
- const std::vector<std::string>& args) |
148 |
- { |
149 |
- return cppbash_builtin::exec(name, args, *out, *err, *in, *this); |
150 |
+ const std::vector<std::string>& args, |
151 |
+ std::ostream* output=0, |
152 |
+ std::ostream* error=0, |
153 |
+ std::istream* input=0) |
154 |
+ { |
155 |
+ return cppbash_builtin::exec(name, |
156 |
+ args, |
157 |
+ output == 0 ? *out : *output, |
158 |
+ error == 0 ? *err : *error, |
159 |
+ input == 0 ? *in : *input, |
160 |
+ *this); |
161 |
} |
162 |
|
163 |
/// \brief perform ${parameter:−word} expansion |