]> Savannah Git Hosting - gnulib.git/commitdiff
yesno: make EOL optional in ENABLE_NLS case also
authorTobias Stoeckmann <tobias@stoeckmann.org>
Tue, 24 Mar 2015 12:15:27 +0000 (12:15 +0000)
committerPádraig Brady <P@draigBrady.com>
Tue, 24 Mar 2015 12:33:12 +0000 (12:33 +0000)
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).

ChangeLog
lib/yesno.c
tests/test-yesno.sh

index df175fd1affa547ca0ef8b27acb75a2072d3b1ca..93a5a4de2c929e6e05cc9df6caf9daa2fb04f3a3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index d5d9eec5cee4e0bce86829433fa89df68dba1c77..331a40833a8ff0e7899a7e49aa690fec86b77a00 100644 (file)
@@ -42,7 +42,10 @@ yesno (void)
     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));
     }
 
index 9d195413cfe30bba680da4e603fffa1197684a5b..8bc50a884884b47f74b529d137f89cbebd5f8d9b 100755 (executable)
@@ -49,6 +49,14 @@ echo yes | test-yesno 2 > 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 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