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

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
formatter_flush (struct formatter *self, FILE *stream)
{
@ -2579,23 +2596,13 @@ formatter_flush (struct formatter *self, FILE *stream)
int attribute_ignore = 0;
LIST_FOR_EACH (struct formatter_item, iter, self->items)
{
switch (iter->type)
{
char *term;
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:
if (iter->type == FORMATTER_ITEM_TEXT)
formatter_flush_text (self->ctx, iter->text, stream);
else if (iter->type == FORMATTER_ITEM_IGNORE_ATTR)
attribute_ignore += iter->attribute;
break;
default:
if (attribute_ignore <= 0
&& !formatter_flush_attr (&state, iter))
else if (attribute_ignore <= 0
&& !formatter_flush_attr (&state, iter))
hard_assert (!"unhandled formatter item type");
}
}
attribute_printer_reset (&state);
}