summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-12-23 11:23:59 +0100
committerLennart Poettering <lennart@poettering.net>2012-12-23 11:28:14 +0100
commite8bc0ea2b100158bf35c635039226235aa879068 (patch)
tree112767f16aada7f070cbbba4cf9b33ec69941284 /src/shared/util.c
parent2d0b0528ace89d378051c280bf3be367b2a7d2de (diff)
journalctl: strip TABs and ANSI color sequences from log messages when displaying them
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index d5f975e744..9ec6e2fe2f 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5666,3 +5666,86 @@ oom:
free(r);
return NULL;
}
+
+char *strip_tab_ansi(char **ibuf, size_t *_isz) {
+ const char *i, *begin;
+ enum {
+ STATE_OTHER,
+ STATE_ESCAPE,
+ STATE_BRACKET
+ } state = STATE_OTHER;
+ char *obuf = NULL;
+ size_t osz = 0, isz;
+ FILE *f;
+
+ assert(ibuf);
+ assert(*ibuf);
+
+ /* Strips ANSI color and replaces TABs by 8 spaces */
+
+ isz = _isz ? *_isz : strlen(*ibuf);
+
+ f = open_memstream(&obuf, &osz);
+ if (!f)
+ return NULL;
+
+ for (i = *ibuf; i < *ibuf + isz + 1; i++) {
+
+ switch (state) {
+
+ case STATE_OTHER:
+ if (i >= *ibuf + isz) /* EOT */
+ break;
+ else if (*i == '\x1B')
+ state = STATE_ESCAPE;
+ else if (*i == '\t')
+ fputs(" ", f);
+ else
+ fputc(*i, f);
+ break;
+
+ case STATE_ESCAPE:
+ if (i >= *ibuf + isz) { /* EOT */
+ fputc('\x1B', f);
+ break;
+ } else if (*i == '[') {
+ state = STATE_BRACKET;
+ begin = i + 1;
+ } else {
+ fputc('\x1B', f);
+ fputc(*i, f);
+ state = STATE_OTHER;
+ }
+
+ break;
+
+ case STATE_BRACKET:
+
+ if (i >= *ibuf + isz || /* EOT */
+ (!(*i >= '0' && *i <= '9') && *i != ';' && *i != 'm')) {
+ fputc('\x1B', f);
+ fputc('[', f);
+ state = STATE_OTHER;
+ i = begin-1;
+ } else if (*i == 'm')
+ state = STATE_OTHER;
+ break;
+ }
+ }
+
+ if (ferror(f)) {
+ fclose(f);
+ free(obuf);
+ return NULL;
+ }
+
+ fclose(f);
+
+ free(*ibuf);
+ *ibuf = obuf;
+
+ if (_isz)
+ *_isz = osz;
+
+ return obuf;
+}