* PATCH: shift -p
@ 2014-01-10 22:37 Peter Stephenson
2014-01-11 17:52 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2014-01-10 22:37 UTC (permalink / raw)
To: Zsh Hackers' List
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 <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: PATCH: shift -p
2014-01-10 22:37 PATCH: shift -p Peter Stephenson
@ 2014-01-11 17:52 ` Bart Schaefer
2014-01-11 17:56 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2014-01-11 17:52 UTC (permalink / raw)
To: Zsh Hackers' List
[off-list]
On Jan 10, 10:37pm, Peter Stephenson wrote:
}
} I've discovered I'm too lazy to write
}
} array="${(@)array[1,-2]}"
}
} any more, so I made "shift -p array" do it instead.
It looks as if you committed B08shift.ztst by mistake along with the
change to _mpc completion from 32245.
Strangly, that test passes even though the code to support the -p option
is not present in builtin.c.
--
Barton E. Schaefer
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: PATCH: shift -p
2014-01-11 17:52 ` Bart Schaefer
@ 2014-01-11 17:56 ` Bart Schaefer
2014-01-11 19:34 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2014-01-11 17:56 UTC (permalink / raw)
To: Zsh Hackers' List
On Jan 11, 9:52am, Bart Schaefer wrote:
}
} [off-list]
Well, botched that one, didn't I.
} It looks as if you committed B08shift.ztst by mistake along with the
} change to _mpc completion from 32245.
Ah, never mind -- you committed an empty B08shift.ztst file, not the
actual test. The "git whatchanged" output looks a little odd but no
real harm done.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: PATCH: shift -p
2014-01-11 17:56 ` Bart Schaefer
@ 2014-01-11 19:34 ` Peter Stephenson
0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2014-01-11 19:34 UTC (permalink / raw)
To: Zsh Hackers' List
On Sat, 11 Jan 2014 09:56:13 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> } It looks as if you committed B08shift.ztst by mistake along with the
> } change to _mpc completion from 32245.
>
> Ah, never mind -- you committed an empty B08shift.ztst file, not the
> actual test. The "git whatchanged" output looks a little odd but no
> real harm done.
I like to invent a little new piece of incompetence each weak, to keep
the world ticking along, but I've now committed the whole shebang
anyway.
pws
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-01-11 19:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-10 22:37 PATCH: shift -p Peter Stephenson
2014-01-11 17:52 ` Bart Schaefer
2014-01-11 17:56 ` Bart Schaefer
2014-01-11 19:34 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).