dvtm

dynamic virtual terminal manager - with my changes
git clone https://pi.duncano.de/git/dvtm.git
Log | Files | Refs | README | LICENSE

commit faadd4db68c928107d2254e42ce17ac2e5d3b3e4
parent 81b0904a5c447a799bf831347669c0d65eb54b2a
Author: Marc Andre Tanner <mat@brain-dump.org>
Date:   Sat, 18 Dec 2010 19:12:56 +0100

madtty: source code cleanups

This is all cosmetic stuff, there should be no functional change.

Signed-off-by: Marc Andre Tanner <mat@brain-dump.org>

Diffstat:
madtty.c | 138++++++++++++++++++++++++++++++++++++-------------------------------------------
1 file changed, 62 insertions(+), 76 deletions(-)

diff --git a/madtty.c b/madtty.c @@ -54,12 +54,10 @@ #define IS_CONTROL(ch) !((ch) & 0xffffff60UL) static int has_default, is_utf8, use_palette; - static const unsigned palette_start = 1; -static const unsigned palette_end = 256; -static unsigned palette_cur; -static short *color2palette; -static unsigned color_hash(short f, short b) { return ((f+1) * (COLORS+1)) + b+1 ; } +static const unsigned palette_end = 256; +static unsigned palette_cur; +static short *color2palette; enum { C0_NUL = 0x00, @@ -189,8 +187,7 @@ static char const * const keytable[KEY_MAX+1] = { [KEY_F(20)] = "\e[34~", }; -__attribute__((const)) -static uint16_t build_attrs(unsigned curattrs) +__attribute__((const)) static uint16_t build_attrs(unsigned curattrs) { return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff)) >> NCURSES_ATTR_SHIFT; @@ -224,27 +221,21 @@ static void t_row_roll(t_row_t *start, t_row_t *end, int count) memcpy(buf, start, count * sizeof(t_row_t)); memmove(start, start + count, (n - count) * sizeof(t_row_t)); memcpy(end - count, buf, count * sizeof(t_row_t)); - for (t_row_t *row = start; row < end; row++) { + for (t_row_t *row = start; row < end; row++) row->dirty = true; - } } } static void clamp_cursor_to_bounds(madtty_t *t) { - if (t->curs_row < t->lines) { + if (t->curs_row < t->lines) t->curs_row = t->lines; - } - if (t->curs_row >= t->lines + t->rows) { + if (t->curs_row >= t->lines + t->rows) t->curs_row = t->lines + t->rows - 1; - } - - if (t->curs_col < 0) { + if (t->curs_col < 0) t->curs_col = 0; - } - if (t->curs_col >= t->cols) { + if (t->curs_col >= t->cols) t->curs_col = t->cols - 1; - } } static void save_curs(madtty_t *t) @@ -280,7 +271,7 @@ static void fill_scroll_buf(madtty_t *t, int s) int ssz = t->scroll_bot - t->scroll_top; if (s > ssz) { fill_scroll_buf(t, ssz); - fill_scroll_buf(t, s-ssz); + fill_scroll_buf(t, s - ssz); return; } if (s < -ssz) { @@ -306,7 +297,7 @@ static void fill_scroll_buf(madtty_t *t, int s) } t_row_roll(t->scroll_top, t->scroll_bot, s); if (s < 0 && t->scroll_buf_sz) { - for (int i = (-s)-1; i >= 0; i--) { + for (int i = (-s) - 1; i >= 0; i--) { t->scroll_buf_ptr--; if (t->scroll_buf_ptr == -1) t->scroll_buf_ptr = t->scroll_buf_sz - 1; @@ -357,8 +348,6 @@ static bool is_valid_csi_ender(int c) /* interprets a 'set attribute' (SGR) CSI escape sequence */ static void interpret_csi_SGR(madtty_t *t, int param[], int pcount) { - int i; - if (pcount == 0) { /* special case: reset attributes */ t->curattrs = A_NORMAL; @@ -366,7 +355,7 @@ static void interpret_csi_SGR(madtty_t *t, int param[], int pcount) return; } - for (i = 0; i < pcount; i++) { + for (int i = 0; i < pcount; i++) { switch (param[i]) { #define CASE(x, op) case x: op; break CASE(0, t->curattrs = A_NORMAL; t->curfg = t->curbg = -1); @@ -429,21 +418,18 @@ static void interpret_csi_ED(madtty_t *t, int param[], int pcount) if (pcount && param[0] == 2) { start = t->lines; end = t->lines + t->rows; - } else - if (pcount && param[0] == 1) { + } else if (pcount && param[0] == 1) { start = t->lines; end = t->curs_row; t_row_set(t->curs_row, 0, t->curs_col + 1, t); } else { - t_row_set(t->curs_row, t->curs_col, - t->cols - t->curs_col, t); + t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t); start = t->curs_row + 1; end = t->lines + t->rows; } - for (row = start; row < end; row++) { + for (row = start; row < end; row++) t_row_set(row, 0, t->cols, t); - } restore_attrs(t); } @@ -468,8 +454,7 @@ static void interpret_csi_CUP(madtty_t *t, 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(madtty_t *t, char verb, int param[], int pcount) +static void interpret_csi_C(madtty_t *t, char verb, int param[], int pcount) { int n = (pcount && param[0] > 0) ? param[0] : 1; @@ -508,13 +493,11 @@ static void interpret_csi_ICH(madtty_t *t, int param[], int pcount) { t_row_t *row = t->curs_row; int n = (pcount && param[0] > 0) ? param[0] : 1; - int i; - if (t->curs_col + n > t->cols) { + if (t->curs_col + n > t->cols) n = t->cols - t->curs_col; - } - for (i = t->cols - 1; i >= t->curs_col + n; i--) { + for (int i = t->cols - 1; i >= t->curs_col + n; i--) { row->text[i] = row->text[i - n]; row->attr[i] = row->attr[i - n]; row->bg [i] = row->bg [i - n]; @@ -529,13 +512,11 @@ static void interpret_csi_DCH(madtty_t *t, int param[], int pcount) { t_row_t *row = t->curs_row; int n = (pcount && param[0] > 0) ? param[0] : 1; - int i; - if (t->curs_col + n > t->cols) { + if (t->curs_col + n > t->cols) n = t->cols - t->curs_col; - } - for (i = t->curs_col; i < t->cols - n; i++) { + for (int i = t->curs_col; i < t->cols - n; i++) { row->text[i] = row->text[i + n]; row->attr[i] = row->attr[i + n]; row->bg [i] = row->bg [i + n]; @@ -558,14 +539,12 @@ static void interpret_csi_IL(madtty_t *t, int param[], int pcount) int n = (pcount && param[0] > 0) ? param[0] : 1; if (t->curs_row + n >= t->scroll_bot) { - for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) { + for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) t_row_set(row, 0, t->cols, t); - } } else { t_row_roll(t->curs_row, t->scroll_bot, -n); - for (t_row_t *row = t->curs_row; row < t->curs_row + n; row++) { + for (t_row_t *row = t->curs_row; row < t->curs_row + n; row++) t_row_set(row, 0, t->cols, t); - } } } @@ -575,14 +554,12 @@ static void interpret_csi_DL(madtty_t *t, int param[], int pcount) int n = (pcount && param[0] > 0) ? param[0] : 1; if (t->curs_row + n >= t->scroll_bot) { - for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) { + for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) t_row_set(row, 0, t->cols, t); - } } else { t_row_roll(t->curs_row, t->scroll_bot, n); - for (t_row_t *row = t->scroll_bot - n; row < t->scroll_bot; row++) { + for (t_row_t *row = t->scroll_bot - n; row < t->scroll_bot; row++) t_row_set(row, 0, t->cols, t); - } } } @@ -591,9 +568,9 @@ static void interpret_csi_ECH(madtty_t *t, int param[], int pcount) { int n = (pcount && param[0] > 0) ? param[0] : 1; - if (t->curs_col + n < t->cols) { + if (t->curs_col + n < t->cols) n = t->cols - t->curs_col; - } + t_row_set(t->curs_row, t->curs_col, n, t); } @@ -607,9 +584,6 @@ static void interpret_csi_DECSTBM(madtty_t *t, int param[], int pcount) t->scroll_top = t->lines; t->scroll_bot = t->lines + t->rows; break; - default: - return; /* malformed */ - case 2: new_top = param[0] - 1; new_bot = param[1]; @@ -630,6 +604,8 @@ static void interpret_csi_DECSTBM(madtty_t *t, int param[], int pcount) t->scroll_bot = t->lines + new_bot; } break; + default: + return; /* malformed */ } } @@ -640,7 +616,7 @@ static void es_interpret_csi(madtty_t *t) const char *p = t->ebuf + 1; char verb = t->ebuf[t->elen - 1]; - p += t->ebuf[1] == '?'; /* CSI private mode */ + p += (t->ebuf[1] == '?'); /* CSI private mode */ /* parse numeric parameters */ while (isdigit((unsigned char)*p) || *p == ';') { @@ -649,7 +625,8 @@ static void es_interpret_csi(madtty_t *t) return; /* too long! */ csiparam[param_count++] = 0; } else { - if (param_count == 0) csiparam[param_count++] = 0; + if (param_count == 0) + csiparam[param_count++] = 0; csiparam[param_count - 1] *= 10; csiparam[param_count - 1] += *p - '0'; } @@ -750,11 +727,13 @@ static void interpret_char_set(madtty_t *t) static void try_interpret_escape_seq(madtty_t *t) { - char lastchar = t->ebuf[t->elen-1]; - if(!*t->ebuf) + char lastchar = t->ebuf[t->elen - 1]; + + if (!*t->ebuf) return; - if(t->handler){ - switch((*(t->handler))(t, t->ebuf)){ + + if (t->handler) { + switch ((*(t->handler))(t, t->ebuf)) { case MADTTY_HANDLER_OK: goto cancel; case MADTTY_HANDLER_NOTYET: @@ -763,6 +742,7 @@ static void try_interpret_escape_seq(madtty_t *t) return; } } + switch (*t->ebuf) { case 'M': interpret_csi_SR(t); @@ -784,9 +764,6 @@ static void try_interpret_escape_seq(madtty_t *t) goto cancel; break; - default: - goto cancel; - case '[': if (is_valid_csi_ender(lastchar)) { es_interpret_csi(t); @@ -806,6 +783,9 @@ static void try_interpret_escape_seq(madtty_t *t) restore_curs(t); cancel_escape_sequence(t); return; + + default: + goto cancel; } if (t->elen + 1 >= (int)sizeof(t->ebuf)) { @@ -834,7 +814,7 @@ static void madtty_process_nonprinting(madtty_t *t, wchar_t wc) case C0_BEL: /* maybe a visual bell would be nice? */ - if(t->bell) + if (t->bell) beep(); break; @@ -874,11 +854,12 @@ static void is_utf8_locale(void) is_utf8 = !strcmp(cset, "UTF-8"); } -// vt100 special graphics and line drawing -// 5f-7e standard vt100 -// 40-5e rxvt extension for extra curses acs chars -static uint16_t const vt100_utf8[62] = { // 41 .. 7e - 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman! +/* vt100 special graphics and line drawing + * 5f-7e standard vt100 + * 40-5e rxvt extension for extra curses acs chars + */ +static uint16_t const vt100_utf8[62] = { + 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f 0, 0, 0, 0, 0, 0, 0, 0, // 50-57 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f @@ -1272,8 +1253,6 @@ void madtty_togglebell(madtty_t *t) t->bell = !t->bell; } -/******************************************************/ - pid_t madtty_forkpty(madtty_t *t, const char *p, const char *argv[], const char *env[], int *pty) { struct winsize ws; @@ -1336,7 +1315,7 @@ void madtty_keypress(madtty_t *t, int keycode) madtty_noscroll(t); if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) { - switch(keycode) { + switch (keycode) { case KEY_UP: case KEY_DOWN: case KEY_RIGHT: @@ -1360,14 +1339,19 @@ void madtty_keypress_sequence(madtty_t *t, const char *seq) * do it. the sequence we look for is \eO + a character * representing the number. */ - if(len == 3 && seq[0] == '\e' && seq[1] == 'O') { + if (len == 3 && seq[0] == '\e' && seq[1] == 'O') { key = seq[2] - 64; - if(key >= '0' && key <= '9') + if (key >= '0' && key <= '9') madtty_keypress(t, key); } else term_write(t, seq, len); } +static unsigned color_hash(short f, short b) +{ + return ((f+1) * (COLORS+1)) + b + 1; +} + void madtty_color_set(WINDOW *win, short fg, short bg) { if (use_palette) { @@ -1379,7 +1363,7 @@ void madtty_color_set(WINDOW *win, short fg, short bg) short f, g; color2palette[c] = palette_cur; pair_content(palette_cur, &f, &g); - color2palette[color_hash(f,g)] = 0; + color2palette[color_hash(f, g)] = 0; init_pair(palette_cur, fg, bg); palette_cur++; if (palette_cur >= palette_end) { @@ -1393,8 +1377,10 @@ void madtty_color_set(WINDOW *win, short fg, short bg) if (has_default) { wcolor_set(win, (fg+1)*16 + bg+1, 0); } else { - if (fg==-1) fg = COLOR_WHITE; - if (bg==-1) bg = COLOR_BLACK; + if (fg == -1) + fg = COLOR_WHITE; + if (bg == -1) + bg = COLOR_BLACK; wcolor_set(win, (7-fg)*8 + bg, 0); } } @@ -1413,7 +1399,7 @@ void madtty_init_colors(void) int bg = 0, fg = 0; for (int i = palette_start; i < palette_end; i++) { init_pair(i, bg, fg); - color2palette[color_hash(bg,fg)] = i; + color2palette[color_hash(bg, fg)] = i; if (++fg == COLORS) { fg = 0; bg++;