Use struct buffer in gdb_readline_no_editing_callback

gdb/ChangeLog:
2016-03-09  Pedro Alves  <palves@redhat.com>

	* event-top.c: Include buffer.h.
	(gdb_readline_no_editing_callback): Use struct buffer instead
	of xrealloc.
This commit is contained in:
Pedro Alves 2016-03-09 18:25:00 +00:00
parent 7a3bde34bc
commit 187212b3c1
2 changed files with 25 additions and 20 deletions

View File

@ -1,3 +1,9 @@
2016-03-09 Pedro Alves <palves@redhat.com>
* event-top.c: Include buffer.h.
(gdb_readline_no_editing_callback): Use struct buffer instead
of xrealloc.
2016-03-09 Pedro Alves <palves@redhat.com> 2016-03-09 Pedro Alves <palves@redhat.com>
* common/buffer.h (buffer_grow_char): New function. * common/buffer.h (buffer_grow_char): New function.

View File

@ -37,6 +37,7 @@
#include "gdbcmd.h" /* for dont_repeat() */ #include "gdbcmd.h" /* for dont_repeat() */
#include "annotate.h" #include "annotate.h"
#include "maint.h" #include "maint.h"
#include "buffer.h"
/* readline include files. */ /* readline include files. */
#include "readline/readline.h" #include "readline/readline.h"
@ -382,7 +383,7 @@ top_level_prompt (void)
return xstrdup (prompt); return xstrdup (prompt);
} }
/* When there is an event ready on the stdin file desriptor, instead /* When there is an event ready on the stdin file descriptor, instead
of calling readline directly throught the callback function, or of calling readline directly throught the callback function, or
instead of calling gdb_readline_no_editing_callback, give gdb a instead of calling gdb_readline_no_editing_callback, give gdb a
chance to detect errors and do something. */ chance to detect errors and do something. */
@ -678,10 +679,11 @@ gdb_readline_no_editing_callback (gdb_client_data client_data)
{ {
int c; int c;
char *result; char *result;
int input_index = 0; struct buffer line_buffer;
int result_size = 80;
static int done_once = 0; static int done_once = 0;
buffer_init (&line_buffer);
/* Unbuffer the input stream, so that, later on, the calls to fgetc /* Unbuffer the input stream, so that, later on, the calls to fgetc
fetch only one char at the time from the stream. The fgetc's will fetch only one char at the time from the stream. The fgetc's will
get up to the first newline, but there may be more chars in the get up to the first newline, but there may be more chars in the
@ -694,8 +696,6 @@ gdb_readline_no_editing_callback (gdb_client_data client_data)
done_once = 1; done_once = 1;
} }
result = (char *) xmalloc (result_size);
/* We still need the while loop here, even though it would seem /* We still need the while loop here, even though it would seem
obvious to invoke gdb_readline_no_editing_callback at every obvious to invoke gdb_readline_no_editing_callback at every
character entered. If not using the readline library, the character entered. If not using the readline library, the
@ -712,32 +712,31 @@ gdb_readline_no_editing_callback (gdb_client_data client_data)
if (c == EOF) if (c == EOF)
{ {
if (input_index > 0) if (line_buffer.used_size > 0)
/* The last line does not end with a newline. Return it, {
and if we are called again fgetc will still return EOF /* The last line does not end with a newline. Return it, and
and we'll return NULL then. */ if we are called again fgetc will still return EOF and
break; we'll return NULL then. */
xfree (result); break;
}
xfree (buffer_finish (&line_buffer));
(*input_handler) (0); (*input_handler) (0);
return; return;
} }
if (c == '\n') if (c == '\n')
{ {
if (input_index > 0 && result[input_index - 1] == '\r') if (line_buffer.used_size > 0
input_index--; && line_buffer.buffer[line_buffer.used_size - 1] == '\r')
line_buffer.used_size--;
break; break;
} }
result[input_index++] = c; buffer_grow_char (&line_buffer, c);
while (input_index >= result_size)
{
result_size *= 2;
result = (char *) xrealloc (result, result_size);
}
} }
result[input_index++] = '\0'; buffer_grow_char (&line_buffer, '\0');
result = buffer_finish (&line_buffer);
(*input_handler) (result); (*input_handler) (result);
} }