From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/
Date: Sun, 22 May 2011 21:00:22 +0000 (UTC) [thread overview]
Message-ID: <13f7e362484d1f486bd51be08e99da397fba2d4f.betelgeuse@gentoo> (raw)
commit: 13f7e362484d1f486bd51be08e99da397fba2d4f
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed May 18 07:47:35 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun May 22 16:16:34 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=13f7e362
Walker: support binary operators in keyword test
We do not support arithmetic expressions inside keyword test for
now.
---
bashast/libbashWalker.g | 4 +-
scripts/test_expr.bash | 2 +
scripts/test_expr.bash.result | 2 +
src/core/bash_condition.cpp | 69 ++++++++++++++++++++++++++++++++
src/core/bash_condition.h | 4 ++
src/core/tests/bash_condition_test.cpp | 42 +++++++++++++++++++-
6 files changed, 121 insertions(+), 2 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 737c7a7..1ae6616 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -509,7 +509,9 @@ cond_expr
common_condition returns[bool status]
// -eq, -ne, -lt, -le, -gt, or -ge for arithmetic. -nt -ot -ef for files
- :^(NAME left_str=string_expr right_str=string_expr)
+ :^(NAME left_str=string_expr right_str=string_expr) {
+ $status = internal::test_binary(walker->get_string($NAME), left_str.libbash_value, right_str.libbash_value);
+ }
// -o for shell option, -z -n for string, -abcdefghkprstuwxOGLSN for files
|^(op=LETTER string_expr) {
$status = internal::test_unary(*reinterpret_cast<const char *>(op->getToken(op)->start),
diff --git a/scripts/test_expr.bash b/scripts/test_expr.bash
index 91c24fd..f89287b 100644
--- a/scripts/test_expr.bash
+++ b/scripts/test_expr.bash
@@ -36,3 +36,5 @@ echo $? # 0
# abc=bcd is treated as a simple string
[[ abc=bcd && abc == abc ]] || echo wrong
[[ -a "/" ]] && echo "true10"
+[[ . -ef . ]] && echo "true11"
+[[ 2 -ge 2 ]] && echo "true12"
diff --git a/scripts/test_expr.bash.result b/scripts/test_expr.bash.result
index 4be03db..d045597 100644
--- a/scripts/test_expr.bash.result
+++ b/scripts/test_expr.bash.result
@@ -17,3 +17,5 @@ true7
true8
true9
true10
+true11
+true12
diff --git a/src/core/bash_condition.cpp b/src/core/bash_condition.cpp
index 61a5826..48b523e 100644
--- a/src/core/bash_condition.cpp
+++ b/src/core/bash_condition.cpp
@@ -113,3 +113,72 @@ bool internal::test_unary(char op, const std::string& target)
return test_file_stat(op, target);
}
}
+
+namespace
+{
+ bool file_comp(char op,
+ const std::string& lhs,
+ const std::string& rhs)
+ {
+ struct stat lst, rst;
+ int lstatus, rstatus;
+
+ lstatus = stat(lhs.c_str(), &lst);
+ rstatus = stat(rhs.c_str(), &rst);
+ if(op == 'e' && (lstatus < 0 || rstatus < 0))
+ return false;
+
+ switch(op)
+ {
+ case 'n':
+ /* -nt */
+ return ((lstatus > rstatus) || (lstatus == 0 && lst.st_mtime > rst.st_mtime));
+ case 'o':
+ /* -ot */
+ return ((lstatus < rstatus) || (rstatus == 0 && lst.st_mtime < rst.st_mtime));
+ case 'e':
+ /* -ef */
+ return (lst.st_dev == rst.st_dev && lst.st_ino == rst.st_ino);
+ default:
+ throw interpreter_exception(std::string("Unrecognized option for file test ") + op);
+ }
+ }
+}
+
+bool internal::test_binary(const std::string& op,
+ const std::string& lhs,
+ const std::string& rhs)
+{
+ if(op.size() != 2)
+ throw interpreter_exception("Unrecognized operator " + op);
+
+ try
+ {
+ if(op == "nt")
+ return file_comp('n', lhs, rhs);
+ else if(op == "ot")
+ return file_comp('o', lhs, rhs);
+ else if(op == "ef")
+ return file_comp('e', lhs, rhs);
+ // We do not support arithmetic expressions inside keyword test for now.
+ // So the operands can only be raw integers.
+ else if(op == "eq")
+ return boost::lexical_cast<int>(lhs) == boost::lexical_cast<int>(rhs);
+ else if(op == "ne")
+ return boost::lexical_cast<int>(lhs) != boost::lexical_cast<int>(rhs);
+ else if(op == "lt")
+ return boost::lexical_cast<int>(lhs) < boost::lexical_cast<int>(rhs);
+ else if(op == "le")
+ return boost::lexical_cast<int>(lhs) <= boost::lexical_cast<int>(rhs);
+ else if(op == "gt")
+ return boost::lexical_cast<int>(lhs) > boost::lexical_cast<int>(rhs);
+ else if(op == "ge")
+ return boost::lexical_cast<int>(lhs) >= boost::lexical_cast<int>(rhs);
+ else
+ throw interpreter_exception("Unrecognized operator " + op);
+ }
+ catch(boost::bad_lexical_cast& e)
+ {
+ return false;
+ }
+}
diff --git a/src/core/bash_condition.h b/src/core/bash_condition.h
index 4e9cfa3..8723c28 100644
--- a/src/core/bash_condition.h
+++ b/src/core/bash_condition.h
@@ -29,6 +29,10 @@
namespace internal
{
bool test_unary(char op, const std::string& target);
+
+ bool test_binary(const std::string& op,
+ const std::string& lhs,
+ const std::string& rhs);
}
#endif
diff --git a/src/core/tests/bash_condition_test.cpp b/src/core/tests/bash_condition_test.cpp
index 8bcf4b3..57ab4e9 100644
--- a/src/core/tests/bash_condition_test.cpp
+++ b/src/core/tests/bash_condition_test.cpp
@@ -63,7 +63,7 @@ namespace
) << "Can't create " << positive << " for test";
set_time(positive, 0, 0);
EXPECT_NE(-1, creat(negative.c_str(), 0)) << "Can't create " << negative << " for test";
- set_time(negative, 1, 0);
+ set_time(negative, 2, 1);
EXPECT_EQ(0, symlink(positive.c_str(), test_link.c_str()));
EXPECT_EQ(0, mkfifo(test_fifo.c_str(), 0));
}
@@ -137,3 +137,43 @@ TEST(bash_condition, string_unary_operator)
EXPECT_THROW(internal::test_unary('o', "extglob"), interpreter_exception);
}
+
+TEST_F(file_test, binary_operator)
+{
+ EXPECT_TRUE(internal::test_binary("nt", negative, positive));
+ EXPECT_FALSE(internal::test_binary("ot", negative, positive));
+
+ EXPECT_TRUE(internal::test_binary("ot", positive, negative));
+ EXPECT_FALSE(internal::test_binary("nt", positive, negative));
+
+ EXPECT_FALSE(internal::test_binary("ot", positive, positive));
+ EXPECT_FALSE(internal::test_binary("nt", positive, positive));
+
+ EXPECT_TRUE(internal::test_binary("ef", positive, positive));
+ EXPECT_FALSE(internal::test_binary("ef", positive, negative));
+}
+
+TEST(bash_condition, arithmetic_operator)
+{
+ EXPECT_TRUE(internal::test_binary("eq", "1", "1"));
+ EXPECT_FALSE(internal::test_binary("eq", "2", "1"));
+
+ EXPECT_TRUE(internal::test_binary("ne", "2", "1"));
+ EXPECT_FALSE(internal::test_binary("ne", "1", "1"));
+
+ EXPECT_TRUE(internal::test_binary("lt", "0", "1"));
+ EXPECT_FALSE(internal::test_binary("lt", "1", "1"));
+ EXPECT_FALSE(internal::test_binary("lt", "2", "1"));
+
+ EXPECT_TRUE(internal::test_binary("le", "0", "1"));
+ EXPECT_TRUE(internal::test_binary("le", "1", "1"));
+ EXPECT_FALSE(internal::test_binary("le", "2", "1"));
+
+ EXPECT_TRUE(internal::test_binary("gt", "1", "0"));
+ EXPECT_FALSE(internal::test_binary("gt", "1", "1"));
+ EXPECT_FALSE(internal::test_binary("gt", "0", "1"));
+
+ EXPECT_TRUE(internal::test_binary("ge", "1", "1"));
+ EXPECT_TRUE(internal::test_binary("ge", "2", "1"));
+ EXPECT_FALSE(internal::test_binary("ge", "0", "1"));
+}
next reply other threads:[~2011-05-22 21:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-22 21:00 Petteri Räty [this message]
-- strict thread matches above, loose matches on Subject: below --
2012-06-03 9:08 [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/ Petteri Räty
2011-06-03 12:43 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-04-28 6:19 Petteri Räty
2011-04-14 4:50 Petteri Räty
2011-04-06 15:07 Petteri Räty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=13f7e362484d1f486bd51be08e99da397fba2d4f.betelgeuse@gentoo \
--to=betelgeuse@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox