Pavel Chromy: telnet line buffer size checking, history does not store repeating lines, improved history printing, log callback tweak

git-svn-id: svn://svn.berlios.de/openocd/trunk@491 b42882b7-edfa-0310-969c-e2dbd0fdcd60
This commit is contained in:
oharboe 2008-03-11 08:15:54 +00:00
parent 7dc025321c
commit 970e16603b
1 changed files with 87 additions and 67 deletions

View File

@ -118,12 +118,20 @@ void telnet_log_callback(void *priv, const char *file, int line,
telnet_connection_t *t_con = connection->priv;
int i;
/* if there is no prompt, simply output the message */
if (t_con->line_cursor < 0)
{
telnet_outputline(connection, string);
return;
}
/* clear the command line */
telnet_write(connection, "\r", 1);
for (i = strlen(t_con->prompt) + t_con->line_size; i>0; i-=16)
telnet_write(connection, " ", i>16 ? 16 : i);
telnet_write(connection, "\r", 1);
/* output the message */
telnet_outputline(connection, string);
/* put the command line to its previous state */
@ -244,9 +252,15 @@ int telnet_input(connection_t *connection)
{
if (isprint(*buf_p)) /* printable character */
{
telnet_write(connection, buf_p, 1);
if (t_con->line_cursor == t_con->line_size)
/* watch buffer size leaving one spare character for string null termination */
if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
{
/* output audible bell if buffer is full */
telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
}
else if (t_con->line_cursor == t_con->line_size)
{
telnet_write(connection, buf_p, 1);
t_con->line[t_con->line_size++] = *buf_p;
t_con->line_cursor++;
}
@ -254,9 +268,10 @@ int telnet_input(connection_t *connection)
{
int i;
memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
t_con->line[t_con->line_cursor++] = *buf_p;
t_con->line[t_con->line_cursor] = *buf_p;
t_con->line_size++;
telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
t_con->line_cursor++;
for (i = t_con->line_cursor; i < t_con->line_size; i++)
{
telnet_write(connection, "\b", 1);
@ -274,13 +289,13 @@ int telnet_input(connection_t *connection)
{
int retval;
/* skip over combinations with CR/LF + NUL */
if (((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)) && (bytes_read > 1))
/* skip over combinations with CR/LF and NUL characters */
if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
{
buf_p++;
bytes_read--;
}
if ((*(buf_p + 1) == 0) && (bytes_read > 1))
if ((bytes_read > 1) && (*(buf_p + 1) == 0))
{
buf_p++;
bytes_read--;
@ -292,11 +307,13 @@ int telnet_input(connection_t *connection)
if (strcmp(t_con->line, "history") == 0)
{
int i;
for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
{
if (t_con->history[i])
/* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
if (history_line)
{
telnet_write(connection, t_con->history[i], strlen(t_con->history[i]));
telnet_write(connection, history_line, strlen(history_line));
telnet_write(connection, "\r\n\x00", 3);
}
}
@ -305,8 +322,9 @@ int telnet_input(connection_t *connection)
continue;
}
/* Save only non-blank lines in the history */
if (t_con->line_size > 0)
/* save only non-blank not repeating lines in the history */
char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
{
/* if the history slot is already taken, free it */
if (t_con->history[t_con->next_history])
@ -331,9 +349,11 @@ int telnet_input(connection_t *connection)
}
t_con->line_size = 0;
t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
retval = command_run_line(command_context, t_con->line);
t_con->line_cursor = 0;
retval = command_run_line(command_context, t_con->line);
if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
return ERROR_SERVER_REMOTE_CLOSED;
@ -453,7 +473,7 @@ int telnet_input(connection_t *connection)
telnet_clear_line(connection, t_con);
t_con->line_size = strlen(t_con->history[last_history]);
t_con->line_cursor = t_con->line_size;
memcpy(t_con->line, t_con->history[last_history], t_con->line_size + 1);
memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
telnet_write(connection, t_con->line, t_con->line_size);
t_con->current_history = last_history;
}
@ -467,7 +487,7 @@ int telnet_input(connection_t *connection)
telnet_clear_line(connection, t_con);
t_con->line_size = strlen(t_con->history[next_history]);
t_con->line_cursor = t_con->line_size;
memcpy(t_con->line, t_con->history[next_history], t_con->line_size + 1);
memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
telnet_write(connection, t_con->line, t_con->line_size);
t_con->current_history = next_history;
}