From 3e96c3390f5ac57e71bca30a2bdb2887ebb992a9 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Wed, 24 Dec 2014 14:54:06 -0600 Subject: [PATCH] Check for TERM changes every time we read input. Also, fix possible crashes when termcap entries aren't found. --- include/libbb.h | 2 -- libbb/lineedit.c | 13 ++++++++++--- shell/hush.c | 1 - 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 1df921378..8af70be68 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1405,8 +1405,6 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC; read_line_input(prompt, command, maxsize) #endif -void init_termcap(void); - #ifndef COMM_LEN # ifdef TASK_COMM_LEN diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 51715344d..0a5899537 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -135,6 +135,7 @@ char *right_cmd = ESC"[C"; char *clear_to_end_of_screen_cmd = ESC"[J"; #endif +char *old_term = NULL; char termcap_string_buf[TERMCAP_BUFSIZ]; /* Terminal escape sequences to process in input (used by read_key) */ @@ -257,7 +258,7 @@ static void add_escape_seq_from_termcap(char *termcap_code, signed char keycode, } } -void init_termcap(void) +static void init_termcap(void) { char *term; char *termcap_buffer; @@ -267,6 +268,11 @@ void init_termcap(void) term = getenv("TERM"); if (term == NULL) term = DEFAULT_TERM; + if (old_term && strcmp(term, old_term) == 0) + return; + free(old_term); + old_term = strdup(term); + termcap_buffer = malloc(TERMCAP_BUFSIZ); if (termcap_buffer == NULL) return; @@ -292,14 +298,14 @@ void init_termcap(void) add_escape_seq_from_termcap("ku", KEYCODE_UP, &string_buf); add_escape_seq_from_termcap("kd", KEYCODE_DOWN, &string_buf); add_escape_seq_from_termcap("kl", KEYCODE_LEFT, &string_buf); - if (strcmp(LAST_ESCAPE_SEQ(), ESC"[D") == 0) { + if (n_escape_seqs && strcmp(LAST_ESCAPE_SEQ(), ESC"[D") == 0) { add_escape_seq(ESC"[1;3D", KEYCODE_ALT_LEFT); add_escape_seq(ESC"[1;9D", KEYCODE_ALT_LEFT); // xterm on OS X: command-left add_escape_seq(ESC"b", KEYCODE_ALT_LEFT); // OS X Terminal: option-left add_escape_seq(ESC"[1;5D", KEYCODE_CTRL_LEFT); } add_escape_seq_from_termcap("kr", KEYCODE_RIGHT, &string_buf); - if (strcmp(LAST_ESCAPE_SEQ(), ESC"[C") == 0) { + if (n_escape_seqs && strcmp(LAST_ESCAPE_SEQ(), ESC"[C") == 0) { add_escape_seq(ESC"[1;3C", KEYCODE_ALT_RIGHT); add_escape_seq(ESC"[1;9C", KEYCODE_ALT_RIGHT); // xterm on OS X: command-right add_escape_seq(ESC"f", KEYCODE_ALT_RIGHT); // OS X Terminal: option-right @@ -2443,6 +2449,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman } init_unicode(); + init_termcap(); // FIXME: audit & improve this if (maxsize > MAX_LINELEN) diff --git a/shell/hush.c b/shell/hush.c index f4f134b54..763187bf2 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -8349,7 +8349,6 @@ int hush_main(int argc, char **argv) #endif #if ENABLE_FEATURE_EDITING - init_termcap(); G.line_input_state = new_line_input_t(FOR_SHELL); #endif