Skip to content

Commit ffb120e

Browse files
committed
Add new \w write command to psql.
1 parent 5e49011 commit ffb120e

File tree

3 files changed

+66
-39
lines changed

3 files changed

+66
-39
lines changed

src/bin/psql/psql.c

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.152 1998/08/06 05:12:55 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.153 1998/08/10 20:31:38 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -254,6 +254,7 @@ slashUsage(PsqlSettings *pset)
254254
fprintf(fout, " \\t -- toggle table headings and row count (currently %s)\n", on(pset->opt.header));
255255
fprintf(fout, " \\T [<html>] -- set html3.0 <table ...> options (currently '%s')\n", pset->opt.tableOpt ? pset->opt.tableOpt : "");
256256
fprintf(fout, " \\x -- toggle expanded output (currently %s)\n", on(pset->opt.expanded));
257+
fprintf(fout, " \\w <fname> -- output current buffer to a file\n");
257258
fprintf(fout, " \\z -- list current grant/revoke permissions\n");
258259
fprintf(fout, " \\! [<cmd>] -- shell escape or command\n");
259260

@@ -2135,13 +2136,34 @@ HandleSlashCmds(PsqlSettings *pset,
21352136
break;
21362137
}
21372138

2139+
case 'H':
2140+
if (toggle(pset, &pset->opt.html3, "HTML3.0 tabular output"))
2141+
pset->opt.standard = 0;
2142+
break;
2143+
21382144
case 'l': /* \l is list database */
21392145
listAllDbs(pset);
21402146
break;
21412147

2142-
case 'H':
2143-
if (toggle(pset, &pset->opt.html3, "HTML3.0 tabular output"))
2144-
pset->opt.standard = 0;
2148+
case 'm': /* monitor like type-setting */
2149+
if (toggle(pset, &pset->opt.standard, "standard SQL separaters and padding"))
2150+
{
2151+
pset->opt.html3 = pset->opt.expanded = 0;
2152+
pset->opt.align = pset->opt.header = 1;
2153+
if (pset->opt.fieldSep)
2154+
free(pset->opt.fieldSep);
2155+
pset->opt.fieldSep = strdup("|");
2156+
if (!pset->quiet)
2157+
printf("field separator changed to '%s'\n", pset->opt.fieldSep);
2158+
}
2159+
else
2160+
{
2161+
if (pset->opt.fieldSep)
2162+
free(pset->opt.fieldSep);
2163+
pset->opt.fieldSep = strdup(DEFAULT_FIELD_SEP);
2164+
if (!pset->quiet)
2165+
printf("field separator changed to '%s'\n", pset->opt.fieldSep);
2166+
}
21452167
break;
21462168

