{
line_input_t *n = xzalloc(sizeof(*n));
n->flags = flags;
+ n->max_history = MAX_HISTORY;
return n;
}
#if MAX_HISTORY > 0
+unsigned size_from_HISTFILESIZE(const char *hp)
+{
+ int size = MAX_HISTORY;
+ if (hp) {
+ size = atoi(hp);
+ if (size <= 0)
+ return 1;
+ if (size > MAX_HISTORY)
+ return MAX_HISTORY;
+ }
+ return size;
+}
+
static void save_command_ps_at_cur_history(void)
{
if (command_ps[0] != BB_NUL) {
temp_h[idx] = line;
st_parm->cnt_history_in_file++;
idx++;
- if (idx == MAX_HISTORY)
+ if (idx == st_parm->max_history)
idx = 0;
}
fclose(fp);
if (st_parm->cnt_history_in_file) {
while (temp_h[idx] == NULL) {
idx++;
- if (idx == MAX_HISTORY)
+ if (idx == st_parm->max_history)
idx = 0;
}
}
/* copy temp_h[] to st_parm->history[] */
- for (i = 0; i < MAX_HISTORY;) {
+ for (i = 0; i < st_parm->max_history;) {
line = temp_h[idx];
if (!line)
break;
idx++;
- if (idx == MAX_HISTORY)
+ if (idx == st_parm->max_history)
idx = 0;
line_len = strlen(line);
if (line_len >= MAX_LINELEN)
/* did we write so much that history file needs trimming? */
state->cnt_history_in_file++;
- if (state->cnt_history_in_file > MAX_HISTORY * 4) {
+ if (state->cnt_history_in_file > state->max_history * 4) {
char *new_name;
line_input_t *st_temp;
if (i && strcmp(state->history[i-1], str) == 0)
return;
- free(state->history[MAX_HISTORY]); /* redundant, paranoia */
- state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */
+ free(state->history[state->max_history]); /* redundant, paranoia */
+ state->history[state->max_history] = NULL; /* redundant, paranoia */
/* If history[] is full, remove the oldest command */
- /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */
- if (i >= MAX_HISTORY) {
+ /* we need to keep history[state->max_history] empty, hence >=, not > */
+ if (i >= state->max_history) {
free(state->history[0]);
- for (i = 0; i < MAX_HISTORY-1; i++)
+ for (i = 0; i < state->max_history-1; i++)
state->history[i] = state->history[i+1];
- /* i == MAX_HISTORY-1 */
+ /* i == state->max_history-1 */
}
- /* i <= MAX_HISTORY-1 */
+ /* i <= state->max_history-1 */
state->history[i++] = xstrdup(str);
- /* i <= MAX_HISTORY */
+ /* i <= state->max_history */
state->cur_history = i;
state->cnt_history = i;
# if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
maxsize = MAX_LINELEN;
S.maxsize = maxsize;
- /* With null flags, no other fields are ever used */
+ /* With zero flags, no other fields are ever used */
state = st ? st : (line_input_t*) &const_int_0;
#if MAX_HISTORY > 0
# if ENABLE_FEATURE_EDITING_SAVEHISTORY
#endif
#if 0
- for (i = 0; i <= MAX_HISTORY; i++)
+ for (i = 0; i <= state->max_history; i++)
bb_error_msg("history[%d]:'%s'", i, state->history[i]);
bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history);
#endif
#if ENABLE_FEATURE_EDITING_SAVEHISTORY
if (iflag) {
const char *hp = lookupvar("HISTFILE");
-
- if (hp == NULL) {
+ if (!hp) {
hp = lookupvar("HOME");
- if (hp != NULL) {
+ if (hp) {
char *defhp = concat_path_file(hp, ".ash_history");
setvar("HISTFILE", defhp, 0);
free(defhp);
const char *hp = lookupvar("HISTFILE");
if (hp)
line_input_state->hist_file = hp;
+# if ENABLE_FEATURE_SH_HISTFILESIZE
+ hp = lookupvar("HISTFILESIZE");
+ line_input_state->max_history = size_from_HISTFILESIZE(hp);
+# endif
}
#endif
state4: /* XXX ??? - why isn't this before the "if" statement */