public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2012-07-08  9:31 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2012-07-08  9:31 UTC (permalink / raw
  To: gentoo-commits

commit:     fed16223f8539afc0a15fdd3496f625bc3f0b821
Author:     André Aparício <aparicio99 <AT> gmail <DOT> com>
AuthorDate: Fri Apr  6 21:08:10 2012 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue Jul  3 01:16:10 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=fed16223

Builtin: implement read builtin

---
 Makefile.am                       |    3 +
 src/builtins/read_builtin.cpp     |   94 +++++++++++++++++++++++++++++++++++++
 src/builtins/read_builtin.h       |   54 +++++++++++++++++++++
 src/builtins/tests/read_tests.cpp |   76 ++++++++++++++++++++++++++++++
 src/cppbash_builtin.cpp           |    2 +
 5 files changed, 229 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index cbc9f3b..ca61091 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -105,6 +105,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/builtins/tests/shift_tests.cpp \
 						src/builtins/tests/shopt_tests.cpp \
 						src/builtins/tests/return_tests.cpp \
+						src/builtins/tests/read_tests.cpp \
 						src/builtins/tests/printf_tests.cpp \
 						test/test.h \
 						test/test.cpp \
@@ -236,6 +237,8 @@ libbash_la_SOURCES = include/common.h \
 					 src/builtins/inherit_builtin.cpp \
 					 src/builtins/unset_builtin.h \
 					 src/builtins/unset_builtin.cpp \
+					 src/builtins/read_builtin.h \
+					 src/builtins/read_builtin.cpp \
 					 src/builtins/builtin_exceptions.h \
 					 $(GENERATED_PARSER_C) \
 					 $(GENERATED_PARSER_H) \

diff --git a/src/builtins/read_builtin.cpp b/src/builtins/read_builtin.cpp
new file mode 100644
index 0000000..ca79094
--- /dev/null
+++ b/src/builtins/read_builtin.cpp
@@ -0,0 +1,94 @@
+/*
+   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 read_builtin.cpp
+/// \brief class that implements the read builtin
+///
+
+#include "builtins/read_builtin.h"
+
+#include <string.h>
+#include <boost/algorithm/string.hpp>
+
+#include "core/interpreter.h"
+#include "builtins/builtin_exceptions.h"
+
+void read_builtin::process(const std::vector<std::string>& args, const std::string& input)
+{
+  std::vector<std::string> split_input;
+  boost::split(split_input, input, boost::is_any_of(" "));
+
+  auto vars = args.begin();
+  for(auto words = split_input.begin(); vars != args.end() && words != split_input.end(); ++vars, ++words)
+  {
+    if(vars != args.end() - 1)
+    {
+      _walker.set_value(*vars, *words);
+    }
+    else
+    {
+      std::string rest;
+      for(; words != split_input.end() - 1; ++words)
+        rest += *words + " ";
+      rest += *words;
+      _walker.set_value(*vars, rest);
+    }
+  }
+
+  for(; vars != args.end(); ++vars)
+    _walker.set_value(*vars, "");
+}
+
+int read_builtin::exec(const std::vector<std::string>& bash_args)
+{
+  int return_value = 0;
+  std::string input;
+  std::stringstream formated_input;
+
+  getline(this->input_buffer(), input);
+
+  if(this->input_buffer().eof())
+    return_value = 1;
+
+  if(input.size() < 1)
+    return return_value;
+
+  while(input[input.length()-1] == '\\') {
+    input.erase(input.end()-1);
+    std::string input_line;
+    getline(this->input_buffer(), input_line);
+
+    if(this->input_buffer().eof())
+      return_value = 1;
+
+    if(input.size() < 1)
+      return return_value;
+
+    input += input_line;
+  }
+
+  cppbash_builtin::transform_escapes(input, formated_input, false);
+
+  if(bash_args.empty())
+    process({"REPLY"}, formated_input.str());
+  else
+    process(bash_args, formated_input.str());
+
+  return return_value;
+}

diff --git a/src/builtins/read_builtin.h b/src/builtins/read_builtin.h
new file mode 100644
index 0000000..caf8e86
--- /dev/null
+++ b/src/builtins/read_builtin.h
@@ -0,0 +1,54 @@
+/*
+   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 read_builtin.h
+/// \brief class that implements the read builtin
+///
+
+#ifndef LIBBASH_BUILTINS_READ_BUILTIN_H_
+#define LIBBASH_BUILTINS_READ_BUILTIN_H_
+
+#include "../cppbash_builtin.h"
+
+///
+/// \class read_builtin
+/// \brief the read builtin for bash
+///
+class read_builtin: public virtual cppbash_builtin
+{
+  public:
+    BUILTIN_CONSTRUCTOR(read)
+
+    ///
+    /// \brief runs the read builtin on the supplied arguments
+    /// \param bash_args the arguments to the read builtin
+    /// \return exit status of read
+    ///
+    virtual int exec(const std::vector<std::string>& bash_args);
+
+  private:
+    ///
+    /// \brief assigns words from input to the variables
+    /// \param args the variables to be used
+    /// \param input the input to be assigned to the variables
+    ///
+    virtual void process(const std::vector<std::string>& bash_args, const std::string& input);
+};
+
+#endif

diff --git a/src/builtins/tests/read_tests.cpp b/src/builtins/tests/read_tests.cpp
new file mode 100644
index 0000000..9bc9683
--- /dev/null
+++ b/src/builtins/tests/read_tests.cpp
@@ -0,0 +1,76 @@
+/*
+   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 read_tests.cpp
+/// \brief series of unit tests for read builtin
+///
+#include <iostream>
+#include <sstream>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+using namespace std;
+
+static void test_read(interpreter& walker, const string& input, std::initializer_list<string> args)
+{
+  stringstream test_input;
+  test_input << input;
+  cppbash_builtin::exec("read", args, std::cout, cerr, test_input, walker);
+}
+
+TEST(read_builtin_test, argument_assignment)
+{
+  interpreter walker;
+
+  test_read(walker, "foo bar", {});
+  EXPECT_STREQ("foo bar", walker.resolve<std::string>("REPLY").c_str());
+
+  test_read(walker, "foo bar", {"var"});
+  EXPECT_STREQ("foo bar", walker.resolve<std::string>("var").c_str());
+
+  test_read(walker, "foo bar", {"var1", "var2"});
+  EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str());
+  EXPECT_STREQ("bar", walker.resolve<std::string>("var2").c_str());
+
+  test_read(walker, "1 2 3 4", {"var1", "var2"});
+  EXPECT_STREQ("1", walker.resolve<std::string>("var1").c_str());
+  EXPECT_STREQ("2 3 4", walker.resolve<std::string>("var2").c_str());
+
+  test_read(walker, "foo", {"var1", "var2"});
+  EXPECT_STREQ("foo", walker.resolve<std::string>("var1").c_str());
+  EXPECT_STREQ("", walker.resolve<std::string>("var2").c_str());
+
+  test_read(walker, "foo    bar", {"var"});
+  EXPECT_STREQ("foo    bar", walker.resolve<std::string>("var").c_str());
+}
+
+TEST(read_builtin_test, line_continuation)
+{
+  interpreter walker;
+
+  test_read(walker, "foo\\\nbar", {});
+  EXPECT_STREQ("foobar", walker.resolve<std::string>("REPLY").c_str());
+
+  test_read(walker, "foo \\\n bar", {});
+  EXPECT_STREQ("foo  bar", walker.resolve<std::string>("REPLY").c_str());
+}

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index 452fe64..c93b94a 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -44,6 +44,7 @@
 #include "builtins/shopt_builtin.h"
 #include "builtins/source_builtin.h"
 #include "builtins/unset_builtin.h"
+#include "builtins/read_builtin.h"
 
 namespace qi = boost::spirit::qi;
 namespace karma = boost::spirit::karma;
@@ -73,6 +74,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
       {"printf", boost::factory<printf_builtin*>()},
       {"let", boost::factory<let_builtin*>()},
       {"unset", boost::factory<unset_builtin*>()},
+      {"read", boost::factory<read_builtin*>()},
   });
   return *p;
 }



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2012-07-08  9:31 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2012-07-08  9:31 UTC (permalink / raw
  To: gentoo-commits

commit:     b0d4f64632b09ffdc8cbba767ae723469e26485a
Author:     André Aparício <aparicio99 <AT> gmail <DOT> com>
AuthorDate: Tue May 29 01:16:26 2012 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun Jul  8 09:22:27 2012 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=b0d4f646

Builtin: Implement unset array support

---
 Makefile.am                        |    1 +
 src/builtins/tests/unset_tests.cpp |   50 ++++++++++++++++++++++++++++++++++++
 src/builtins/unset_builtin.cpp     |   11 +++++++-
 src/cppbash_builtin.cpp            |    1 +
 4 files changed, 62 insertions(+), 1 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index cc57503..9cb00c9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -107,6 +107,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/builtins/tests/return_tests.cpp \
 						src/builtins/tests/read_tests.cpp \
 						src/builtins/tests/set_tests.cpp \
+						src/builtins/tests/unset_tests.cpp \
 						src/builtins/tests/printf_tests.cpp \
 						test/test.h \
 						test/test.cpp \

diff --git a/src/builtins/tests/unset_tests.cpp b/src/builtins/tests/unset_tests.cpp
new file mode 100644
index 0000000..8350507
--- /dev/null
+++ b/src/builtins/tests/unset_tests.cpp
@@ -0,0 +1,50 @@
+/*
+   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 unset_tests.cpp
+/// \brief series of unit tests for unset builtin
+///
+#include <boost/lexical_cast.hpp>
+#include <gtest/gtest.h>
+
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+#include "exceptions.h"
+
+TEST(unset_builtin_test, normal)
+{
+  interpreter walker;
+
+  walker.set_value("var", "foo");
+  EXPECT_EQ(0, cppbash_builtin::exec("unset", {"var"}, std::cout, std::cerr, std::cin, walker));
+  EXPECT_STREQ("", walker.resolve<std::string>("var").c_str());
+}
+
+TEST(unset_builtin_test, array)
+{
+  interpreter walker;
+
+  walker.set_value("array", "foo bar", 2);
+  EXPECT_EQ(0, cppbash_builtin::exec("unset", {"array"}, std::cout, std::cerr, std::cin, walker));
+  EXPECT_STREQ("", walker.resolve<std::string>("array", 2).c_str());
+
+  walker.set_value("array", "foo bar", 2);
+  EXPECT_EQ(0, cppbash_builtin::exec("unset", {"array[2]"}, std::cout, std::cerr, std::cin, walker));
+  EXPECT_STREQ("", walker.resolve<std::string>("array", 2).c_str());
+}

diff --git a/src/builtins/unset_builtin.cpp b/src/builtins/unset_builtin.cpp
index d8178f9..2eaafc2 100644
--- a/src/builtins/unset_builtin.cpp
+++ b/src/builtins/unset_builtin.cpp
@@ -26,6 +26,8 @@
 
 #include "core/interpreter.h"
 
+using namespace boost::xpressive;
+
 int unset_builtin::exec(const std::vector<std::string>& bash_args)
 {
   if(bash_args.empty())
@@ -51,7 +53,14 @@ int unset_builtin::exec(const std::vector<std::string>& bash_args)
    * */
     for_each(bash_args.front() == "-v" ? bash_args.begin() + 1 : bash_args.begin(),
              bash_args.end(),
-             [&](const std::string& name) { _walker.unset(name); });
+             [&](const std::string& name) {
+                static const sregex index_pattern = sregex::compile("^(.*)\\[(\\d*)\\]$");
+                smatch match;
+                if(regex_match(name, match, index_pattern))
+                  _walker.unset(match[1], boost::lexical_cast<unsigned int>(match[2]));
+                else
+                  _walker.unset(name);
+             });
 
   return 0;
 }

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index e4a7c29..51cd15c 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -46,6 +46,7 @@
 #include "builtins/unset_builtin.h"
 #include "builtins/read_builtin.h"
 #include "builtins/set_builtin.h"
