File plymouth-filter-escape-codes.patch of Package plymouth

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,162 @@ 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 color_code[7] = {'\033', '[', 0, 0, 0, 0, 0};
+static unsigned int color_index = 2;
+static uint8_t color_seperator = 0;
+
+static uint8_t* ply_terminal_strip_control_codes(uint8_t* buf, size_t s)
+{
+  uint8_t c;
+  size_t r = s;
+  uint8_t* result = (uint8_t*) malloc(s * 2 + 1);
+  unsigned int index = 0;
+
+  while (r > 0) {
+    c = (unsigned char)*buf;
+
+    switch (state) {
+    case ESnormal:
+    default:
+      state = ESnormal;
+      switch (c) {
+      case '\033':
+        state = ESesc;
+        break;
+      case 128 + 27:
+        state = ESsquare;
+        break;
+      case 0 ... 8:
+      case 14 ... 26:
+      case 28 ... 31:
+      case 127:
+      case '\r': 
+      case '\n':
+      default:
+        result [index++] = c;
+        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;
+              color_code [color_index++] = c;
+          }
+        npar++;
+        break;
+      } else if (c >= '0' && c <= '9') {
+        if (color_index < 7) {
+            if (!color_seperator && color_index >= 4)
+                break;
+
+            color_code [color_index++] = c;
+        }
+        break;
+      } else {
+          if (c == 'm' && color_index <=7) {
+            memcpy(result+index, &color_code, color_index);
+            index += color_index;
+            result [index++] = c;
+          }
+        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,6 +617,7 @@ 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);
@@ -468,8 +625,18 @@ ply_terminal_session_on_new_data (ply_te
 
   bytes_read = read (session_fd, buffer, sizeof (buffer));
 
-  if (bytes_read > 0)
-    ply_terminal_session_log_bytes (session, buffer, bytes_read);
+  if (bytes_read > 0) {
+    int filtered_len;
+
+    filtered_buffer = ply_terminal_strip_control_codes(buffer, bytes_read);
+
+    filtered_len = strlen (filtered_buffer);
+    if (filtered_len > 0)
+      ply_terminal_session_log_bytes (session, filtered_buffer, filtered_len);
+
+    free(filtered_buffer);
+    filtered_buffer = NULL;
+  }
 
   ply_logger_flush (session->logger);
 }
openSUSE Build Service is sponsored by