File plymouth-filter-escape-codes.patch of Package plymouth.603
Index: plymouth-0.9.0/src/libply/ply-terminal-session.c
===================================================================
--- plymouth-0.9.0.orig/src/libply/ply-terminal-session.c
+++ plymouth-0.9.0/src/libply/ply-terminal-session.c
@@ -65,6 +65,168 @@ static bool ply_terminal_session_execute
static void ply_terminal_session_start_logging (ply_terminal_session_t *session);
static void ply_terminal_session_stop_logging (ply_terminal_session_t *session);
+enum {
+ ESnormal,
+ ESesc,
+ ESsquare,
+ ESgetpars,
+ ESgotpars,
+ ESfunckey,
+ EShash,
+ ESsetG0,
+ ESsetG1,
+ ESpercent,
+ ESignore,
+ ESnonstd,
+ ESpalette
+};
+
+#define NPAR 16
+static unsigned int state = ESnormal;
+static int npar;
+
+static uint8_t* ply_terminal_strip_control_codes(uint8_t* buf, size_t s)
+{
+ int c;
+ size_t r = s;
+ uint8_t* result = (uint8_t*) malloc(s);
+ unsigned int index = 0;
+
+ uint8_t color_code[7] = {'\033', '['};
+ unsigned int color_index = 2;
+ uint8_t color_seperator = 0;
+ memset(color_code+2, 0, 5);
+
+ while (r > 0) {
+ c = (unsigned char)*buf;
+
+ switch (state) {
+ case ESnormal:
+ default:
+ state = ESnormal;
+ switch (c) {
+ case 0 ... 8:
+ case 14 ... 26:
+ case 28 ... 31:
+ case 127:
+ case '\r':
+ break;
+ case '\033':
+ state = ESesc;
+ break;
+ case 128 + 27:
+ state = ESsquare;
+ break;
+ case '\n':
+ default:
+ memcpy(result+index, &c, 1);
+ ++index;
+ break;
+ }
+ break;
+ case ESesc:
+ state = ESnormal;
+ switch ((unsigned char)c) {
+ case '[':
+ state = ESsquare;
+ break;
+ case ']':
+ state = ESnonstd;
+ break;
+ case '%':
+ state = ESpercent;
+ break;
+ case '(':
+ state = ESsetG0;
+ break;
+ case ')':
+ state = ESsetG1;
+ break;
+ case '#':
+ state = EShash;
+ break;
+ default:
+ break;
+ }
+ break;
+ case ESnonstd:
+ if (c == 'P') {
+ npar = 0;
+ state = ESpalette;
+ } else if (c == 'R')
+ state = ESnormal;
+ else
+ state = ESnormal;
+ break;
+ case ESpalette:
+ if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') ||
+ (c >= 'a' && c <= 'f')) {
+ npar++;
+ if (npar == 7)
+ state = ESnormal;
+ } else
+ state = ESnormal;
+ break;
+ case ESsquare:
+ npar = 0;
+ state = ESgetpars;
+ if (c == '[') {
+ state = ESfunckey;
+ break;
+ }
+ if (c == '?')
+ break;
+ case ESgetpars:
+ if (c == ';' && npar < NPAR - 1) {
+ if (color_index > 1 && color_index < 7) {
+ color_seperator = 1;
+ memcpy(color_code + color_index, &c, 1);
+ ++color_index;
+ }
+ npar++;
+ break;
+ } else if (c >= '0' && c <= '9') {
+ if (color_index < 7) {
+ if (!color_seperator && color_index >= 4)
+ break;
+
+ memcpy(color_code + color_index, &c, 1);
+ ++color_index;
+ }
+ break;
+ } else {
+ if (c == 'm' && color_index <=7) {
+ memcpy(result+index, &color_code, color_index);
+ index += color_index;
+ memcpy(result+index, &c, 1);
+ ++index;
+ }
+ state = ESgotpars;
+ }
+ case ESgotpars:
+ memset(color_code+2, 0, 5);
+ color_index = 2;
+ color_seperator = 0;
+ state = ESnormal;
+ break;
+ case ESpercent:
+ state = ESnormal;
+ break;
+ case ESfunckey:
+ case EShash:
+ case ESsetG0:
+ case ESsetG1:
+ state = ESnormal;
+ break;
+ }
+ buf++;
+ r--;
+ }
+
+ result[index] = '\0';
+ return result;
+}
+
static bool
ply_terminal_session_open_console (ply_terminal_session_t *session)
{
@@ -461,15 +623,20 @@ ply_terminal_session_on_new_data (ply_te
int session_fd)
{
uint8_t buffer[4096];
+ uint8_t* filtered_buffer;
ssize_t bytes_read;
assert (session != NULL);
assert (session_fd >= 0);
bytes_read = read (session_fd, buffer, sizeof (buffer));
+ filtered_buffer = ply_terminal_strip_control_codes(buffer, bytes_read);
if (bytes_read > 0)
- ply_terminal_session_log_bytes (session, buffer, bytes_read);
+ ply_terminal_session_log_bytes (session, filtered_buffer, strlen(filtered_buffer));
+
+ free(filtered_buffer);
+ filtered_buffer = NULL;
ply_logger_flush (session->logger);
}