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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "util.h"
static struct feed **feeds;
static char *line;
static size_t linesize;
static time_t comparetime;
static unsigned long totalnew;
static void
printfeed(FILE *fpitems, FILE *fpin, struct feed *f)
{
char *fields[FieldLast];
ssize_t linelen;
unsigned int isnew;
struct tm *tm;
time_t parsedtime;
/* menu if not unnamed */
if (f->name[0]) {
fputs("<h2 id=\"", fpitems);
xmlencode(f->name, fpitems);
fputs("\"><a href=\"#", fpitems);
xmlencode(f->name, fpitems);
fputs("\">", fpitems);
xmlencode(f->name, fpitems);
fputs("</a></h2>\n", fpitems);
}
while ((linelen = getline(&line, &linesize, fpin)) > 0) {
if (line[linelen - 1] == '\n')
line[--linelen] = '\0';
if (!parseline(line, fields))
break;
parsedtime = 0;
if (strtotime(fields[FieldUnixTimestamp], &parsedtime))
continue;
if (!(tm = localtime(&parsedtime)))
err(1, "localtime");
isnew = (parsedtime >= comparetime) ? 1 : 0;
totalnew += isnew;
f->totalnew += isnew;
f->total++;
fprintf(fpitems, "%04d-%02d-%02d %02d:%02d ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min);
if (isnew)
fputs("<b><u>", fpitems);
if (fields[FieldLink][0]) {
fputs("<a href=\"", fpitems);
xmlencode(fields[FieldLink], fpitems);
fputs("\">", fpitems);
xmlencode(fields[FieldTitle], fpitems);
fputs("</a>", fpitems);
} else {
xmlencode(fields[FieldTitle], fpitems);
}
if (isnew)
fputs("</u></b>", fpitems);
fputs("\n", fpitems);
}
}
int
main(int argc, char *argv[])
{
FILE *fpindex, *fpitems, *fpmenu = NULL, *fp;
char *name;
int i, showsidebar = (argc > 1);
struct feed *f;
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
if (!(feeds = calloc(argc, sizeof(struct feed *))))
err(1, "calloc");
if ((comparetime = time(NULL)) == -1)
err(1, "time");
/* 1 day is old news */
comparetime -= 86400;
/* write main index page */
if (!(fpindex = fopen("index.html", "wb")))
err(1, "fopen: index.html");
if (!(fpitems = fopen("items.html", "wb")))
err(1, "fopen: items.html");
if (showsidebar && !(fpmenu = fopen("menu.html", "wb")))
err(1, "fopen: menu.html");
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
fputs("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>"
"<body class=\"frame\"><div id=\"items\"><pre>", fpitems);
if (argc == 1) {
if (!(feeds[0] = calloc(1, sizeof(struct feed))))
err(1, "calloc");
feeds[0]->name = "";
printfeed(fpitems, stdin, feeds[0]);
} else {
for (i = 1; i < argc; i++) {
if (!(feeds[i - 1] = calloc(1, sizeof(struct feed))))
err(1, "calloc");
name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
feeds[i - 1]->name = name;
if (!(fp = fopen(argv[i], "r")))
err(1, "fopen: %s", argv[i]);
printfeed(fpitems, fp, feeds[i - 1]);
if (ferror(fp))
err(1, "ferror: %s", argv[i]);
fclose(fp);
}
}
fputs("</pre>\n</div></body>\n</html>\n", fpitems); /* div items */
if (showsidebar) {
fputs("<html><head>"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
"</head><body class=\"frame\"><div id=\"sidebar\">", fpmenu);
for (i = 1; i < argc; i++) {
f = feeds[i - 1];
if (f->totalnew)
fputs("<a class=\"n\" href=\"items.html#", fpmenu);
else
fputs("<a href=\"items.html#", fpmenu);
xmlencode(f->name, fpmenu);
fputs("\" target=\"items\">", fpmenu);
if (f->totalnew > 0)
fputs("<b><u>", fpmenu);
xmlencode(f->name, fpmenu);
fprintf(fpmenu, " (%lu)", f->totalnew);
if (f->totalnew > 0)
fputs("</u></b>", fpmenu);
fputs("</a><br/>\n", fpmenu);
}
fputs("</div></body></html>\n", fpmenu);
}
fputs("<!DOCTYPE html><html><head>\n\t<title>Newsfeed (", fpindex);
fprintf(fpindex, "%lu", totalnew);
fputs(")</title>\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
"</head>\n", fpindex);
if (showsidebar) {
fputs("<frameset framespacing=\"0\" cols=\"250,*\" frameborder=\"1\">\n"
"\t<frame name=\"menu\" src=\"menu.html\" target=\"menu\">\n", fpindex);
} else {
fputs("<frameset framespacing=\"0\" cols=\"*\" frameborder=\"1\">\n", fpindex);
}
fputs(
"\t<frame name=\"items\" src=\"items.html\" target=\"items\">\n"
"</frameset>\n"
"</html>\n", fpindex);
fclose(fpindex);
fclose(fpitems);
if (fpmenu)
fclose(fpmenu);
return 0;
}
|