+#include "builtins/unset_builtin.h"
 
 namespace qi = boost::spirit::qi;
 namespace karma = boost::spirit::karma;



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-06-25 10:05 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-06-25 10:05 UTC (permalink / raw
  To: gentoo-commits

commit:     a0fa189c0f2322ab00e2034b0d01a98da4e36625
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Jun 20 14:10:54 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Jun 23 03:24:42 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=a0fa189c

Builtin: implement break built-in

---
 Makefile.am                        |    3 ++
 src/builtins/break_builtin.cpp     |   51 ++++++++++++++++++++++++++
 src/builtins/break_builtin.h       |   35 ++++++++++++++++++
 src/builtins/tests/break_tests.cpp |   69 ++++++++++++++++++++++++++++++++++++
 src/cppbash_builtin.cpp            |    2 +
 5 files changed, 160 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index a706c43..3c70a0f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -101,6 +101,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/core/tests/bash_ast_test.cpp \
 						src/core/tests/bash_condition_test.cpp \
 						src/builtins/tests/continue_tests.cpp \
+						src/builtins/tests/break_tests.cpp \
 						src/builtins/tests/echo_tests.cpp \
 						src/builtins/tests/declare_tests.cpp \
 						src/builtins/tests/boolean_tests.cpp \
@@ -181,6 +182,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/cppbash_builtin.h \
 						src/builtins/continue_builtin.cpp \
 						src/builtins/continue_builtin.h \
+						src/builtins/break_builtin.cpp \
+						src/builtins/break_builtin.h \
 						src/builtins/echo_builtin.cpp \
 						src/builtins/echo_builtin.h \
 						src/builtins/eval_builtin.cpp \

diff --git a/src/builtins/break_builtin.cpp b/src/builtins/break_builtin.cpp
new file mode 100644
index 0000000..0ee20a7
--- /dev/null
+++ b/src/builtins/break_builtin.cpp
@@ -0,0 +1,51 @@
+/*
+   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 break_builtin.h
+/// \brief implementation for the break builtin
+///
+#include "builtins/break_builtin.h"
+
+#include <boost/lexical_cast.hpp>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter_exception.h"
+
+int break_builtin::exec(const std::vector<std::string>& bash_args)
+{
+  int nth = 1;
+
+  if(bash_args.size() > 1)
+  {
+    throw libbash::interpreter_exception("break: too many arguments");
+  }
+  else if(bash_args.size() == 1)
+  {
+    try
+    {
+      nth = boost::lexical_cast<int>(bash_args[0]);
+    }
+    catch(boost::bad_lexical_cast& e)
+    {
+      throw libbash::interpreter_exception("break: argument should be an integer");
+    }
+  }
+
+  throw break_exception(nth);
+}

diff --git a/src/builtins/break_builtin.h b/src/builtins/break_builtin.h
new file mode 100644
index 0000000..fec1bc7
--- /dev/null
+++ b/src/builtins/break_builtin.h
@@ -0,0 +1,35 @@
+/*
+   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 break_builtin.h
+/// \brief implementation for the break builtin
+///
+#ifndef LIBBASH_BUILTINS_BREAK_BUILTIN_H_
+#define LIBBASH_BUILTINS_BREAK_BUILTIN_H_
+
+#include "cppbash_builtin.h"
+
+class break_builtin : public virtual cppbash_builtin
+{
+public:
+  BUILTIN_CONSTRUCTOR(break)
+  virtual int exec(const std::vector<std::string>& );
+};
+
+#endif

diff --git a/src/builtins/tests/break_tests.cpp b/src/builtins/tests/break_tests.cpp
new file mode 100644
index 0000000..eba422e
--- /dev/null
+++ b/src/builtins/tests/break_tests.cpp
@@ -0,0 +1,69 @@
+/*
+   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 break_tests.cpp
+/// \brief series of unit tests for break builtin
+///
+#include <boost/lexical_cast.hpp>
+#include <gtest/gtest.h>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+TEST(break_builtin_test, bad_argument)
+{
+  interpreter walker;
+  EXPECT_THROW(cppbash_builtin::exec("break", {"abc"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("break", {"1", "2"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("break", {"0"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("break", {"-1"}, std::cout, std::cerr, std::cin, walker), libbash::interpreter_exception);
+}
+
+TEST(break_builtin_test, throw_exception)
+{
+  interpreter walker;
+  try
+  {
+    cppbash_builtin::exec("break", {}, std::cout, std::cerr, std::cin, walker);
+    FAIL();
+  }
+  catch(break_exception& e)
+  {
+    EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+  }
+
+  try
+  {
+    cppbash_builtin::exec("break", {"2"}, std::cout, std::cerr, std::cin, walker);
+    FAIL();
+  }
+  catch(break_exception& e)
+  {
+    try
+    {
+      e.rethrow_unless_correct_frame();
+      FAIL();
+    }
+    catch(break_exception& e)
+    {
+      EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+    }
+  }
+}

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index 97ab789..52c5c9b 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -29,6 +29,7 @@
 
 #include "builtins/boolean_builtins.h"
 #include "builtins/builtin_exceptions.h"
+#include "builtins/break_builtin.h"
 #include "builtins/continue_builtin.h"
 #include "builtins/declare_builtin.h"
 #include "builtins/echo_builtin.h"
@@ -51,6 +52,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 {
+      {"break", boost::factory<break_builtin*>()},
       {"continue", boost::factory<continue_builtin*>()},
       {"echo", boost::factory<echo_builtin*>()},
       {"eval", boost::factory<eval_builtin*>()},



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-05-29 11:20 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-05-29 11:20 UTC (permalink / raw
  To: gentoo-commits

commit:     1b7da0e59f91f963e616e8762f2d03e736908ad4
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Thu May 26 13:51:42 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun May 29 11:44:46 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=1b7da0e5

Builtin: support continue built-in

---
 Makefile.am                                        |    3 +
 src/builtins/builtin_exceptions.h                  |   21 ++++++
 .../{builtin_exceptions.h => continue_builtin.cpp} |   43 ++++++++-----
 .../{builtin_exceptions.h => continue_builtin.h}   |   22 ++----
 src/builtins/tests/continue_tests.cpp              |   69 ++++++++++++++++++++
 src/cppbash_builtin.cpp                            |    2 +
 6 files changed, 130 insertions(+), 30 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index ebe12ee..423bcc4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,6 +98,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/core/tests/interpreter_test.cpp \
 						src/core/tests/bash_ast_test.cpp \
 						src/core/tests/bash_condition_test.cpp \
+						src/builtins/tests/continue_tests.cpp \
 						src/builtins/tests/echo_tests.cpp \
 						src/builtins/tests/declare_tests.cpp \
 						src/builtins/tests/boolean_tests.cpp \
@@ -174,6 +175,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/libbash.cpp \
 						src/cppbash_builtin.cpp \
 						src/cppbash_builtin.h \
+						src/builtins/continue_builtin.cpp \
+						src/builtins/continue_builtin.h \
 						src/builtins/echo_builtin.cpp \
 						src/builtins/echo_builtin.h \
 						src/builtins/declare_builtin.cpp \

diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/builtin_exceptions.h
index 5548d14..a5cc28b 100644
--- a/src/builtins/builtin_exceptions.h
+++ b/src/builtins/builtin_exceptions.h
@@ -27,6 +27,8 @@
 
 #include <stdexcept>
 
+#include "core/interpreter_exception.h"
+
 ///
 /// \class return_exception
 /// \brief thrown when executing the return builtin
@@ -38,4 +40,23 @@ public:
     runtime_error("return exception"){}
 };
 
+class continue_exception: public std::exception
+{
+  int count;
+public:
+  explicit continue_exception(int c): count(c)
+  {
+    if(c < 1)
+      throw interpreter_exception("continue: argument should be greater than or equal to 1");
+  }
+
+  void rethrow_unless_correct_frame()
+  {
+    if(count != 1)
+    {
+      --count;
+      throw *this;
+    }
+  }
+};
 #endif

diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/continue_builtin.cpp
similarity index 52%
copy from src/builtins/builtin_exceptions.h
copy to src/builtins/continue_builtin.cpp
index 5548d14..87dd59a 100644
--- a/src/builtins/builtin_exceptions.h
+++ b/src/builtins/continue_builtin.cpp
@@ -17,25 +17,36 @@
    along with libbash.  If not, see <http://www.gnu.org/licenses/>.
 */
 ///
-/// \file builtin_exceptions.h
+/// \file continue_builtin.h
 /// \author Mu Qiao
-/// \brief implementations for builtin exceptions
+/// \brief implementation for the continue builtin
 ///
+#include <boost/lexical_cast.hpp>
 
-#ifndef LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_
-#define LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter_exception.h"
 
-#include <stdexcept>
+#include "builtins/continue_builtin.h"
 
-///
-/// \class return_exception
-/// \brief thrown when executing the return builtin
-///
-class return_exception: public std::runtime_error
+int continue_builtin::exec(const std::vector<std::string>& bash_args)
 {
-public:
-  explicit return_exception():
-    runtime_error("return exception"){}
-};
-
-#endif
+  int nth = 1;
+
+  if(bash_args.size() > 1)
+  {
+    throw interpreter_exception("continue: too many arguments");
+  }
+  else if(bash_args.size() == 1)
+  {
+    try
+    {
+      nth = boost::lexical_cast<int>(bash_args[0]);
+    }
+    catch(boost::bad_lexical_cast& e)
+    {
+      throw interpreter_exception("continue: argument should be an integer");
+    }
+  }
+
+  throw continue_exception(nth);
+}

