sfeed_opml_import.c (2136B) - raw


      1 #include <ctype.h>
      2 #include <stdio.h>
      3 #include <strings.h>
      4 
      5 #include "util.h"
      6 #include "xml.h"
      7 
      8 static XMLParser parser; /* XML parser state */
      9 static char text[256], title[256], xmlurl[2048];
     10 
     11 static void
     12 printsafe(const char *s)
     13 {
     14 	for (; *s; s++) {
     15 		if (iscntrl((unsigned char)*s))
     16 			continue;
     17 		else if (*s == '\\')
     18 			fputs("\\\\", stdout);
     19 		else if (*s == '\'')
     20 			fputs("'\\''", stdout);
     21 		else
     22 			putchar(*s);
     23 	}
     24 }
     25 
     26 static void
     27 xmltagstart(XMLParser *p, const char *t, size_t tl)
     28 {
     29 	text[0] = title[0] = xmlurl[0] = '\0';
     30 }
     31 
     32 static void
     33 xmltagend(XMLParser *p, const char *t, size_t tl, int isshort)
     34 {
     35 	if (strcasecmp(t, "outline"))
     36 		return;
     37 
     38 	if (xmlurl[0]) {
     39 		fputs("\tfeed '", stdout);
     40 		/* prefer title over text attribute */
     41 		if (title[0])
     42 			printsafe(title);
     43 		else if (text[0])
     44 			printsafe(text);
     45 		else
     46 			fputs("unnamed", stdout);
     47 		fputs("' '", stdout);
     48 		printsafe(xmlurl);
     49 		fputs("'\n", stdout);
     50 	}
     51 
     52 	text[0] = title[0] = xmlurl[0] = '\0';
     53 }
     54 
     55 static void
     56 xmlattr(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
     57 	const char *v, size_t vl)
     58 {
     59 	if (strcasecmp(t, "outline"))
     60 		return;
     61 
     62 	if (!strcasecmp(n, "text"))
     63 		strlcat(text, v, sizeof(text));
     64 	else if (!strcasecmp(n, "title"))
     65 		strlcat(title, v, sizeof(title));
     66 	else if (!strcasecmp(n, "xmlurl"))
     67 		strlcat(xmlurl, v, sizeof(xmlurl));
     68 }
     69 
     70 static void
     71 xmlattrentity(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
     72 	const char *v, size_t vl)
     73 {
     74 	char buf[16];
     75 	int len;
     76 
     77 	if ((len = xml_entitytostr(v, buf, sizeof(buf))) > 0)
     78 		xmlattr(p, t, tl, n, nl, buf, len);
     79 	else
     80 		xmlattr(p, t, tl, n, nl, v, vl);
     81 }
     82 
     83 int
     84 main(void)
     85 {
     86 	if (pledge("stdio", NULL) == -1)
     87 		err(1, "pledge");
     88 
     89 	parser.xmlattr = xmlattr;
     90 	parser.xmlattrentity = xmlattrentity;
     91 	parser.xmltagstart = xmltagstart;
     92 	parser.xmltagend = xmltagend;
     93 
     94 	fputs(
     95 	    "#sfeedpath=\"$HOME/.sfeed/feeds\"\n"
     96 	    "\n"
     97 	    "# list of feeds to fetch:\n"
     98 	    "feeds() {\n"
     99 	    "	# feed <name> <feedurl> [basesiteurl] [encoding]\n", stdout);
    100 	/* NOTE: getnext is defined in xml.h for inline optimization */
    101 	xml_parse(&parser);
    102 	fputs("}\n", stdout);
    103 
    104 	return 0;
    105 }