aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/termptyesc.c
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2015-06-02 22:48:50 +0200
committerBoris Faure <billiob@gmail.com>2015-06-03 23:02:31 +0200
commit681bb029c232fd162a7be9acea1d60074808f2f8 (patch)
tree7814fa6e19a9325d63b25b21ce1f5fb7d7338055 /src/bin/termptyesc.c
parentRevert "ensure cursor coordinates are always valid" (diff)
downloadterminology-681bb029c232fd162a7be9acea1d60074808f2f8.tar.gz
ensure cursor coordinates are always valid, 2nd version
Diffstat (limited to 'src/bin/termptyesc.c')
-rw-r--r--src/bin/termptyesc.c42
1 files changed, 22 insertions, 20 deletions
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 14dbbb7..94831c9 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -66,7 +66,7 @@ _handle_cursor_control(Termpty *ty, const Eina_Unicode *cc)
ty->termstate.had_cr = 0;
ty->termstate.wrapnext = 0;
ty->cursor_state.cx--;
- if (ty->cursor_state.cx < 0) ty->cursor_state.cx = 0;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
return;
case 0x09: // HT '\t' (horizontal tab)
DBG("->HT");
@@ -75,8 +75,7 @@ _handle_cursor_control(Termpty *ty, const Eina_Unicode *cc)
ty->termstate.wrapnext = 0;
ty->cursor_state.cx += 8;
ty->cursor_state.cx = (ty->cursor_state.cx / 8) * 8;
- if (ty->cursor_state.cx >= ty->w)
- ty->cursor_state.cx = ty->w - 1;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
return;
case 0x0a: // LF '\n' (new line)
case 0x0b: // VT '\v' (vertical tab)
@@ -682,6 +681,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
termpty_text_append(ty, blank, 1);
ty->termstate.insert = pi;
ty->cursor_state.cx = cx;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
}
break;
case 'A': // cursor up N
@@ -691,6 +691,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("cursor up %d", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cy = MAX(0, ty->cursor_state.cy - arg);
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
break;
case 'B': // cursor down N
arg = _csi_arg_get(&b);
@@ -698,6 +699,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("cursor down %d", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cy = MIN(ty->h - 1, ty->cursor_state.cy + arg);
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
break;
case 'D': // cursor left N
arg = _csi_arg_get(&b);
@@ -705,10 +707,8 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("cursor left %d", arg);
ty->termstate.wrapnext = 0;
for (i = 0; i < arg; i++)
- {
- ty->cursor_state.cx--;
- if (ty->cursor_state.cx < 0) ty->cursor_state.cx = 0;
- }
+ ty->cursor_state.cx--;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
break;
case 'C': // cursor right N
case 'a': // cursor right N
@@ -717,10 +717,8 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("cursor right %d", arg);
ty->termstate.wrapnext = 0;
for (i = 0; i < arg; i++)
- {
- ty->cursor_state.cx++;
- if (ty->cursor_state.cx >= ty->w) ty->cursor_state.cx = ty->w - 1;
- }
+ ty->cursor_state.cx++;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
break;
case 'H': // cursor pos set
case 'f': // cursor pos set
@@ -740,6 +738,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
if (b)
{
ty->cursor_state.cy = arg;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
arg = _csi_arg_get(&b);
if (arg < 1) arg = 1;
arg--;
@@ -747,9 +746,14 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
else arg = 0;
if (arg >= ty->w) arg = ty->w - 1;
- if (b) ty->cursor_state.cx = arg;
+ if (b)
+ {
+ ty->cursor_state.cx = arg;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
+ }
}
ty->cursor_state.cy += ty->termstate.margin_top;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
break;
case 'G': // to column N
arg = _csi_arg_get(&b);
@@ -757,8 +761,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("to column %d", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cx = arg - 1;
- if (ty->cursor_state.cx < 0) ty->cursor_state.cx = 0;
- else if (ty->cursor_state.cx >= ty->w) ty->cursor_state.cx = ty->w - 1;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
break;
case 'd': // to row N
arg = _csi_arg_get(&b);
@@ -766,8 +769,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("to row %d", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cy = arg - 1;
- if (ty->cursor_state.cy < 0) ty->cursor_state.cy = 0;
- else if (ty->cursor_state.cy >= ty->h) ty->cursor_state.cy = ty->h - 1;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
break;
case 'E': // down relative N rows, and to col 0
arg = _csi_arg_get(&b);
@@ -775,8 +777,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("down relative %d rows, and to col 0", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cy += arg;
- if (ty->cursor_state.cy < 0) ty->cursor_state.cy = 0;
- else if (ty->cursor_state.cy >= ty->h) ty->cursor_state.cy = ty->h - 1;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
ty->cursor_state.cx = 0;
break;
case 'F': // up relative N rows, and to col 0
@@ -785,8 +786,7 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
DBG("up relative %d rows, and to col 0", arg);
ty->termstate.wrapnext = 0;
ty->cursor_state.cy -= arg;
- if (ty->cursor_state.cy < 0) ty->cursor_state.cy = 0;
- else if (ty->cursor_state.cy >= ty->h) ty->cursor_state.cy = ty->h - 1;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
ty->cursor_state.cx = 0;
break;
case 'X': // erase N chars
@@ -1029,7 +1029,9 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, Eina_Unicode *ce)
if (!arg)
{
ty->cursor_state.cx = cx;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cx, 0, ty->w);
ty->cursor_state.cy = cy;
+ TERMPTY_RESTRICT_FIELD(ty->cursor_state.cy, 0, ty->h);
}
}
break;