add HISTSIZE for readline to limit the number of cached commands

This commit is contained in:
chenxiaobin
2021-03-19 15:39:15 +08:00
parent f72445a6cf
commit ee87e03e47
9 changed files with 96 additions and 1 deletions

View File

@ -2833,6 +2833,22 @@ bar
</listitem>
</varlistentry>
<varlistentry>
<term><varname>HISTSIZE</varname></term>
<listitem>
<para>
The number of commands to store in the command history. The
default value is 500.
</para>
<note>
<para>
This feature was shamelessly plagiarized from
<application>Bash</application>.
</para>
</note>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>HOST</varname></term>
<listitem>

View File

@ -1051,6 +1051,12 @@ static backslashResult exec_command(const char* cmd, PsqlScanState scan_state, P
success = false;
}
#ifdef USE_READLINE
if (useReadline && pset.cur_cmd_interactive) {
setHistSize(opt0, newval, false);
}
#endif
free(newval);
newval = NULL;
if (temp_opt != NULL)
@ -1255,6 +1261,12 @@ static backslashResult exec_command(const char* cmd, PsqlScanState scan_state, P
success = false;
}
#ifdef USE_READLINE
if (useReadline && pset.cur_cmd_interactive) {
setHistSize(opt, NULL, true);
}
#endif
if (NULL != opt) {
free(opt);
opt = NULL;

View File

@ -248,6 +248,45 @@ bool SensitiveStrCheck(const char* target)
}
}
void setHistSize(const char* targetName, const char* targetValue, bool setToDefault)
{
#ifndef ENABLE_LLT
char* end = NULL;
long int result;
#define MAXHISTSIZE 500
#define DEFHISTSIZE 32
if (targetName == NULL) {
return;
}
if (strcmp(targetName, "HISTSIZE") == 0) {
if (!setToDefault) {
if (targetValue == NULL || strlen(targetValue) == 0) {
fprintf(stderr, "warning:\"HISTSIZE\" is not changed,because its value can not be null\n");
return;
} else {
errno = 0;
result = strtol(targetValue, &end, 0);
if ((errno == ERANGE && (result == LONG_MAX || result == LONG_MIN)) || (errno != 0 && result == 0)) {
fprintf(stderr, "warning:\"HISTSIZE\" is not changed,because its value overflows\n");
return;
}
if (*end || result < 0) {
fprintf(stderr, "warning:\"HISTSIZE\" is not changed,because its value must be positive integer\n");
return;
}
}
if (result > MAXHISTSIZE) {
fprintf(stderr, "warning:\"HISTSIZE\" is set to 500,because its value can not be greater than 500\n");
result = MAXHISTSIZE;
}
} else {
result = DEFHISTSIZE;
}
stifle_history((int)result);
}
#endif
}
/*
* Put any startup stuff related to input in here. It's good to maintain
* abstraction this way.

View File

@ -8,7 +8,6 @@
#ifndef INPUT_H
#define INPUT_H
#ifdef HAVE_LIBREADLINE
/*
* If some other file needs to have access to readline/history, include this
* file and save yourself all this work.
@ -17,6 +16,8 @@
*/
#define USE_READLINE 1
#ifdef HAVE_LIBREADLINE
#if defined(HAVE_READLINE_READLINE_H)
#include <readline/readline.h>
#include <readline/history.h>
@ -36,6 +37,7 @@ char* gets_fromFile(FILE* source);
void pg_append_history(const char* s, PQExpBuffer history_buf);
void pg_send_history(PQExpBuffer history_buf);
void setHistSize(const char* targetName, const char* targetValue, bool setToDefault);
extern bool useReadline;
extern bool SensitiveStrCheck(const char* target);

View File

@ -148,6 +148,11 @@ int MainLoop(FILE* source, char* querystring)
}
pset.lineno = 0;
if (pset.cur_cmd_interactive) {
const char* val = GetVariable(pset.vars, "HISTSIZE");
setHistSize("HISTSIZE", val, val == NULL);
}
/* Create working state */
scan_state = psql_scan_create();

View File

@ -781,6 +781,7 @@ static void parse_psql_options(int argc, char* const argv[], struct adhoc_opts*
fprintf(stderr, _("%s: could not set variable \"%s\"\n"), pset.progname, value);
exit(EXIT_FAILURE);
}
setHistSize(value, equal_loc + 1, false);
}
free(value);

View File

@ -754,8 +754,12 @@ void initialize_readline(void)
{
rl_readline_name = (char *)pset.progname;
#ifdef HAVE_READLINE_READLINE_H
/* PsqlCompletion is deleted because it's too complex and not be used at all. */
rl_attempted_completion_function = PsqlCompletion;
#else
rl_attempted_completion_function = NULL;
#endif
rl_basic_word_break_characters = WORD_BREAKS;

View File

@ -7,3 +7,11 @@ select * from 数据库;
1
(1 row)
\set HISTSIZE -1
\set HISTSIZE 30.5
\set HISTSIZE 0
\set HISTSIZE 6
\set HISTSIZE 500
\set HISTSIZE 600
\set HISTSIZE 66666666666666666666666600
\set HISTSIZE -666666666666666666666666666

View File

@ -2,3 +2,11 @@
create table (f int);
insert into values (1);
select * from ;
\set HISTSIZE -1
\set HISTSIZE 30.5
\set HISTSIZE 0
\set HISTSIZE 6
\set HISTSIZE 500
\set HISTSIZE 600
\set HISTSIZE 66666666666666666666666600
\set HISTSIZE -666666666666666666666666666