21472169
case 'o':
@@ -2175,31 +2197,6 @@ HandleSlashCmds(PsqlSettings *pset,
21752197
#endif
21762198
break;
21772199

2178-
case 'm': /* monitor like type-setting */
2179-
if (toggle(pset, &pset->opt.standard, "standard SQL separaters and padding"))
2180-
{
2181-
pset->opt.html3 = pset->opt.expanded = 0;
2182-
pset->opt.align = pset->opt.header = 1;
2183-
if (pset->opt.fieldSep)
2184-
free(pset->opt.fieldSep);
2185-
pset->opt.fieldSep = strdup("|");
2186-
if (!pset->quiet)
2187-
printf("field separator changed to '%s'\n", pset->opt.fieldSep);
2188-
}
2189-
else
2190-
{
2191-
if (pset->opt.fieldSep)
2192-
free(pset->opt.fieldSep);
2193-
pset->opt.fieldSep = strdup(DEFAULT_FIELD_SEP);
2194-
if (!pset->quiet)
2195-
printf("field separator changed to '%s'\n", pset->opt.fieldSep);
2196-
}
2197-
break;
2198-
2199-
case 'z': /* list table rights (grant/revoke) */
2200-
rightsList(pset);
2201-
break;
2202-
22032200
case 't': /* toggle headers */
22042201
toggle(pset, &pset->opt.header, "output headings and row count");
22052202
break;
@@ -2216,10 +2213,34 @@ HandleSlashCmds(PsqlSettings *pset,
22162213
}
22172214
break;
22182215

2216+
case 'w':
2217+
{
2218+
FILE *fd;
2219+
2220+
if (!optarg)
2221+
{
2222+
fprintf(stderr, "\\w must be followed by a file name\n");
2223+
break;
2224+
}
2225+
if ((fd = fopen(optarg, "w")) == NULL)
2226+
{
2227+
fprintf(stderr, "file named %s could not be opened\n", optarg);
2228+
break;
2229+
}
2230+
fputs(query, fd);
2231+
fputs("\n", fd);
2232+
fclose(fd);
2233+
break;
2234+
}
2235+
22192236
case 'x':
22202237
toggle(pset, &pset->opt.expanded, "expanded table representation");
22212238
break;
22222239

2240+
case 'z': /* list table rights (grant/revoke) */
2241+
rightsList(pset);
2242+
break;
2243+
22232244
case '!':
22242245
do_shell(optarg);
22252246
break;
@@ -2252,13 +2273,17 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
22522273
int successResult = 1;
22532274
int slashCmdStatus = CMD_SEND;
22542275

2255-
/*
2256-
* slashCmdStatus can be: CMD_UNKNOWN - send currently constructed
2257-
* query to backend (i.e. we got a \g) CMD_SEND - send
2258-
* currently constructed query to backend (i.e. we got a \g)
2259-
* CMD_SKIP_LINE - skip processing of this line, continue building
2260-
* up query CMD_TERMINATE - terminate processing of this query
2261-
* entirely CMD_NEWEDIT - new query supplied by edit
2276+
/*--------------------------------------------------------------
2277+
* slashCmdStatus can be:
2278+
* CMD_UNKNOWN - send currently constructed query to backend
2279+
* (i.e. we got a \g)
2280+
* CMD_SEND - send currently constructed query to backend
2281+
* (i.e. we got a \g)
2282+
* CMD_SKIP_LINE - skip processing of this line, continue building
2283+
* up query
2284+
* CMD_TERMINATE - terminate processing of this query entirely
2285+
* CMD_NEWEDIT - new query supplied by edit
2286+
*---------------------------------------------------------------
22622287
*/
22632288

22642289
bool querySent = false;
@@ -2358,7 +2383,6 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
23582383
* query - pointer to current command query_start - placeholder
23592384
* for next command
23602385
*/
2361-
23622386
if (line == NULL || (!interactive && *line == '\0'))
23632387
{ /* No more input. Time to quit, or \i
23642388
* done */

src/man/psql.1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/psql.1,v 1.27 1998/08/03 05:54:30 momjian Exp $
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/psql.1,v 1.28 1998/08/10 20:31:40 momjian Exp $
44
.TH PSQL UNIX 1/20/96 PostgreSQL PostgreSQL
55
.SH NAME
66
psql - run the interactive query front-end
@@ -380,6 +380,9 @@ Toggles extended row format mode. When enabled each row will have its column
380380
names printed on the left with the column values printed on the right.
381381
This is useful for rows which are otherwise too long to fit into
382382
one screen line. HTML row output mode supports this flag too.
383+
.IP "\ew [\fIfilename\fR]"
384+
Outputs current query buffer to
385+
.IR filename .
383386
.IP "\ez"
384387
Produces a list of all tables in database with their appropriate ACLs
385388
(grant/revoke permissions) listed.

src/tools/backend/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ <H2 ALIGN=CENTER>
162162
<ADDRESS>
163163
Maintainer: Bruce Momjian (<A
164164
HREF="mailto:maillist@candle.pha.pa.us">maillist@candle.pha.pa.us</A>)<BR>
165-
Last updated: Tue Dec 9 17:56:08 EST 1997
165+
Last updated: Mon Aug 10 10:48:06 EDT 1998
166166
</ADDRESS>
167167
</SMALL>
168168
</BODY>

0 commit comments

Comments
 (0)