From cbb7acface7115dfb17674a68086bc82a3b8fa11 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Fri, 22 Sep 2006 18:50:41 +0000
Subject: [PATCH] Rearrange yes/no prompting code so that the prompts always
 show the (possibly (un)translated) letters that are actually expected as
 input. Also reject invalid responses instead of silenty taken them as "no".

with help from Bernd Helmle
---
 src/bin/scripts/common.c     | 33 ++++++++++++++++++++++-----------
 src/bin/scripts/common.h     |  4 ++--
 src/bin/scripts/createuser.c | 17 ++++-------------
 src/bin/scripts/dropdb.c     |  7 ++-----
 src/bin/scripts/dropuser.c   |  7 ++-----
 src/bin/scripts/nls.mk       |  4 ++--
 6 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c
index e171568e6b0..2396ed8c515 100644
--- a/src/bin/scripts/common.c
+++ b/src/bin/scripts/common.c
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.20 2006/03/05 15:58:52 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.21 2006/09/22 18:50:41 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -198,18 +198,29 @@ executeCommand(PGconn *conn, const char *query,
  * Check yes/no answer in a localized way.	1=yes, 0=no, -1=neither.
  */
 
-/* translator: Make sure the (y/n) prompts match the translation of this. */
+/* translator: abbreviation for "yes" */
 #define PG_YESLETTER gettext_noop("y")
-/* translator: Make sure the (y/n) prompts match the translation of this. */
+/* translator: abbreviation for "no" */
 #define PG_NOLETTER gettext_noop("n")
 
-int
-check_yesno_response(const char *string)
+bool
+yesno_prompt(const char *question)
 {
-	if (strcmp(string, _(PG_YESLETTER)) == 0)
-		return 1;
-	else if (strcmp(string, _(PG_NOLETTER)) == 0)
-		return 0;
-	else
-		return -1;
+	static char prompt[128];
+
+	for (;;)
+	{
+		char *resp;
+
+		/* translator: This is a question followed by the translated options for "yes" and "no". */
+		snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "), _(question), _(PG_YESLETTER), _(PG_NOLETTER));
+		resp = simple_prompt(prompt, 1, true);
+
+		if (strcmp(resp, _(PG_YESLETTER)) == 0)
+			return true;
+		else if (strcmp(resp, _(PG_NOLETTER)) == 0)
+			return false;
+
+		printf(_("Please answer \"%s\" or \"%s\".\n"), _(PG_YESLETTER), _(PG_NOLETTER));
+	}
 }
diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h
index d231edd623a..986a07ad78d 100644
--- a/src/bin/scripts/common.h
+++ b/src/bin/scripts/common.h
@@ -4,7 +4,7 @@
  *
  *	Copyright (c) 2003-2006, PostgreSQL Global Development Group
  *
- *	$PostgreSQL: pgsql/src/bin/scripts/common.h,v 1.14 2006/07/14 14:52:27 momjian Exp $
+ *	$PostgreSQL: pgsql/src/bin/scripts/common.h,v 1.15 2006/09/22 18:50:41 petere Exp $
  */
 #ifndef COMMON_H
 #define COMMON_H
@@ -35,6 +35,6 @@ extern PGresult *executeQuery(PGconn *conn, const char *query,
 extern void executeCommand(PGconn *conn, const char *query,
 			   const char *progname, bool echo);
 
-extern int	check_yesno_response(const char *string);
+extern bool yesno_prompt(const char *question);
 
 #endif   /* COMMON_H */
diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
index e671298fda2..a3f5747004e 100644
--- a/src/bin/scripts/createuser.c
+++ b/src/bin/scripts/createuser.c
@@ -5,7 +5,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.32 2006/06/01 00:15:36 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.33 2006/09/22 18:50:41 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -192,10 +192,7 @@ main(int argc, char *argv[])
 
 	if (superuser == 0)
 	{
-		char	   *reply;
-
-		reply = simple_prompt("Shall the new role be a superuser? (y/n) ", 1, true);
-		if (check_yesno_response(reply) == 1)
+		if (yesno_prompt("Shall the new role be a superuser?"))
 			superuser = TRI_YES;
 		else
 			superuser = TRI_NO;
@@ -210,10 +207,7 @@ main(int argc, char *argv[])
 
 	if (createdb == 0)
 	{
-		char	   *reply;
-
-		reply = simple_prompt("Shall the new role be allowed to create databases? (y/n) ", 1, true);
-		if (check_yesno_response(reply) == 1)
+		if (yesno_prompt("Shall the new role be allowed to create databases?"))
 			createdb = TRI_YES;
 		else
 			createdb = TRI_NO;
@@ -221,10 +215,7 @@ main(int argc, char *argv[])
 
 	if (createrole == 0)
 	{
-		char	   *reply;
-
-		reply = simple_prompt("Shall the new role be allowed to create more new roles? (y/n) ", 1, true);
-		if (check_yesno_response(reply) == 1)
+		if (yesno_prompt("Shall the new role be allowed to create more new roles?"))
 			createrole = TRI_YES;
 		else
 			createrole = TRI_NO;
diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c
index 8769dcb94cc..a7ef4891751 100644
--- a/src/bin/scripts/dropdb.c
+++ b/src/bin/scripts/dropdb.c
@@ -5,7 +5,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/scripts/dropdb.c,v 1.17 2006/05/29 19:52:46 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/scripts/dropdb.c,v 1.18 2006/09/22 18:50:41 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -104,11 +104,8 @@ main(int argc, char *argv[])
 
 	if (interactive)
 	{
-		char	   *reply;
-
 		printf(_("Database \"%s\" will be permanently removed.\n"), dbname);
-		reply = simple_prompt("Are you sure? (y/n) ", 1, true);
-		if (check_yesno_response(reply) != 1)
+		if (!yesno_prompt("Are you sure?"))
 			exit(0);
 	}
 
diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c
index 5d083caef53..1d22368b19e 100644
--- a/src/bin/scripts/dropuser.c
+++ b/src/bin/scripts/dropuser.c
@@ -5,7 +5,7 @@
  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/bin/scripts/dropuser.c,v 1.18 2006/05/29 19:52:46 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/scripts/dropuser.c,v 1.19 2006/09/22 18:50:41 petere Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -105,11 +105,8 @@ main(int argc, char *argv[])
 
 	if (interactive)
 	{
-		char	   *reply;
-
 		printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
-		reply = simple_prompt("Are you sure? (y/n) ", 1, true);
-		if (check_yesno_response(reply) != 1)
+		if (!yesno_prompt("Are you sure?"))
 			exit(0);
 	}
 
diff --git a/src/bin/scripts/nls.mk b/src/bin/scripts/nls.mk
index 29d89a3f7a6..46b36452c81 100644
--- a/src/bin/scripts/nls.mk
+++ b/src/bin/scripts/nls.mk
@@ -1,8 +1,8 @@
-# $PostgreSQL: pgsql/src/bin/scripts/nls.mk,v 1.19 2005/07/29 15:13:11 momjian Exp $
+# $PostgreSQL: pgsql/src/bin/scripts/nls.mk,v 1.20 2006/09/22 18:50:41 petere Exp $
 CATALOG_NAME    := pgscripts
 AVAIL_LANGUAGES := cs de es fr it ko pt_BR ro ru sk sl sv tr zh_CN zh_TW
 GETTEXT_FILES   := createdb.c createlang.c createuser.c \
                    dropdb.c droplang.c dropuser.c \
                    clusterdb.c vacuumdb.c reindexdb.c \
                    common.c
-GETTEXT_TRIGGERS:= _ simple_prompt
+GETTEXT_TRIGGERS:= _ simple_prompt yesno_prompt
-- 
GitLab