1
0
mirror of git://git.code.sf.net/p/zsh/code synced 2024-11-15 21:43:09 +01:00

32246: option "shift -p" pops arguments from end of array

This commit is contained in:
Peter Stephenson 2014-01-11 19:31:29 +00:00
parent 8fd6d5766c
commit ed44ddad22
5 changed files with 69 additions and 7 deletions

@ -1,3 +1,9 @@
2014-01-11 Peter Stephenson <p.w.stephenson@ntlworld.com>
* 32246: Doc/Zsh/builtins.yo, Src/builtin.c, Test/.distfiles,
Test/B08shift.ztst: add "shift -p" to pop arguments from the end
of arrays.
2014-01-10 Peter Stephenson <p.w.stephenson@ntlworld.com>
* Koen Wilde: 32245: Completion/Unix/Command/_mpc: complete more

@ -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) ... ])(

@ -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;
}

@ -15,6 +15,7 @@ B04read.ztst
B05eval.ztst
B06fc.ztst
B07emulate.ztst
B08shift.ztst
C01arith.ztst
C02cond.ztst
C03traps.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