From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29722 invoked by alias); 10 Jan 2014 22:43:42 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 32246 Received: (qmail 29966 invoked from network); 10 Jan 2014 22:43:36 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-Originating-IP: [86.6.157.246] X-Spam: 0 X-Authority: v=2.1 cv=KqzD2AmN c=1 sm=1 tr=0 a=BvYiZ/UW0Fmn8Wufq9dPrg==:117 a=BvYiZ/UW0Fmn8Wufq9dPrg==:17 a=NLZqzBF-AAAA:8 a=uObrxnre4hsA:10 a=kj9zAlcOel0A:10 a=vPajef5yMiEA:10 a=0RhNIY_jS9bgmaXZbFcA:9 a=LlzSUMc2qxKS0PLg:21 a=LSHcptcg-TmdDL7m:21 a=CjuIK1q_8ugA:10 a=_dQi-Dcv4p4A:10 Date: Fri, 10 Jan 2014 22:37:59 +0000 From: Peter Stephenson To: Zsh Hackers' List Subject: PATCH: shift -p Message-ID: <20140110223759.511403ea@pws-pc.ntlworld.com> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit I've discovered I'm too lazy to write array="${(@)array[1,-2]}" any more, so I made "shift -p array" do it instead. diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index 8c7bc85..ea2f68d 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -1500,12 +1500,15 @@ POSIX standard, but tt(setopt) is not. ) findex(shift) cindex(parameters, positional) -item(tt(shift) [ var(n) ] [ var(name) ... ])( +item(tt(shift) [ tt(-p) ] [ var(n) ] [ var(name) ... ])( The positional parameters tt(${)var(n)PLUS()1tt(}) ... are renamed to tt($1) ..., where var(n) is an arithmetic expression that defaults to 1. If any var(name)s are given then the arrays with these names are shifted instead of the positional parameters. + +If the option tt(-p) is given arguments are instead removed (popped) +from the end rather than the start of the array. ) findex(source) item(tt(source) var(file) [ var(arg) ... ])( diff --git a/Src/builtin.c b/Src/builtin.c index c3f0169..9bcbcf7 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -111,7 +111,7 @@ static struct builtin builtins[] = BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL), BUILTIN("set", BINF_PSPECIAL | BINF_HANDLES_OPTS, bin_set, 0, -1, 0, NULL, NULL), BUILTIN("setopt", 0, bin_setopt, 0, -1, BIN_SETOPT, NULL, NULL), - BUILTIN("shift", BINF_PSPECIAL, bin_shift, 0, -1, 0, NULL, NULL), + BUILTIN("shift", BINF_PSPECIAL, bin_shift, 0, -1, 0, "p", NULL), BUILTIN("source", BINF_PSPECIAL, bin_dot, 1, -1, 0, NULL, NULL), BUILTIN("suspend", 0, bin_suspend, 0, 0, 0, "f", NULL), BUILTIN("test", BINF_HANDLES_OPTS, bin_test, 0, -1, BIN_TEST, NULL, NULL), @@ -4509,7 +4509,7 @@ bin_print(char *name, char **args, Options ops, int func) /**/ int -bin_shift(char *name, char **argv, UNUSED(Options ops), UNUSED(int func)) +bin_shift(char *name, char **argv, Options ops, UNUSED(int func)) { int num = 1, l, ret = 0; char **s; @@ -4533,7 +4533,19 @@ bin_shift(char *name, char **argv, UNUSED(Options ops), UNUSED(int func)) ret++; continue; } - s = zarrdup(s + num); + if (OPT_ISSET(ops,'p')) { + char **s2, **src, **dst; + int count; + l = arrlen(s); + src = s; + dst = s2 = (char **)zalloc((l - num + 1) * sizeof(char *)); + for (count = l - num; count; count--) + *dst++ = ztrdup(*src++); + *dst = NULL; + s = s2; + } else { + s = zarrdup(s + num); + } setaparam(*argv, s); } } else { @@ -4542,9 +4554,16 @@ bin_shift(char *name, char **argv, UNUSED(Options ops), UNUSED(int func)) ret = 1; } else { s = zalloc((l - num + 1) * sizeof(char *)); - memcpy(s, pparams + num, (l - num + 1) * sizeof(char *)); - while (num--) - zsfree(pparams[num]); + if (OPT_ISSET(ops,'p')) { + memcpy(s, pparams, (l - num) * sizeof(char *)); + s[l-num] = NULL; + while (num--) + zsfree(pparams[l-1-num]); + } else { + memcpy(s, pparams + num, (l - num + 1) * sizeof(char *)); + while (num--) + zsfree(pparams[num]); + } zfree(pparams, (l + 1) * sizeof(char *)); pparams = s; } diff --git a/Test/.distfiles b/Test/.distfiles index 1c01028..5826e75 100644 --- a/Test/.distfiles +++ b/Test/.distfiles @@ -15,6 +15,7 @@ B04read.ztst B05eval.ztst B06fc.ztst B07emulate.ztst +B08shift.ztst C01arith.ztst C02cond.ztst C03traps.ztst diff --git a/Test/B08shift.ztst b/Test/B08shift.ztst index e69de29..0aa9226 100644 --- a/Test/B08shift.ztst +++ b/Test/B08shift.ztst @@ -0,0 +1,33 @@ +# Test the shift builtin. + +%test + + set -- one two three four five six seven eight nine ten + shift + print $* + shift 2 + print $* + shift -p 3 + print $* + shift -p + print $* +0:shifting positional parameters +>two three four five six seven eight nine ten +>four five six seven eight nine ten +>four five six seven +>four five six + + array=(yan tan tether mether pip azer sezar akker conter dick) + shift 2 array + print $array + shift array + print $array + shift -p 3 array + print $array + shift -p array + print $array +0:shifting array +>tether mether pip azer sezar akker conter dick +>mether pip azer sezar akker conter dick +>mether pip azer sezar +>mether pip azer -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/