Rearrange yes/no prompting code so that the prompts always show the
authorPeter Eisentraut <[email protected]>
Fri, 22 Sep 2006 18:50:41 +0000 (18:50 +0000)
committerPeter Eisentraut <[email protected]>
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 765c52ae9a726ad17a184bb3984cb3c4d0fc0d6f..4b5f94b895a379a1318648248ac0fb1c6c1c62a5 100644 (file)
@@ -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 728522dbdda417de932937674b464a02857289c1..125397c4faac7a17915e17cf4f37a0cdbf8e8c2e 100644 (file)
@@ -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 8ce206293cc53d83f21572b7b1aed6e39aca57f0..45d64475bc5b49c94d20582081b8b4703f284d96 100644 (file)
@@ -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 cce6249f88e961c0e664bca0157846867ac8efcf..7b114914c004dea21947c0aaf51f7109fd051dae 100644 (file)
@@ -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 e34c5ed24d8f3ebf1dabfd12345a813ed17068e7..a946823fdaf5b27cb610b8328379b2d211169926 100644 (file)
@@ -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 ad9093bc44f682dbf8cbada791cd51726826204e..054202e4eb765d0c70f1fff040ee4bb6b37ce944 100644 (file)
@@ -5,4 +5,4 @@ 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