Index: input.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.c,v retrieving revision 1.46 diff -c -r1.46 input.c *** input.c 15 Oct 2005 02:49:40 -0000 1.46 --- input.c 5 Dec 2005 04:07:32 -0000 *************** *** 90,106 **** #ifdef USE_READLINE char *s; - static char *prev_hist = NULL; - if (useReadline) /* On some platforms, readline is declared as readline(char *) */ s = readline((char *) prompt); else s = gets_basic(prompt); ! if (useHistory && s && s[0]) { enum histcontrol HC; HC = GetHistControlConfig(); --- 90,155 ---- #ifdef USE_READLINE char *s; if (useReadline) /* On some platforms, readline is declared as readline(char *) */ s = readline((char *) prompt); else s = gets_basic(prompt); ! return s; ! #else ! return gets_basic(prompt); ! #endif ! } ! ! /* Put the line in the history buffer and also add the trailing \n */ ! char *pgadd_history(char *s, char *history_buf, int *cur_len) ! { ! #ifdef USE_READLINE ! ! int slen; ! char *history_buf1 = 0; ! if (useReadline && useHistory && s && s[0]) { + slen = strlen(s); + history_buf1 = history_buf; + history_buf1 = realloc(history_buf1, (*cur_len + slen + 2) * sizeof(char)); + strcpy(history_buf1 + *cur_len, s); + if (s[slen-1]!='\n') + { + *cur_len += (slen + 1); + history_buf1[*cur_len - 1] = '\n'; + history_buf1[*cur_len] = 0; + } + else + { + *cur_len += (slen); + history_buf1[*cur_len] = 0; + } + + } + return history_buf1; + #endif + } + + /* Feed the contents of the history buffer to readline */ + void pgflush_history(char **history_buf, int *cur_len) + { + + #ifdef USE_READLINE + + char *s; + static char *prev_hist; + + if (useReadline && useHistory && ((*cur_len) > 0)) + { + enum histcontrol HC; + + s = *history_buf; + prev_hist = NULL; + + s[(*cur_len) - 1] = 0; HC = GetHistControlConfig(); *************** *** 115,128 **** prev_hist = pg_strdup(s); add_history(s); } } - - return s; - #else - return gets_basic(prompt); #endif - } /* --- 164,177 ---- prev_hist = pg_strdup(s); add_history(s); } + + free(s); + *history_buf = 0; + *cur_len = 0; } #endif + } /* Index: input.h =================================================================== RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.h,v retrieving revision 1.23 diff -c -r1.23 input.h *** input.h 1 Jan 2005 05:43:08 -0000 1.23 --- input.h 5 Dec 2005 04:07:32 -0000 *************** *** 39,42 **** --- 39,45 ---- void initializeInput(int flags); bool saveHistory(char *fname); + char *pgadd_history(char *s, char *history_buf, int *cur_len); + void pgflush_history(char **history_buf, int *cur_len); + #endif /* INPUT_H */ Index: mainloop.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/bin/psql/mainloop.c,v retrieving revision 1.68 diff -c -r1.68 mainloop.c *** mainloop.c 15 Oct 2005 02:49:40 -0000 1.68 --- mainloop.c 5 Dec 2005 04:07:32 -0000 *************** *** 38,43 **** --- 38,45 ---- PQExpBuffer previous_buf; /* if there isn't anything in the new buffer * yet, use this one for \e, etc. */ char *line; /* current line of input */ + char *history_buf = 0; + int history_buf_len = 0; int added_nl_pos; bool success; volatile int successResult = EXIT_SUCCESS; *************** *** 138,143 **** --- 140,154 ---- psql_scan_reset(scan_state); slashCmdStatus = CMD_UNKNOWN; prompt_status = PROMPT_READY; + + if ( pset.cur_cmd_interactive ) + { + /* Pass all the contents of history_buf to readline + * and free the history buffer. + */ + pgflush_history(&history_buf, &history_buf_len); + } + } /* *************** *** 212,218 **** */ psql_scan_setup(scan_state, line, strlen(line)); success = true; ! while (success || !die_on_error) { PsqlScanResult scan_result; --- 223,235 ---- */ psql_scan_setup(scan_state, line, strlen(line)); success = true; ! ! if (pset.cur_cmd_interactive) ! { ! /* Put current line in the history buffer */ ! history_buf = pgadd_history(line, history_buf, &history_buf_len); ! } ! while (success || !die_on_error) { PsqlScanResult scan_result; *************** *** 229,234 **** --- 246,252 ---- (scan_result == PSCAN_EOL && GetVariableBool(pset.vars, "SINGLELINE"))) { + /* execute query */ success = SendQuery(query_buf->data); slashCmdStatus = success ? CMD_SEND : CMD_ERROR; *************** *** 249,258 **** --- 267,279 ---- * newline again. This avoids any change to query_buf when a * line contains only a backslash command. */ + + if (query_buf->len == added_nl_pos) query_buf->data[--query_buf->len] = '\0'; added_nl_pos = -1; + slashCmdStatus = HandleSlashCmds(scan_state, query_buf->len > 0 ? query_buf : previous_buf); *************** *** 266,271 **** --- 287,293 ---- appendPQExpBufferStr(query_buf, previous_buf->data); } + if (slashCmdStatus == CMD_SEND) { success = SendQuery(query_buf->data); *************** *** 287,292 **** --- 309,322 ---- scan_result == PSCAN_EOL) break; } + + if (pset.cur_cmd_interactive && (prompt_status != PROMPT_CONTINUE)) + { + /* Pass all the contents of history_buf to readline + and free the history buffer. + */ + pgflush_history(&history_buf, &history_buf_len); + } psql_scan_finish(scan_state); free(line);