33 *
44 * Copyright (c) 2000-2005, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.69 2005/12/18 02:17:16 petere Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.70 2006/02/11 21:55:35 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "mainloop.h"
@@ -37,6 +37,7 @@ MainLoop(FILE *source)
3737PQExpBuffer query_buf ;/* buffer for query being accumulated */
3838PQExpBuffer previous_buf ;/* if there isn't anything in the new buffer
3939 * yet, use this one for \e, etc. */
40+ PQExpBuffer history_buf ;
4041char * line ;/* current line of input */
4142int added_nl_pos ;
4243bool success ;
@@ -66,7 +67,9 @@ MainLoop(FILE *source)
6667
6768query_buf = createPQExpBuffer ();
6869previous_buf = createPQExpBuffer ();
69- if (!query_buf || !previous_buf )
70+ history_buf = createPQExpBuffer ();
71+
72+ if (!query_buf || !previous_buf || !history_buf )
7073{
7174psql_error ("out of memory\n" );
7275exit (EXIT_FAILURE );
@@ -90,7 +93,7 @@ MainLoop(FILE *source)
9093successResult = EXIT_USER ;
9194break ;
9295}
93-
96+ pgclear_history ( history_buf );
9497cancel_pressed = false;
9598}
9699
@@ -106,6 +109,8 @@ MainLoop(FILE *source)
106109count_eof = 0 ;
107110slashCmdStatus = PSQL_CMD_UNKNOWN ;
108111prompt_status = PROMPT_READY ;
112+ if (pset .cur_cmd_interactive )
113+ pgclear_history (history_buf );
109114
110115if (pset .cur_cmd_interactive )
111116putc ('\n' ,stdout );
@@ -138,11 +143,15 @@ MainLoop(FILE *source)
138143psql_scan_reset (scan_state );
139144slashCmdStatus = PSQL_CMD_UNKNOWN ;
140145prompt_status = PROMPT_READY ;
146+
147+ if (pset .cur_cmd_interactive )
148+ /*
149+ *Pass all the contents of history_buf to readline
150+ *and free the history buffer.
151+ */
152+ pgflush_history (history_buf );
141153}
142-
143- /*
144- * otherwise, get another line
145- */
154+ /* otherwise, get another line */
146155else if (pset .cur_cmd_interactive )
147156{
148157/* May need to reset prompt, eg after \r command */
@@ -212,7 +221,11 @@ MainLoop(FILE *source)
212221 */
213222psql_scan_setup (scan_state ,line ,strlen (line ));
214223success = true;
215-
224+
225+ if (pset .cur_cmd_interactive )
226+ /* Put current line in the history buffer */
227+ pgadd_history (line ,history_buf );
228+
216229while (success || !die_on_error )
217230{
218231PsqlScanResult scan_result ;
@@ -287,6 +300,13 @@ MainLoop(FILE *source)
287300scan_result == PSCAN_EOL )
288301break ;
289302}
303+
304+ if (pset .cur_cmd_interactive && prompt_status != PROMPT_CONTINUE )
305+ /*
306+ *Pass all the contents of history_buf to readline
307+ *and free the history buffer.
308+ */
309+ pgflush_history (history_buf );
290310
291311psql_scan_finish (scan_state );
292312free (line );
@@ -333,6 +353,7 @@ MainLoop(FILE *source)
333353
334354destroyPQExpBuffer (query_buf );
335355destroyPQExpBuffer (previous_buf );
356+ destroyPQExpBuffer (history_buf );
336357
337358psql_scan_destroy (scan_state );
338359