diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-07-11 04:40:48 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-07-11 04:55:34 +0200 |
commit | d27a23a7d6ad3f5b35967cfecf64a4ebfa4b31c6 (patch) | |
tree | a48e3e3e9b48c9da44fc79fb59cdb3ee705c542c | |
parent | e2c0240a842f193286b070cdbc1c4bd4fd3542e9 (diff) | |
download | xK-d27a23a7d6ad3f5b35967cfecf64a4ebfa4b31c6.tar.gz xK-d27a23a7d6ad3f5b35967cfecf64a4ebfa4b31c6.tar.xz xK-d27a23a7d6ad3f5b35967cfecf64a4ebfa4b31c6.zip |
degesch: sanitize terminal output
-rw-r--r-- | degesch.c | 37 |
1 files changed, 22 insertions, 15 deletions
@@ -2561,6 +2561,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) { terminal_printer_fn printer = get_attribute_printer (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); } |