From f46c74119660de56f0bdc40f75d74acd3abf86fb Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sat, 11 Nov 2006 01:56:36 +0100 Subject: [PATCH] coding rules Signed-off-by: Pierre Habouzit --- madtty/inject.c | 261 +++++++++++++++++++++++++----------------------- 1 file changed, 135 insertions(+), 126 deletions(-) diff --git a/madtty/inject.c b/madtty/inject.c index b60b6c5..5d1f7d7 100644 --- a/madtty/inject.c +++ b/madtty/inject.c @@ -21,6 +21,7 @@ #include #include +#include #include "madtty.h" #include "roteprivate.h" @@ -32,7 +33,7 @@ static inline void clamp_cursor_to_bounds(RoteTerm *rt) if (rt->crow < 0) rt->curpos_dirty = true, rt->crow = 0; if (rt->ccol < 0) rt->curpos_dirty = true, rt->ccol = 0; - if (rt->crow >= rt->rows) + if (rt->crow >= rt->rows) rt->curpos_dirty = true, rt->crow = rt->rows - 1; if (rt->ccol >= rt->cols) @@ -42,11 +43,13 @@ static inline void clamp_cursor_to_bounds(RoteTerm *rt) static void cursor_line_down(RoteTerm *rt) { int i; + rt->crow++; rt->curpos_dirty = true; - if (rt->crow <= rt->pd->scrollbottom) return; + if (rt->crow <= rt->pd->scrollbottom) + return; - /* must scroll the scrolling region up by 1 line, and put cursor on + /* must scroll the scrolling region up by 1 line, and put cursor on * last line of it */ rt->crow = rt->pd->scrollbottom; @@ -62,17 +65,18 @@ static void cursor_line_down(RoteTerm *rt) rt->cells[rt->pd->scrollbottom][i].ch = 0x20; rt->cells[rt->pd->scrollbottom][i].attr = 0x70; } - } static void cursor_line_up(RoteTerm *rt) { int i; + rt->crow--; rt->curpos_dirty = true; - if (rt->crow >= rt->pd->scrolltop) return; + if (rt->crow >= rt->pd->scrolltop) + return; - /* must scroll the scrolling region up by 1 line, and put cursor on + /* must scroll the scrolling region up by 1 line, and put cursor on * first line of it */ rt->crow = rt->pd->scrolltop; @@ -88,7 +92,6 @@ static void cursor_line_up(RoteTerm *rt) rt->cells[rt->pd->scrolltop][i].ch = 0x20; rt->cells[rt->pd->scrolltop][i].attr = 0x70; } - } static inline void put_normal_char(RoteTerm *rt, char c) @@ -118,7 +121,7 @@ static inline void put_graphmode_char(RoteTerm *rt, char c) char nc; /* do some very pitiful translation to regular ascii chars */ switch (c) { - case 'j': case 'k': case 'l': case 'm': case 'n': case 't': + case 'j': case 'k': case 'l': case 'm': case 'n': case 't': case 'u': case 'v': case 'w': nc = '+'; break; case 'x': @@ -144,54 +147,61 @@ static inline void cancel_escape_sequence(RoteTerm *rt) rt->pd->esbuf[0] = '\0'; } -static void handle_control_char(RoteTerm *rt, char c) +static void handle_control_char(RoteTerm *rt, int c) { switch (c) { - case '\r': rt->ccol = 0; break; /* carriage return */ + case '\r': /* carriage return */ + rt->ccol = 0; + break; + case '\n': /* line feed */ - rt->ccol = 0; cursor_line_down(rt); - rt->curpos_dirty = true; - break; + rt->ccol = 0; cursor_line_down(rt); + rt->curpos_dirty = true; + break; + case '\b': /* backspace */ - if (rt->ccol > 0) rt->ccol--; - rt->curpos_dirty = true; - break; + if (rt->ccol > 0) rt->ccol--; + rt->curpos_dirty = true; + break; + case '\t': /* tab */ - rt->ccol += 8 - (rt->ccol % 8); - clamp_cursor_to_bounds(rt); - break; - case '\x1B': /* begin escape sequence (aborting previous one if any) */ - new_escape_sequence(rt); - break; - case '\x0E': /* enter graphical character mode */ - rt->pd->graphmode = true; - break; - case '\x0F': /* exit graphical character mode */ - rt->pd->graphmode = false; - break; - case '\x9B': /* CSI character. Equivalent to ESC [ */ - new_escape_sequence(rt); - rt->pd->esbuf[rt->pd->esbuf_len++] = '['; - break; - case '\x18': case '\x1A': /* these interrupt escape sequences */ - cancel_escape_sequence(rt); - break; + rt->ccol += 8 - (rt->ccol % 8); + clamp_cursor_to_bounds(rt); + break; + + case '\x1b': /* begin escape sequence (aborting previous one if any) */ + new_escape_sequence(rt); + break; + + case '\x0e': /* enter graphical character mode */ + rt->pd->graphmode = true; + break; + + case '\x0f': /* exit graphical character mode */ + rt->pd->graphmode = false; + break; + + case '\x9b': /* CSI character. Equivalent to ESC [ */ + new_escape_sequence(rt); + rt->pd->esbuf[rt->pd->esbuf_len++] = '['; + break; + + case '\x18': + case '\x1a': /* these interrupt escape sequences */ + cancel_escape_sequence(rt); + break; + case '\a': /* bell */ - /* do nothing for now... maybe a visual bell would be nice? */ - break; -#ifdef DEBUG - default: - fprintf(stderr, "Unrecognized control char: %d (^%c)\n", c, c + '@'); - break; -#endif + /* do nothing for now... maybe a visual bell would be nice? */ + break; } } static inline bool is_valid_csi_ender(char c) { - return (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - c == '@' || c == '`'; + return (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c == '@' || c == '`'); } static void try_interpret_escape_seq(RoteTerm *rt) @@ -203,37 +213,24 @@ static void try_interpret_escape_seq(RoteTerm *rt) if (rt->pd->handler) { /* call custom handler */ -#ifdef DEBUG - fprintf(stderr, "Calling custom handler for ES <%s>.\n", rt->pd->esbuf); -#endif int answer = (*(rt->pd->handler))(rt, rt->pd->esbuf); if (answer == ROTE_HANDLERESULT_OK) { /* successfully handled */ -#ifdef DEBUG - fprintf(stderr, "Handler returned OK. Done with escape sequence.\n"); -#endif cancel_escape_sequence(rt); return; - } - else if (answer == ROTE_HANDLERESULT_NOTYET) { - /* handler might handle it when more characters are appended to + } else + if (answer == ROTE_HANDLERESULT_NOTYET) { + /* handler might handle it when more characters are appended to * it. So for now we don't interpret it */ -#ifdef DEBUG - fprintf(stderr, "Handler returned NOTYET. Waiting for more chars.\n"); -#endif - return; } /* If we got here then answer == ROTE_HANDLERESULT_NOWAY */ /* handler said it can't handle that escape sequence, - * but we can still try handling it ourselves, so + * but we can still try handling it ourselves, so * we proceed normally. */ -#ifdef DEBUG - fprintf(stderr, "Handler returned NOWAY. Trying our handlers.\n"); -#endif } /* interpret ESC-M as reverse line-feed */ @@ -245,10 +242,6 @@ static void try_interpret_escape_seq(RoteTerm *rt) if (firstchar != '[' && firstchar != ']') { /* unrecognized escape sequence. Let's forget about it. */ -#ifdef DEBUG - fprintf(stderr, "Unrecognized ES: <%s>\n", rt->pd->esbuf); -#endif - cancel_escape_sequence(rt); return; } @@ -257,27 +250,27 @@ static void try_interpret_escape_seq(RoteTerm *rt) /* we have a csi escape sequence: interpret it */ rote_es_interpret_csi(rt); cancel_escape_sequence(rt); - } - else if (firstchar == ']' && lastchar == '\a') { + } else if (firstchar == ']' && lastchar == '\a') { /* we have an xterm escape sequence: interpret it */ /* rote_es_interpret_xterm_es(rt); -- TODO!*/ -#ifdef DEBUG - fprintf(stderr, "Ignored XTerm ES.\n"); -#endif cancel_escape_sequence(rt); } /* if the escape sequence took up all available space and could * not yet be parsed, abort it */ - if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) cancel_escape_sequence(rt); + if (rt->pd->esbuf_len + 1 >= ESEQ_BUF_SIZE) + cancel_escape_sequence(rt); } void rote_vt_inject(RoteTerm *rt, const char *data, int len) { int i; + for (i = 0; i < len; i++, data++) { - if (*data == 0) continue; /* completely ignore NUL */ + if (*data == 0) + continue; /* completely ignore NUL */ + if (*data >= 1 && *data <= 31) { handle_control_char(rt, *data); continue; @@ -289,11 +282,12 @@ void rote_vt_inject(RoteTerm *rt, const char *data, int len) rt->pd->esbuf[++rt->pd->esbuf_len] = 0; try_interpret_escape_seq(rt); - } - else if (rt->pd->graphmode) + } else + if (rt->pd->graphmode) { put_graphmode_char(rt, *data); - else + } else { put_normal_char(rt, *data); + } } } @@ -371,16 +365,23 @@ static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) int start_row, start_col, end_row, end_col; /* decide range */ - if (pcount && param[0] == 2) - start_row = 0, start_col = 0, end_row = rt->rows - 1, - end_col = rt->cols - 1; - - else if (pcount && param[0] == 1) - start_row = 0, start_col = 0, end_row = rt->crow, - end_col = rt->ccol; - - else start_row = rt->crow, start_col = rt->ccol, - end_row = rt->rows - 1, end_col = rt->cols - 1; + if (pcount && param[0] == 2) { + start_row = 0; + start_col = 0; + end_row = rt->rows - 1; + end_col = rt->cols - 1; + } else + if (pcount && param[0] == 1) { + start_row = 0; + start_col = 0; + end_row = rt->crow; + end_col = rt->ccol; + } else { + start_row = rt->crow; + start_col = rt->ccol; + end_row = rt->rows - 1; + end_col = rt->cols - 1; + } /* clean range */ for (r = start_row; r <= end_row; r++) { @@ -388,7 +389,8 @@ static void interpret_csi_ED(RoteTerm *rt, int param[], int pcount) for (c = (r == start_row ? start_col : 0); c <= (r == end_row ? end_col : rt->cols - 1); - c++) { + c++) + { rt->cells[r][c].ch = 0x20; rt->cells[r][c].attr = rt->curattr; } @@ -402,8 +404,10 @@ static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) /* special case */ rt->crow = rt->ccol = 0; return; + } else + if (pcount < 2) { + return; /* malformed */ } - else if (pcount < 2) return; /* malformed */ rt->crow = param[0] - 1; /* convert from 1-based to 0-based */ rt->ccol = param[1] - 1; /* convert from 1-based to 0-based */ @@ -415,8 +419,8 @@ static void interpret_csi_CUP(RoteTerm *rt, int param[], int pcount) /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL, * CPL, CHA, HPR, VPA, VPR, HPA */ -static void interpret_csi_C(RoteTerm *rt, char verb, - int param[], int pcount) { +static void interpret_csi_C(RoteTerm *rt, char verb, int param[], int pcount) +{ int n = (pcount && param[0] > 0) ? param[0] : 1; switch (verb) { @@ -441,13 +445,13 @@ static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) int cmd = pcount ? param[0] : 0; switch (cmd) { - case 1: erase_start = 0; erase_end = rt->ccol; break; - case 2: erase_start = 0; erase_end = rt->cols - 1; break; - default: erase_start = rt->ccol; erase_end = rt->cols - 1; break; + case 1: erase_start = 0; erase_end = rt->ccol; break; + case 2: erase_start = 0; erase_end = rt->cols - 1; break; + default: erase_start = rt->ccol; erase_end = rt->cols - 1; break; } for (i = erase_start; i <= erase_end; i++) { - rt->cells[rt->crow][i].ch = 0x20; + rt->cells[rt->crow][i].ch = 0x20; rt->cells[rt->crow][i].attr = rt->curattr; } @@ -457,10 +461,13 @@ static void interpret_csi_EL(RoteTerm *rt, int param[], int pcount) /* Interpret the 'insert blanks' sequence (ICH) */ static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) { - int n = (pcount && param[0] > 0) ? param[0] : 1; + int n = (pcount && param[0] > 0) ? param[0] : 1; int i; - for (i = rt->cols - 1; i >= rt->ccol + n; i--) + + for (i = rt->cols - 1; i >= rt->ccol + n; i--) { rt->cells[rt->crow][i] = rt->cells[rt->crow][i - n]; + } + for (i = rt->ccol; i < rt->ccol + n; i++) { rt->cells[rt->crow][i].ch = 0x20; rt->cells[rt->crow][i].attr = rt->curattr; @@ -472,12 +479,13 @@ static void interpret_csi_ICH(RoteTerm *rt, int param[], int pcount) /* Interpret the 'delete chars' sequence (DCH) */ static void interpret_csi_DCH(RoteTerm *rt, int param[], int pcount) { - int n = (pcount && param[0] > 0) ? param[0] : 1; + int n = (pcount && param[0] > 0) ? param[0] : 1; int i; + for (i = rt->ccol; i < rt->cols; i++) { - if (i + n < rt->cols) + if (i + n < rt->cols) { rt->cells[rt->crow][i] = rt->cells[rt->crow][i + n]; - else { + } else { rt->cells[rt->crow][i].ch = 0x20; rt->cells[rt->crow][i].attr = rt->curattr; } @@ -492,13 +500,15 @@ static void interpret_csi_IL(RoteTerm *rt, int param[], int pcount) int n = (pcount && param[0] > 0) ? param[0] : 1; int i, j; - for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) + for (i = rt->pd->scrollbottom; i >= rt->crow + n; i--) { memcpy(rt->cells[i], rt->cells[i - n], sizeof(RoteCell) * rt->cols); + } for (i = rt->crow; i < rt->crow + n && i <= rt->pd->scrollbottom; i++) { rt->line_dirty[i] = true; - for (j = 0; j < rt->cols; j++) + for (j = 0; j < rt->cols; j++) { rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr; + } } } @@ -511,11 +521,12 @@ static void interpret_csi_DL(RoteTerm *rt, int param[], int pcount) for (i = rt->crow; i <= rt->pd->scrollbottom; i++) { rt->line_dirty[i] = true; - if (i + n <= rt->pd->scrollbottom) + if (i + n <= rt->pd->scrollbottom) { memcpy(rt->cells[i], rt->cells[i+n], sizeof(RoteCell) * rt->cols); - else { - for (j = 0; j < rt->cols; j++) + } else { + for (j = 0; j < rt->cols; j++) { rt->cells[i][j].ch = 0x20, rt->cells[i][j].attr = rt->curattr; + } } } } @@ -542,21 +553,27 @@ static void interpret_csi_DECSTBM(RoteTerm *rt, int param[], int pcount) if (!pcount) { newtop = 0; newbottom = rt->rows - 1; + } else + if (pcount < 2) { + return; /* malformed */ } - else if (pcount < 2) return; /* malformed */ - else { - newtop = param[0] - 1; - newbottom = param[1] - 1; - } + + newtop = param[0] - 1; + newbottom = param[1] - 1; /* clamp to bounds */ - if (newtop < 0) newtop = 0; - if (newtop >= rt->rows) newtop = rt->rows - 1; - if (newbottom < 0) newbottom = 0; - if (newbottom >= rt->rows) newbottom = rt->rows - 1; + if (newtop < 0) + newtop = 0; + if (newtop >= rt->rows) + newtop = rt->rows - 1; + if (newbottom < 0) + newbottom = 0; + if (newbottom >= rt->rows) + newbottom = rt->rows - 1; /* check for range validity */ - if (newtop > newbottom) return; + if (newtop > newbottom) + return; rt->pd->scrolltop = newtop; rt->pd->scrollbottom = newbottom; } @@ -582,19 +599,15 @@ void rote_es_interpret_csi(RoteTerm *rt) char verb = rt->pd->esbuf[rt->pd->esbuf_len - 1]; if (!strncmp(rt->pd->esbuf, "[?", 2)) { /* private-mode CSI, ignore */ -#ifdef DEBUG - fprintf(stderr, "Ignoring private-mode CSI: <%s>\n", rt->pd->esbuf); -#endif - return; + return; } /* parse numeric parameters */ - while ((*p >= '0' && *p <= '9') || *p == ';') { + while (isdigit((unsigned char)*p) || *p == ';') { if (*p == ';') { if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */ csiparam[param_count++] = 0; - } - else { + } else { if (param_count == 0) csiparam[param_count++] = 0; csiparam[param_count - 1] *= 10; csiparam[param_count - 1] += *p - '0'; @@ -606,7 +619,7 @@ void rote_es_interpret_csi(RoteTerm *rt) /* delegate handling depending on command character (verb) */ switch (verb) { case 'h': - if (param_count == 1 && csiparam[0] == 4) /* insert mode */ + if (param_count == 1 && csiparam[0] == 4) /* insert mode */ rt->insert = true; break; case 'l': @@ -642,10 +655,6 @@ void rote_es_interpret_csi(RoteTerm *rt) case 'u': /* restore cursor location */ interpret_csi_RESTORECUR(rt, csiparam, param_count); break; default: -#ifdef DEBUG - fprintf(stderr, "Unrecogized CSI: verb=%c <%s>\n", - verb, rt->pd->esbuf); -#endif break; } } -- 2.20.1