changeset 17948:88a13cbc41e7

yesno: make EOL optional in ENABLE_NLS case also yesno behaves differently in a corner case depending on ENABLE_NLS. With an input of "y" followed by an EOF the input is considered to be "no", because the last character is replaced with '\0'. It was assumed that there is a newline, which doesn't have to be true. If ENABLE_NLS is not set, getchar() reads y and accepts it as "yes", looping through more getchar() calls until reaching newline or EOF. * lib/yesno.c (yesno): Check for EOL before replacing. * tests/test-yesno.sh: Add a test case (test along with gettext).
author Tobias Stoeckmann <tobias@stoeckmann.org>
date Tue, 24 Mar 2015 12:15:27 +0000
parents 101ada775ae6
children b25f1b45e6fd
files ChangeLog lib/yesno.c tests/test-yesno.sh
diffstat 3 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Mar 22 19:47:24 2015 -0700
+++ b/ChangeLog	Tue Mar 24 12:15:27 2015 +0000
@@ -1,3 +1,9 @@
+2015-03-24  Tobias Stoeckmann  <tobias@stoeckmann.org>
+
+	yesno: make EOL optional in ENABLE_NLS case also (trival)
+	* lib/yesno.c (yesno): Check for EOL before replacing.
+	* tests/test-yesno.sh: Add a test case (test along with gettext).
+
 2015-03-22  Paul Eggert  <eggert@cs.ucla.edu>
 
 	fdopendir-tests: test it does not close its arg
--- a/lib/yesno.c	Sun Mar 22 19:47:24 2015 -0700
+++ b/lib/yesno.c	Tue Mar 24 12:15:27 2015 +0000
@@ -42,7 +42,10 @@
     yes = false;
   else
     {
-      response[response_len - 1] = '\0';
+      /* Remove EOL if present as that's not part of the matched response,
+         and not matched by $ for example.  */
+      if (response[response_len - 1] == '\n')
+        response[response_len - 1] = '\0';
       yes = (0 < rpmatch (response));
     }
 
--- a/tests/test-yesno.sh	Sun Mar 22 19:47:24 2015 -0700
+++ b/tests/test-yesno.sh	Tue Mar 24 12:15:27 2015 +0000
@@ -49,6 +49,14 @@
 LC_ALL=C tr -d "$cr" < out1.tmp > out.tmp || fail=1
 cmp xout.tmp out.tmp || fail=1
 
+# Test for behavior with no EOL at EOF
+cat <<EOF > xout.tmp
+Y
+EOF
+printf y | test-yesno 1 > out1.tmp || fail=1
+LC_ALL=C tr -d "$cr" < out1.tmp > out.tmp || fail=1
+cmp xout.tmp out.tmp || fail=1
+
 # Test for behavior on EOF
 cat <<EOF > xout.tmp
 N