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
|
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include "util.h"
#include "xml.h"
static XMLParser parser; /* XML parser state */
static char url[2048], text[256], title[256];
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
xmltagend(XMLParser *p, const char *t, size_t tl, int isshort)
{
if (strcasecmp(t, "outline"))
return;
if (url[0]) {
fputs("\tfeed '", stdout);
if (title[0])
printsafe(title);
else if (text[0])
printsafe(text);
else
fputs("unnamed", stdout);
fputs("' '", stdout);
printsafe(url);
fputs("'\n", stdout);
}
url[0] = text[0] = title[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, "title"))
strlcat(title, v, sizeof(title));
else if (!strcasecmp(n, "text"))
strlcat(text, v, sizeof(text));
else if (!strcasecmp(n, "xmlurl"))
strlcat(url, v, sizeof(url));
}
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];
ssize_t len;
if ((len = xml_entitytostr(v, buf, sizeof(buf))) < 0)
return;
if (len > 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.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;
}
|