diff options
Diffstat (limited to 'libbb/lineedit.c')
-rw-r--r-- | libbb/lineedit.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 9a773b4b8..071fc5bce 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -114,8 +114,8 @@ struct lineedit_statics { | |||
114 | unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */ | 114 | unsigned cmdedit_prmt_len; /* length of prompt (without colors etc) */ |
115 | 115 | ||
116 | unsigned cursor; | 116 | unsigned cursor; |
117 | unsigned command_len; | 117 | int command_len; /* must be signed (^D returns -1 len) */ |
118 | /* *int* maxsize: we want x in "if (x > S.maxsize)" | 118 | /* signed maxsize: we want x in "if (x > S.maxsize)" |
119 | * to _not_ be promoted to unsigned */ | 119 | * to _not_ be promoted to unsigned */ |
120 | int maxsize; | 120 | int maxsize; |
121 | CHAR_T *command_ps; | 121 | CHAR_T *command_ps; |
@@ -1095,15 +1095,15 @@ static void save_command_ps_at_cur_history(void) | |||
1095 | int cur = state->cur_history; | 1095 | int cur = state->cur_history; |
1096 | free(state->history[cur]); | 1096 | free(state->history[cur]); |
1097 | 1097 | ||
1098 | #if ENABLE_FEATURE_ASSUME_UNICODE | 1098 | # if ENABLE_FEATURE_ASSUME_UNICODE |
1099 | { | 1099 | { |
1100 | char tbuf[MAX_LINELEN]; | 1100 | char tbuf[MAX_LINELEN]; |
1101 | save_string(tbuf, sizeof(tbuf)); | 1101 | save_string(tbuf, sizeof(tbuf)); |
1102 | state->history[cur] = xstrdup(tbuf); | 1102 | state->history[cur] = xstrdup(tbuf); |
1103 | } | 1103 | } |
1104 | #else | 1104 | # else |
1105 | state->history[cur] = xstrdup(command_ps); | 1105 | state->history[cur] = xstrdup(command_ps); |
1106 | #endif | 1106 | # endif |
1107 | } | 1107 | } |
1108 | } | 1108 | } |
1109 | 1109 | ||
@@ -1131,7 +1131,7 @@ static int get_next_history(void) | |||
1131 | return 0; | 1131 | return 0; |
1132 | } | 1132 | } |
1133 | 1133 | ||
1134 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY | 1134 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY |
1135 | /* We try to ensure that concurrent additions to the history | 1135 | /* We try to ensure that concurrent additions to the history |
1136 | * do not overwrite each other. | 1136 | * do not overwrite each other. |
1137 | * Otherwise shell users get unhappy. | 1137 | * Otherwise shell users get unhappy. |
@@ -1256,10 +1256,10 @@ static void save_history(char *str) | |||
1256 | free_line_input_t(st_temp); | 1256 | free_line_input_t(st_temp); |
1257 | } | 1257 | } |
1258 | } | 1258 | } |
1259 | #else | 1259 | # else |
1260 | #define load_history(a) ((void)0) | 1260 | # define load_history(a) ((void)0) |
1261 | #define save_history(a) ((void)0) | 1261 | # define save_history(a) ((void)0) |
1262 | #endif /* FEATURE_COMMAND_SAVEHISTORY */ | 1262 | # endif /* FEATURE_COMMAND_SAVEHISTORY */ |
1263 | 1263 | ||
1264 | static void remember_in_history(char *str) | 1264 | static void remember_in_history(char *str) |
1265 | { | 1265 | { |
@@ -1290,15 +1290,15 @@ static void remember_in_history(char *str) | |||
1290 | /* i <= MAX_HISTORY */ | 1290 | /* i <= MAX_HISTORY */ |
1291 | state->cur_history = i; | 1291 | state->cur_history = i; |
1292 | state->cnt_history = i; | 1292 | state->cnt_history = i; |
1293 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY | 1293 | # if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
1294 | if ((state->flags & SAVE_HISTORY) && state->hist_file) | 1294 | if ((state->flags & SAVE_HISTORY) && state->hist_file) |
1295 | save_history(str); | 1295 | save_history(str); |
1296 | #endif | 1296 | # endif |
1297 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;) | 1297 | IF_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines++;) |
1298 | } | 1298 | } |
1299 | 1299 | ||
1300 | #else /* MAX_HISTORY == 0 */ | 1300 | #else /* MAX_HISTORY == 0 */ |
1301 | #define remember_in_history(a) ((void)0) | 1301 | # define remember_in_history(a) ((void)0) |
1302 | #endif /* MAX_HISTORY */ | 1302 | #endif /* MAX_HISTORY */ |
1303 | 1303 | ||
1304 | 1304 | ||
@@ -1476,11 +1476,11 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1476 | c = *prmt_ptr++; | 1476 | c = *prmt_ptr++; |
1477 | 1477 | ||
1478 | switch (c) { | 1478 | switch (c) { |
1479 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR | 1479 | # if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
1480 | case 'u': | 1480 | case 'u': |
1481 | pbuf = user_buf ? user_buf : (char*)""; | 1481 | pbuf = user_buf ? user_buf : (char*)""; |
1482 | break; | 1482 | break; |
1483 | #endif | 1483 | # endif |
1484 | case 'h': | 1484 | case 'h': |
1485 | pbuf = free_me = safe_gethostname(); | 1485 | pbuf = free_me = safe_gethostname(); |
1486 | *strchrnul(pbuf, '.') = '\0'; | 1486 | *strchrnul(pbuf, '.') = '\0'; |
@@ -1488,7 +1488,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1488 | case '$': | 1488 | case '$': |
1489 | c = (geteuid() == 0 ? '#' : '$'); | 1489 | c = (geteuid() == 0 ? '#' : '$'); |
1490 | break; | 1490 | break; |
1491 | #if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR | 1491 | # if ENABLE_FEATURE_GETUSERNAME_AND_HOMEDIR |
1492 | case 'w': | 1492 | case 'w': |
1493 | /* /home/user[/something] -> ~[/something] */ | 1493 | /* /home/user[/something] -> ~[/something] */ |
1494 | pbuf = cwd_buf; | 1494 | pbuf = cwd_buf; |
@@ -1501,7 +1501,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1501 | pbuf = free_me = xasprintf("~%s", cwd_buf + l); | 1501 | pbuf = free_me = xasprintf("~%s", cwd_buf + l); |
1502 | } | 1502 | } |
1503 | break; | 1503 | break; |
1504 | #endif | 1504 | # endif |
1505 | case 'W': | 1505 | case 'W': |
1506 | pbuf = cwd_buf; | 1506 | pbuf = cwd_buf; |
1507 | cp = strrchr(pbuf, '/'); | 1507 | cp = strrchr(pbuf, '/'); |
@@ -1688,13 +1688,15 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
1688 | 1688 | ||
1689 | /* With null flags, no other fields are ever used */ | 1689 | /* With null flags, no other fields are ever used */ |
1690 | state = st ? st : (line_input_t*) &const_int_0; | 1690 | state = st ? st : (line_input_t*) &const_int_0; |
1691 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY | 1691 | #if MAX_HISTORY > 0 |
1692 | # if ENABLE_FEATURE_EDITING_SAVEHISTORY | ||
1692 | if ((state->flags & SAVE_HISTORY) && state->hist_file) | 1693 | if ((state->flags & SAVE_HISTORY) && state->hist_file) |
1693 | if (state->cnt_history == 0) | 1694 | if (state->cnt_history == 0) |
1694 | load_history(state); | 1695 | load_history(state); |
1695 | #endif | 1696 | # endif |
1696 | if (state->flags & DO_HISTORY) | 1697 | if (state->flags & DO_HISTORY) |
1697 | state->cur_history = state->cnt_history; | 1698 | state->cur_history = state->cnt_history; |
1699 | #endif | ||
1698 | 1700 | ||
1699 | /* prepare before init handlers */ | 1701 | /* prepare before init handlers */ |
1700 | cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */ | 1702 | cmdedit_y = 0; /* quasireal y, not true if line > xt*yt */ |
@@ -1716,7 +1718,7 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
1716 | new_settings.c_cc[VTIME] = 0; | 1718 | new_settings.c_cc[VTIME] = 0; |
1717 | /* Turn off CTRL-C, so we can trap it */ | 1719 | /* Turn off CTRL-C, so we can trap it */ |
1718 | #ifndef _POSIX_VDISABLE | 1720 | #ifndef _POSIX_VDISABLE |
1719 | #define _POSIX_VDISABLE '\0' | 1721 | # define _POSIX_VDISABLE '\0' |
1720 | #endif | 1722 | #endif |
1721 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; | 1723 | new_settings.c_cc[VINTR] = _POSIX_VDISABLE; |
1722 | tcsetattr_stdin_TCSANOW(&new_settings); | 1724 | tcsetattr_stdin_TCSANOW(&new_settings); |
@@ -2037,7 +2039,8 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
2037 | rewrite_line: | 2039 | rewrite_line: |
2038 | /* Rewrite the line with the selected history item */ | 2040 | /* Rewrite the line with the selected history item */ |
2039 | /* change command */ | 2041 | /* change command */ |
2040 | command_len = load_string(state->history[state->cur_history] ? : "", maxsize); | 2042 | command_len = load_string(state->history[state->cur_history] ? |
2043 | state->history[state->cur_history] : "", maxsize); | ||
2041 | /* redraw and go to eol (bol, in vi) */ | 2044 | /* redraw and go to eol (bol, in vi) */ |
2042 | redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0); | 2045 | redraw(cmdedit_y, (state->flags & VI_MODE) ? 9999 : 0); |
2043 | break; | 2046 | break; |
@@ -2121,7 +2124,9 @@ int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, li | |||
2121 | #undef command | 2124 | #undef command |
2122 | 2125 | ||
2123 | #if ENABLE_FEATURE_ASSUME_UNICODE | 2126 | #if ENABLE_FEATURE_ASSUME_UNICODE |
2124 | command_len = save_string(command, maxsize - 1); | 2127 | command[0] = '\0'; |
2128 | if (command_len > 0) | ||
2129 | command_len = save_string(command, maxsize - 1); | ||
2125 | free(command_ps); | 2130 | free(command_ps); |
2126 | #endif | 2131 | #endif |
2127 | 2132 | ||