s390/tty3270: split up tty3270_convert_line()
authorSven Schnelle <svens@linux.ibm.com>
Mon, 28 Nov 2022 18:55:57 +0000 (19:55 +0100)
committerHeiko Carstens <hca@linux.ibm.com>
Mon, 9 Jan 2023 13:33:59 +0000 (14:33 +0100)
To make the code easier to read, split up tty3270_convertline()
into several subfunctions:

- tty3270_resize_line() to realloc the line if it doesn't have enough
  space left
- tty3270_required_length() to calculate how much space we need
- tty3270_add_attributes() to add the color and highlight attributes
- tty3270_add_reset_attributes() to reset the attributes at the end of
  the line

Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Tested-by: Niklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
drivers/s390/char/con3270.c

index 61c73eb2471f5ff1366be1d7c4b6dfaa7191583c..f4e7ac4065112f14f51e5c20d6945096dfb1ff3c 100644 (file)
@@ -1101,6 +1101,18 @@ static char tty3270_graphics_translate(struct tty3270 *tp, char ch)
        }
 }
 
+static struct string *tty3270_resize_line(struct tty3270 *tp, struct string *s, int newlen)
+{
+       struct string *n = tty3270_alloc_string(tp, newlen);
+
+       list_add(&n->list, &s->list);
+       list_del_init(&s->list);
+       if (!list_empty(&s->update))
+               list_del_init(&s->update);
+       free_string(&tp->freemem, s);
+       return n;
+}
+
 /*
  * Insert character into the screen at the current position with the
  * current color and highlight. This function does NOT do cursor movement.
@@ -1128,26 +1140,19 @@ static void tty3270_put_character(struct tty3270 *tp, char ch)
        cell->attributes = tp->attributes;
 }
 
-/*
- * Convert a tty3270_line to a 3270 data fragment usable for output.
- */
-static void tty3270_convert_line(struct tty3270 *tp, int line_nr)
+static int tty3270_required_length(struct tty3270 *tp, int line_nr)
 {
+       unsigned char f_color, b_color, highlight;
        struct tty3270_line *line;
        struct tty3270_cell *cell;
-       struct string *s, *n;
-       unsigned char highlight;
-       unsigned char f_color, b_color;
-       char *cp;
-       int flen, i;
+       int i, flen = 3;                /* Prefix (TO_SBA). */
 
-       /* Determine how long the fragment will be. */
-       flen = 3;               /* Prefix (TO_SBA). */
        line = tp->screen + line_nr;
        flen += line->len;
        highlight = TAX_RESET;
        f_color = TAC_RESET;
        b_color = TAC_RESET;
+
        for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
                if (cell->attributes.highlight != highlight) {
                        flen += 3;      /* TO_SA to switch highlight. */
@@ -1173,80 +1178,107 @@ static void tty3270_convert_line(struct tty3270 *tp, int line_nr)
        if (line->len < tp->view.cols)
                flen += 4;      /* Postfix (TO_RA). */
 
-       /* Find the line in the list. */
-       i = tp->view.rows - 2 - line_nr;
-       list_for_each_entry_reverse(s, &tp->lines, list)
-               if (--i <= 0)
-                       break;
-       /*
-        * Check if the line needs to get reallocated.
-        */
-       if (s->len != flen) {
-               /* Reallocate string. */
-               n = tty3270_alloc_string(tp, flen);
-               list_add(&n->list, &s->list);
-               list_del_init(&s->list);
-               if (!list_empty(&s->update))
-                       list_del_init(&s->update);
-               free_string(&tp->freemem, s);
-               s = n;
+       return flen;
+}
+
+static char *tty3270_add_reset_attributes(struct tty3270 *tp, struct tty3270_line *line,
+                                         char *cp, struct tty3270_attribute *attr)
+{
+       if (attr->highlight != TAX_RESET) {
+               *cp++ = TO_SA;
+               *cp++ = TAT_EXTHI;
+               *cp++ = TAX_RESET;
+       }
+       if (attr->f_color != TAC_RESET) {
+               *cp++ = TO_SA;
+               *cp++ = TAT_FGCOLOR;
+               *cp++ = TAC_RESET;
+       }
+       if (attr->b_color != TAC_RESET) {
+               *cp++ = TO_SA;
+               *cp++ = TAT_BGCOLOR;
+               *cp++ = TAC_RESET;
+       }
+       if (line->len < tp->view.cols) {
+               *cp++ = TO_RA;
+               *cp++ = 0;
+               *cp++ = 0;
+               *cp++ = 0;
        }
+       return cp;
+}
+
+static char *tty3270_add_attributes(struct tty3270_line *line, struct tty3270_attribute *attr,
+                                   char *cp)
+{
+       struct tty3270_cell *cell;
+       int i;
 
-       /* Write 3270 data fragment. */
-       cp = s->string;
        *cp++ = TO_SBA;
        *cp++ = 0;
        *cp++ = 0;
 
-       highlight = TAX_RESET;
-       f_color = TAC_RESET;
-       b_color = TAC_RESET;
        for (i = 0, cell = line->cells; i < line->len; i++, cell++) {
-               if (cell->attributes.highlight != highlight) {
+               if (cell->attributes.highlight != attr->highlight) {
                        *cp++ = TO_SA;
                        *cp++ = TAT_EXTHI;
                        *cp++ = cell->attributes.highlight;
-                       highlight = cell->attributes.highlight;
+                       attr->highlight = cell->attributes.highlight;
                }
-               if (cell->attributes.f_color != f_color) {
+               if (cell->attributes.f_color != attr->f_color) {
                        *cp++ = TO_SA;
                        *cp++ = TAT_FGCOLOR;
                        *cp++ = cell->attributes.f_color;
-                       f_color = cell->attributes.f_color;
+                       attr->f_color = cell->attributes.f_color;
                }
-               if (cell->attributes.b_color != b_color) {
+               if (cell->attributes.b_color != attr->b_color) {
                        *cp++ = TO_SA;
                        *cp++ = TAT_BGCOLOR;
                        *cp++ = cell->attributes.b_color;
-                       b_color = cell->attributes.b_color;
+                       attr->b_color = cell->attributes.b_color;
                }
                if (cell->attributes.alternate_charset)
                        *cp++ = TO_GE;
-
                *cp++ = cell->character;
        }
-       if (highlight != TAX_RESET) {
-               *cp++ = TO_SA;
-               *cp++ = TAT_EXTHI;
-               *cp++ = TAX_RESET;
-       }
-       if (f_color != TAC_RESET) {
-               *cp++ = TO_SA;
-               *cp++ = TAT_FGCOLOR;
-               *cp++ = TAC_RESET;
-       }
-       if (b_color != TAC_RESET) {
-               *cp++ = TO_SA;
-               *cp++ = TAT_BGCOLOR;
-               *cp++ = TAC_RESET;
-       }
-       if (line->len < tp->view.cols) {
-               *cp++ = TO_RA;
-               *cp++ = 0;
-               *cp++ = 0;
-               *cp++ = 0;
-       }
+       return cp;
+}
 
+static void tty3270_reset_attributes(struct tty3270_attribute *attr)
+{
+       attr->highlight = TAX_RESET;
+       attr->f_color = TAC_RESET;
+       attr->b_color = TAC_RESET;
+}
+
+/*
+ * Convert a tty3270_line to a 3270 data fragment usable for output.
+ */
+static void tty3270_convert_line(struct tty3270 *tp, int line_nr)
+{
+       struct tty3270_line *line = tp->screen + line_nr;
+       struct tty3270_attribute attr;
+       struct string *s;
+       int flen, i;
+       char *cp;
+
+       /* Determine how long the fragment will be. */
+       flen = tty3270_required_length(tp, line_nr);
+       /* Find the line in the list. */
+       i = tp->view.rows - 2 - line_nr;
+       list_for_each_entry_reverse(s, &tp->lines, list)
+               if (--i <= 0)
+                       break;
+       /*
+        * Check if the line needs to get reallocated.
+        */
+       if (s->len != flen)
+               s = tty3270_resize_line(tp, s, flen);
+
+       /* Write 3270 data fragment. */
+       tty3270_reset_attributes(&attr);
+       cp = tty3270_add_attributes(line, &attr, s->string);
+       cp = tty3270_add_reset_attributes(tp, line, cp, &attr);
        if (tp->nr_up + line_nr < tp->view.rows - 2) {
                /* Line is currently visible on screen. */
                tty3270_update_string(tp, s, line_nr);
@@ -1297,13 +1329,6 @@ static void tty3270_ri(struct tty3270 *tp)
        }
 }
 
-static void tty3270_reset_attributes(struct tty3270_attribute *attr)
-{
-       attr->highlight = TAX_RESET;
-       attr->f_color = TAC_RESET;
-       attr->b_color = TAC_RESET;
-}
-
 static void tty3270_reset_cell(struct tty3270 *tp, struct tty3270_cell *cell)
 {
        cell->character = tp->view.ascebc[' '];