From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QOFlW-0000CY-Cl for garchives@archives.gentoo.org; Sun, 22 May 2011 21:00:47 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8A28D1C08F; Sun, 22 May 2011 21:00:24 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 3EC401C08E for ; Sun, 22 May 2011 21:00:24 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 95E791B401B for ; Sun, 22 May 2011 21:00:23 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id C60CC8050D for ; Sun, 22 May 2011 21:00:22 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: <13f7e362484d1f486bd51be08e99da397fba2d4f.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g scripts/test_expr.bash scripts/test_expr.bash.result src/core/bash_condition.cpp src/core/bash_condition.h src/core/tests/bash_condition_test.cpp X-VCS-Directories: scripts/ src/core/ bashast/ src/core/tests/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 13f7e362484d1f486bd51be08e99da397fba2d4f Date: Sun, 22 May 2011 21:00:22 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 439cd07aec5e489f009095a6e64c8acc commit: 13f7e362484d1f486bd51be08e99da397fba2d4f Author: Mu Qiao gentoo org> AuthorDate: Wed May 18 07:47:35 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Sun May 22 16:16:34 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D13f7e362 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 =20 common_condition returns[bool status] // -eq, -ne, -lt, -le, -gt, or -ge for arithmetic. -nt -ot -ef for file= s - :^(NAME left_str=3Dstring_expr right_str=3Dstring_expr) + :^(NAME left_str=3Dstring_expr right_str=3Dstring_expr) { + $status =3D 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 f= iles |^(op=3DLETTER string_expr) { $status =3D internal::test_unary(*reinterpret_cast(op->g= etToken(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=3Dbcd is treated as a simple string [[ abc=3Dbcd && abc =3D=3D 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.resul= t 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 =3D stat(lhs.c_str(), &lst); + rstatus =3D stat(rhs.c_str(), &rst); + if(op =3D=3D 'e' && (lstatus < 0 || rstatus < 0)) + return false; + + switch(op) + { + case 'n': + /* -nt */ + return ((lstatus > rstatus) || (lstatus =3D=3D 0 && lst.st_mtime= > rst.st_mtime)); + case 'o': + /* -ot */ + return ((lstatus < rstatus) || (rstatus =3D=3D 0 && lst.st_mtime= < rst.st_mtime)); + case 'e': + /* -ef */ + return (lst.st_dev =3D=3D rst.st_dev && lst.st_ino =3D=3D 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() !=3D 2) + throw interpreter_exception("Unrecognized operator " + op); + + try + { + if(op =3D=3D "nt") + return file_comp('n', lhs, rhs); + else if(op =3D=3D "ot") + return file_comp('o', lhs, rhs); + else if(op =3D=3D "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 =3D=3D "eq") + return boost::lexical_cast(lhs) =3D=3D boost::lexical_cast(rhs); + else if(op =3D=3D "ne") + return boost::lexical_cast(lhs) !=3D boost::lexical_cast= (rhs); + else if(op =3D=3D "lt") + return boost::lexical_cast(lhs) < boost::lexical_cast(rh= s); + else if(op =3D=3D "le") + return boost::lexical_cast(lhs) <=3D boost::lexical_cast= (rhs); + else if(op =3D=3D "gt") + return boost::lexical_cast(lhs) > boost::lexical_cast(rh= s); + else if(op =3D=3D "ge") + return boost::lexical_cast(lhs) >=3D boost::lexical_cast= (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); } =20 #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) =20 EXPECT_THROW(internal::test_unary('o', "extglob"), interpreter_excepti= on); } + +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")); +}