From: Andrew Borodin Date: Tue, 1 Jan 2013 15:53:11 +0000 (+0400) Subject: (subshell_prompt): changed to GString. X-Git-Tag: 4.8.8~29^2~8 X-Git-Url: https://repo.or.cz/w/midnight-commander.git/commitdiff_plain/e35f044ccdd41922f925c99e6d50930ea8c7c47e (subshell_prompt): changed to GString. (read_subshell_prompt): refactoring to ret rid of low-level memory reallocation. Signed-off-by: Andrew Borodin --- diff --git a/src/filemanager/layout.c b/src/filemanager/layout.c index 2219ec5de..fca176510 100644 --- a/src/filemanager/layout.c +++ b/src/filemanager/layout.c @@ -816,7 +816,7 @@ setup_cmdline (void) #ifdef ENABLE_SUBSHELL if (mc_global.tty.use_subshell) - tmp_prompt = strip_ctrl_codes (subshell_prompt); + tmp_prompt = strip_ctrl_codes (subshell_prompt->str); if (tmp_prompt == NULL) #endif tmp_prompt = (char *) mc_prompt; diff --git a/src/main.c b/src/main.c index 0ef21dd6b..1123624a5 100644 --- a/src/main.c +++ b/src/main.c @@ -376,16 +376,8 @@ main (int argc, char *argv[]) if (mc_global.tty.alternate_plus_minus) application_keypad_mode (); -#ifdef ENABLE_SUBSHELL - if (mc_global.tty.use_subshell) - { - mc_prompt = strip_ctrl_codes (subshell_prompt); - if (mc_prompt == NULL) - mc_prompt = (geteuid () == 0) ? "# " : "$ "; - } - else -#endif /* ENABLE_SUBSHELL */ - mc_prompt = (geteuid () == 0) ? "# " : "$ "; + /* subshell_prompt is NULL here */ + mc_prompt = (geteuid () == 0) ? "# " : "$ "; if (config_migrated) { diff --git a/src/subshell.c b/src/subshell.c index 01636b40c..e5175cec1 100644 --- a/src/subshell.c +++ b/src/subshell.c @@ -75,7 +75,7 @@ enum subshell_state_enum subshell_state; /* Holds the latest prompt captured from the subshell */ -char *subshell_prompt = NULL; +GString *subshell_prompt = NULL; /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */ /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */ @@ -984,24 +984,23 @@ invoke_subshell (const char *command, int how, vfs_path_t ** new_dir_vpath) gboolean read_subshell_prompt (void) { - static int prompt_size = INITIAL_PROMPT_SIZE; - int bytes = 0, i, rc = 0; + int rc = 0; + ssize_t bytes = 0; struct timeval timeleft = { 0, 0 }; fd_set tmp; FD_ZERO (&tmp); FD_SET (mc_global.tty.subshell_pty, &tmp); + /* First time through */ if (subshell_prompt == NULL) - { /* First time through */ - subshell_prompt = g_malloc (prompt_size); - *subshell_prompt = '\0'; - prompt_pos = 0; - } + subshell_prompt = g_string_sized_new (INITIAL_PROMPT_SIZE); while (subshell_alive - && (rc = select (mc_global.tty.subshell_pty + 1, &tmp, NULL, NULL, &timeleft))) + && (rc = select (mc_global.tty.subshell_pty + 1, &tmp, NULL, NULL, &timeleft)) != 0) { + ssize_t i; + /* Check for `select' errors */ if (rc == -1) { @@ -1020,23 +1019,12 @@ read_subshell_prompt (void) bytes = read (mc_global.tty.subshell_pty, pty_buffer, sizeof (pty_buffer)); /* Extract the prompt from the shell output */ - - for (i = 0; i < bytes; ++i) + g_string_set_size (subshell_prompt, 0); + for (i = 0; i < bytes; i++) if (pty_buffer[i] == '\n' || pty_buffer[i] == '\r') - { - prompt_pos = 0; - } - else - { - if (!pty_buffer[i]) - continue; - - subshell_prompt[prompt_pos++] = pty_buffer[i]; - if (prompt_pos == prompt_size) - subshell_prompt = g_realloc (subshell_prompt, prompt_size *= 2); - } - - subshell_prompt[prompt_pos] = '\0'; + g_string_set_size (subshell_prompt, 0); + else if (pty_buffer[i] != '\0') + g_string_append_c (subshell_prompt, pty_buffer[i]); } return (rc != 0 || bytes != 0); @@ -1049,7 +1037,7 @@ do_update_prompt (void) { if (update_subshell_prompt) { - printf ("\r\n%s", subshell_prompt); + printf ("\r\n%s", subshell_prompt->str); fflush (stdout); update_subshell_prompt = FALSE; } @@ -1077,7 +1065,7 @@ exit_subshell (void) tcsh_fifo, unix_error_string (errno)); } - g_free (subshell_prompt); + g_string_free (subshell_prompt, TRUE); subshell_prompt = NULL; pty_buffer[0] = '\0'; } diff --git a/src/subshell.h b/src/subshell.h index 5775e3490..d14e8a7a7 100644 --- a/src/subshell.h +++ b/src/subshell.h @@ -32,7 +32,7 @@ enum extern enum subshell_state_enum subshell_state; /* Holds the latest prompt captured from the subshell */ -extern char *subshell_prompt; +extern GString *subshell_prompt; extern gboolean update_subshell_prompt;