3
3
*
4
4
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
5
5
*
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 $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "mainloop.h"
@@ -37,6 +37,7 @@ MainLoop(FILE *source)
37
37
PQExpBuffer query_buf ; /* buffer for query being accumulated */
38
38
PQExpBuffer previous_buf ; /* if there isn't anything in the new buffer
39
39
* yet, use this one for \e, etc. */
40
+ PQExpBuffer history_buf ;
40
41
char * line ; /* current line of input */
41
42
int added_nl_pos ;
42
43
bool success ;
@@ -66,7 +67,9 @@ MainLoop(FILE *source)
66
67
67
68
query_buf = createPQExpBuffer ();
68
69
previous_buf = createPQExpBuffer ();
69
- if (!query_buf || !previous_buf )
70
+ history_buf = createPQExpBuffer ();
71
+
72
+ if (!query_buf || !previous_buf || !history_buf )
70
73
{
71
74
psql_error ("out of memory\n" );
72
75
exit (EXIT_FAILURE );
@@ -90,7 +93,7 @@ MainLoop(FILE *source)
90
93
successResult = EXIT_USER ;
91
94
break ;
92
95
}
93
-
96
+ pgclear_history ( history_buf );
94
97
cancel_pressed = false;
95
98
}
96
99
@@ -106,6 +109,8 @@ MainLoop(FILE *source)
106
109
count_eof = 0 ;
107
110
slashCmdStatus = PSQL_CMD_UNKNOWN ;
108
111
prompt_status = PROMPT_READY ;
112
+ if (pset .cur_cmd_interactive )
113
+ pgclear_history (history_buf );
109
114
110
115
if (pset .cur_cmd_interactive )
111
116
putc ('\n' , stdout );
@@ -138,11 +143,15 @@ MainLoop(FILE *source)
138
143
psql_scan_reset (scan_state );
139
144
slashCmdStatus = PSQL_CMD_UNKNOWN ;
140
145
prompt_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 );
141
153
}
142
-
143
- /*
144
- * otherwise, get another line
145
- */
154
+ /* otherwise, get another line */
146
155
else if (pset .cur_cmd_interactive )
147
156
{
148
157
/* May need to reset prompt, eg after \r command */
@@ -212,7 +221,11 @@ MainLoop(FILE *source)
212
221
*/
213
222
psql_scan_setup (scan_state , line , strlen (line ));
214
223
success = true;
215
-
224
+
225
+ if (pset .cur_cmd_interactive )
226
+ /* Put current line in the history buffer */
227
+ pgadd_history (line , history_buf );
228
+
216
229
while (success || !die_on_error )
217
230
{
218
231
PsqlScanResult scan_result ;
@@ -287,6 +300,13 @@ MainLoop(FILE *source)
287
300
scan_result == PSCAN_EOL )
288
301
break ;
289
302
}
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 );
290
310
291
311
psql_scan_finish (scan_state );
292
312
free (line );
@@ -333,6 +353,7 @@ MainLoop(FILE *source)
333
353
334
354
destroyPQExpBuffer (query_buf );
335
355
destroyPQExpBuffer (previous_buf );
356
+ destroyPQExpBuffer (history_buf );
336
357
337
358
psql_scan_destroy (scan_state );
338
359
0 commit comments