Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: read -A bug



% read -A array
  mit Leerzeichen am Ende     
#                        ^^^^^ with whitespace at the end
% print $#array "'$array[-1]'"
5 ''

I presume this is a bug here, since read usually strips trailing whitespace
(try without the -A).  In fact, I think the `first' variable is in the
source for exactly this purpose: this patch maintains the distinction
between whitespace and non-whitespace word separators (see the description
of $IFS in the manual) so that

% fn() { local IFS=:; read -A array; }
% fn
:one:two::
% print $#array
5

still works.  (This is not commented in the source, so if someone assures me
that actually `first' is used to signal the start of the tea break, I won't
argue.)

`read -A array' with just a newline assigns a single empty element.  Is
this correct?  It seems OK to me, since the same happens with a scalar, but
you could argue it should just set the array empty.

Anyway, it's short enough that I'll commit it and see if anyone asks
questions afterwards.  This should go into a `read' test, except there
isn't one yet.

Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.24
diff -u -r1.24 builtin.c
--- Src/builtin.c	2000/06/22 20:57:14	1.24
+++ Src/builtin.c	2000/06/23 09:26:57
@@ -3553,6 +3553,15 @@
 	    }
 	    if (c == EOF || c == '\n')
 		break;
+	    /*
+	     * `first' is non-zero if any separator we encounter is a
+	     * non-whitespace separator, which means that anything
+	     * (even an empty string) between, before or after separators
+	     * is significant.  If it is zero, we have a whitespace
+	     * separator, which shouldn't cause extra empty strings to
+	     * be emitted.  Hence the test for (*buf || first) when
+	     * we assign the result of reading a word.
+	     */
 	    if (!bslash && isep(c)) {
 		if (bptr != buf || (!iwsep(c) && first)) {
 		    first |= !iwsep(c);
@@ -3587,7 +3596,7 @@
 	    zputs(buf, stdout);
 	    putchar('\n');
 	}
-	if (!ops['e']) {
+	if (!ops['e'] && (*buf || first)) {
 	    if (ops['A']) {
 		addlinknode(readll, buf);
 		al++;

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxxx>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070



Messages sorted by: Reverse Date, Date, Thread, Author