3
3
*
4
4
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
5
5
*
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 $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "mainloop.h"
@@ -41,6 +41,8 @@ MainLoop(FILE *source)
41
41
char * line ; /* current line of input */
42
42
int added_nl_pos ;
43
43
bool success ;
44
+ bool first_query_scan ;
45
+
44
46
volatile int successResult = EXIT_SUCCESS ;
45
47
volatile backslashResult slashCmdStatus = PSQL_CMD_UNKNOWN ;
46
48
volatile promptStatus_t prompt_status = PROMPT_READY ;
@@ -93,7 +95,7 @@ MainLoop(FILE *source)
93
95
successResult = EXIT_USER ;
94
96
break ;
95
97
}
96
- pgclear_history (history_buf );
98
+ pg_clear_history (history_buf );
97
99
cancel_pressed = false;
98
100
}
99
101
@@ -110,7 +112,7 @@ MainLoop(FILE *source)
110
112
slashCmdStatus = PSQL_CMD_UNKNOWN ;
111
113
prompt_status = PROMPT_READY ;
112
114
if (pset .cur_cmd_interactive )
113
- pgclear_history (history_buf );
115
+ pg_clear_history (history_buf );
114
116
115
117
if (pset .cur_cmd_interactive )
116
118
putc ('\n' , stdout );
@@ -145,11 +147,14 @@ MainLoop(FILE *source)
145
147
prompt_status = PROMPT_READY ;
146
148
147
149
if (pset .cur_cmd_interactive )
150
+ {
148
151
/*
149
152
* Pass all the contents of history_buf to readline
150
153
* and free the history buffer.
151
154
*/
152
- pgflush_history (history_buf );
155
+ pg_write_history (history_buf -> data );
156
+ pg_clear_history (history_buf );
157
+ }
153
158
}
154
159
/* otherwise, get another line */
155
160
else if (pset .cur_cmd_interactive )
@@ -221,10 +226,7 @@ MainLoop(FILE *source)
221
226
*/
222
227
psql_scan_setup (scan_state , line , strlen (line ));
223
228
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;
228
230
229
231
while (success || !die_on_error )
230
232
{
@@ -234,6 +236,23 @@ MainLoop(FILE *source)
234
236
scan_result = psql_scan (scan_state , query_buf , & prompt_tmp );
235
237
prompt_status = prompt_tmp ;
236
238
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
+
237
256
/*
238
257
* Send command if semicolon found, or if end of line and we're in
239
258
* single-line mode.
@@ -302,11 +321,14 @@ MainLoop(FILE *source)
302
321
}
303
322
304
323
if (pset .cur_cmd_interactive && prompt_status != PROMPT_CONTINUE )
324
+ {
305
325
/*
306
326
* Pass all the contents of history_buf to readline
307
327
* and free the history buffer.
308
328
*/
309
- pgflush_history (history_buf );
329
+ pg_write_history (history_buf -> data );
330
+ pg_clear_history (history_buf );
331
+ }
310
332
311
333
psql_scan_finish (scan_state );
312
334
free (line );
0 commit comments