Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ca8f279

Browse files
committed
In psql, save history of backslash commands used in multi-line
statements before the multi-line statement, rather than inside the multi-line statement.
1 parent 99114a2 commit ca8f279

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/bin/psql/input.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.51 2006/03/05 15:58:51 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.52 2006/03/06 04:45:21 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -114,7 +114,7 @@ gets_interactive(const char *prompt)
114114

115115
/* Put the line in the history buffer and also add the trailing \n */
116116
void
117-
pgadd_history(char *s, PQExpBuffer history_buf)
117+
pg_append_history(char *s, PQExpBuffer history_buf)
118118
{
119119
#ifdef USE_READLINE
120120

@@ -134,20 +134,20 @@ pgadd_history(char *s, PQExpBuffer history_buf)
134134
}
135135

136136

137-
/* Feed the contents of the history buffer to readline */
137+
/*
138+
* Feed the string to readline
139+
*/
138140
void
139-
pgflush_history(PQExpBuffer history_buf)
141+
pg_write_history(char *s)
140142
{
141-
#ifdef USE_READLINE
142-
char *s;
143+
#ifdef USE_READLINE
143144
static char *prev_hist;
144145
int slen, i;
145146

146147
if (useReadline && useHistory )
147148
{
148149
enum histcontrol HC;
149150

150-
s = history_buf->data;
151151
prev_hist = NULL;
152152

153153
HC = GetHistControlConfig();
@@ -168,14 +168,12 @@ pgflush_history(PQExpBuffer history_buf)
168168
prev_hist = pg_strdup(s);
169169
add_history(s);
170170
}
171-
172-
resetPQExpBuffer(history_buf);
173171
}
174172
#endif
175173
}
176174

177175
void
178-
pgclear_history(PQExpBuffer history_buf)
176+
pg_clear_history(PQExpBuffer history_buf)
179177
{
180178
#ifdef USE_READLINE
181179
if (useReadline && useHistory)

src/bin/psql/input.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/input.h,v 1.25 2006/03/05 15:58:51 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/input.h,v 1.26 2006/03/06 04:45:21 momjian Exp $
77
*/
88
#ifndef INPUT_H
99
#define INPUT_H
@@ -39,9 +39,9 @@ char *gets_fromFile(FILE *source);
3939
void initializeInput(int flags);
4040
bool saveHistory(char *fname);
4141

42-
void pgadd_history(char *s, PQExpBuffer history_buf);
43-
void pgclear_history(PQExpBuffer history_buf);
44-
void pgflush_history(PQExpBuffer history_buf);
42+
void pg_append_history(char *s, PQExpBuffer history_buf);
43+
void pg_clear_history(PQExpBuffer history_buf);
44+
void pg_write_history(char *s);
4545

4646

4747
#endif /* INPUT_H */

src/bin/psql/mainloop.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.71 2006/03/05 15:58:51 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.72 2006/03/06 04:45:21 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "mainloop.h"
@@ -41,6 +41,8 @@ MainLoop(FILE *source)
4141
char *line; /* current line of input */
4242
int added_nl_pos;
4343
bool success;
44+
bool first_query_scan;
45+
4446
volatile int successResult = EXIT_SUCCESS;
4547
volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN;
4648
volatile promptStatus_t prompt_status = PROMPT_READY;
@@ -93,7 +95,7 @@ MainLoop(FILE *source)
9395
successResult = EXIT_USER;
9496
break;
9597
}
96-
pgclear_history(history_buf);
98+
pg_clear_history(history_buf);
9799
cancel_pressed = false;
98100
}
99101

@@ -110,7 +112,7 @@ MainLoop(FILE *source)
110112
slashCmdStatus = PSQL_CMD_UNKNOWN;
111113
prompt_status = PROMPT_READY;
112114
if (pset.cur_cmd_interactive)
113-
pgclear_history(history_buf);
115+
pg_clear_history(history_buf);
114116

115117
if (pset.cur_cmd_interactive)
116118
putc('\n', stdout);
@@ -145,11 +147,14 @@ MainLoop(FILE *source)
145147
prompt_status = PROMPT_READY;
146148

147149
if (pset.cur_cmd_interactive)
150+
{
148151
/*
149152
* Pass all the contents of history_buf to readline
150153
* and free the history buffer.
151154
*/
152-
pgflush_history(history_buf);
155+
pg_write_history(history_buf->data);
156+
pg_clear_history(history_buf);
157+
}
153158
}
154159
/* otherwise, get another line */
155160
else if (pset.cur_cmd_interactive)
@@ -221,10 +226,7 @@ MainLoop(FILE *source)
221226
*/
222227
psql_scan_setup(scan_state, line, strlen(line));
223228
success = true;
224-
225-
if (pset.cur_cmd_interactive)
226-
/* Put current line in the history buffer */
227-
pgadd_history(line, history_buf);
229+
first_query_scan = true;
228230

229231
while (success || !die_on_error)
230232
{
@@ -234,6 +236,23 @@ MainLoop(FILE *source)
234236
scan_result = psql_scan(scan_state, query_buf, &prompt_tmp);
235237
prompt_status = prompt_tmp;
236238

239+
/*
240+
* If we append to history a backslash command that is inside
241+
* a multi-line query, then when we recall the history, the
242+
* backslash command will make the query invalid, so we write
243+
* backslash commands immediately rather than keeping them
244+
* as part of the current multi-line query.
245+
*/
246+
if (first_query_scan && pset.cur_cmd_interactive)
247+
{
248+
if (scan_result == PSCAN_BACKSLASH && query_buf->len != 0)
249+
pg_write_history(line);
250+
else
251+
pg_append_history(line, history_buf);
252+
}
253+
254+
first_query_scan = false;
255+
237256
/*
238257
* Send command if semicolon found, or if end of line and we're in
239258
* single-line mode.
@@ -302,11 +321,14 @@ MainLoop(FILE *source)
302321
}
303322

304323
if (pset.cur_cmd_interactive && prompt_status != PROMPT_CONTINUE)
324+
{
305325
/*
306326
* Pass all the contents of history_buf to readline
307327
* and free the history buffer.
308328
*/
309-
pgflush_history(history_buf);
329+
pg_write_history(history_buf->data);
330+
pg_clear_history(history_buf);
331+
}
310332

311333
psql_scan_finish(scan_state);
312334
free(line);

0 commit comments

Comments
 (0)