diff --git a/src/builtins/builtin_exceptions.h b/src/builtins/continue_builtin.h
similarity index 64%
copy from src/builtins/builtin_exceptions.h
copy to src/builtins/continue_builtin.h
index 5548d14..cd5223b 100644
--- a/src/builtins/builtin_exceptions.h
+++ b/src/builtins/continue_builtin.h
@@ -17,25 +17,19 @@
    along with libbash.  If not, see <http://www.gnu.org/licenses/>.
 */
 ///
-/// \file builtin_exceptions.h
-/// \author Mu Qiao
-/// \brief implementations for builtin exceptions
+/// \file continue_builtin.h
+/// \brief implementation for the continue builtin
 ///
+#ifndef LIBBASH_BUILTINS_CONTINUE_BUILTIN_H_
+#define LIBBASH_BUILTINS_CONTINUE_BUILTIN_H_
 
-#ifndef LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_
-#define LIBBASH_BUILTINS_BUILTIN_EXCEPTIONS_H_
+#include "cppbash_builtin.h"
 
-#include <stdexcept>
-
-///
-/// \class return_exception
-/// \brief thrown when executing the return builtin
-///
-class return_exception: public std::runtime_error
+class continue_builtin : public virtual cppbash_builtin
 {
 public:
-  explicit return_exception():
-    runtime_error("return exception"){}
+  BUILTIN_CONSTRUCTOR(continue)
+  virtual int exec(const std::vector<std::string>& );
 };
 
 #endif

