public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/, utils/
@ 2011-06-16 16:53 Petteri Räty
  0 siblings, 0 replies; only message in thread
From: Petteri Räty @ 2011-06-16 16:53 UTC (permalink / raw
  To: gentoo-commits

commit:     c06b64823924adfefd9541f5bb0aac7dba602440
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Jun 13 14:43:47 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Mon Jun 13 15:02:48 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=c06b6482

Core: first support for bash options

This commit is intended to support $- so no method for changing bash
options is added.

---
 src/core/interpreter.cpp            |   41 +++++++++++++++++++++++++++++++++++
 src/core/interpreter.h              |    4 +++
 src/core/tests/interpreter_test.cpp |    7 ++++++
 utils/variable_printer.cpp          |    2 +-
 4 files changed, 53 insertions(+), 1 deletions(-)

diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 509640e..eca7987 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -33,12 +33,29 @@
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/foreach.hpp>
+#include <boost/range/adaptor/filtered.hpp>
 #include <boost/range/adaptor/map.hpp>
 #include <boost/range/algorithm/copy.hpp>
 
 #include "core/bash_ast.h"
 #include "core/unset_exception.h"
 
+namespace
+{
+  std::string get_options(std::map<char, bool>& options)
+  {
+    std::string result;
+    boost::copy(options | boost::adaptors::map_keys
+                        | boost::adaptors::filtered(
+                          [&](char option) -> bool {
+                            return options[option];
+                          }
+                        ),
+                back_inserter(result));
+    return result;
+  }
+}
+
 interpreter::interpreter(): _out(&std::cout), _err(&std::cerr), _in(&std::cin), additional_options(
     {
       {"autocd", false},
@@ -80,9 +97,33 @@ interpreter::interpreter(): _out(&std::cout), _err(&std::cerr), _in(&std::cin),
       {"sourcepath", false},
       {"xpg_echo", false},
     }
+    ), options(
+    {
+      {'a', false},
+      {'b', false},
+      {'e', false},
+      {'f', false},
+      {'h', true},
+      {'k', false},
+      {'m', false},
+      {'n', false},
+      {'p', false},
+      {'t', false},
+      {'u', false},
+      {'v', false},
+      {'x', false},
+      {'B', true},
+      {'C', false},
+      {'E', false},
+      {'H', false},
+      {'P', false},
+      {'T', false},
+    }
     )
 {
   define("IFS", " \t\n");
+  // We do not support the options set by the shell itself (such as the -i option)
+  define("-", get_options(options));
 }
 
 std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name) const

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 428544a..3f43b0e 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -69,6 +69,10 @@ class interpreter: public boost::noncopyable
   // std::map is chosen for sorted output in shopt -p
   std::map<std::string, bool> additional_options;
 
+  // std::map is chosen for sorted output in $-. The order may not be the same
+  // as bash implementation.
+  std::map<char, bool> options;
+
   /// \brief calculate the correct offset when offset < 0 and check whether
   ///        the real offset is in legal range
   /// \param[in,out] a value/result argument referring to offset

diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
index d7a7418..54971ab 100644
--- a/src/core/tests/interpreter_test.cpp
+++ b/src/core/tests/interpreter_test.cpp
@@ -247,3 +247,10 @@ TEST(interpreter, bash_additional_option)
   walker.set_additional_option("extglob", true);
   EXPECT_TRUE(walker.get_additional_option("extglob"));
 }
+
+TEST(interpreter, bash_option)
+{
+  interpreter walker;
+
+  EXPECT_STREQ("Bh", walker.resolve<std::string>("-").c_str());
+}

diff --git a/utils/variable_printer.cpp b/utils/variable_printer.cpp
index 9104273..84cd252 100644
--- a/utils/variable_printer.cpp
+++ b/utils/variable_printer.cpp
@@ -33,7 +33,7 @@
 
 static const std::vector<std::string> special_variables
 {
-  "IFS", "?", "*", "0"
+  "IFS", "?", "*", "0", "-"
 };
 
 static std::string get_src_dir()



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-06-16 16:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-16 16:53 [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/, utils/ 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