Rearrange yes/no prompting code so that the prompts always show the
authorPeter Eisentraut
Fri, 22 Sep 2006 18:50:41 +0000 (18:50 +0000)
committerPeter Eisentraut
Fri, 22 Sep 2006 18:50:41 +0000 (18:50 +0000)
(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
src/bin/scripts/common.h
src/bin/scripts/createuser.c
src/bin/scripts/dropdb.c
src/bin/scripts/dropuser.c
src/bin/scripts/nls.mk

index e171568e6b081077c42b408f430d163e8e05d65b..2396ed8c515888657e1e87bbbd4759ddeb6af425 100644 (file)
@@ -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));
+   }
 }
index d231edd623aaaf85e1f1349efb181f44d2f518f7..986a07ad78d6cb47f04c233ff66dd6e963a506df 100644 (file)
@@ -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 */
index e671298fda271adc54bceea0040430d153482a20..a3f5747004ee1bc286c310e9d795453ab02b822a 100644 (file)
@@ -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;
index 8769dcb94cc83a82fdcbb129fadb8c57843de0b4..a7ef4891751b3376240aa5514ca11fdb6c09cbd6 100644 (file)
@@ -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);
    }
 
index 5d083caef5346e2568df00d956cbb378320b6c76..1d22368b19e58e2e225017cba4a689e85d00b161 100644 (file)
@@ -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);
    }
 
index 29d89a3f7a69387f81b0734b1eee156ba18cc537..46b36452c813893f74e36d3e88215b8611a8dd4d 100644 (file)
@@ -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