Add psql \prompt capability.
authorBruce Momjian <bruce@momjian.us>
Fri, 23 Feb 2007 18:20:59 +0000 (18:20 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 23 Feb 2007 18:20:59 +0000 (18:20 +0000)
Chad Wagner

doc/src/sgml/ref/psql-ref.sgml
src/bin/psql/command.c
src/bin/psql/help.c
src/bin/psql/tab-complete.c

index ce936a0f98ed917e32841d59052febbebd47bcea..3075d6957594be543afe11242d15bd35d4106538 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.186 2007/02/21 23:22:42 momjian Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.187 2007/02/23 18:20:58 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -1429,6 +1429,24 @@ lo_import 152801
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
+        <listitem>
+        <para>
+         Prompts the user to set variable <replaceable
+         class="parameter">name</>.  An optional prompt, <replaceable
+         class="parameter">text</>, can be specified.  (For multi-word
+         prompts, use single-quotes.)
+        </para>
+
+        <para>
+         By default, <literal>\prompt</> uses the terminal for input and
+         output.  However, if the <option>-f</> command line switch is
+         used, <literal>\prompt</> uses standard input and standard output.
+        </para>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><literal>\pset <replaceable class="parameter">parameter</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
 
index c25a5e6f0f7a4b3ab27d407d3b5ed076449d4554..bcaf684a8ecac9b61c0cfbedace34a953a480e1a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.177 2007/01/05 22:19:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.178 2007/02/23 18:20:58 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -712,6 +712,57 @@ exec_command(const char *cmd,
        free(pw2);
    }
 
+   /* \prompt -- prompt and set variable */
+   else if (strcmp(cmd, "prompt") == 0)
+   {
+       char       *opt, *prompt_text = NULL;
+       char       *arg1, *arg2;
+
+       arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+       arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+
+       if (!arg1)
+       {
+           psql_error("\\%s: missing required argument\n", cmd);
+           success = false;
+       }
+       else
+       {
+           char       *result;
+
+           if (arg2)
+           {
+               prompt_text = arg1;
+               opt = arg2;
+           }
+           else
+               opt = arg1;
+
+           if (!pset.inputfile)
+               result = simple_prompt(prompt_text, 4096, true);
+           else
+           {
+               if (prompt_text)
+               {
+                   fputs(prompt_text, stdout);
+                   fflush(stdout);
+               }
+               result = gets_fromFile(stdin);
+           }
+
+           if (!SetVariable(pset.vars, opt, result))
+           {
+               psql_error("\\%s: error\n", cmd);
+               success = false;
+           }
+
+           free(result);
+           if (prompt_text)
+               free(prompt_text);
+           free(opt);
+       }
+   }
+
    /* \pset -- set printing parameters */
    else if (strcmp(cmd, "pset") == 0)
    {
index e16c0289975d8818ea6ec1834954e524357db4ec..027b5cb0207cde13e967e1fcd6230675cdc36b27 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.116 2007/01/05 22:19:49 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.117 2007/02/23 18:20:59 momjian Exp $
  */
 #include "postgres_fe.h"
 
@@ -161,7 +161,7 @@ slashUsage(unsigned short int pager)
 {
    FILE       *output;
 
-   output = PageOutput(67, pager);
+   output = PageOutput(69, pager);
 
    /* if you add/remove a line here, change the row count above */
 
@@ -184,6 +184,8 @@ slashUsage(unsigned short int pager)
    fprintf(output, _("  \\timing        toggle timing of commands (currently %s)\n"),
            ON(pset.timing));
    fprintf(output, _("  \\unset NAME    unset (delete) internal variable\n"));
+   fprintf(output, _("  \\prompt [TEXT] NAME\n"
+                     "                 prompt user to set internal variable\n"));
    fprintf(output, _("  \\! [COMMAND]   execute command in shell or start interactive shell\n"));
    fprintf(output, "\n");
 
index 8012b70a4dc70d3a6c4e923d223274392d312fec..63db48546bf3c7e59a82b152d23e490af812467d 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2007, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.158 2007/02/07 00:52:35 petere Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.159 2007/02/23 18:20:59 momjian Exp $
  */
 
 /*----------------------------------------------------------------------
@@ -542,7 +542,7 @@ psql_completion(char *text, int start, int end)
        "\\e", "\\echo", "\\encoding",
        "\\f", "\\g", "\\h", "\\help", "\\H", "\\i", "\\l",
        "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
-       "\\o", "\\p", "\\password", "\\pset", "\\q", "\\qecho", "\\r",
+       "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
        "\\set", "\\t", "\\T",
        "\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
    };