From c32005c3c1f31b2518b888d2f33d0c04e3c42f85 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 2 Aug 2015 19:47:57 +0200 Subject: sfeed_frames: wrap plain-text, encode as XML/HTML 2.0 --- sfeed_frames.c | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'sfeed_frames.c') diff --git a/sfeed_frames.c b/sfeed_frames.c index ab18408..e5252a5 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -26,24 +26,53 @@ static struct feed **feeds = NULL; /* Unescape / decode fields printed by string_print_encoded() * "\\" to "\", "\t", to TAB, "\n" to newline. Unrecognised escape sequences - * are ignored: "\z" etc. Call `fn` on each escaped character. */ -void + * are ignored: "\z" etc. */ +static void printcontent(const char *s, FILE *fp) { for (; *s; s++) { - if (*s == '\\') { + switch (*s) { + case '\\': switch (*(++s)) { case '\0': return; /* ignore */ case '\\': fputc('\\', fp); break; case 't': fputc('\t', fp); break; case 'n': fputc('\n', fp); break; } - } else { + break; + default: fputc((int)*s, fp); } } } +/* Unescape / decode fields printed by string_print_encoded() + * "\\" to "\", "\t", to TAB, "\n" to newline. Unrecognised escape sequences + * are ignored: "\z" etc. Encode HTML 2.0 / XML 1.0 entities. */ +static void +printcontentxml(const char *s, FILE *fp) +{ + for (; *s; s++) { + switch (*s) { + case '\\': + switch (*(++s)) { + case '\0': return; /* ignore */ + case '\\': fputc('\\', fp); break; + case 't': fputc('\t', fp); break; + case 'n': fputc('\n', fp); break; + } + break; + /* XML entities */ + case '<': fputs("<", fp); break; + case '>': fputs(">", fp); break; + case '\'': fputs("'", fp); break; + case '&': fputs("&", fp); break; + case '"': fputs(""", fp); break; + default: fputc((int)*s, fp); + } + } +} + /* normalize path names, transform to lower-case and replace non-alpha and * non-digit with '-' */ static size_t @@ -135,7 +164,14 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) /* NOTE: this prints the raw HTML of the feed, this is * potentially dangerous, it is up to the user / browser * to trust a feed it's HTML content. */ - printcontent(fields[FieldContent], fpcontent); + if (!strcmp(fields[FieldContentType], "html")) { + printcontent(fields[FieldContent], fpcontent); + } else { + /* plain-text, wrap with
 */
+				fputs("
", fpcontent);
+				printcontentxml(fields[FieldContent], fpcontent);
+				fputs("
", fpcontent); + } fputs("", fpcontent); fclose(fpcontent); } -- cgit v1.2.3