summaryrefslogtreecommitdiff
path: root/sfeed_opml_import.c
blob: 6eef0cab0a5265d07bcfbe93aa248873bde9bda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <strings.h>

#include "util.h"
#include "xml.h"

static XMLParser parser; /* XML parser state */
static char text[256], title[256], xmlurl[2048];

static void
printsafe(const char *s)
{
	for (; *s; s++) {
		if (iscntrl((unsigned char)*s))
			continue;
		else if (*s == '\\')
			fputs("\\\\", stdout);
		else if (*s == '\'')
			fputs("'\\''", stdout);
		else
			putchar(*s);
	}
}

static void
xmltagstart(XMLParser *p, const char *t, size_t tl)
{
	text[0] = title[0] = xmlurl[0] = '\0';
}

static void
xmltagend(XMLParser *p, const char *t, size_t tl, int isshort)
{
	if (strcasecmp(t, "outline"))
		return;

	if (xmlurl[0]) {
		fputs("\tfeed '", stdout);
		/* prefer title over text attribute */
		if (title[0])
			printsafe(title);
		else if (text[0])
			printsafe(text);
		else
			fputs("unnamed", stdout);
		fputs("' '", stdout);
		printsafe(xmlurl);
		fputs("'\n", stdout);
	}

	text[0] = title[0] = xmlurl[0] = '\0';
}

static void
xmlattr(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
	const char *v, size_t vl)
{
	if (strcasecmp(t, "outline"))
		return;

	if (!strcasecmp(n, "text"))
		strlcat(text, v, sizeof(text));
	else if (!strcasecmp(n, "title"))
		strlcat(title, v, sizeof(title));
	else if (!strcasecmp(n, "xmlurl"))
		strlcat(xmlurl, v, sizeof(xmlurl));
}

static void
xmlattrentity(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
	const char *v, size_t vl)
{
	char buf[16];
	int len;

	if ((len = xml_entitytostr(v, buf, sizeof(buf))) > 0)
		xmlattr(p, t, tl, n, nl, buf, len);
	else
		xmlattr(p, t, tl, n, nl, v, vl);
}

int
main(void)
{
	if (pledge("stdio", NULL) == -1)
		err(1, "pledge");

	parser.xmlattr = xmlattr;
	parser.xmlattrentity = xmlattrentity;
	parser.xmltagstart = xmltagstart;
	parser.xmltagend = xmltagend;

	fputs(
	    "#sfeedpath=\"$HOME/.sfeed/feeds\"\n"
	    "\n"
	    "# list of feeds to fetch:\n"
	    "feeds() {\n"
	    "	# feed <name> <feedurl> [basesiteurl] [encoding]\n", stdout);
	/* NOTE: getnext is defined in xml.h for inline optimization */
	xml_parse(&parser);
	fputs("}\n", stdout);

	return 0;
}