diff --git a/src/builtins/tests/continue_tests.cpp b/src/builtins/tests/continue_tests.cpp
new file mode 100644
index 0000000..4a8d67c
--- /dev/null
+++ b/src/builtins/tests/continue_tests.cpp
@@ -0,0 +1,69 @@
+/*
+   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 continue_tests.cpp
+/// \brief series of unit tests for continue builtin
+///
+#include <boost/lexical_cast.hpp>
+#include <gtest/gtest.h>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+TEST(continue_builtin_test, bad_argument)
+{
+  interpreter walker;
+  EXPECT_THROW(cppbash_builtin::exec("continue", {"abc"}, std::cout, std::cerr, std::cin, walker), interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("continue", {"1", "2"}, std::cout, std::cerr, std::cin, walker), interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("continue", {"0"}, std::cout, std::cerr, std::cin, walker), interpreter_exception);
+  EXPECT_THROW(cppbash_builtin::exec("continue", {"-1"}, std::cout, std::cerr, std::cin, walker), interpreter_exception);
+}
+
+TEST(continue_builtin_test, throw_exception)
+{
+  interpreter walker;
+  try
+  {
+    cppbash_builtin::exec("continue", {}, std::cout, std::cerr, std::cin, walker);
+    FAIL();
+  }
+  catch(continue_exception& e)
+  {
+    EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+  }
+
+  try
+  {
+    cppbash_builtin::exec("continue", {"2"}, std::cout, std::cerr, std::cin, walker);
+    FAIL();
+  }
+  catch(continue_exception& e)
+  {
+    try
+    {
+      e.rethrow_unless_correct_frame();
+      FAIL();
+    }
+    catch(continue_exception& e)
+    {
+      EXPECT_NO_THROW(e.rethrow_unless_correct_frame());
+    }
+  }
+}

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index 98d3acc..c762f5f 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/continue_builtin.h"
 #include "builtins/declare_builtin.h"
 #include "builtins/echo_builtin.h"
 #include "builtins/inherit_builtin.h"
@@ -40,6 +41,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 {
+      {"continue", boost::factory<continue_builtin*>()},
       {"echo", boost::factory<echo_builtin*>()},
       {"declare", boost::factory<declare_builtin*>()},
       {"source", boost::factory<source_builtin*>()},



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-05-25 19:42 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-05-25 19:42 UTC (permalink / raw
  To: gentoo-commits

commit:     818ae8fb5415365640a7d08a05db21e5b94a3bf2
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue May 24 08:29:39 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed May 25 08:47:10 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=818ae8fb

Builtin: first support for the shopt built-in

No logic is implemented for the built-in. Now it's just used for
the detection of shell option usage.

---
 Makefile.am                        |    3 ++
 src/builtins/shopt_builtin.cpp     |   67 ++++++++++++++++++++++++++++++++++++
 src/builtins/shopt_builtin.h       |   35 +++++++++++++++++++
 src/builtins/tests/shopt_tests.cpp |   33 +++++++++++++++++
 src/cppbash_builtin.cpp            |    2 +
 5 files changed, 140 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index c4605cd..86c2583 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -102,6 +102,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/builtins/tests/declare_tests.cpp \
 						src/builtins/tests/boolean_tests.cpp \
 						src/builtins/tests/source_tests.cpp \
+						src/builtins/tests/shopt_tests.cpp \
 						src/builtins/tests/return_tests.cpp \
 						test/test.h \
 						test/test.cpp \
@@ -180,6 +181,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/builtins/boolean_builtins.h \
 						src/builtins/source_builtin.h \
 						src/builtins/source_builtin.cpp \
+						src/builtins/shopt_builtin.h \
+						src/builtins/shopt_builtin.cpp \
 						src/builtins/return_builtin.h \
 						src/builtins/return_builtin.cpp \
 						src/builtins/let_builtin.h \

diff --git a/src/builtins/shopt_builtin.cpp b/src/builtins/shopt_builtin.cpp
new file mode 100644
index 0000000..33ca3fc
--- /dev/null
+++ b/src/builtins/shopt_builtin.cpp
@@ -0,0 +1,67 @@
+/*
+   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 shopt_builtin.cpp
+/// \author Mu Qiao
+/// \brief implementation for the shopt builtin
+///
+
+#include "core/interpreter_exception.h"
+#include "cppbash_builtin.h"
+
+#include "builtins/shopt_builtin.h"
+
+namespace
+{
+  int disable_opt(const std::vector<std::string>& bash_args)
+  {
+    auto iter = find(bash_args.begin() + 1, bash_args.end(), "extglob");
+    if(iter != bash_args.end())
+      throw interpreter_exception("Disabling extglob is not allowed");
+    return 0;
+  }
+}
+
+int shopt_builtin::exec(const std::vector<std::string>& bash_args)
+{
+  if(bash_args.empty())
+  {
+    *_err_stream << "Arguments required for shopt" << std::endl;
+    return 1;
+  }
+  else if(bash_args[0].size() != 2)
+  {
+    *_err_stream << "Multiple arguments are not supported" << std::endl;
+    return 1;
+  }
+
+  switch(bash_args[0][1])
+  {
+    case 'u':
+      return disable_opt(bash_args);
+    case 's':
+    case 'q':
+    case 'o':
+      *_err_stream << "shopt " << bash_args[0] << " is not supported yet" << std::endl;
+      return 1;
+    default:
+      *_err_stream << "Unrecognized option for shopt: " << bash_args[0] << std::endl;
+      return 1;
+  }
+}

diff --git a/src/builtins/shopt_builtin.h b/src/builtins/shopt_builtin.h
new file mode 100644
index 0000000..bd3291e
--- /dev/null
+++ b/src/builtins/shopt_builtin.h
@@ -0,0 +1,35 @@
+/*
+   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 shopt_builtin.h
+/// \brief implementation for the shopt builtin
+///
+#ifndef LIBBASH_BUILTINS_SHOPT_BUILTIN_H_
+#define LIBBASH_BUILTINS_SHOPT_BUILTIN_H_
+
+#include "cppbash_builtin.h"
+
+class shopt_builtin : public virtual cppbash_builtin
+{
+public:
+  BUILTIN_CONSTRUCTOR(shopt)
+  virtual int exec(const std::vector<std::string>& );
+};
+
+#endif

diff --git a/src/builtins/tests/shopt_tests.cpp b/src/builtins/tests/shopt_tests.cpp
new file mode 100644
index 0000000..5b4c45c
--- /dev/null
+++ b/src/builtins/tests/shopt_tests.cpp
@@ -0,0 +1,33 @@
+/*
+   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 shopt_tests.cpp
+/// \brief series of unit tests for shopt builtin
+///
+#include <gtest/gtest.h>
+
+#include "builtins/builtin_exceptions.h"
+#include "core/interpreter.h"
+#include "cppbash_builtin.h"
+
+TEST(return_builtin_test, disable_extglob)
+{
+  interpreter walker;
+  EXPECT_THROW(cppbash_builtin::exec("shopt", {"-u", "extglob"}, std::cout, std::cerr, std::cin, walker), interpreter_exception);
+}

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index 88fdf99..9308ebb 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -30,6 +30,7 @@
 #include "builtins/inherit_builtin.h"
 #include "builtins/let_builtin.h"
 #include "builtins/return_builtin.h"
+#include "builtins/shopt_builtin.h"
 #include "builtins/source_builtin.h"
 
 cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker)
@@ -41,6 +42,7 @@ cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
       {"echo", boost::factory<echo_builtin*>()},
       {"declare", boost::factory<declare_builtin*>()},
       {"source", boost::factory<source_builtin*>()},
+      {"shopt", boost::factory<shopt_builtin*>()},
       {"inherit", boost::factory<inherit_builtin*>()},
       {":", boost::factory<true_builtin*>()},
       {"true", boost::factory<true_builtin*>()},



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-05-24 14:50 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-05-24 14:50 UTC (permalink / raw
  To: gentoo-commits

commit:     ff1810c6f86b643aa8ac16951b7233b2d5daf632
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon May 23 12:11:37 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue May 24 12:18:58 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=ff1810c6

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.

---
 Makefile.am                          |    3 ++
 src/builtins/declare_builtin.cpp     |   66 ++++++++++++++++++++++++++++++++++
 src/builtins/declare_builtin.h       |   47 ++++++++++++++++++++++++
 src/builtins/tests/declare_tests.cpp |   66 ++++++++++++++++++++++++++++++++++
 src/cppbash_builtin.cpp              |    2 +
 5 files changed, 184 insertions(+), 0 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 5b33379..c8c702b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,6 +98,7 @@ cppunittests_SOURCES =  test/run_tests.cpp \
 						src/core/tests/interpreter_test.cpp \
 						src/core/tests/bash_ast_test.cpp \
 						src/builtins/tests/echo_tests.cpp \
+						src/builtins/tests/declare_tests.cpp \
 						src/builtins/tests/boolean_tests.cpp \
 						src/builtins/tests/source_tests.cpp \
 						src/builtins/tests/return_tests.cpp \
@@ -173,6 +174,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/cppbash_builtin.h \
 						src/builtins/echo_builtin.cpp \
 						src/builtins/echo_builtin.h \
+						src/builtins/declare_builtin.cpp \
+						src/builtins/declare_builtin.h \
 						src/builtins/boolean_builtins.h \
 						src/builtins/source_builtin.h \
 						src/builtins/source_builtin.cpp \

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*>()},



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-03-06 12:05 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-03-06 12:05 UTC (permalink / raw
  To: gentoo-commits

commit:     2a301b4b4181dd8bee647c5c2f1dfbcdba002087
Author:     Petteri Räty <petsku <AT> petteriraty <DOT> eu>
AuthorDate: Sun Aug 29 22:23:06 2010 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun Aug 29 22:27:57 2010 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=2a301b4b

Implement true and false builtins

Implementations of true and false builtins to see how much code
implementing a very simple builtin takes.

---
 Makefile.am                                        |    1 +
 .../boolean_builtins.h}                            |   29 +++++++--------
 src/builtins/tests/boolean_tests.cpp               |   38 ++++++++++++++++++++
 src/cppbash_builtin.cpp                            |    5 ++-
 4 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index e66ceb2..33064bb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,6 +31,7 @@ TESTS += builtin_unittests
 check_PROGRAMS = builtin_unittests
 builtin_unittests_SOURCES = src/builtins/tests/run_tests.cpp
 builtin_unittests_SOURCES += src/builtins/tests/echo_tests.cpp
+builtin_unittests_SOURCES += src/builtins/tests/boolean_tests.cpp
 builtin_unittests_LDADD = ${GTEST_LIBS} libcppbash.la
 endif
 

diff --git a/src/cppbash_builtin.cpp b/src/builtins/boolean_builtins.h
similarity index 53%
copy from src/cppbash_builtin.cpp
copy to src/builtins/boolean_builtins.h
index ec6f683..b7afa12 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/builtins/boolean_builtins.h
@@ -1,5 +1,5 @@
 /*
-   Copyright 2010 Nathan Eloe
+   Copyright 2010 Petteri Räty
 
    This file is part of libbash.
 
@@ -14,23 +14,22 @@
 
    You should have received a copy of the GNU General Public License
    along with libbash.  If not, see <http://www.gnu.org/licenses/>.
-   */
+*/
 ///
