about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <p.w.stephenson@ntlworld.com>2022-03-03 19:19:35 +0000
committerPeter Stephenson <p.w.stephenson@ntlworld.com>2022-03-03 19:19:35 +0000
commit1640457f4755a4a5248f5e9c0106fa01f8f1e9ff (patch)
tree17e020e967e53bd2dd04c9a1604dede4a6eb2064
parent2be2efc122b70a84a9debeaf0a11b99e11df3758 (diff)
downloadzsh-1640457f4755a4a5248f5e9c0106fa01f8f1e9ff.tar.gz
zsh-1640457f4755a4a5248f5e9c0106fa01f8f1e9ff.tar.xz
zsh-1640457f4755a4a5248f5e9c0106fa01f8f1e9ff.zip
49792: Non-interative shell input is line buffered.
-rw-r--r--ChangeLog3
-rw-r--r--Src/input.c21
-rw-r--r--Test/A01grammar.ztst9
3 files changed, 26 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index a5ba0fa6a..ec015533a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2022-03-03  Peter Stephenson  <p.w.stephenson@ntlworld.com>
 
+	* 49792: Src/input.c, Test/A01grammar.ztst: Use line buffering
+	for non-interactive input.
+
 	* 49787: Test/W02jobs.ztst, Test/W03jobparameters.ztst: test for
 	jobs fix in 49783.
 
diff --git a/Src/input.c b/Src/input.c
index caeaff0e3..c59232681 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -223,13 +223,20 @@ shingetchar(void)
 	return STOUC(*shinbufptr++);
 
     shinbufreset();
-    do {
-	errno = 0;
-	nread = read(SHIN, shinbuffer, SHINBUFSIZE);
-    } while (nread < 0 && errno == EINTR);
-    if (nread <= 0)
-	return -1;
-    shinbufendptr = shinbuffer + nread;
+    for (;;) {
+       errno = 0;
+       nread = read(SHIN, shinbufendptr, 1);
+       if (nread > 0) {
+           /* Use line buffering (POSIX requirement) */
+           if (*shinbufendptr++ == '\n')
+               break;
+           if (shinbufendptr == shinbuffer + SHINBUFSIZE)
+               break;
+       } else if (nread == 0 || errno != EINTR)
+           break;
+    }
+    if (shinbufendptr == shinbuffer)
+        return -1;
     return STOUC(*shinbufptr++);
 }
 
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
index 4e39a8f3c..0312fe94e 100644
--- a/Test/A01grammar.ztst
+++ b/Test/A01grammar.ztst
@@ -961,3 +961,12 @@ F:Note that the behaviour of 'exit' inside try-list inside a function is unspeci
 F:This test was written to ensure the behaviour doesn't change silently.
 F:If this test fails during development, it *might* be appropriate to change
 F:its expectations.
+
+ (
+   export VALUE=first
+   print -l 'echo Value is $VALUE' 'VALUE=second sh' 'echo Value is $VALUE' |
+   $ZTST_testdir/../Src/zsh -f
+ )
+0:Non-interactive shell command input is line buffered
+>Value is first
+>Value is second