1 |
commit: 545417a9c8286a81642a3750df4af7cd977a3be0 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu May 26 12:18:30 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri May 27 14:38:07 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=545417a9 |
7 |
|
8 |
Builtin: support unset built-in |
9 |
|
10 |
We do not support unsetting array indexes currently. In addition, |
11 |
read-only functions can be unset for now. Note that if no options are |
12 |
supplied, or the -v option is given, each name refers to a shell |
13 |
variable(This behavior is supported by bash 3.2, bash-4.1 tries to |
14 |
unset function if the name doesn't match a variable). |
15 |
|
16 |
--- |
17 |
Makefile.am | 2 + |
18 |
scripts/command_execution.bash | 24 +++++++++++++ |
19 |
scripts/command_execution.bash.result | 7 ++++ |
20 |
src/builtins/unset_builtin.cpp | 59 +++++++++++++++++++++++++++++++++ |
21 |
src/builtins/unset_builtin.h | 35 +++++++++++++++++++ |
22 |
src/cppbash_builtin.cpp | 2 + |
23 |
6 files changed, 129 insertions(+), 0 deletions(-) |
24 |
|
25 |
diff --git a/Makefile.am b/Makefile.am |
26 |
index 3b72d46..ebe12ee 100644 |
27 |
--- a/Makefile.am |
28 |
+++ b/Makefile.am |
29 |
@@ -189,6 +189,8 @@ libcppbash_la_SOURCES = src/common.h \ |
30 |
src/builtins/let_builtin.cpp \ |
31 |
src/builtins/inherit_builtin.h \ |
32 |
src/builtins/inherit_builtin.cpp \ |
33 |
+ src/builtins/unset_builtin.h \ |
34 |
+ src/builtins/unset_builtin.cpp \ |
35 |
src/builtins/builtin_exceptions.h \ |
36 |
$(GENERATED_PARSER_C) \ |
37 |
$(GENERATED_PARSER_H) \ |
38 |
|
39 |
diff --git a/scripts/command_execution.bash b/scripts/command_execution.bash |
40 |
index 95e1594..b1670f2 100644 |
41 |
--- a/scripts/command_execution.bash |
42 |
+++ b/scripts/command_execution.bash |
43 |
@@ -19,3 +19,27 @@ FOO="abc" echo "command environment" |
44 |
export FOO003=1 FOO004=abc FOO005=(1 2 3) FOO002 |
45 |
abc=1 export foo |
46 |
true > /dev/null |
47 |
+ |
48 |
+function unset_inner() |
49 |
+{ |
50 |
+ local FOO006=3 |
51 |
+ unset FOO006 FOO007 |
52 |
+} |
53 |
+function unset_outer() |
54 |
+{ |
55 |
+ local FOO006=1 FOO007=2 |
56 |
+ unset_inner |
57 |
+ echo "FOO006=$FOO006 in unset_outer" |
58 |
+ echo "FOO007=$FOO007 in unset_outer" |
59 |
+ unset FOO006 |
60 |
+ echo "FOO006=$FOO006 in unset_outer" |
61 |
+} |
62 |
+unset_outer |
63 |
+echo "FOO006=$FOO006 in global" |
64 |
+FOO006=0 |
65 |
+echo "FOO006=$FOO006 in global" |
66 |
+unset FOO006 |
67 |
+echo "FOO006=$FOO006 in global" |
68 |
+declare -F unset_outer |
69 |
+unset -f unset_outer |
70 |
+declare -F unset_outer |
71 |
|
72 |
diff --git a/scripts/command_execution.bash.result b/scripts/command_execution.bash.result |
73 |
index ae71fa5..d941a6d 100644 |
74 |
--- a/scripts/command_execution.bash.result |
75 |
+++ b/scripts/command_execution.bash.result |
76 |
@@ -6,6 +6,13 @@ end |
77 |
command environment |
78 |
We do not support command env before the export builtin. |
79 |
Redirection is not supported yet |
80 |
+FOO006=1 in unset_outer |
81 |
+FOO007= in unset_outer |
82 |
+FOO006= in unset_outer |
83 |
+FOO006= in global |
84 |
+FOO006=0 in global |
85 |
+FOO006= in global |
86 |
+unset_outer |
87 |
DEFAULTED=yes |
88 |
FOO001=hello |
89 |
FOO002=Hello World |
90 |
|
91 |
diff --git a/src/builtins/unset_builtin.cpp b/src/builtins/unset_builtin.cpp |
92 |
new file mode 100644 |
93 |
index 0000000..1490013 |
94 |
--- /dev/null |
95 |
+++ b/src/builtins/unset_builtin.cpp |
96 |
@@ -0,0 +1,59 @@ |
97 |
+/* |
98 |
+ Please use git log for copyright holder and year information |
99 |
+ |
100 |
+ This file is part of libbash. |
101 |
+ |
102 |
+ libbash is free software: you can redistribute it and/or modify |
103 |
+ it under the terms of the GNU General Public License as published by |
104 |
+ the Free Software Foundation, either version 2 of the License, or |
105 |
+ (at your option) any later version. |
106 |
+ |
107 |
+ libbash is distributed in the hope that it will be useful, |
108 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
109 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
110 |
+ GNU General Public License for more details. |
111 |
+ |
112 |
+ You should have received a copy of the GNU General Public License |
113 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
114 |
+*/ |
115 |
+/// |
116 |
+/// \file unset_builtin.h |
117 |
+/// \author Mu Qiao |
118 |
+/// \brief implementation for the unset builtin |
119 |
+/// |
120 |
+#include <functional> |
121 |
+ |
122 |
+#include "core/interpreter.h" |
123 |
+#include "core/unset_exception.h" |
124 |
+ |
125 |
+#include "builtins/unset_builtin.h" |
126 |
+ |
127 |
+int unset_builtin::exec(const std::vector<std::string>& bash_args) |
128 |
+{ |
129 |
+ if(bash_args.empty()) |
130 |
+ return 0; |
131 |
+ |
132 |
+ if(bash_args[0] == "-f") |
133 |
+ for_each(bash_args.begin() + 1, |
134 |
+ bash_args.end(), |
135 |
+ std::bind(&interpreter::unset_function, &_walker, std::placeholders::_1)); |
136 |
+ else |
137 |
+ /* POSIX says if neither -f nor -v is specified, name refers to a variable; |
138 |
+ * if a variable by that name does not exist, it is unspecified whether a |
139 |
+ * function by that name, if any, shall be unset. |
140 |
+ * |
141 |
+ * >=bash-4.1: without options, unset first tries to unset a variable, and |
142 |
+ * if that fails, tries to unset a function. |
143 |
+ * (We haven't checked bash-4.0) |
144 |
+ * |
145 |
+ * bash-3.2: if no options are supplied, or the -v option is given, each |
146 |
+ * name refers to a shell variable. |
147 |
+ * |
148 |
+ * We addhere to bash-3.2 |
149 |
+ * */ |
150 |
+ for_each(bash_args.front() == "-v" ? bash_args.begin() + 1 : bash_args.begin(), |
151 |
+ bash_args.end(), |
152 |
+ [&](const std::string& name) { _walker.unset(name); }); |
153 |
+ |
154 |
+ return 0; |
155 |
+} |
156 |
|
157 |
diff --git a/src/builtins/unset_builtin.h b/src/builtins/unset_builtin.h |
158 |
new file mode 100644 |
159 |
index 0000000..202e78a |
160 |
--- /dev/null |
161 |
+++ b/src/builtins/unset_builtin.h |
162 |
@@ -0,0 +1,35 @@ |
163 |
+/* |
164 |
+ Please use git log for copyright holder and year information |
165 |
+ |
166 |
+ This file is part of libbash. |
167 |
+ |
168 |
+ libbash is free software: you can redistribute it and/or modify |
169 |
+ it under the terms of the GNU General Public License as published by |
170 |
+ the Free Software Foundation, either version 2 of the License, or |
171 |
+ (at your option) any later version. |
172 |
+ |
173 |
+ libbash is distributed in the hope that it will be useful, |
174 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of |
175 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
176 |
+ GNU General Public License for more details. |
177 |
+ |
178 |
+ You should have received a copy of the GNU General Public License |
179 |
+ along with libbash. If not, see <http://www.gnu.org/licenses/>. |
180 |
+*/ |
181 |
+/// |
182 |
+/// \file unset_builtin.h |
183 |
+/// \brief implementation for the unset builtin |
184 |
+/// |
185 |
+#ifndef LIBBASH_BUILTINS_UNSET_BUILTIN_H_ |
186 |
+#define LIBBASH_BUILTINS_UNSET_BUILTIN_H_ |
187 |
+ |
188 |
+#include "cppbash_builtin.h" |
189 |
+ |
190 |
+class unset_builtin : public virtual cppbash_builtin |
191 |
+{ |
192 |
+public: |
193 |
+ BUILTIN_CONSTRUCTOR(unset) |
194 |
+ virtual int exec(const std::vector<std::string>& bash_args); |
195 |
+}; |
196 |
+ |
197 |
+#endif |
198 |
|
199 |
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp |
200 |
index 9308ebb..98d3acc 100644 |
201 |
--- a/src/cppbash_builtin.cpp |
202 |
+++ b/src/cppbash_builtin.cpp |
203 |
@@ -32,6 +32,7 @@ |
204 |
#include "builtins/return_builtin.h" |
205 |
#include "builtins/shopt_builtin.h" |
206 |
#include "builtins/source_builtin.h" |
207 |
+#include "builtins/unset_builtin.h" |
208 |
|
209 |
cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker) |
210 |
{ |
211 |
@@ -49,6 +50,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() { |
212 |
{"false", boost::factory<false_builtin*>()}, |
213 |
{"return", boost::factory<return_builtin*>()}, |
214 |
{"let", boost::factory<let_builtin*>()}, |
215 |
+ {"unset", boost::factory<unset_builtin*>()}, |
216 |
}); |
217 |
return *p; |
218 |
} |