-/// \file cppbash_builtin.cpp
-/// \author Nathan Eloe
-/// \brief Implementation of class to inherit builtins from
+/// \file boolean_builtins.cpp
+/// \brief implementations for the true and false builtins
 ///
 
-#include "cppbash_builtin.h"
-#include "builtins/echo_builtin.h"
+#include "../cppbash_builtin.h"
 
-cppbash_builtin::cppbash_builtin(std::ostream &outstream, std::ostream &errstream, std::istream &instream): _out_stream(&outstream), _err_stream(&errstream), _inp_stream(&instream)
+struct true_builtin : public virtual cppbash_builtin
 {
-}
+  BUILTIN_CONSTRUCTOR(true)
+  virtual int exec(const std::vector<std::string>& bash_args) { return 0; }
+};
 
-cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
-  static boost::scoped_ptr<builtins_type> p(new builtins_type {
-      {"echo", boost::factory<echo_builtin*>()}
-  });
-  return *p;
-}
+struct false_builtin : public virtual cppbash_builtin
+{
+  BUILTIN_CONSTRUCTOR(false)
+  virtual int exec(const std::vector<std::string>& bash_args) { return 1; }
+};

diff --git a/src/builtins/tests/boolean_tests.cpp b/src/builtins/tests/boolean_tests.cpp
new file mode 100644
index 0000000..ff65a9b
--- /dev/null
+++ b/src/builtins/tests/boolean_tests.cpp
@@ -0,0 +1,38 @@
+/*
+Copyright 2010 Petteri Räty
+
+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 echo_tests.cpp
+/// \brief series of unit tests for echo built in
+///
+#include <iostream>
+#include "../../cppbash_builtin.h"
+#include <gtest/gtest.h>
+
+using namespace std;
+
+TEST(boolean_builtin_test, true)
+{
+  int result = cppbash_builtin::exec("true", {}, std::cout, std::cerr, std::cin);
+  ASSERT_EQ(0, result);
+}
+
+TEST(boolean_builtin_test, false)
+{
+  int result = cppbash_builtin::exec("false", {}, std::cout, std::cerr, std::cin);
+  ASSERT_EQ(1, result);
+}

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index ec6f683..a486bd6 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -23,6 +23,7 @@
 
 #include "cppbash_builtin.h"
 #include "builtins/echo_builtin.h"
+#include "builtins/boolean_builtins.h"
 
 cppbash_builtin::cppbash_builtin(std::ostream &outstream, std::ostream &errstream, std::istream &instream): _out_stream(&outstream), _err_stream(&errstream), _inp_stream(&instream)
 {
@@ -30,7 +31,9 @@ cppbash_builtin::cppbash_builtin(std::ostream &outstream, std::ostream &errstrea
 
 cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
   static boost::scoped_ptr<builtins_type> p(new builtins_type {
-      {"echo", boost::factory<echo_builtin*>()}
+      {"echo", boost::factory<echo_builtin*>()},
+      {"true", boost::factory<true_builtin*>()},
+      {"false", boost::factory<false_builtin*>()}
   });
   return *p;
 }



^ permalink raw reply related	[flat|nested] 8+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/
@ 2011-03-06 12:05 Petteri Räty
  0 siblings, 0 replies; 8+ messages in thread
From: Petteri Räty @ 2011-03-06 12:05 UTC (permalink / raw
  To: gentoo-commits

commit:     6ca2a52a9c7dcf574ba0ed54946a1e33e4580d2b
Author:     Petteri Räty <petsku <AT> petteriraty <DOT> eu>
AuthorDate: Sun Aug 29 22:16:10 2010 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun Aug 29 22:27:57 2010 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=6ca2a52a

Add ability to dynamically execute builtins

Builtins can now be executed with a string lookup. There's a static
function in cppbash_builtin for executing builtins.

---
 Makefile.am                       |    3 +--
 configure.ac                      |    2 +-
 src/builtins/builtins.h           |   28 ----------------------------
 src/builtins/echo_builtin.cpp     |    4 ----
 src/builtins/echo_builtin.h       |    9 ++-------
 src/builtins/tests/echo_tests.cpp |    9 ++++-----
 src/cppbash_builtin.cpp           |    7 +++++++
 src/cppbash_builtin.h             |   26 +++++++++++++++++++++++++-
 8 files changed, 40 insertions(+), 48 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 5b5e667..e66ceb2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -34,12 +34,11 @@ builtin_unittests_SOURCES += src/builtins/tests/echo_tests.cpp
 builtin_unittests_LDADD = ${GTEST_LIBS} libcppbash.la
 endif
 
-AM_CXXFLAGS=-std=c++0x
+AM_CXXFLAGS=$(BOOST_CPPFLAGS) -std=c++0x
 
 lib_LTLIBRARIES = libcppbash.la
 libcppbash_la_SOURCES = src/cppbash_builtin.cpp
 libcppbash_la_SOURCES += src/builtins/echo_builtin.cpp
-libcppbash_la_CPPFLAGS = $(BOOST_CPPFLAGS)
 
 coding_standard.pdf: coding_standard/coding_standard.tex
 	@PDFLATEX@ coding_standard/coding_standard.tex 2&>1 > /dev/null

diff --git a/configure.ac b/configure.ac
index de60050..7b361f9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,7 +22,7 @@ AC_INIT([libbash],[0.1],[powerofazure@gmail.com])
 AM_INIT_AUTOMAKE([parallel-tests subdir-objects])
 AC_PROG_CXX
 LT_INIT
-AX_BOOST_BASE
+AX_BOOST_BASE(1.43.0,[:],[AC_MSG_ERROR([Needed boost not found])])
 AC_PATH_PROG([JAVA],[java],"no")
 if test "$JAVA" = "no"; then
 	AC_MSG_ERROR([No java executable found])

diff --git a/src/builtins/builtins.h b/src/builtins/builtins.h
deleted file mode 100644
index 42415e1..0000000
--- a/src/builtins/builtins.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
-   Copyright 2010 Nathan Eloe
-
-   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 builtins.h
-/// \author Nathan Eloe
-/// \brief An include file for all of the builtin implementations
-///
-#ifndef BUILTINS_H
-#define BUILTINS_H
-
-#include "echo_builtin.h"
-
-#endif

diff --git a/src/builtins/echo_builtin.cpp b/src/builtins/echo_builtin.cpp
index abf624a..4f498bf 100644
--- a/src/builtins/echo_builtin.cpp
+++ b/src/builtins/echo_builtin.cpp
@@ -35,10 +35,6 @@ class suppress_output
 {
 };
 
-echo_builtin::echo_builtin(std::ostream &outstream, std::ostream &errstream, std::istream&instream) : cppbash_builtin(outstream, errstream, instream)
-{
-}
-
 int echo_builtin::exec(const std::vector<std::string>& bash_args)
 {
   bool suppress_nl = false;

diff --git a/src/builtins/echo_builtin.h b/src/builtins/echo_builtin.h
index 0b4fd28..0f55fa9 100644
--- a/src/builtins/echo_builtin.h
+++ b/src/builtins/echo_builtin.h
@@ -35,13 +35,8 @@
 class echo_builtin: public virtual cppbash_builtin
 {
   public:
-    ///
-    /// \brief default constructor, sets default streams
-    /// \param outstream where to send standard output. Default: cout
-    /// \param errstream where to send standard error. Default: cerr
-    /// \param instream where to get standard input from.  Default cin
-    ///
-    echo_builtin(std::ostream &outstream=std::cout, std::ostream &errstream=std::cerr, std::istream &instream=std::cin);
+    BUILTIN_CONSTRUCTOR(echo)
+
     ///
     /// \brief runs the echo plugin on the supplied arguments
     /// \param bash_args the arguments to the echo builtin

diff --git a/src/builtins/tests/echo_tests.cpp b/src/builtins/tests/echo_tests.cpp
index 432be75..4826c2f 100644
--- a/src/builtins/tests/echo_tests.cpp
+++ b/src/builtins/tests/echo_tests.cpp
@@ -21,7 +21,7 @@ along with libbash.  If not, see <http://www.gnu.org/licenses/>.
 /// \author Nathan Eloe
 ///
 #include <iostream>
-#include "../builtins.h"
+#include "../../cppbash_builtin.h"
 #include <sstream>
 #include <vector>
 #include <gtest/gtest.h>
@@ -30,10 +30,9 @@ using namespace std;
 
 static void test_echo(const string& expected, std::initializer_list<string> args)
 {
-	stringstream test_output;
-	echo_builtin my_echo(test_output,cerr,cin);
-	my_echo.exec(vector<string>(args));
-	ASSERT_EQ(expected, test_output.str());
+  stringstream test_output;
+  cppbash_builtin::exec("echo",args,test_output,cerr,cin);
+  ASSERT_EQ(expected, test_output.str());
 }
 
 #define TEST_ECHO(name, expected, ...) \

diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
index ddb4310..ec6f683 100644
--- a/src/cppbash_builtin.cpp
+++ b/src/cppbash_builtin.cpp
@@ -22,8 +22,15 @@
 ///
 
 #include "cppbash_builtin.h"
+#include "builtins/echo_builtin.h"
 
 cppbash_builtin::cppbash_builtin(std::ostream &outstream, std::ostream &errstream, std::istream &instream): _out_stream(&outstream), _err_stream(&errstream), _inp_stream(&instream)
 {
 }
 
+cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
+  static boost::scoped_ptr<builtins_type> p(new builtins_type {
+      {"echo", boost::factory<echo_builtin*>()}
+  });
+  return *p;
+}

diff --git a/src/cppbash_builtin.h b/src/cppbash_builtin.h
index 6506ba1..180034d 100644
--- a/src/cppbash_builtin.h
+++ b/src/cppbash_builtin.h
@@ -26,7 +26,13 @@
 
 #include <iostream>
 #include <vector>
+#include <map>
 #include <string>
+#include <boost/scoped_ptr.hpp>
+#include <boost/functional/factory.hpp>
+#include <boost/function.hpp>
+
+#define STREAM_ARGS std::ostream &out, std::ostream &err, std::istream &in
 
 ///
 /// \class cppbash_builtin
@@ -41,7 +47,9 @@ class cppbash_builtin
     /// \param errstream where to send standard error.  Default: cerr
     /// \param instream where to get standard input from.  Default: stdin
     ///
-    cppbash_builtin(std::ostream &outstream, std::ostream &errstream, std::istream &instream);
+    cppbash_builtin(STREAM_ARGS);
+
+    virtual ~cppbash_builtin() {};
     ///
     /// \brief executes the code associated with the builtin
     /// \param bash_args arguments passed to the builtin
@@ -63,6 +71,13 @@ class cppbash_builtin
     /// \return input buffer for the builtin
     ///
     std::istream& input_buffer() {return *_inp_stream;}
+
+    static int exec(const std::string& builtin, const std::vector<std::string>& args, STREAM_ARGS)
+    {
+      boost::scoped_ptr<cppbash_builtin> p(builtins()[builtin](out,err,in));
+      return p->exec(args);
+    }
+
   protected:
     ///
     /// \var *_out_stream
@@ -79,6 +94,15 @@ class cppbash_builtin
     /// \brief current standard input stream
     ///
     std::istream *_inp_stream;
+    ///
+    /// \var builtins
+    /// \brief holds factories for creating instances of child classes
+    ///
+    typedef std::map<std::string, boost::function< cppbash_builtin*(STREAM_ARGS) >> builtins_type;
+    static builtins_type& builtins();
 };
 
+#define BUILTIN_CONSTRUCTOR(name) \
+  name ## _builtin(STREAM_ARGS) : cppbash_builtin(out, err, in) {}
+
 #endif



^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-07-08  9:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-08  9:31 [gentoo-commits] proj/libbash:master commit in: src/, /, src/builtins/tests/, src/builtins/ Petteri Räty
  -- strict thread matches above, loose matches on Subject: below --
2012-07-08  9:31 Petteri Räty
2011-06-25 10:05 Petteri Räty
2011-05-29 11:20 Petteri Räty
2011-05-25 19:42 Petteri Räty
2011-05-24 14:50 Petteri Räty
2011-03-06 12:05 Petteri Räty
2011-03-06 12:05 Petteri Räty

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox