(subshell_prompt): changed to GString.

(read_subshell_prompt): refactoring to ret rid of low-level memory reallocation.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-01-01 19:53:11 +04:00
parent 0e7810eb5f
commit e35f044ccd
4 changed files with 19 additions and 39 deletions

View File

@ -816,7 +816,7 @@ setup_cmdline (void)
#ifdef ENABLE_SUBSHELL #ifdef ENABLE_SUBSHELL
if (mc_global.tty.use_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) if (tmp_prompt == NULL)
#endif #endif
tmp_prompt = (char *) mc_prompt; tmp_prompt = (char *) mc_prompt;

View File

@ -376,16 +376,8 @@ main (int argc, char *argv[])
if (mc_global.tty.alternate_plus_minus) if (mc_global.tty.alternate_plus_minus)
application_keypad_mode (); application_keypad_mode ();
#ifdef ENABLE_SUBSHELL /* subshell_prompt is NULL here */
if (mc_global.tty.use_subshell) mc_prompt = (geteuid () == 0) ? "# " : "$ ";
{
mc_prompt = strip_ctrl_codes (subshell_prompt);
if (mc_prompt == NULL)
mc_prompt = (geteuid () == 0) ? "# " : "$ ";
}
else
#endif /* ENABLE_SUBSHELL */
mc_prompt = (geteuid () == 0) ? "# " : "$ ";
if (config_migrated) if (config_migrated)
{ {

View File

@ -75,7 +75,7 @@
enum subshell_state_enum subshell_state; enum subshell_state_enum subshell_state;
/* Holds the latest prompt captured from the subshell */ /* 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 */ /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
/* We need to paint it after CONSOLE_RESTORE, see: load_prompt */ /* 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 gboolean
read_subshell_prompt (void) read_subshell_prompt (void)
{ {
static int prompt_size = INITIAL_PROMPT_SIZE; int rc = 0;
int bytes = 0, i, rc = 0; ssize_t bytes = 0;
struct timeval timeleft = { 0, 0 }; struct timeval timeleft = { 0, 0 };
fd_set tmp; fd_set tmp;
FD_ZERO (&tmp); FD_ZERO (&tmp);
FD_SET (mc_global.tty.subshell_pty, &tmp); FD_SET (mc_global.tty.subshell_pty, &tmp);
/* First time through */
if (subshell_prompt == NULL) if (subshell_prompt == NULL)
{ /* First time through */ subshell_prompt = g_string_sized_new (INITIAL_PROMPT_SIZE);
subshell_prompt = g_malloc (prompt_size);
*subshell_prompt = '\0';
prompt_pos = 0;
}
while (subshell_alive 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 */ /* Check for `select' errors */
if (rc == -1) if (rc == -1)
{ {
@ -1020,23 +1019,12 @@ read_subshell_prompt (void)
bytes = read (mc_global.tty.subshell_pty, pty_buffer, sizeof (pty_buffer)); bytes = read (mc_global.tty.subshell_pty, pty_buffer, sizeof (pty_buffer));
/* Extract the prompt from the shell output */ /* Extract the prompt from the shell output */
g_string_set_size (subshell_prompt, 0);
for (i = 0; i < bytes; ++i) for (i = 0; i < bytes; i++)
if (pty_buffer[i] == '\n' || pty_buffer[i] == '\r') if (pty_buffer[i] == '\n' || pty_buffer[i] == '\r')
{ g_string_set_size (subshell_prompt, 0);
prompt_pos = 0; else if (pty_buffer[i] != '\0')
} g_string_append_c (subshell_prompt, pty_buffer[i]);
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';
} }
return (rc != 0 || bytes != 0); return (rc != 0 || bytes != 0);
@ -1049,7 +1037,7 @@ do_update_prompt (void)
{ {
if (update_subshell_prompt) if (update_subshell_prompt)
{ {
printf ("\r\n%s", subshell_prompt); printf ("\r\n%s", subshell_prompt->str);
fflush (stdout); fflush (stdout);
update_subshell_prompt = FALSE; update_subshell_prompt = FALSE;
} }
@ -1077,7 +1065,7 @@ exit_subshell (void)
tcsh_fifo, unix_error_string (errno)); tcsh_fifo, unix_error_string (errno));
} }
g_free (subshell_prompt); g_string_free (subshell_prompt, TRUE);
subshell_prompt = NULL; subshell_prompt = NULL;
pty_buffer[0] = '\0'; pty_buffer[0] = '\0';
} }

View File

@ -32,7 +32,7 @@ enum
extern enum subshell_state_enum subshell_state; extern enum subshell_state_enum subshell_state;
/* Holds the latest prompt captured from the subshell */ /* Holds the latest prompt captured from the subshell */
extern char *subshell_prompt; extern GString *subshell_prompt;
extern gboolean update_subshell_prompt; extern gboolean update_subshell_prompt;