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 1QOWDt-0005Ol-8P for garchives@archives.gentoo.org; Mon, 23 May 2011 14:35:09 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 77FD41C05A; Mon, 23 May 2011 14:34:22 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 373E01C059 for ; Mon, 23 May 2011 14:34:22 +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 C92721B402A for ; Mon, 23 May 2011 14:34:21 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 350278050A for ; Mon, 23 May 2011 14:34:21 +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: <1b777e0ff35171fc0778e051c55e782d5eb4a0ad.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/bashast.g bashast/libbashWalker.g scripts/var_expansion.bash scripts/var_expansion.bash.result src/core/interpreter.cpp src/core/interpreter.h src/core/symbols.hpp src/core/tests/symbols_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: 1b777e0ff35171fc0778e051c55e782d5eb4a0ad Date: Mon, 23 May 2011 14:34:21 +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: 012d63270388c2d73348085eaf8e7831 commit: 1b777e0ff35171fc0778e051c55e782d5eb4a0ad Author: Mu Qiao gentoo org> AuthorDate: Sat May 21 05:04:53 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Mon May 23 15:04:45 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D1b777e0f Walker: support ${*:x:y} expansion We don't support $0 for now so ${*:0:x} will give you a wrong script name. --- bashast/bashast.g | 8 +++--- bashast/libbashWalker.g | 3 +- scripts/var_expansion.bash | 9 +++++++ scripts/var_expansion.bash.result | 6 ++++ src/core/interpreter.cpp | 49 +++++++++++++++++++++++++++++++= ------ src/core/interpreter.h | 10 +++++-- src/core/symbols.hpp | 5 +--- src/core/tests/symbols_test.cpp | 5 ++- 8 files changed, 73 insertions(+), 22 deletions(-) diff --git a/bashast/bashast.g b/bashast/bashast.g index 404de43..693ee96 100644 --- a/bashast/bashast.g +++ b/bashast/bashast.g @@ -280,9 +280,7 @@ var_exp : var_name ( | LSQUARE (op=3DTIMES|op=3DAT) RSQUARE -> ^(LIST_EXPAND var_name_f= or_bang $op) | -> ^(VAR_REF var_name_for_bang) ) - | var_size_ref - | TIMES - | AT; + | var_size_ref; parameter_delete_operator : POUND -> LAZY_REMOVE_AT_START | POUNDPOUND -> REPLACE_AT_START @@ -312,7 +310,9 @@ parameter_replace_operator //either directly or through array var_name : num - | var_name_no_digit; + | var_name_no_digit + | TIMES + | AT; //Inside arithmetic we can't allow digits var_name_no_digit : name^ LSQUARE! (AT|TIMES|explicit_arithmetic) RSQUARE! diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 07d0f84..55aab88 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -327,7 +327,8 @@ var_name returns[std::string libbash_value, unsigned = index] |name { $libbash_value =3D $name.libbash_value; $index =3D $name.index; - }; + } + |TIMES { $libbash_value =3D "*"; }; =20 var_expansion returns[std::string libbash_value] @declarations { diff --git a/scripts/var_expansion.bash b/scripts/var_expansion.bash index 8db702d..8fcb943 100644 --- a/scripts/var_expansion.bash +++ b/scripts/var_expansion.bash @@ -82,3 +82,12 @@ FOO080=3D${FOO039/@([a-c]|[k-m])} FOO081=3D${FOO039//@([a-c]|[k-m])} target=3D"abc123abc" FOO082=3D"${target##+(ab[c])*([[:digit:]])}" +function positional_parameter_test(){ + FOO083=3D${*} + FOO084=3D${*:1} + FOO085=3D${*:1:2} + FOO086=3D${*: -1} + FOO087=3D${*: -2:5} + FOO088=3D${*:0} +} +positional_parameter_test 1 2 3 4 5 diff --git a/scripts/var_expansion.bash.result b/scripts/var_expansion.ba= sh.result index c01e769..5ec7c65 100644 --- a/scripts/var_expansion.bash.result +++ b/scripts/var_expansion.bash.result @@ -83,4 +83,10 @@ FOO079=3DH Wrd FOO080=3DHelo World FOO081=3DHeo Word FOO082=3Dabc +FOO083=3D1 2 3 4 5 +FOO084=3D1 2 3 4 5 +FOO085=3D1 2 +FOO086=3D5 +FOO087=3D4 5 +FOO088=3Dfilename 1 2 3 4 5 target=3Dabc123abc diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp index 80bc8be..7e7de9c 100644 --- a/src/core/interpreter.cpp +++ b/src/core/interpreter.cpp @@ -89,14 +89,49 @@ bool interpreter::is_unset_or_null(const std::string&= name, return i->second->is_null(index); } =20 +// This method temporarily supports array offset expansion for $* and $@= . +// That logic will be refactored and applied to normal array variables i= n future. +std::string interpreter::get_substring(const std::string& name, + int offset, + int length, + const unsigned index) const +{ + if(name !=3D "*" && name !=3D "@") + { + std::string value =3D resolve(name, index); + if(!get_real_offset(offset, value.size())) + return ""; + return value.substr(offset, length); + } + else + { + std::vector array; + // ${*:1} has the same content as ${*}, ${*:0} contains current scri= pt name as the first element + if(offset > 0) + offset--; + else if(offset =3D=3D 0) + // Need to replace this with the real script name + array.push_back("filename"); + if(resolve_array(name, array) && get_real_offset(offset, array.size(= ))) + { + int max_length =3D array.size() - offset; + if(length =3D=3D -1 || length > max_length) + length =3D max_length; + return boost::algorithm::join( + std::vector(array.begin() + offset, + array.begin() + offset + length), + resolve("IFS").substr(0, 1)); + } + else + return ""; + } +} + const std::string interpreter::do_substring_expansion(const std::string&= name, int offset, const unsigned ind= ex) const { - std::string value =3D resolve(name, index); - if(!get_real_offset(offset, value)) - return ""; - return value.substr(offset); + return get_substring(name, offset, -1, index); } =20 const std::string interpreter::do_substring_expansion(const std::string&= name, @@ -106,10 +141,8 @@ const std::string interpreter::do_substring_expansio= n(const std::string& name, { if(length < 0) throw interpreter_exception("length of substring expression should b= e greater or equal to zero"); - std::string value =3D resolve(name, index); - if(!get_real_offset(offset, value)) - return ""; - return value.substr(offset, length); + + return get_substring(name, offset, length, index); } =20 std::string interpreter::do_replace_expansion(const std::string& name, diff --git a/src/core/interpreter.h b/src/core/interpreter.h index aa13b4e..4c6f69c 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -73,10 +73,10 @@ class interpreter /// \param[in,out] a value/result argument referring to offset /// \param[in] the original string /// \return whether the real offset is in legal range - bool get_real_offset(int& offset, const std::string& str) const + bool get_real_offset(int& offset, const int size) const { - offset =3D (offset >=3D 0? offset : str.size() + offset); - return !(offset < 0 || offset >=3D static_cast(str.size())); + offset =3D (offset >=3D 0? offset : size + offset); + return !(offset < 0 || offset >=3D size); } =20 void get_all_elements_joined(const std::string& name, @@ -88,6 +88,10 @@ class interpreter void define_function_arguments(scope& current_stack, const std::vector& argumen= ts); =20 + std::string get_substring(const std::string& name, + int offset, + int length, + const unsigned index) const; public: =20 /// diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index 2d6b6a5..8ceb25a 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -159,15 +159,12 @@ public: } =20 /// \brief retrieve all values of the array - /// \param[out] vector that stores all array values, values in the arr= ays will - /// be cleared first + /// \param[out] vector that stores all array values template void get_all_values(std::vector& all_values) const { static converter visitor; =20 - all_values.clear(); - for(auto iter =3D value.begin(); iter !=3D value.end(); ++iter) all_values.push_back( boost::apply_visitor(visitor, iter->second)); diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_tes= t.cpp index 5853a4f..358f5b6 100644 --- a/src/core/tests/symbols_test.cpp +++ b/src/core/tests/symbols_test.cpp @@ -109,9 +109,10 @@ TEST(symbol_test, get_all_values) EXPECT_STREQ("3", string_values[2].c_str()); =20 variable a_string("foo", 10); + // Won't clear the original vector a_string.get_all_values(string_values); - EXPECT_EQ(1, string_values.size()); - EXPECT_STREQ("10", string_values[0].c_str()); + EXPECT_EQ(4, string_values.size()); + EXPECT_STREQ("1", string_values[0].c_str()); =20 variable an_int("foo", 10); vector int_values;