Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit02e95a5

Browse files
committed
Add \warn command to psql.
This is like \echo except that the text is sent to stderr not stdout.In passing, fix a pre-existing bug in \echo and \qecho: per documentationthe -n switch should only be recognized when it is the first argument,but actually any argument matching "-n" was treated as a switch.(Should we back-patch that?)David Fetter (bug fix by me), reviewed by Fabien CoelhoDiscussion:https://postgr.es/m/20190421183115.GA4311@fetter.org
1 parente8fdcac commit02e95a5

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,22 +1863,22 @@ testdb=>
18631863
<term><literal>\echo <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
18641864
<listitem>
18651865
<para>
1866-
Prints the arguments tothestandard output, separated by one
1867-
space and followed by a newline. This can be useful to
1866+
Prints theevaluatedarguments to standard output, separated by
1867+
spaces and followed by a newline. This can be useful to
18681868
intersperse information in the output of scripts. For example:
18691869
<programlisting>
18701870
=&gt; <userinput>\echo `date`</userinput>
18711871
Tue Oct 26 21:40:57 CEST 1999
18721872
</programlisting>
18731873
If the first argument is an unquoted <literal>-n</literal> the trailing
1874-
newline is not written.
1874+
newline is not written (nor is the first argument).
18751875
</para>
18761876

18771877
<tip>
18781878
<para>
18791879
If you use the <command>\o</command> command to redirect your
18801880
query output you might wish to use <command>\qecho</command>
1881-
instead of this command.
1881+
instead of this command. See also <command>\warn</command>.
18821882
</para>
18831883
</tip>
18841884
</listitem>
@@ -3226,6 +3226,18 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
32263226
</varlistentry>
32273227

32283228

3229+
<varlistentry>
3230+
<term><literal>\warn <replaceable class="parameter">text</replaceable> [ ... ]</literal></term>
3231+
<listitem>
3232+
<para>
3233+
This command is identical to <command>\echo</command> except
3234+
that the output will be written to <application>psql</application>'s
3235+
standard error channel, rather than standard output.
3236+
</para>
3237+
</listitem>
3238+
</varlistentry>
3239+
3240+
32293241
<varlistentry>
32303242
<term><literal>\watch [ <replaceable class="parameter">seconds</replaceable> ]</literal></term>
32313243
<listitem>

‎src/bin/psql/command.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ exec_command(const char *cmd,
319319
status=exec_command_ef_ev(scan_state,active_branch,query_buf, true);
320320
elseif (strcmp(cmd,"ev")==0)
321321
status=exec_command_ef_ev(scan_state,active_branch,query_buf, false);
322-
elseif (strcmp(cmd,"echo")==0||strcmp(cmd,"qecho")==0)
322+
elseif (strcmp(cmd,"echo")==0||strcmp(cmd,"qecho")==0||
323+
strcmp(cmd,"warn")==0)
323324
status=exec_command_echo(scan_state,active_branch,cmd);
324325
elseif (strcmp(cmd,"elif")==0)
325326
status=exec_command_elif(scan_state,cstack,query_buf);
@@ -1114,7 +1115,7 @@ exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
11141115
}
11151116

11161117
/*
1117-
* \echoand \qecho -- echo arguments to stdout orquery output
1118+
* \echo, \qecho,and \warn -- echo arguments to stdout,query output, or stderr
11181119
*/
11191120
staticbackslashResult
11201121
exec_command_echo(PsqlScanStatescan_state,boolactive_branch,constchar*cmd)
@@ -1129,13 +1130,15 @@ exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
11291130

11301131
if (strcmp(cmd,"qecho")==0)
11311132
fout=pset.queryFout;
1133+
elseif (strcmp(cmd,"warn")==0)
1134+
fout=stderr;
11321135
else
11331136
fout=stdout;
11341137

11351138
while ((value=psql_scan_slash_option(scan_state,
11361139
OT_NORMAL,&quoted, false)))
11371140
{
1138-
if (!quoted&&strcmp(value,"-n")==0)
1141+
if (first&& !no_newline&&!quoted&&strcmp(value,"-n")==0)
11391142
no_newline= true;
11401143
else
11411144
{

‎src/bin/psql/help.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ slashUsage(unsigned short int pager)
169169
* Use "psql --help=commands | wc" to count correctly. It's okay to count
170170
* the USE_READLINE line even in builds without that.
171171
*/
172-
output=PageOutput(127,pager ?&(pset.popt.topt) :NULL);
172+
output=PageOutput(128,pager ?&(pset.popt.topt) :NULL);
173173

174174
fprintf(output,_("General\n"));
175175
fprintf(output,_(" \\copyright show PostgreSQL usage and distribution terms\n"));
@@ -206,11 +206,12 @@ slashUsage(unsigned short int pager)
206206

207207
fprintf(output,_("Input/Output\n"));
208208
fprintf(output,_(" \\copy ... perform SQL COPY with data stream to the client host\n"));
209-
fprintf(output,_(" \\echo [STRING]write string to standard output\n"));
209+
fprintf(output,_(" \\echo [-n] [STRING] write string to standard output (-n for no newline)\n"));
210210
fprintf(output,_(" \\i FILE execute commands from file\n"));
211211
fprintf(output,_(" \\ir FILE as \\i, but relative to location of current script\n"));
212212
fprintf(output,_(" \\o [FILE] send all query results to file or |pipe\n"));
213-
fprintf(output,_(" \\qecho [STRING] write string to query output stream (see \\o)\n"));
213+
fprintf(output,_(" \\qecho [-n] [STRING] write string to \\o output stream (-n for no newline)\n"));
214+
fprintf(output,_(" \\warn [-n] [STRING] write string to standard error (-n for no newline)\n"));
214215
fprintf(output,"\n");
215216

216217
fprintf(output,_("Conditional\n"));

‎src/bin/psql/tab-complete.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ psql_completion(const char *text, int start, int end)
14381438
"\\t","\\T","\\timing",
14391439
"\\unset",
14401440
"\\x",
1441-
"\\w","\\watch",
1441+
"\\w","\\warn","\\watch",
14421442
"\\z",
14431443
"\\!","\\?",
14441444
NULL

‎src/test/regress/expected/psql.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,6 +4180,25 @@ drop table psql_serial_tab;
41804180
\pset format aligned
41814181
\pset expanded off
41824182
\pset border 1
4183+
-- \echo and allied features
4184+
\echo this is a test
4185+
this is a test
4186+
\echo -n without newline
4187+
without newline\echo with -n newline
4188+
with -n newline
4189+
\echo '-n' with newline
4190+
-n with newline
4191+
\set foo bar
4192+
\echo foo = :foo
4193+
foo = bar
4194+
\qecho this is a test
4195+
this is a test
4196+
\qecho foo = :foo
4197+
foo = bar
4198+
\warn this is a test
4199+
this is a test
4200+
\warn foo = :foo
4201+
foo = bar
41834202
-- tests for \if ... \endif
41844203
\if true
41854204
select 'okay';

‎src/test/regress/sql/psql.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,22 @@ drop table psql_serial_tab;
771771
\pset expanded off
772772
\pset border1
773773

774+
-- \echo and allied features
775+
776+
\echo this is a test
777+
\echo-n without newline
778+
\echo with-n newline
779+
\echo'-n' with newline
780+
781+
\set foo bar
782+
\echo foo= :foo
783+
784+
\qecho this is a test
785+
\qecho foo= :foo
786+
787+
\warn this is a test
788+
\warn foo= :foo
789+
774790
-- tests for \if ... \endif
775791

776792
\if true

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp