degesch: sanitize terminal output

This commit is contained in:
Přemysl Eric Janouch 2015-07-11 04:40:48 +02:00
parent e2c0240a84
commit d27a23a7d6
1 changed files with 22 additions and 15 deletions

View File

@ -2560,6 +2560,23 @@ formatter_flush_attr
} }
} }
static void
formatter_flush_text (struct app_context *ctx, const char *text, FILE *stream)
{
struct str sanitized;
str_init (&sanitized);
// Throw away any potentially harmful control characters
char *term = iconv_xstrdup (ctx->term_from_utf8, (char *) text, -1, NULL);
for (char *p = term; *p; p++)
if (!strchr ("\a\b\x1b", *p))
str_append_c (&sanitized, *p);
free (term);
fputs (sanitized.str, stream);
str_free (&sanitized);
}
static void static void
formatter_flush (struct formatter *self, FILE *stream) formatter_flush (struct formatter *self, FILE *stream)
{ {
@ -2579,24 +2596,14 @@ formatter_flush (struct formatter *self, FILE *stream)
int attribute_ignore = 0; int attribute_ignore = 0;
LIST_FOR_EACH (struct formatter_item, iter, self->items) LIST_FOR_EACH (struct formatter_item, iter, self->items)
{ {
switch (iter->type) if (iter->type == FORMATTER_ITEM_TEXT)
{ formatter_flush_text (self->ctx, iter->text, stream);
char *term; else if (iter->type == FORMATTER_ITEM_IGNORE_ATTR)
case FORMATTER_ITEM_TEXT:
term = iconv_xstrdup
(self->ctx->term_from_utf8, iter->text, -1, NULL);
fputs (term, stream);
free (term);
break;
case FORMATTER_ITEM_IGNORE_ATTR:
attribute_ignore += iter->attribute; attribute_ignore += iter->attribute;
break; else if (attribute_ignore <= 0
default:
if (attribute_ignore <= 0
&& !formatter_flush_attr (&state, iter)) && !formatter_flush_attr (&state, iter))
hard_assert (!"unhandled formatter item type"); hard_assert (!"unhandled formatter item type");
} }
}
attribute_printer_reset (&state); attribute_printer_reset (&state);
} }