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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "util.h"
static struct tm tmnow;
static time_t now;
static char *line;
static size_t linesize;
static void
printcontent(const char *s)
{
for (; *s; ++s) {
switch (*s) {
case '<': fputs("<", stdout); break;
case '>': fputs(">", stdout); break;
case '\'': fputs("'", stdout); break;
case '&': fputs("&", stdout); break;
case '"': fputs(""", stdout); break;
case '\\':
s++;
switch (*s) {
case 'n': putchar('\n'); break;
case '\\': putchar('\\'); break;
case 't': putchar('\t'); break;
}
break;
default: putchar(*s);
}
}
}
static void
printfeed(FILE *fp, const char *feedname)
{
char *fields[FieldLast], *p, *tmp;
struct tm parsedtm, *tm;
time_t parsedtime;
ssize_t linelen;
int c;
while ((linelen = getline(&line, &linesize, fp)) > 0 &&
!ferror(stdout)) {
if (line[linelen - 1] == '\n')
line[--linelen] = '\0';
parseline(line, fields);
fputs("<entry>\n\t<title>", stdout);
if (feedname[0]) {
fputs("[", stdout);
xmlencode(feedname, stdout);
fputs("] ", stdout);
}
xmlencode(fields[FieldTitle], stdout);
fputs("</title>\n", stdout);
if (fields[FieldLink][0]) {
fputs("\t<link rel=\"alternate\" href=\"", stdout);
xmlencode(fields[FieldLink], stdout);
fputs("\" />\n", stdout);
}
/* prefer link over id for Atom <id>. */
fputs("\t<id>", stdout);
if (fields[FieldLink][0])
xmlencode(fields[FieldLink], stdout);
else if (fields[FieldId][0])
xmlencode(fields[FieldId], stdout);
fputs("</id>\n", stdout);
if (fields[FieldEnclosure][0]) {
fputs("\t<link rel=\"enclosure\" href=\"", stdout);
xmlencode(fields[FieldEnclosure], stdout);
fputs("\" />\n", stdout);
}
parsedtime = 0;
if (strtotime(fields[FieldUnixTimestamp], &parsedtime) ||
!(tm = gmtime_r(&parsedtime, &parsedtm)))
tm = &tmnow;
fprintf(stdout, "\t<updated>%04d-%02d-%02dT%02d:%02d:%02dZ</updated>\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (fields[FieldAuthor][0]) {
fputs("\t<author><name>", stdout);
xmlencode(fields[FieldAuthor], stdout);
fputs("</name></author>\n", stdout);
}
if (fields[FieldContent][0]) {
if (!strcmp(fields[FieldContentType], "html")) {
fputs("\t<content type=\"html\">", stdout);
} else {
/* NOTE: an RSS/Atom viewer may or may not format
whitespace such as newlines.
Workaround: type="html" and <![CDATA[<pre></pre>]]> */
fputs("\t<content type=\"text\">", stdout);
}
printcontent(fields[FieldContent]);
fputs("</content>\n", stdout);
}
for (p = fields[FieldCategory]; (tmp = strchr(p, '|')); p = tmp + 1) {
c = *tmp;
*tmp = '\0'; /* temporary NUL-terminate */
if (*p) {
fputs("\t<category term=\"", stdout);
xmlencode(p, stdout);
fputs("\" />\n", stdout);
}
*tmp = c; /* restore */
}
fputs("</entry>\n", stdout);
}
}
int
main(int argc, char *argv[])
{
struct tm *tm;
FILE *fp;
char *name;
int i;
if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
err(1, "pledge");
if ((now = time(NULL)) == (time_t)-1)
errx(1, "time");
if (!(tm = gmtime_r(&now, &tmnow)))
err(1, "gmtime_r");
fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
"\t<title type=\"text\">Newsfeed</title>\n"
"\t<author><name>sfeed</name></author>\n", stdout);
printf("\t<id>urn:newsfeed:%lld</id>\n"
"\t<updated>%04d-%02d-%02dT%02d:%02d:%02dZ</updated>\n",
(long long)now,
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (argc == 1) {
printfeed(stdin, "");
checkfileerror(stdin, "<stdin>", 'r');
} else {
for (i = 1; i < argc; i++) {
if (!(fp = fopen(argv[i], "r")))
err(1, "fopen: %s", argv[i]);
name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
printfeed(fp, name);
checkfileerror(fp, argv[i], 'r');
checkfileerror(stdout, "<stdout>", 'w');
fclose(fp);
}
}
fputs("</feed>\n", stdout);
checkfileerror(stdout, "<stdout>", 'w');
return 0;
}
|