sfeed

simple feed reader - forked from git.codemadness.org/sfeed
git clone git://src.gearsix.net/sfeed
Log | Files | Refs | Atom | README | LICENSE

commit 0df57225a5c7e475116cad33c9a9a92b907ade56
parent 072e7e6b1fdf17d10fdb573adaecaed320718b90
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Tue,  1 Apr 2014 00:19:30 +0200

fix crlf newlines, add fp arg to xmlparser_init

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
Msfeed.c | 2+-
Msfeed_opml_import.c | 2+-
Msfeed_web.c | 141+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msfeed_xmlenc.c | 90++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mxml.c | 4++--
Mxml.h | 2+-
6 files changed, 119 insertions(+), 122 deletions(-)

diff --git a/sfeed.c b/sfeed.c @@ -819,7 +819,7 @@ main(int argc, char **argv) { feeditem.contenttype = ContentTypePlain; feeditem.feedtype = FeedTypeNone; - xmlparser_init(&parser); + xmlparser_init(&parser, stdin); parser.xmltagstart = xml_handler_start_element; parser.xmltagend = xml_handler_end_element; parser.xmldata = xml_handler_data; diff --git a/sfeed_opml_import.c b/sfeed_opml_import.c @@ -54,7 +54,7 @@ xml_handler_attr(XMLParser *p, const char *tag, size_t taglen, int main(void) { - xmlparser_init(&parser); + xmlparser_init(&parser, stdin); parser.xmltagstart = xml_handler_start_element; parser.xmltagend = xml_handler_end_element; diff --git a/sfeed_web.c b/sfeed_web.c @@ -1,72 +1,69 @@ -#include <stdio.h> -#include <string.h> -#include <strings.h> -#include <stdlib.h> -#include <ctype.h> - -#include "util.h" -#include "xml.h" - -static unsigned int isbase = 0, islink = 0, isfeedlink = 0, found = 0; -static char feedlink[4096], basehref[4096]; - -static void -xmltagstart(XMLParser *p, const char *tag, size_t taglen) { - isbase = islink = isfeedlink = 0; - if(taglen == 4) { /* optimization */ - if(!strncasecmp(tag, "base", taglen)) - isbase = 1; - else if(!strncasecmp(tag, "link", taglen)) - islink = 1; - } -} - -static void -xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) { - if(isfeedlink) { - printlink(feedlink, basehref, stdout); - putchar('\n'); - found++; - } -} - -static void -xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, - size_t namelen, const char *value, size_t valuelen) { - - if(namelen != 4) /* optimization */ - return; - if(isbase) { - if(!strncasecmp(name, "href", namelen)) - strlcpy(basehref, value, sizeof(basehref) - 1); - } else if(islink) { - if(!strncasecmp(name, "type", namelen)) { - if(!strncasecmp(value, "application/atom", strlen("application/atom")) || - !strncasecmp(value, "application/rss", strlen("application/rss"))) { - isfeedlink = 1; - } - } else if(!strncasecmp(name, "href", namelen)) - strlcpy(feedlink, value, sizeof(feedlink) - 1); - } -} - -int -main(int argc, char **argv) { - XMLParser x; - - feedlink[0] = '\0'; - /* base href */ - if(argc > 1) - strlcpy(basehref, argv[1], sizeof(basehref) - 1); - else - basehref[0] = '\0'; - - xmlparser_init(&x); - x.xmltagstart = xmltagstart; - x.xmlattr = xmlattr; - x.xmltagstartparsed = xmltagstartparsed; - - xmlparser_parse(&x); - - return found > 0 ? EXIT_SUCCESS : EXIT_FAILURE; -} +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <ctype.h> + +#include "util.h" +#include "xml.h" + +static unsigned int isbase = 0, islink = 0, isfeedlink = 0, found = 0; +static char feedlink[4096] = "", basehref[4096] = ""; + +static void +xmltagstart(XMLParser *p, const char *tag, size_t taglen) { + isbase = islink = isfeedlink = 0; + if(taglen == 4) { /* optimization */ + if(!strncasecmp(tag, "base", taglen)) + isbase = 1; + else if(!strncasecmp(tag, "link", taglen)) + islink = 1; + } +} + +static void +xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) { + if(isfeedlink) { + printlink(feedlink, basehref, stdout); + putchar('\n'); + found++; + } +} + +static void +xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, + size_t namelen, const char *value, size_t valuelen) { + + if(namelen != 4) /* optimization */ + return; + if(isbase) { + if(!strncasecmp(name, "href", namelen)) + strlcpy(basehref, value, sizeof(basehref) - 1); + } else if(islink) { + if(!strncasecmp(name, "type", namelen)) { + if(!strncasecmp(value, "application/atom", strlen("application/atom")) || + !strncasecmp(value, "application/rss", strlen("application/rss"))) { + isfeedlink = 1; + } + } else if(!strncasecmp(name, "href", namelen)) + strlcpy(feedlink, value, sizeof(feedlink) - 1); + } +} + +int +main(int argc, char **argv) { + XMLParser x; + + /* base href */ + if(argc > 1) + strlcpy(basehref, argv[1], sizeof(basehref) - 1); + + xmlparser_init(&x, stdin); + x.xmltagstart = xmltagstart; + x.xmlattr = xmlattr; + x.xmltagstartparsed = xmltagstartparsed; + + xmlparser_parse(&x); + + return found > 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/sfeed_xmlenc.c b/sfeed_xmlenc.c @@ -1,45 +1,45 @@ -#include <stdio.h> -#include <string.h> -#include <strings.h> -#include <stdlib.h> -#include <ctype.h> - -#include "xml.h" - -static int isxmlpi = 0, tags = 0; - -static void -xmltagstart(XMLParser *p, const char *tag, size_t taglen) { - if(tags > 3) /* optimization: try to find processing instruction at start */ - exit(EXIT_FAILURE); - isxmlpi = (tag[0] == '?' && (!strncasecmp(tag, "?xml", taglen))) ? 1 : 0; - tags++; -} - -static void -xmltagend(XMLParser *p, const char *tag, size_t taglen, int isshort) { - isxmlpi = 0; -} - -static void -xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, size_t namelen, const char *value, size_t valuelen) { - if(isxmlpi && (!strncasecmp(name, "encoding", namelen))) { - for(; *value; value++) - putc(tolower((int)*value), stdout); /* output lowercase */ - exit(EXIT_SUCCESS); - } -} - -int -main(int argc, char **argv) { - XMLParser x; - - xmlparser_init(&x); - x.xmltagstart = xmltagstart; - x.xmltagend = xmltagend; - x.xmlattr = xmlattr; - - xmlparser_parse(&x); - - return EXIT_FAILURE; -} +#include <stdio.h> +#include <string.h> +#include <strings.h> +#include <stdlib.h> +#include <ctype.h> + +#include "xml.h" + +static int isxmlpi = 0, tags = 0; + +static void +xmltagstart(XMLParser *p, const char *tag, size_t taglen) { + if(tags > 3) /* optimization: try to find processing instruction at start */ + exit(EXIT_FAILURE); + isxmlpi = (tag[0] == '?' && (!strncasecmp(tag, "?xml", taglen))) ? 1 : 0; + tags++; +} + +static void +xmltagend(XMLParser *p, const char *tag, size_t taglen, int isshort) { + isxmlpi = 0; +} + +static void +xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, size_t namelen, const char *value, size_t valuelen) { + if(isxmlpi && (!strncasecmp(name, "encoding", namelen))) { + for(; *value; value++) + putc(tolower((int)*value), stdout); /* output lowercase */ + exit(EXIT_SUCCESS); + } +} + +int +main(int argc, char **argv) { + XMLParser x; + + xmlparser_init(&x, stdin); + x.xmltagstart = xmltagstart; + x.xmltagend = xmltagend; + x.xmlattr = xmlattr; + + xmlparser_parse(&x); + + return EXIT_FAILURE; +} diff --git a/xml.c b/xml.c @@ -6,9 +6,9 @@ #include "xml.h" void -xmlparser_init(XMLParser *x) { +xmlparser_init(XMLParser *x, FILE *fp) { memset(x, 0, sizeof(XMLParser)); - x->fp = stdin; + x->fp = fp; } __inline__ int /* like getc(), but do some smart buffering */ diff --git a/xml.h b/xml.h @@ -35,5 +35,5 @@ typedef struct xmlparser { unsigned char readbuf[BUFSIZ]; /* read buffer used by xmlparser_getnext() */ } XMLParser; -void xmlparser_init(XMLParser *x); +void xmlparser_init(XMLParser *x, FILE *fp); void xmlparser_parse(XMLParser *x);