sfeed

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

commit 50d0bc170c2b7c783e09d81d38a824422712b6e5
parent 6df391bcd6dc4cd03349907400d98e6445211f66
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri,  3 Aug 2012 14:59:22 +0200

Rename sfeed_opml_config to sfeed_opml_import

Because I'm adding a sfeed_opml_export script.

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

Diffstat:
Dsfeed_opml_config.1 | 11-----------
Dsfeed_opml_config.c | 87-------------------------------------------------------------------------------
Asfeed_opml_import.1 | 11+++++++++++
Asfeed_opml_import.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 100 insertions(+), 98 deletions(-)

diff --git a/sfeed_opml_config.1 b/sfeed_opml_config.1 @@ -1,11 +0,0 @@ -.TH SFEED 1 sfeed\-VERSION -.SH NAME -sfeed_opml_config \- generate a sfeedrc config file based on an opml file -.SH SYNOPSIS -.B sfeed_opml_config -.SH DESCRIPTION -Reads the opml XML data from stdin and writes the config file text to stdout. -.SH SEE ALSO -.BR sfeed_update(1) -.SH BUGS -Please report them! diff --git a/sfeed_opml_config.c b/sfeed_opml_config.c @@ -1,87 +0,0 @@ -/* convert an opml file to sfeedrc file */ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <strings.h> -#include <expat.h> /* libexpat */ - -XML_Parser parser; /* expat XML parser state */ - -char * /* search for attr value by attr name in attributes list */ -getattrvalue(const char **atts, const char *name) { - const char **attr = NULL, *key, *value; - if(!atts || !(*atts)) - return NULL; - for(attr = atts; *attr; ) { - key = *(attr++); - value = *(attr++); - if(key && value && !strcasecmp(key, name)) - return (char *)value; - } - return NULL; -} - -void XMLCALL -xml_handler_start_element(void *data, const char *name, const char **atts) { - char *feedurl = NULL, *feedname = NULL;; - - if(!strcasecmp(name, "outline")) { - if(!(feedname = getattrvalue(atts, "text")) && - !(feedname = getattrvalue(atts, "title"))) - feedname = "unnamed"; - if(!(feedurl = getattrvalue(atts, "xmlurl"))) - feedurl = ""; - printf("\tfeed \"%s\" \"%s\"\n", feedname, feedurl); - } -} - -void XMLCALL -xml_handler_end_element(void *data, const char *name) { -} - -int /* parse XML from stream using setup parser, return 1 on success, 0 on failure. */ -xml_parse_stream(XML_Parser parser, FILE *fp) { - char buffer[BUFSIZ]; - int done = 0, len = 0; - - while(!feof(fp)) { - len = fread(buffer, 1, sizeof(buffer), fp); - done = (feof(fp) || ferror(fp)); - if(XML_Parse(parser, buffer, len, done) == XML_STATUS_ERROR && (len > 0)) { - if(XML_GetErrorCode(parser) == XML_ERROR_NO_ELEMENTS) - return 1; /* Ignore "no elements found" / empty document as an error */ - fprintf(stderr, "sfeed_opml_config: error parsing xml %s at line %lu column %lu\n", - XML_ErrorString(XML_GetErrorCode(parser)), (unsigned long)XML_GetCurrentLineNumber(parser), - (unsigned long)XML_GetCurrentColumnNumber(parser)); - return 0; - } - } while(!done); - return 1; -} - -int main(void) { - int status; - - if(!(parser = XML_ParserCreate("UTF-8"))) { - fputs("sfeed_opml_config: can't create parser", stderr); - exit(EXIT_FAILURE); - } - XML_SetElementHandler(parser, xml_handler_start_element, xml_handler_end_element); - - fputs( - "# paths\n" - "# NOTE: make sure to uncomment all these if you change it.\n" - "#sfeedpath=\"$HOME/.sfeed\"\n" - "#sfeedfile=\"$sfeedpath/feeds\"\n" - "#sfeedfilenew=\"$sfeedfile.new\"\n" - "\n" - "# list of feeds to fetch:\n" - "feeds() {\n" - " # feed <name> <url> [encoding]\n", stdout); - status = xml_parse_stream(parser, stdin); - fputs("}\n", stdout); - - XML_ParserFree(parser); - - return status ? EXIT_SUCCESS : EXIT_FAILURE; -} diff --git a/sfeed_opml_import.1 b/sfeed_opml_import.1 @@ -0,0 +1,11 @@ +.TH SFEED 1 sfeed\-VERSION +.SH NAME +sfeed_opml_import \- generate a sfeedrc config file based on an opml file +.SH SYNOPSIS +.B sfeed_opml_import +.SH DESCRIPTION +Reads the opml XML data from stdin and writes the config file text to stdout. +.SH SEE ALSO +.BR sfeed_update(1) +.SH BUGS +Please report them! diff --git a/sfeed_opml_import.c b/sfeed_opml_import.c @@ -0,0 +1,89 @@ +/* convert an opml file to sfeedrc file */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <strings.h> +#include <expat.h> /* libexpat */ + +XML_Parser parser; /* expat XML parser state */ + +char * /* search for attr value by attr name in attributes list */ +getattrvalue(const char **atts, const char *name) { + const char **attr = NULL, *key, *value; + if(!atts || !(*atts)) + return NULL; + for(attr = atts; *attr; ) { + key = *(attr++); + value = *(attr++); + if(key && value && !strcasecmp(key, name)) + return (char *)value; + } + return NULL; +} + +void XMLCALL +xml_handler_start_element(void *data, const char *name, const char **atts) { + char *feedurl = NULL, *feedname = NULL, *basesiteurl = NULL; + + if(!strcasecmp(name, "outline")) { + if(!(feedname = getattrvalue(atts, "text")) && + !(feedname = getattrvalue(atts, "title"))) + feedname = "unnamed"; + if(!(basesiteurl = getattrvalue(atts, "htmlurl"))) + basesiteurl = ""; + if(!(feedurl = getattrvalue(atts, "xmlurl"))) + feedurl = ""; + printf("\tfeed \"%s\" \"%s\" \"%s\"\n", feedname, feedurl, basesiteurl); + } +} + +void XMLCALL +xml_handler_end_element(void *data, const char *name) { +} + +int /* parse XML from stream using setup parser, return 1 on success, 0 on failure. */ +xml_parse_stream(XML_Parser parser, FILE *fp) { + char buffer[BUFSIZ]; + int done = 0, len = 0; + + while(!feof(fp)) { + len = fread(buffer, 1, sizeof(buffer), fp); + done = (feof(fp) || ferror(fp)); + if(XML_Parse(parser, buffer, len, done) == XML_STATUS_ERROR && (len > 0)) { + if(XML_GetErrorCode(parser) == XML_ERROR_NO_ELEMENTS) + return 1; /* Ignore "no elements found" / empty document as an error */ + fprintf(stderr, "sfeed_opml_config: error parsing xml %s at line %lu column %lu\n", + XML_ErrorString(XML_GetErrorCode(parser)), (unsigned long)XML_GetCurrentLineNumber(parser), + (unsigned long)XML_GetCurrentColumnNumber(parser)); + return 0; + } + } while(!done); + return 1; +} + +int main(void) { + int status; + + if(!(parser = XML_ParserCreate("UTF-8"))) { + fputs("sfeed_opml_config: can't create parser", stderr); + exit(EXIT_FAILURE); + } + XML_SetElementHandler(parser, xml_handler_start_element, xml_handler_end_element); + + fputs( + "# paths\n" + "# NOTE: make sure to uncomment all these if you change it.\n" + "#sfeedpath=\"$HOME/.sfeed\"\n" + "#sfeedfile=\"$sfeedpath/feeds\"\n" + "#sfeedfilenew=\"$sfeedfile.new\"\n" + "\n" + "# list of feeds to fetch:\n" + "feeds() {\n" + " # feed <name> <url> [encoding]\n", stdout); + status = xml_parse_stream(parser, stdin); + fputs("}\n", stdout); + + XML_ParserFree(parser); + + return status ? EXIT_SUCCESS : EXIT_FAILURE; +}