1
0
mirror of https://github.com/systemd/systemd.git synced 2025-05-29 01:05:59 +03:00

ptyfwd: clean up logic of color state transition in pty_forward_ansi_process()

Drop all 'continue', to make the logic easy to understand.
No functional change, just refactoring and preparation for later commit.
This commit is contained in:
Yu Watanabe 2024-12-16 16:23:49 +09:00
parent 106f64cbd6
commit 9bc2acad8f

View File

@ -36,8 +36,6 @@ typedef enum AnsiColorState {
ANSI_COLOR_STATE_ESC, ANSI_COLOR_STATE_ESC,
ANSI_COLOR_STATE_CSI_SEQUENCE, ANSI_COLOR_STATE_CSI_SEQUENCE,
ANSI_COLOR_STATE_OSC_SEQUENCE, ANSI_COLOR_STATE_OSC_SEQUENCE,
ANSI_COLOR_STATE_NEWLINE,
ANSI_COLOR_STATE_CARRIAGE_RETURN,
_ANSI_COLOR_STATE_MAX, _ANSI_COLOR_STATE_MAX,
_ANSI_COLOR_STATE_INVALID = -EINVAL, _ANSI_COLOR_STATE_INVALID = -EINVAL,
} AnsiColorState; } AnsiColorState;
@ -427,39 +425,33 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
switch (f->ansi_color_state) { switch (f->ansi_color_state) {
case ANSI_COLOR_STATE_TEXT: case ANSI_COLOR_STATE_TEXT:
break; if (IN_SET(c, '\n', '\r')) {
/* Immediately after a newline (ASCII 10) or carriage return (ASCII 13) insert an
case ANSI_COLOR_STATE_NEWLINE: * ANSI sequence set the background color back. */
case ANSI_COLOR_STATE_CARRIAGE_RETURN: r = insert_background_color(f, i+1);
/* Immediately after a newline (ASCII 10) or carriage return (ASCII 13) insert an if (r < 0)
* ANSI sequence set the background color back. */ return r;
i += r;
r = insert_background_color(f, i); } else if (c == 0x1B) /* ESC */
if (r < 0) f->ansi_color_state = ANSI_COLOR_STATE_ESC;
return r;
i += r;
break; break;
case ANSI_COLOR_STATE_ESC: case ANSI_COLOR_STATE_ESC:
if (c == '[') { if (c == '[')
f->ansi_color_state = ANSI_COLOR_STATE_CSI_SEQUENCE; f->ansi_color_state = ANSI_COLOR_STATE_CSI_SEQUENCE;
continue; else if (c == ']')
} else if (c == ']') {
f->ansi_color_state = ANSI_COLOR_STATE_OSC_SEQUENCE; f->ansi_color_state = ANSI_COLOR_STATE_OSC_SEQUENCE;
continue; else if (c == 'c') {
} else if (c == 'c') { /* "Full reset" aka "Reset to initial state" */
/* "Full reset" aka "Reset to initial state"*/
r = insert_background_color(f, i+1); r = insert_background_color(f, i+1);
if (r < 0) if (r < 0)
return r; return r;
i += r; i += r;
f->ansi_color_state = ANSI_COLOR_STATE_TEXT; f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
continue; } else
} f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
break; break;
case ANSI_COLOR_STATE_CSI_SEQUENCE: case ANSI_COLOR_STATE_CSI_SEQUENCE:
@ -472,7 +464,7 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
/* Safety check: lets not accept unbounded CSI sequences */ /* Safety check: lets not accept unbounded CSI sequences */
f->csi_sequence = mfree(f->csi_sequence); f->csi_sequence = mfree(f->csi_sequence);
break; f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
} else if (!strextend(&f->csi_sequence, CHAR_TO_STR(c))) } else if (!strextend(&f->csi_sequence, CHAR_TO_STR(c)))
return -ENOMEM; return -ENOMEM;
} else { } else {
@ -498,7 +490,7 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
f->csi_sequence = mfree(f->csi_sequence); f->csi_sequence = mfree(f->csi_sequence);
f->ansi_color_state = ANSI_COLOR_STATE_TEXT; f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
} }
continue; break;
case ANSI_COLOR_STATE_OSC_SEQUENCE: case ANSI_COLOR_STATE_OSC_SEQUENCE:
@ -506,7 +498,7 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
if (strlen_ptr(f->osc_sequence) >= ANSI_SEQUENCE_LENGTH_MAX) { if (strlen_ptr(f->osc_sequence) >= ANSI_SEQUENCE_LENGTH_MAX) {
/* Safety check: lets not accept unbounded OSC sequences */ /* Safety check: lets not accept unbounded OSC sequences */
f->osc_sequence = mfree(f->osc_sequence); f->osc_sequence = mfree(f->osc_sequence);
break; f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
} else if (!strextend(&f->osc_sequence, CHAR_TO_STR(c))) } else if (!strextend(&f->osc_sequence, CHAR_TO_STR(c)))
return -ENOMEM; return -ENOMEM;
} else { } else {
@ -534,20 +526,11 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
f->osc_sequence = mfree(f->osc_sequence); f->osc_sequence = mfree(f->osc_sequence);
f->ansi_color_state = ANSI_COLOR_STATE_TEXT; f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
} }
continue; break;
default: default:
assert_not_reached(); assert_not_reached();
} }
if (c == '\n')
f->ansi_color_state = ANSI_COLOR_STATE_NEWLINE;
else if (c == '\r')
f->ansi_color_state = ANSI_COLOR_STATE_CARRIAGE_RETURN;
else if (c == 0x1B) /* ESC */
f->ansi_color_state = ANSI_COLOR_STATE_ESC;
else
f->ansi_color_state = ANSI_COLOR_STATE_TEXT;
} }
return 0; return 0;