diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-03-31 13:16:52 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-03-31 13:16:52 +0200 |
commit | 2c4de5b045a79db73052d5b865474a00c9a87e99 (patch) | |
tree | 12a5c2c7a0685ab1674e6b07959688a3470359fd | |
parent | a439fa93f64e6eb34f0633d00d203b4267d58521 (diff) | |
download | busybox-w32-2c4de5b045a79db73052d5b865474a00c9a87e99.tar.gz busybox-w32-2c4de5b045a79db73052d5b865474a00c9a87e99.tar.bz2 busybox-w32-2c4de5b045a79db73052d5b865474a00c9a87e99.zip |
ash,hush: optional support for $HISTFILESIZE.
Based on patch from Alexey Fomenko (ext-alexey.fomenko AT nokia.com)
function old new delta
size_from_HISTFILESIZE - 44 +44
hush_main 998 1025 +27
ash_main 1348 1374 +26
read_line_input 3361 3372 +11
new_line_input_t 17 24 +7
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/Config.src | 5 | ||||
-rw-r--r-- | libbb/lineedit.c | 44 | ||||
-rw-r--r-- | shell/Config.src | 10 | ||||
-rw-r--r-- | shell/ash.c | 9 | ||||
-rw-r--r-- | shell/hush.c | 4 |
6 files changed, 55 insertions, 21 deletions
diff --git a/include/libbb.h b/include/libbb.h index c371e35f2..f2f3313b4 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1373,8 +1373,9 @@ void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; | |||
1373 | 1373 | ||
1374 | #if ENABLE_FEATURE_EDITING | 1374 | #if ENABLE_FEATURE_EDITING |
1375 | /* It's NOT just ENABLEd or disabled. It's a number: */ | 1375 | /* It's NOT just ENABLEd or disabled. It's a number: */ |
1376 | # ifdef CONFIG_FEATURE_EDITING_HISTORY | 1376 | # if defined CONFIG_FEATURE_EDITING_HISTORY && CONFIG_FEATURE_EDITING_HISTORY > 0 |
1377 | # define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0) | 1377 | # define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0) |
1378 | unsigned size_from_HISTFILESIZE(const char *hp); | ||
1378 | # else | 1379 | # else |
1379 | # define MAX_HISTORY 0 | 1380 | # define MAX_HISTORY 0 |
1380 | # endif | 1381 | # endif |
@@ -1384,6 +1385,7 @@ typedef struct line_input_t { | |||
1384 | # if MAX_HISTORY | 1385 | # if MAX_HISTORY |
1385 | int cnt_history; | 1386 | int cnt_history; |
1386 | int cur_history; | 1387 | int cur_history; |
1388 | int max_history; /* must never be <= 0 */ | ||
1387 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY | 1389 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
1388 | unsigned cnt_history_in_file; | 1390 | unsigned cnt_history_in_file; |
1389 | const char *hist_file; | 1391 | const char *hist_file; |
diff --git a/libbb/Config.src b/libbb/Config.src index a25af23b4..0ea8f43ab 100644 --- a/libbb/Config.src +++ b/libbb/Config.src | |||
@@ -80,11 +80,12 @@ config FEATURE_EDITING_VI | |||
80 | 80 | ||
81 | config FEATURE_EDITING_HISTORY | 81 | config FEATURE_EDITING_HISTORY |
82 | int "History size" | 82 | int "History size" |
83 | range 0 99999 | 83 | # Don't allow way too big values here, code uses fixed "char *history[N]" struct member |
84 | range 0 9999 | ||
84 | default 255 | 85 | default 255 |
85 | depends on FEATURE_EDITING | 86 | depends on FEATURE_EDITING |
86 | help | 87 | help |
87 | Specify command history size. | 88 | Specify command history size (0 - disable). |
88 | 89 | ||
89 | config FEATURE_EDITING_SAVEHISTORY | 90 | config FEATURE_EDITING_SAVEHISTORY |
90 | bool "History saving" | 91 | bool "History saving" |
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index b7a2b31dc..095ccfbef 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -1243,12 +1243,26 @@ line_input_t* FAST_FUNC new_line_input_t(int flags) | |||
1243 | { | 1243 | { |
1244 | line_input_t *n = xzalloc(sizeof(*n)); | 1244 | line_input_t *n = xzalloc(sizeof(*n)); |
1245 | n->flags = flags; | 1245 | n->flags = flags; |
1246 | n->max_history = MAX_HISTORY; | ||
1246 | return n; | 1247 | return n; |
1247 | } | 1248 | } |
1248 | 1249 | ||
1249 | 1250 | ||
1250 | #if MAX_HISTORY > 0 | 1251 | #if MAX_HISTORY > 0 |
1251 | 1252 | ||
1253 | unsigned size_from_HISTFILESIZE(const char *hp) | ||
1254 | { | ||
1255 | int size = MAX_HISTORY; | ||
1256 | if (hp) { | ||
1257 | size = atoi(hp); | ||
1258 | if (size <= 0) | ||
1259 | return 1; | ||
1260 | if (size > MAX_HISTORY) | ||
1261 | return MAX_HISTORY; | ||
1262 | } | ||
1263 | return size; | ||
1264 | } | ||
1265 | |||
1252 | static void save_command_ps_at_cur_history(void) | 1266 | static void save_command_ps_at_cur_history(void) |
1253 | { | 1267 | { |
1254 | if (command_ps[0] != BB_NUL) { | 1268 | if (command_ps[0] != BB_NUL) { |
@@ -1339,7 +1353,7 @@ static void load_history(line_input_t *st_parm) | |||
1339 | temp_h[idx] = line; | 1353 | temp_h[idx] = line; |
1340 | st_parm->cnt_history_in_file++; | 1354 | st_parm->cnt_history_in_file++; |
1341 | idx++; | 1355 | idx++; |
1342 | if (idx == MAX_HISTORY) | 1356 | if (idx == st_parm->max_history) |
1343 | idx = 0; | 1357 | idx = 0; |
1344 | } | 1358 | } |
1345 | fclose(fp); | 1359 | fclose(fp); |
@@ -1348,18 +1362,18 @@ static void load_history(line_input_t *st_parm) | |||
1348 | if (st_parm->cnt_history_in_file) { | 1362 | if (st_parm->cnt_history_in_file) { |
1349 | while (temp_h[idx] == NULL) { | 1363 | while (temp_h[idx] == NULL) { |
1350 | idx++; | 1364 | idx++; |
1351 | if (idx == MAX_HISTORY) | 1365 | if (idx == st_parm->max_history) |
1352 | idx = 0; | 1366 | idx = 0; |
1353 | } | 1367 | } |
1354 | } | 1368 | } |
1355 | 1369 | ||
1356 | /* copy temp_h[] to st_parm->history[] */ | 1370 | /* copy temp_h[] to st_parm->history[] */ |
1357 | for (i = 0; i < MAX_HISTORY;) { | 1371 | for (i = 0; i < st_parm->max_history;) { |
1358 | line = temp_h[idx]; | 1372 | line = temp_h[idx]; |
1359 | if (!line) | 1373 | if (!line) |
1360 | break; | 1374 | break; |
1361 | idx++; | 1375 | idx++; |
1362 | if (idx == MAX_HISTORY) | 1376 | if (idx == st_parm->max_history) |
1363 | idx = 0; | 1377 | idx = 0; |
1364 | line_len = strlen(line); | 1378 | line_len = strlen(line); |
1365 | if (line_len >= MAX_LINELEN) | 1379 | if (line_len >= MAX_LINELEN) |
@@ -1390,7 +1404,7 @@ static void save_history(char *str) | |||
1390 | 1404 | ||
1391 | /* did we write so much that history file needs trimming? */ | 1405 | /* did we write so much that history file needs trimming? */ |
1392 | state->cnt_history_in_file++; | 1406 | state->cnt_history_in_file++; |
1393 | if (state->cnt_history_in_file > MAX_HISTORY * 4) { | 1407 | if (state->cnt_history_in_file > state->max_history * 4) { |
1394 | char *new_name; | 1408 | char *new_name; |
1395 | line_input_t *st_temp; | 1409 | line_input_t *st_temp; |
1396 | 1410 | ||
@@ -1436,20 +1450,20 @@ static void remember_in_history(char *str) | |||
1436 | if (i && strcmp(state->history[i-1], str) == 0) | 1450 | if (i && strcmp(state->history[i-1], str) == 0) |
1437 | return; | 1451 | return; |
1438 | 1452 | ||
1439 | free(state->history[MAX_HISTORY]); /* redundant, paranoia */ | 1453 | free(state->history[state->max_history]); /* redundant, paranoia */ |
1440 | state->history[MAX_HISTORY] = NULL; /* redundant, paranoia */ | 1454 | state->history[state->max_history] = NULL; /* redundant, paranoia */ |
1441 | 1455 | ||
1442 | /* If history[] is full, remove the oldest command */ | 1456 | /* If history[] is full, remove the oldest command */ |
1443 | /* we need to keep history[MAX_HISTORY] empty, hence >=, not > */ | 1457 | /* we need to keep history[state->max_history] empty, hence >=, not > */ |
1444 | if (i >= MAX_HISTORY) { | 1458 | if (i >= state->max_history) { |
1445 | free(state->history[0]); | 1459 | free(state->history[0]); |
1446 | for (i = 0; i < MAX_HISTORY-1; i++) | 1460 | for (i = 0; i < state->max_history-1; i++) |
1447 | state->history[i] = state->history[i+1]; | 1461 | state->history[i] = state->history[i+1]; |
1448 | /* i == MAX_HISTORY-1 */ | 1462 | /* i == state->max_history-1 */ |
1449 | } | 1463 | } |
1450 | /* i <= MAX_HISTORY-1 */ | 1464 | /* i <= state->max_history-1 */ |
1451 | state->history[i++] = xstrdup(str); | 1465 | state->history[i++] = xstrdup(str); |
1452 | /* i <= MAX_HISTORY */ | 1466 | /* i <= state->max_history */ |
1453 | state->cur_history = i; | 1467 | state->cur_history = i; |
1454 | state->cnt_history = i; | 1468 | state->cnt_history = i; |
1455 | # if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY | 1469 | # if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
@@ -1970,7 +1984,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman | |||
1970 | maxsize = MAX_LINELEN; | 1984 | maxsize = MAX_LINELEN; |
1971 | S.maxsize = maxsize; | 1985 | S.maxsize = maxsize; |
1972 | 1986 | ||
1973 | /* With null flags, no other fields are ever used */ | 1987 | /* With zero flags, no other fields are ever used */ |
1974 | state = st ? st : (line_input_t*) &const_int_0; | 1988 | state = st ? st : (line_input_t*) &const_int_0; |
1975 | #if MAX_HISTORY > 0 | 1989 | #if MAX_HISTORY > 0 |
1976 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY | 1990 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
@@ -2022,7 +2036,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman | |||
2022 | #endif | 2036 | #endif |
2023 | 2037 | ||
2024 | #if 0 | 2038 | #if 0 |
2025 | for (i = 0; i <= MAX_HISTORY; i++) | 2039 | for (i = 0; i <= state->max_history; i++) |
2026 | bb_error_msg("history[%d]:'%s'", i, state->history[i]); | 2040 | bb_error_msg("history[%d]:'%s'", i, state->history[i]); |
2027 | bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history); | 2041 | bb_error_msg("cur_history:%d cnt_history:%d", state->cur_history, state->cnt_history); |
2028 | #endif | 2042 | #endif |
diff --git a/shell/Config.src b/shell/Config.src index e96c21620..b31e62dda 100644 --- a/shell/Config.src +++ b/shell/Config.src | |||
@@ -136,4 +136,14 @@ config FEATURE_SH_NOFORK | |||
136 | This feature is relatively new. Use with care. Report bugs | 136 | This feature is relatively new. Use with care. Report bugs |
137 | to project mailing list. | 137 | to project mailing list. |
138 | 138 | ||
139 | config FEATURE_SH_HISTFILESIZE | ||
140 | bool "Use $HISTFILESIZE" | ||
141 | default y | ||
142 | depends on HUSH || ASH | ||
143 | help | ||
144 | This option makes busybox shells to use $HISTFILESIZE variable | ||
145 | to set shell history size. Note that its max value is capped | ||
146 | by "History size" setting in library tuning section. | ||
147 | |||
148 | |||
139 | endmenu | 149 | endmenu |
diff --git a/shell/ash.c b/shell/ash.c index 1520c5ae5..11ba9774a 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -13143,10 +13143,9 @@ int ash_main(int argc UNUSED_PARAM, char **argv) | |||
13143 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY | 13143 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
13144 | if (iflag) { | 13144 | if (iflag) { |
13145 | const char *hp = lookupvar("HISTFILE"); | 13145 | const char *hp = lookupvar("HISTFILE"); |
13146 | 13146 | if (!hp) { | |
13147 | if (hp == NULL) { | ||
13148 | hp = lookupvar("HOME"); | 13147 | hp = lookupvar("HOME"); |
13149 | if (hp != NULL) { | 13148 | if (hp) { |
13150 | char *defhp = concat_path_file(hp, ".ash_history"); | 13149 | char *defhp = concat_path_file(hp, ".ash_history"); |
13151 | setvar("HISTFILE", defhp, 0); | 13150 | setvar("HISTFILE", defhp, 0); |
13152 | free(defhp); | 13151 | free(defhp); |
@@ -13195,6 +13194,10 @@ int ash_main(int argc UNUSED_PARAM, char **argv) | |||
13195 | const char *hp = lookupvar("HISTFILE"); | 13194 | const char *hp = lookupvar("HISTFILE"); |
13196 | if (hp) | 13195 | if (hp) |
13197 | line_input_state->hist_file = hp; | 13196 | line_input_state->hist_file = hp; |
13197 | # if ENABLE_FEATURE_SH_HISTFILESIZE | ||
13198 | hp = lookupvar("HISTFILESIZE"); | ||
13199 | line_input_state->max_history = size_from_HISTFILESIZE(hp); | ||
13200 | # endif | ||
13198 | } | 13201 | } |
13199 | #endif | 13202 | #endif |
13200 | state4: /* XXX ??? - why isn't this before the "if" statement */ | 13203 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
diff --git a/shell/hush.c b/shell/hush.c index e698e6c52..d3e957c2f 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -7614,6 +7614,10 @@ int hush_main(int argc, char **argv) | |||
7614 | //set_local_var(xasprintf("HISTFILE=%s", ...)); | 7614 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
7615 | } | 7615 | } |
7616 | } | 7616 | } |
7617 | # if ENABLE_FEATURE_SH_HISTFILESIZE | ||
7618 | hp = get_local_var_value("HISTFILESIZE"); | ||
7619 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); | ||
7620 | # endif | ||
7617 | } | 7621 | } |
7618 | # endif | 7622 | # endif |
7619 | #endif | 7623 | #endif |