svn commit: [25910] trunk/busybox/shell: ash_test/ash-read

vda at busybox.net vda at busybox.net
Tue Mar 31 19:18:18 UTC 2009


Author: vda
Date: 2009-03-31 19:18:17 +0000 (Tue, 31 Mar 2009)
New Revision: 25910

Log:
ash: fix $IFS handling in read. closes bug 235



Added:
   trunk/busybox/shell/ash_test/ash-read/read_ifs.right
   trunk/busybox/shell/ash_test/ash-read/read_ifs.tests

Modified:
   trunk/busybox/shell/ash.c


Changeset:
Modified: trunk/busybox/shell/ash.c
===================================================================
--- trunk/busybox/shell/ash.c	2009-03-31 17:24:49 UTC (rev 25909)
+++ trunk/busybox/shell/ash.c	2009-03-31 19:18:17 UTC (rev 25910)
@@ -12574,7 +12574,7 @@
 #endif
 
 	status = 0;
-	startword = 1;
+	startword = 2;
 	backslash = 0;
 #if ENABLE_ASH_READ_TIMEOUT
 	if (timeout) /* NB: ensuring end_ms is nonzero */
@@ -12582,6 +12582,8 @@
 #endif
 	STARTSTACKSTR(p);
 	do {
+		const char *is_ifs;
+
 #if ENABLE_ASH_READ_TIMEOUT
 		if (end_ms) {
 			struct pollfd pfd[1];
@@ -12611,25 +12613,34 @@
 			continue;
 		}
 		if (!rflag && c == '\\') {
-			backslash++;
+			backslash = 1;
 			continue;
 		}
 		if (c == '\n')
 			break;
-		if (startword && *ifs == ' ' && strchr(ifs, c)) {
-			continue;
+		is_ifs = strchr(ifs, c);
+		if (startword && is_ifs) {
+			if (isspace(c))
+				continue;
+			/* non-space ifs char */
+			startword--;
+			if (startword == 1) /* first one? */
+				continue;
 		}
 		startword = 0;
-		if (ap[1] != NULL && strchr(ifs, c) != NULL) {
+		if (ap[1] != NULL && is_ifs) {
+			const char *beg;
 			STACKSTRNUL(p);
-			setvar(*ap, stackblock(), 0);
+			beg = stackblock();
+			setvar(*ap, beg, 0);
 			ap++;
-			startword = 1;
+			/* can we skip one non-space ifs? (2: yes) */
+			startword = isspace(c) ? 2 : 1;
 			STARTSTACKSTR(p);
-		} else {
+			continue;
+		}
  put:
-			STPUTC(c, p);
-		}
+		STPUTC(c, p);
 	}
 /* end of do {} while: */
 #if ENABLE_ASH_READ_NCHARS
@@ -12643,8 +12654,8 @@
 #endif
 
 	STACKSTRNUL(p);
-	/* Remove trailing blanks */
-	while ((char *)stackblock() <= --p && strchr(ifs, *p) != NULL)
+	/* Remove trailing space ifs chars */
+	while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL)
 		*p = '\0';
 	setvar(*ap, stackblock(), 0);
 	while (*++ap != NULL)

Added: trunk/busybox/shell/ash_test/ash-read/read_ifs.right
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_ifs.right	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_ifs.right	2009-03-31 19:18:17 UTC (rev 25910)
@@ -0,0 +1,7 @@
+.a. .b. .c.
+.a. .b. .c.
+.a. .. .b,c.
+.a. .. .b,c.
+.a. .. .c.
+.a. .. .c. .d.
+.a. .. .b,c,d  ,  ,.

Added: trunk/busybox/shell/ash_test/ash-read/read_ifs.tests
===================================================================
--- trunk/busybox/shell/ash_test/ash-read/read_ifs.tests	                        (rev 0)
+++ trunk/busybox/shell/ash_test/ash-read/read_ifs.tests	2009-03-31 19:18:17 UTC (rev 25910)
@@ -0,0 +1,7 @@
+printf 'a\t\tb\tc\n' | ( IFS=$(printf "\t") read a b c; echo ".$a. .$b. .$c." )
+printf 'a\t\tb\tc\n' | ( IFS=$(printf " \t") read a b c; echo ".$a. .$b. .$c." )
+printf 'a,,b,c\n'    | ( IFS="," read a b c; echo ".$a. .$b. .$c." )
+printf 'a,,b,c\n'    | ( IFS=" ," read a b c; echo ".$a. .$b. .$c." )
+printf 'a ,, c\n'    | ( IFS=" ," read a b c; echo ".$a. .$b. .$c." )
+printf 'a ,, c d\n'  | ( IFS=" ," read a b c d; echo ".$a. .$b. .$c. .$d." )
+printf ' a,,b,c,d  ,  ,\n' | ( IFS=" ," read a b c; echo ".$a. .$b. .$c." )


Property changes on: trunk/busybox/shell/ash_test/ash-read/read_ifs.tests
___________________________________________________________________
Added: svn:executable
   + *



More information about the busybox-cvs mailing list