From 411a46eff224c2cade990436d52956aa50dbd77b Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 11 Aug 2019 01:40:12 -0400 Subject: Use low numbers for states --- term.c | 110 +++++++++++++++++++++++++++++++++-------------------------------- term.h | 2 +- 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/term.c b/term.c index 8c2ee9f..7bc6282 100644 --- a/term.c +++ b/term.c @@ -36,10 +36,10 @@ static void unhandled(const char *format, ...) { va_end(ap); } -static struct Cell *cell(struct Term *t, uint y, uint x) { - assert(y < t->rows); - assert(x < t->cols); - return &t->cells[y * t->cols + x]; +static struct Cell *cell(struct Term *term, uint y, uint x) { + assert(y < term->rows); + assert(x < term->cols); + return &term->cells[y * term->cols + x]; } static void erase(struct Style style, struct Cell *a, struct Cell *b) { @@ -53,49 +53,51 @@ static void move(struct Cell *dst, struct Cell *src, uint len) { memmove(dst, src, sizeof(*dst) * len); } -static void scrollUp(struct Term *t, uint top, uint n) { +static void scrollUp(struct Term *term, uint top, uint n) { move( - cell(t, top, 0), - cell(t, top + n, 0), - t->cols * (1 + t->scroll.bot - top - n) + cell(term, top, 0), + cell(term, top + n, 0), + term->cols * (1 + term->scroll.bot - top - n) ); erase( - t->style, - cell(t, 1 + t->scroll.bot - n, 0), - cell(t, t->scroll.bot, t->cols - 1) + term->style, + cell(term, 1 + term->scroll.bot - n, 0), + cell(term, term->scroll.bot, term->cols - 1) ); } -static void scrollDown(struct Term *t, uint top, uint n) { +static void scrollDown(struct Term *term, uint top, uint n) { move( - cell(t, top + n, 0), - cell(t, top, 0), - t->cols * (1 + t->scroll.bot - top - n) + cell(term, top + n, 0), + cell(term, top, 0), + term->cols * (1 + term->scroll.bot - top - n) ); erase( - t->style, - cell(t, top, 0), - cell(t, top + n - 1, t->cols - 1) + term->style, + cell(term, top, 0), + cell(term, top + n - 1, term->cols - 1) ); } -typedef void Action(struct Term *, wchar_t ch); +typedef void Action(struct Term *, wchar_t); #define ACTION(name) \ - static void name(struct Term *t, wchar_t _ch __attribute__((__unused__))) + static void name(struct Term *t, wchar_t _ch __attribute__((unused))) enum { - G0 = '(', - CSI = '[', - ST = '\\', - OSC = ']', + Def, + Esc, + G0, + CSI, + OSC, + OSCEsc, }; -ACTION(nop) { (void)t; } -ACTION(esc) { t->state = ESC; } -ACTION(g0) { t->state = G0; } -ACTION(osc) { t->state = OSC; } -ACTION(st) { t->state = ST; } +ACTION(nop) { (void)t; } +ACTION(esc) { t->state = Esc; } +ACTION(g0) { t->state = G0; } +ACTION(osc) { t->state = OSC; } +ACTION(oscEsc) { t->state = OSCEsc; } ACTION(csi) { t->state = CSI; memset(&t->param, 0, sizeof(t->param)); @@ -357,23 +359,23 @@ static void add(struct Term *t, wchar_t ch) { } static Action *Actions[][128] = { - [NUL][0] = add, - [NUL][BEL] = nop, - [NUL][BS] = bs, - [NUL][HT] = ht, - [NUL][NL] = nl, - [NUL][CR] = cr, - [NUL][ESC] = esc, - - [ESC][0] = escUnhandled, - [ESC]['('] = g0, - [ESC]['7'] = decsc, - [ESC]['8'] = decrc, - [ESC]['='] = nop, - [ESC]['>'] = nop, - [ESC]['M'] = ri, - [ESC]['['] = csi, - [ESC][']'] = osc, + [Def][0] = add, + [Def][BEL] = nop, + [Def][BS] = bs, + [Def][HT] = ht, + [Def][NL] = nl, + [Def][CR] = cr, + [Def][ESC] = esc, + + [Esc][0] = escUnhandled, + [Esc]['('] = g0, + [Esc]['7'] = decsc, + [Esc]['8'] = decrc, + [Esc]['='] = nop, + [Esc]['>'] = nop, + [Esc]['M'] = ri, + [Esc]['['] = csi, + [Esc][']'] = osc, [G0][0] = nop, @@ -404,18 +406,19 @@ static Action *Actions[][128] = { [OSC][0] = osc, [OSC][BEL] = nop, - [OSC][ESC] = st, - [ST][0] = osc, - [ST]['\\'] = nop, + [OSC][ESC] = oscEsc, + + [OSCEsc][0] = osc, + [OSCEsc]['\\'] = nop, }; void termUpdate(struct Term *term, wchar_t ch) { - assert((uint)term->state < sizeof(Actions) / sizeof(Actions[0])); + assert(term->state < sizeof(Actions) / sizeof(Actions[0])); Action *action = NULL; - if (ch < 128) action = Actions[(uint)term->state][ch]; - if (!action) action = Actions[(uint)term->state][0]; + if (ch < 128) action = Actions[term->state][ch]; + if (!action) action = Actions[term->state][0]; assert(action); - term->state = NUL; + term->state = Def; action(term, ch); } @@ -428,7 +431,6 @@ struct Term *termAlloc(uint rows, uint cols) { term->rows = rows; term->cols = cols; term->mode = Wrap | Cursor; - term->scroll.top = 0; term->scroll.bot = rows - 1; term->style = Default; erase(Default, cell(term, 0, 0), cell(term, rows - 1, cols - 1)); diff --git a/term.h b/term.h index 03d9ac5..2bc4dcb 100644 --- a/term.h +++ b/term.h @@ -58,7 +58,7 @@ enum { ParamCap = 16 }; struct Term { uint rows, cols; - char state; + uint state; struct { bool q; uint s[ParamCap]; -- cgit 1.4.1