Skip to content

Commit 7031dd6

Browse files
committed
Add psql \prompt capability.
Chad Wagner
1 parent cc77005 commit 7031dd6

File tree

4 files changed

+77
-6
lines changed

4 files changed

+77
-6
lines changed

doc/src/sgml/ref/psql-ref.sgml

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.186 2007/02/21 23:22:42 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.187 2007/02/23 18:20:58 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1429,6 +1429,24 @@ lo_import 152801
14291429
</listitem>
14301430
</varlistentry>
14311431

1432+
<varlistentry>
1433+
<term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
1434+
<listitem>
1435+
<para>
1436+
Prompts the user to set variable <replaceable
1437+
class="parameter">name</>. An optional prompt, <replaceable
1438+
class="parameter">text</>, can be specified. (For multi-word
1439+
prompts, use single-quotes.)
1440+
</para>
1441+
1442+
<para>
1443+
By default, <literal>\prompt</> uses the terminal for input and
1444+
output. However, if the <option>-f</> command line switch is
1445+
used, <literal>\prompt</> uses standard input and standard output.
1446+
</para>
1447+
</listitem>
1448+
</varlistentry>
1449+
14321450
<varlistentry>
14331451
<term><literal>\pset <replaceable class="parameter">parameter</replaceable> [ <replaceable class="parameter">value</replaceable> ]</literal></term>
14341452

src/bin/psql/command.c

+52-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.177 2007/01/05 22:19:49 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.178 2007/02/23 18:20:58 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -712,6 +712,57 @@ exec_command(const char *cmd,
712712
free(pw2);
713713
}
714714

715+
/* \prompt -- prompt and set variable */
716+
else if (strcmp(cmd, "prompt") == 0)
717+
{
718+
char *opt, *prompt_text = NULL;
719+
char *arg1, *arg2;
720+
721+
arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
722+
arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
723+
724+
if (!arg1)
725+
{
726+
psql_error("\\%s: missing required argument\n", cmd);
727+
success = false;
728+
}
729+
else
730+
{
731+
char *result;
732+
733+
if (arg2)
734+
{
735+
prompt_text = arg1;
736+
opt = arg2;
737+
}
738+
else
739+
opt = arg1;
740+
741+
if (!pset.inputfile)
742+
result = simple_prompt(prompt_text, 4096, true);
743+
else
744+
{
745+
if (prompt_text)
746+
{
747+
fputs(prompt_text, stdout);
748+
fflush(stdout);
749+
}
750+
result = gets_fromFile(stdin);
751+
}
752+
753+
if (!SetVariable(pset.vars, opt, result))
754+
{
755+
psql_error("\\%s: error\n", cmd);
756+
success = false;
757+
}
758+
759+
free(result);
760+
if (prompt_text)
761+
free(prompt_text);
762+
free(opt);
763+
}
764+
}
765+
715766
/* \pset -- set printing parameters */
716767
else if (strcmp(cmd, "pset") == 0)
717768
{

src/bin/psql/help.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.116 2007/01/05 22:19:49 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.117 2007/02/23 18:20:59 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -161,7 +161,7 @@ slashUsage(unsigned short int pager)
161161
{
162162
FILE *output;
163163

164-
output = PageOutput(67, pager);
164+
output = PageOutput(69, pager);
165165

166166
/* if you add/remove a line here, change the row count above */
167167

@@ -184,6 +184,8 @@ slashUsage(unsigned short int pager)
184184
fprintf(output, _(" \\timing toggle timing of commands (currently %s)\n"),
185185
ON(pset.timing));
186186
fprintf(output, _(" \\unset NAME unset (delete) internal variable\n"));
187+
fprintf(output, _(" \\prompt [TEXT] NAME\n"
188+
" prompt user to set internal variable\n"));
187189
fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));
188190
fprintf(output, "\n");
189191

src/bin/psql/tab-complete.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.158 2007/02/07 00:52:35 petere Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/tab-complete.c,v 1.159 2007/02/23 18:20:59 momjian Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -542,7 +542,7 @@ psql_completion(char *text, int start, int end)
542542
"\\e", "\\echo", "\\encoding",
543543
"\\f", "\\g", "\\h", "\\help", "\\H", "\\i", "\\l",
544544
"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
545-
"\\o", "\\p", "\\password", "\\pset", "\\q", "\\qecho", "\\r",
545+
"\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
546546
"\\set", "\\t", "\\T",
547547
"\\timing", "\\unset", "\\x", "\\w", "\\z", "\\!", NULL
548548
};

0 commit comments

Comments
 (0)