GUACAMOLE-148: Move cursor bounds checking to common location (where necessary).

This commit is contained in:
Michael Jumper 2017-01-28 22:48:45 -08:00
parent 073fbe684d
commit 6c1eeb96b0

View File

@ -89,6 +89,39 @@ static void guac_terminal_reverse_linefeed(guac_terminal* term) {
} }
/**
* Sets the position of the cursor without exceeding terminal bounds. Values
* which are out of bounds will be shifted to the nearest legal boundary.
*
* @param term
* The guac_terminal whose cursor position is being set.
*
* @param row
* The desired new row position.
*
* @param col
* The desired new column position.
*/
static void guac_terminal_move_cursor(guac_terminal* term, int row, int col) {
/* Ensure cursor row is within terminal bounds */
if (row >= term->term_height)
row = term->term_height - 1;
else if (row < 0)
row = 0;
/* Ensure cursor column is within terminal bounds */
if (col >= term->term_width)
col = term->term_width - 1;
else if (col < 0)
col = 0;
/* Update cursor position */
term->cursor_row = row;
term->cursor_col = col;
}
int guac_terminal_echo(guac_terminal* term, unsigned char c) { int guac_terminal_echo(guac_terminal* term, unsigned char c) {
int width; int width;
@ -163,13 +196,14 @@ int guac_terminal_echo(guac_terminal* term, unsigned char c) {
/* Backspace */ /* Backspace */
case 0x08: case 0x08:
if (term->cursor_col >= 1) guac_terminal_move_cursor(term, term->cursor_row,
term->cursor_col--; term->cursor_col - 1);
break; break;
/* Tab */ /* Tab */
case 0x09: case 0x09:
term->cursor_col = guac_terminal_next_tab(term, term->cursor_col); guac_terminal_move_cursor(term, term->cursor_row,
guac_terminal_next_tab(term, term->cursor_col));
break; break;
/* Line feed / VT / FF */ /* Line feed / VT / FF */
@ -186,7 +220,7 @@ int guac_terminal_echo(guac_terminal* term, unsigned char c) {
/* Carriage return */ /* Carriage return */
case '\r': case '\r':
term->cursor_col = 0; guac_terminal_move_cursor(term, term->cursor_row, 0);
break; break;
/* SO (activates character set G1) */ /* SO (activates character set G1) */
@ -288,14 +322,9 @@ int guac_terminal_escape(guac_terminal* term, unsigned char c) {
/* Restore Cursor (DECRC) */ /* Restore Cursor (DECRC) */
case '8': case '8':
guac_terminal_move_cursor(term,
term->cursor_row = term->saved_cursor_row; term->saved_cursor_row,
if (term->cursor_row >= term->term_height) term->saved_cursor_col);
term->cursor_row = term->term_height - 1;
term->cursor_col = term->saved_cursor_col;
if (term->cursor_col >= term->term_width)
term->cursor_col = term->term_width - 1;
term->char_handler = guac_terminal_echo; term->char_handler = guac_terminal_echo;
break; break;
@ -308,7 +337,7 @@ int guac_terminal_escape(guac_terminal* term, unsigned char c) {
/* Next Line (NEL) */ /* Next Line (NEL) */
case 'E': case 'E':
term->cursor_col = 0; guac_terminal_move_cursor(term, term->cursor_row, 0);
guac_terminal_linefeed(term); guac_terminal_linefeed(term);
term->char_handler = guac_terminal_echo; term->char_handler = guac_terminal_echo;
break; break;
@ -474,9 +503,9 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor */
term->cursor_row -= amount; guac_terminal_move_cursor(term,
if (term->cursor_row < 0) term->cursor_row - amount,
term->cursor_row = 0; term->cursor_col);
break; break;
@ -489,9 +518,9 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor */
term->cursor_row += amount; guac_terminal_move_cursor(term,
if (term->cursor_row >= term->term_height) term->cursor_row + amount,
term->cursor_row = term->term_height - 1; term->cursor_col);
break; break;
@ -504,9 +533,9 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor */
term->cursor_col += amount; guac_terminal_move_cursor(term,
if (term->cursor_col >= term->term_width) term->cursor_row,
term->cursor_col = term->term_width - 1; term->cursor_col + amount);
break; break;
@ -518,9 +547,9 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor */
term->cursor_col -= amount; guac_terminal_move_cursor(term,
if (term->cursor_col < 0) term->cursor_row,
term->cursor_col = 0; term->cursor_col - amount);
break; break;
@ -531,13 +560,10 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
amount = argv[0]; amount = argv[0];
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor down, reset to column 1 */
term->cursor_row += amount; guac_terminal_move_cursor(term,
if (term->cursor_row >= term->term_height) term->cursor_row + amount,
term->cursor_row = term->term_height - 1; 0);
/* Reset to column 1 */
term->cursor_col = 0;
break; break;
@ -548,13 +574,10 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
amount = argv[0]; amount = argv[0];
if (amount == 0) amount = 1; if (amount == 0) amount = 1;
/* Move cursor */ /* Move cursor up , reset to column 1 */
term->cursor_row -= amount; guac_terminal_move_cursor(term,
if (term->cursor_row < 0) term->cursor_row - amount,
term->cursor_row = 0; 0);
/* Reset to column 1 */
term->cursor_col = 0;
break; break;
@ -562,7 +585,7 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
case '`': case '`':
case 'G': case 'G':
col = argv[0]; if (col != 0) col--; col = argv[0]; if (col != 0) col--;
term->cursor_col = col; guac_terminal_move_cursor(term, term->cursor_row, col);
break; break;
/* H: Move cursor */ /* H: Move cursor */
@ -572,8 +595,7 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
row = argv[0]; if (row != 0) row--; row = argv[0]; if (row != 0) row--;
col = argv[1]; if (col != 0) col--; col = argv[1]; if (col != 0) col--;
term->cursor_row = row; guac_terminal_move_cursor(term, row, col);
term->cursor_col = col;
break; break;
/* J: Erase display */ /* J: Erase display */
@ -684,7 +706,7 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
/* d: Move cursor, current col */ /* d: Move cursor, current col */
case 'd': case 'd':
row = argv[0]; if (row != 0) row--; row = argv[0]; if (row != 0) row--;
term->cursor_row = row; guac_terminal_move_cursor(term, row, term->cursor_col);
break; break;
/* g: Clear tab */ /* g: Clear tab */
@ -829,15 +851,9 @@ int guac_terminal_csi(guac_terminal* term, unsigned char c) {
/* Restore Cursor */ /* Restore Cursor */
case 'u': case 'u':
guac_terminal_move_cursor(term,
term->cursor_row = term->saved_cursor_row; term->saved_cursor_row,
if (term->cursor_row >= term->term_height) term->saved_cursor_col);
term->cursor_row = term->term_height - 1;
term->cursor_col = term->saved_cursor_col;
if (term->cursor_col >= term->term_width)
term->cursor_col = term->term_width - 1;
break; break;
/* Warn of unhandled codes */ /* Warn of unhandled codes */