summaryrefslogtreecommitdiff
path: root/src/shared
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
parent2d0b0528ace89d378051c280bf3be367b2a7d2de (diff)
journalctl: strip TABs and ANSI color sequences from log messages when displaying them
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/logs-show.c3
-rw-r--r--src/shared/util.c83
-rw-r--r--src/shared/util.h2
3 files changed, 88 insertions, 0 deletions
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index ca5ad43b63..04450a5504 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -179,6 +179,9 @@ static int output_short(
if (!message)
return 0;
+ if (!(flags & OUTPUT_SHOW_ALL))
+ strip_tab_ansi(&message, &message_len);
+
if (priority_len == 1 && *priority >= '0' && *priority <= '7')
p = *priority - '0';
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;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index bf24272ea6..25b349a17a 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -559,3 +559,5 @@ typedef enum DrawSpecialChar {
const char *draw_special_char(DrawSpecialChar ch);
char *strreplace(const char *text, const char *old_string, const char *new_string);
+
+char *strip_tab_ansi(char **p, size_t *l);