aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-05-23 20:11:37 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-05-24 20:18:58 +0800
commitff1810c6f86b643aa8ac16951b7233b2d5daf632 (patch)
tree086de8dac9a81fbdab6b994f784018196cb52870 /src
parentWalker: accept some bash redirection syntax (diff)
downloadlibbash-ff1810c6f86b643aa8ac16951b7233b2d5daf632.tar.gz
libbash-ff1810c6f86b643aa8ac16951b7233b2d5daf632.tar.bz2
libbash-ff1810c6f86b643aa8ac16951b7233b2d5daf632.zip
Builtin: support the declare built-in syntax
There's no actual logic for the built-in right now. Multiple options to the built-in is not supported for now.
Diffstat (limited to 'src')
-rw-r--r--src/builtins/declare_builtin.cpp66
-rw-r--r--src/builtins/declare_builtin.h47
-rw-r--r--src/builtins/tests/declare_tests.cpp66
-rw-r--r--src/cppbash_builtin.cpp2
4 files changed, 181 insertions, 0 deletions
diff --git a/src/builtins/declare_builtin.cpp b/src/builtins/declare_builtin.cpp
new file mode 100644
index 0000000..5d21bb3
--- /dev/null
+++ b/src/builtins/declare_builtin.cpp
@@ -0,0 +1,66 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file declare_builtin.cpp
+/// \author Mu Qiao
+/// \brief class that implements the declare builtin
+///
+#include <iostream>
+
+#include "builtins/declare_builtin.h"
+
+int declare_builtin::exec(const std::vector<std::string>& bash_args)
+{
+ if(bash_args.empty())
+ {
+ *_err_stream << "Arguments required for declare" << std::endl;
+ return 1;
+ }
+ else if(bash_args[0].size() != 2)
+ {
+ *_err_stream << "Multiple arguments are not supported" << std::endl;
+ return 1;
+ }
+
+ if(bash_args[0][0] != '-' && bash_args[0][0] != '+')
+ {
+ *_err_stream << "Invalid option for declare builtin" << std::endl;
+ return 1;
+ }
+
+ switch(bash_args[0][1])
+ {
+ case 'a':
+ case 'A':
+ case 'f':
+ case 'F':
+ case 'i':
+ case 'l':
+ case 'r':
+ case 't':
+ case 'u':
+ case 'x':
+ case 'p':
+ *_err_stream << "declare " << bash_args[0] << " is not supported yet" << std::endl;
+ return 1;
+ default:
+ *_err_stream << "Unrecognized option for declare: " << bash_args[0] << std::endl;
+ return 1;
+ }
+}
diff --git a/src/builtins/declare_builtin.h b/src/builtins/declare_builtin.h
new file mode 100644
index 0000000..8a3b1ed
--- /dev/null
+++ b/src/builtins/declare_builtin.h
@@ -0,0 +1,47 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file declare_builtin.h
+/// \author Mu Qiao
+/// \brief class that implements the declare builtin
+///
+
+#ifndef LIBBASH_BUILTINS_DECLARE_BUILTIN_H_
+#define LIBBASH_BUILTINS_DECLARE_BUILTIN_H_
+
+#include "cppbash_builtin.h"
+
+///
+/// \class declare_builtin
+/// \brief the declare builtin for bash
+///
+class declare_builtin: public virtual cppbash_builtin
+{
+public:
+ BUILTIN_CONSTRUCTOR(declare)
+
+ ///
+ /// \brief runs the declare builtin on the supplied arguments
+ /// \param bash_args the arguments to the declare builtin
+ /// \return exit status of declare
+ ///
+ virtual int exec(const std::vector<std::string>& bash_args);
+};
+
+#endif
diff --git a/src/builtins/tests/declare_tests.cpp b/src/builtins/tests/declare_tests.cpp
new file mode 100644
index 0000000..925c5b9
--- /dev/null
+++ b/src/builtins/tests/declare_tests.cpp
@@ -0,0 +1,66 @@
+/*
+ Please use git log for copyright holder and year information
+
+ This file is part of libbash.
+
+ libbash is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ libbash is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with libbash. If not, see <http://www.gnu.org/licenses/>.
+*/
+///
+/// \file declare_tests.cpp
+/// \brief series of unit tests for declare built in
+/// \author Mu Qiao Eloe
+///
+#include <iostream>
+#include <sstream>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+using namespace std;
+
+static void test_declare(const string& expected, std::initializer_list<string> args)
+{
+ stringstream test_output;
+ interpreter walker;
+ cppbash_builtin::exec("declare",args,cout,test_output,cin,walker);
+ EXPECT_EQ(expected, test_output.str());
+}
+
+#define TEST_DECLARE(name, expected, ...) \
+ TEST(declare_builtin_test, name) { test_declare(expected, {__VA_ARGS__}); }
+
+TEST_DECLARE(_a, "declare -a is not supported yet\n", "-a", "world")
+TEST_DECLARE(_A, "declare -A is not supported yet\n", "-A", "world")
+TEST_DECLARE(_f, "declare -f is not supported yet\n", "-f", "world")
+TEST_DECLARE(_F, "declare -F is not supported yet\n", "-F", "world")
+TEST_DECLARE(_i, "declare -i is not supported yet\n", "-i", "world")
+TEST_DECLARE(_l, "declare -l is not supported yet\n", "-l", "world")
+TEST_DECLARE(_r, "declare -r is not supported yet\n", "-r", "world")
+TEST_DECLARE(_t, "declare -t is not supported yet\n", "-t", "world")
+TEST_DECLARE(_u, "declare -u is not supported yet\n", "-u", "world")
+TEST_DECLARE(_x, "declare -x is not supported yet\n", "-x", "world")
+TEST_DECLARE(_p, "declare -p is not supported yet\n", "-p", "world")
+TEST_DECLARE(pa, "declare +a is not supported yet\n", "+a", "world")
+TEST_DECLARE(pA, "declare +A is not supported yet\n", "+A", "world")
+TEST_DECLARE(pf, "declare +f is not supported yet\n", "+f", "world")
+TEST_DECLARE(pF, "declare +F is not supported yet\n", "+F", "world")
+TEST_DECLARE(pi, "declare +i is not supported yet\n", "+i", "world")
+TEST_DECLARE(pl, "declare +l is not supported yet\n", "+l", "world")
+TEST_DECLARE(pr, "declare +r is not supported yet\n", "+r", "world")
+TEST_DECLARE(pt, "declare +t is not supported yet\n", "+t", "world")
+TEST_DECLARE(pu, "declare +u is not supported yet\n", "+u", "world")
+TEST_DECLARE(px, "declare +x is not supported yet\n", "+x", "world")
diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index aee18c9..edbfa8c 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -25,6 +25,7 @@
#include "cppbash_builtin.h"
#include "builtins/boolean_builtins.h"
+#include "builtins/declare_builtin.h"
#include "builtins/echo_builtin.h"
#include "builtins/inherit_builtin.h"
#include "builtins/return_builtin.h"
@@ -37,6 +38,7 @@ cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&
cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
static boost::scoped_ptr<builtins_type> p(new builtins_type {
{"echo", boost::factory<echo_builtin*>()},
+ {"declare", boost::factory<declare_builtin*>()},
{"source", boost::factory<source_builtin*>()},
{"inherit", boost::factory<inherit_builtin*>()},
{":", boost::factory<true_builtin*>()},