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