1 |
commit: a4da23bd461a004396138e889ca1111a3cfc5d9d |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed May 25 08:22:48 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed May 25 10:13:52 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=a4da23bd |
7 |
|
8 |
Core: support setting and getting bash option |
9 |
|
10 |
--- |
11 |
src/core/interpreter.cpp | 64 +++++++++++++++++++++++++++++++++++ |
12 |
src/core/interpreter.h | 18 ++++++++-- |
13 |
src/core/tests/interpreter_test.cpp | 12 ++++++ |
14 |
3 files changed, 90 insertions(+), 4 deletions(-) |
15 |
|
16 |
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp |
17 |
index 7e7de9c..2337e87 100644 |
18 |
--- a/src/core/interpreter.cpp |
19 |
+++ b/src/core/interpreter.cpp |
20 |
@@ -39,6 +39,52 @@ |
21 |
|
22 |
#include "libbashWalker.h" |
23 |
|
24 |
+interpreter::interpreter(): out(&std::cout), err(&std::cerr), in(&std::cin), bash_options( |
25 |
+ { |
26 |
+ {"autocd", false}, |
27 |
+ {"cdable_vars", false}, |
28 |
+ {"cdspell", false}, |
29 |
+ {"checkhash", false}, |
30 |
+ {"checkjobs", false}, |
31 |
+ {"checkwinsize", false}, |
32 |
+ {"cmdhist", false}, |
33 |
+ {"compat31", false}, |
34 |
+ {"dirspell", false}, |
35 |
+ {"dotglob", false}, |
36 |
+ {"execfail", false}, |
37 |
+ {"expand_aliases", false}, |
38 |
+ {"extdebug", false}, |
39 |
+ {"extglob", false}, |
40 |
+ {"extquote", false}, |
41 |
+ {"failglob", false}, |
42 |
+ {"force_fignore", false}, |
43 |
+ {"globstar", false}, |
44 |
+ {"gnu_errfmt", false}, |
45 |
+ {"histappend", false}, |
46 |
+ {"histreedit", false}, |
47 |
+ {"histverify", false}, |
48 |
+ {"hostcomplete", false}, |
49 |
+ {"huponexit", false}, |
50 |
+ {"interactive", false}, |
51 |
+ {"lithist", false}, |
52 |
+ {"login_shell", false}, |
53 |
+ {"mailwarn", false}, |
54 |
+ {"no_empty_cmd_completion", false}, |
55 |
+ {"nocaseglob", false}, |
56 |
+ {"nocasematch", false}, |
57 |
+ {"nullglob", false}, |
58 |
+ {"progcomp", false}, |
59 |
+ {"promptvars", false}, |
60 |
+ {"restricted", false}, |
61 |
+ {"shift_verbose", false}, |
62 |
+ {"sourcepath", false}, |
63 |
+ {"xpg_echo", false}, |
64 |
+ } |
65 |
+ ) |
66 |
+{ |
67 |
+ define("IFS", " \t\n"); |
68 |
+} |
69 |
+ |
70 |
std::string interpreter::get_string(pANTLR3_BASE_TREE node) |
71 |
{ |
72 |
pANTLR3_COMMON_TOKEN token = node->getToken(node); |
73 |
@@ -315,3 +361,21 @@ void interpreter::unset(const std::string& name, |
74 |
else |
75 |
iter->second->unset_value(index); |
76 |
} |
77 |
+ |
78 |
+bool interpreter::get_option(const std::string& name) const |
79 |
+{ |
80 |
+ auto iter = bash_options.find(name); |
81 |
+ if(iter == bash_options.end()) |
82 |
+ throw interpreter_exception("Invalid bash option"); |
83 |
+ |
84 |
+ return iter->second; |
85 |
+} |
86 |
+ |
87 |
+void interpreter::set_option(const std::string& name, bool value) |
88 |
+{ |
89 |
+ auto iter = bash_options.find(name); |
90 |
+ if(iter == bash_options.end()) |
91 |
+ throw interpreter_exception("Invalid bash option"); |
92 |
+ |
93 |
+ iter->second = value; |
94 |
+} |
95 |
|
96 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
97 |
index 4c6f69c..3c85450 100644 |
98 |
--- a/src/core/interpreter.h |
99 |
+++ b/src/core/interpreter.h |
100 |
@@ -68,6 +68,8 @@ class interpreter |
101 |
|
102 |
std::istream* in; |
103 |
|
104 |
+ std::unordered_map<std::string, bool> bash_options; |
105 |
+ |
106 |
/// \brief calculate the correct offset when offset < 0 and check whether |
107 |
/// the real offset is in legal range |
108 |
/// \param[in,out] a value/result argument referring to offset |
109 |
@@ -114,10 +116,7 @@ public: |
110 |
} |
111 |
}; |
112 |
|
113 |
- interpreter(): out(&std::cout), err(&std::cerr), in(&std::cin) |
114 |
- { |
115 |
- define("IFS", " \t\n"); |
116 |
- } |
117 |
+ interpreter(); |
118 |
|
119 |
/// |
120 |
/// \brief return the number of variables |
121 |
@@ -661,6 +660,17 @@ public: |
122 |
//. \param[out] the splitted result will be appended to output |
123 |
void split_word(const std::string& word, std::vector<std::string>& output); |
124 |
|
125 |
+ /// \brief get the status of shell optional behavior |
126 |
+ /// \param the option name |
127 |
+ /// \return zero unless the name is not a valid shell option |
128 |
+ bool get_option(const std::string& name) const; |
129 |
+ |
130 |
+ /// \brief set the status of shell optional behavior |
131 |
+ /// \param the option name |
132 |
+ /// \param[in] true if option is enabled, false otherwise |
133 |
+ /// \return zero unless the name is not a valid shell option |
134 |
+ void set_option(const std::string& name, bool value); |
135 |
+ |
136 |
/// \brief perform expansion like ${var//foo/bar} |
137 |
/// \param the value to be expanded |
138 |
/// \param the pattern used to match the value |
139 |
|
140 |
diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp |
141 |
index a5e4ff9..ab24449 100644 |
142 |
--- a/src/core/tests/interpreter_test.cpp |
143 |
+++ b/src/core/tests/interpreter_test.cpp |
144 |
@@ -189,3 +189,15 @@ TEST(interpreter, word_split) |
145 |
EXPECT_STREQ("foo", splitted_values[0].c_str()); |
146 |
EXPECT_STREQ("bar", splitted_values[1].c_str()); |
147 |
} |
148 |
+ |
149 |
+TEST(interpreter, bash_option) |
150 |
+{ |
151 |
+ interpreter walker; |
152 |
+ |
153 |
+ EXPECT_THROW(walker.set_option("not exist", false), interpreter_exception); |
154 |
+ EXPECT_THROW(walker.get_option("not exist"), interpreter_exception); |
155 |
+ |
156 |
+ EXPECT_FALSE(walker.get_option("extglob")); |
157 |
+ walker.set_option("extglob", true); |
158 |
+ EXPECT_TRUE(walker.get_option("extglob")); |
159 |
+} |