/
parser.c
474 lines (448 loc) · 11.8 KB
/
parser.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (c) 2008,2009 Colin Didier <cdidier@cybione.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <expat.h>
#include "document.h"
void strchomp(char *);
int insert_text(char **, const char *, size_t);
time_t rfc822_date(char *);
time_t rfc3339_date(char *);
#define RSS "rss"
#define RSS_CHANNEL "channel"
#define RSS_ITEM "item"
#define RSS_TITLE "title"
#define RSS_LINK "link"
#define RSS_DESCR "description"
#define RSS_AUTHOR "author"
#define RSS_ID "guid"
#define RSS_DATE "pubDate"
#define RSS_CATEGORY "category"
#define RSS_ENCLOSURE "enclosure"
#define ATOM_XMLNS "http://www.w3.org/2005/Atom"
#define ATOM_FEED "feed"
#define ATOM_ENTRY "entry"
#define ATOM_TITLE RSS_TITLE
#define ATOM_CONTENT "content"
#define ATOM_LINK RSS_LINK
#define ATOM_NAME "name"
#define ATOM_ID "id"
#define ATOM_UPDATED "updated"
#define RDF "rdf:RDF"
#define RDF_CHANNEL RSS_CHANNEL
#define RDF_TITLE RSS_TITLE
#define RDF_LIST_ITEM "rdf:li"
#define RDF_LIST_ID "rdf:resource"
#define RDF_ITEM RSS_ITEM
#define RDF_ID "rdf:about"
#define RDF_LINK RSS_LINK
#define RDF_DATE "dc:date"
#define RDF_CREATOR "dc:creator"
#define RDF_DESCR "description"
#define RDF_SUBJECT "dc:subject"
static SLIST_HEAD(, feed) feeds;
static struct feed *cf; /* current feed */
static struct item *ci; /* current item */
static int depth;
static int read_data;
static char *data;
static void XMLCALL
rss_start_elt(void *user_data, const char *name, const char **atts)
{
switch (depth++) {
case 0:
if (strcasecmp(name, RSS_CHANNEL) == 0) {
if ((cf = malloc(sizeof(struct feed))) == NULL)
err(1, "malloc");
INIT_FEED(cf);
SLIST_INSERT_HEAD(&feeds, cf, next);
}
break;
case 1:
if (cf == NULL)
break;
if (strcasecmp(name, RSS_TITLE) == 0)
read_data = 1;
else if (strcasecmp(name, RSS_ITEM) == 0) {
if ((ci = malloc(sizeof(struct item))) == NULL)
err(1, "malloc");
INIT_ITEM(ci);
SLIST_INSERT_HEAD(&cf->items, ci, next);
}
break;
case 2:
if (ci == NULL)
break;
if ((ci->title == NULL && strcasecmp(name, RSS_TITLE) == 0)
|| (ci->link == NULL && strcasecmp(name, RSS_LINK) == 0)
|| (ci->descr == NULL && strcasecmp(name, RSS_DESCR) == 0)
|| (ci->author == NULL && strcasecmp(name, RSS_AUTHOR) == 0)
|| (ci->id == NULL && strcasecmp(name, RSS_ID) == 0)
|| (ci->date == NULL && strcasecmp(name, RSS_DATE) == 0)
|| (strcasecmp(name, RSS_CATEGORY) == 0))
read_data = 1;
else if (strcasecmp(name, RSS_ENCLOSURE) == 0) {
struct enclosure *e;
int i;
if ((e = malloc(sizeof(struct enclosure))) == NULL)
err(1, "malloc");
INIT_ENCLOSURE(e);
SLIST_INSERT_HEAD(&ci->enclosures, e, next);
for (i = 0; atts[i] != NULL; i += 2) {
if (strcmp(atts[i], "url") == 0) {
if ((e->url = strdup(atts[i+1])) == NULL)
err(1, "strdup");
} else if (strcmp(atts[i], "length") == 0) {
if ((e->size = strdup(atts[i+1])) == NULL)
err(1, "strdup");
} else if (strcmp(atts[i], "type") == 0) {
if ((e->type = strdup(atts[i+1])) == NULL)
err(1, "strdup");
}
}
}
}
}
static void XMLCALL
rss_end_elt(void *user_data, const char *name)
{
switch (--depth) {
case 0:
if (strcasecmp(name, RSS_CHANNEL) == 0)
cf = NULL;
break;
case 1:
if (strcasecmp(name, RSS_ITEM) == 0) {
ci = NULL;
break;
}
if (data == NULL || cf == NULL)
break;
strchomp(data);
if (strcasecmp(name, RSS_TITLE) == 0)
cf->title = data;
else
free(data);
break;
case 2:
if (data == NULL || ci == NULL)
break;
strchomp(data);
if (strcasecmp(name, RSS_TITLE) == 0)
ci->title = data;
else if (strcasecmp(name, RSS_LINK) == 0)
ci->link = data;
else if (strcasecmp(name, RSS_DESCR) == 0)
ci->descr = data;
else if (strcasecmp(name, RSS_AUTHOR) == 0)
ci->author = data;
else if (strcasecmp(name, RSS_ID) == 0)
ci->id = data;
else if (strcasecmp(name, RSS_DATE) == 0) {
ci->date = data;
ci->time = rfc822_date(data);
} else if (strcasecmp(name, RSS_CATEGORY) == 0) {
struct category *cat;
if ((cat = malloc(sizeof(struct category))) == NULL)
err(1, "malloc");
cat->name = data;
SLIST_INSERT_HEAD(&ci->categories, cat, next);
} else
free(data);
}
read_data = 0;
data = NULL;
}
static void XMLCALL
atom_start_elt(void *user_data, const char *name, const char **atts)
{
switch (depth++) {
case 0:
if (strcasecmp(name, ATOM_TITLE) == 0)
read_data = 1;
else if (strcasecmp(name, ATOM_ENTRY) == 0) {
if ((ci = malloc(sizeof(struct item))) == NULL)
err(1, "malloc");
INIT_ITEM(ci);
SLIST_INSERT_HEAD(&cf->items, ci, next);
}
break;
case 1:
if (ci == NULL)
break;
if ((ci->title == NULL && strcasecmp(name, ATOM_TITLE) == 0)
|| (ci->descr == NULL && strcasecmp(name, ATOM_CONTENT) == 0)
|| (ci->id == NULL && strcasecmp(name, ATOM_ID) == 0)
|| (ci->date == NULL && strcasecmp(name, ATOM_UPDATED) == 0))
read_data = 1;
else if (ci->link == NULL && strcasecmp(name, ATOM_LINK) == 0) {
int i;
const char *s;
s = NULL;
for (i = 0; atts[i] != NULL; i += 2) {
if (strcasecmp(atts[i], "rel") == 0
&& strcmp(atts[i+1], "alternate") != 0)
break;
if (strcasecmp(atts[i], "type") == 0
&& strcasecmp(atts[i+1], "text/html") != 0)
break;
if (strcasecmp(atts[i], "href") == 0)
s = atts[i+1];
}
if (s == NULL)
break;
if ((ci->link = strdup(s)) == NULL)
err(1, "strdup");
strchomp(ci->link);
}
break;
case 2:
if (ci == NULL)
break;
if (ci->author == NULL && strcasecmp(name, ATOM_NAME) == 0)
read_data = 1;
break;
}
}
static void XMLCALL
atom_end_elt(void *user_data, const char *name)
{
switch (--depth) {
case 0:
if (strcasecmp(name, ATOM_ENTRY) == 0) {
ci = NULL;
break;
}
if (data == NULL || cf == NULL)
break;
strchomp(data);
if (strcasecmp(name, ATOM_TITLE) == 0)
cf->title = data;
else
free(data);
break;
case 1:
if (data == NULL || ci == NULL)
break;
strchomp(data);
if (strcasecmp(name, ATOM_TITLE) == 0)
ci->title = data;
else if (strcasecmp(name, ATOM_CONTENT) == 0)
ci->descr = data;
else if (strcasecmp(name, ATOM_ID) == 0)
ci->id = data;
else if (strcasecmp(name, ATOM_UPDATED) == 0) {
ci->date = data;
ci->time = rfc3339_date(data);
} else
free(data);
break;
case 2:
if (data == NULL || ci == NULL)
break;
strchomp(data);
if (strcasecmp(name, ATOM_NAME) == 0)
ci->author = data;
else
free(data);
}
read_data = 0;
data = NULL;
}
static struct item *
find_item(const char *id)
{
struct feed *f;
struct item *i;
SLIST_FOREACH(f, &feeds, next)
SLIST_FOREACH(i, &f->items, next)
if (strcmp(i->id, id) == 0)
return i;
return NULL;
}
static void XMLCALL
rdf_start_elt(void *user_data, const char *name, const char **atts)
{
switch (depth++) {
case 0:
if (strcasecmp(name, RDF_CHANNEL) == 0) {
if ((cf = malloc(sizeof(struct feed))) == NULL)
err(1, "malloc");
INIT_FEED(cf);
SLIST_INSERT_HEAD(&feeds, cf, next);
}
if (strcasecmp(name, RDF_ITEM) == 0) {
int i;
const char *s = NULL;
for (i = 0; atts[i] != NULL; i += 2)
if (strcasecmp(atts[i], RDF_ID) == 0)
s = atts[i+1];
if (s == NULL || (ci = find_item(s)) == NULL)
break;
}
break;
case 1:
if (cf != NULL) {
if (strcasecmp(name, RDF_TITLE) == 0)
read_data = 1;
}
if (ci == NULL)
break;
if ((ci->title == NULL && strcasecmp(name, RDF_TITLE) == 0)
|| (ci->link == NULL && strcasecmp(name, RSS_LINK) == 0)
|| (ci->descr == NULL && strcasecmp(name, RDF_DESCR) == 0)
|| (ci->author == NULL && strcasecmp(name, RDF_CREATOR) == 0)
|| (ci->date == NULL && strcasecmp(name, RDF_DATE) == 0)
|| strcasecmp(name, RDF_SUBJECT) == 0)
read_data = 1;
break;
case 3:
if (cf == NULL)
break;
if (strcasecmp(name, RDF_LIST_ITEM) == 0) {
int i;
const char *s = NULL;
for (i = 0; atts[i] != NULL; i += 2)
if (strcasecmp(atts[i], RDF_LIST_ID) == 0)
s = atts[i+1];
if (s == NULL)
break;
if ((ci = malloc(sizeof(struct item))) == NULL)
err(1, "malloc");
if ((ci->id = strdup(s)) == NULL)
err(1, "strdup");
SLIST_INSERT_HEAD(&cf->items, ci, next);
ci = NULL;
}
}
}
static void XMLCALL
rdf_end_elt(void *user_data, const char *name)
{
switch (--depth) {
case 0:
if (strcasecmp(name, RSS_CHANNEL) == 0)
cf = NULL;
if (strcasecmp(name, RDF_ITEM) == 0)
ci = NULL;
break;
case 1:
if (data == NULL)
break;
strchomp(data);
if (cf != NULL) {
if (strcasecmp(name, RDF_TITLE) == 0)
cf->title = data;
else
free(data);
} else if (ci != NULL) {
if (strcasecmp(name, RDF_TITLE) == 0)
ci->title = data;
else if (strcasecmp(name, RDF_LINK) == 0)
ci->link = data;
else if (strcasecmp(name, RDF_DESCR) == 0)
ci->descr = data;
else if (strcasecmp(name, RDF_CREATOR) == 0)
ci->author = data;
else if (strcasecmp(name, RDF_DATE) == 0) {
ci->date = data;
ci->time = rfc3339_date(data);
} else if (strcasecmp(name, RDF_SUBJECT) == 0) {
struct category *cat;
if ((cat = malloc(sizeof(struct category))) == NULL)
err(1, "malloc");
cat->name = data;
SLIST_INSERT_HEAD(&ci->categories, cat, next);
} else
free(data);
} else
free(data);
break;
}
read_data = 0;
data = NULL;
}
static void XMLCALL
data_handler(void *user_data, const XML_Char *s, int len)
{
if (read_data != 0)
if (insert_text(&data, s, (size_t)len) == -1)
err(1, "insert_text");
}
static void XMLCALL
start_elt(void *user_data, const char *name, const char **atts)
{
XML_Parser parser = (XML_Parser)user_data;
int i, supported;
cf = NULL;
ci = NULL;
depth = 0;
read_data = 0;
data = NULL;
SLIST_INIT(&feeds);
supported = 0;
if (strcasecmp(name, RSS) == 0) {
supported = 1;
XML_SetElementHandler(parser, rss_start_elt, rss_end_elt);
} else if (strcasecmp(name, ATOM_FEED) == 0) {
for (i = 0; atts[i] != NULL && !supported; i += 2)
if (strcasecmp(atts[i], "xmlns") == 0
&& strcmp(atts[i+1], ATOM_XMLNS) == 0) {
supported = 1;
XML_SetElementHandler(parser, atom_start_elt,
atom_end_elt);
if ((cf = malloc(sizeof(struct feed))) == NULL)
err(1, "malloc");
INIT_FEED(cf);
SLIST_INSERT_HEAD(&feeds, cf, next);
}
} else if (strcasecmp(name, RDF) == 0) {
supported = 1;
XML_SetElementHandler(parser, rdf_start_elt, rdf_end_elt);
}
if (!supported)
errx(1, "Unsupported feed");
XML_SetCharacterDataHandler(parser, data_handler);
}
struct feed *
parse_feeds(FILE *fin)
{
XML_Parser parser;
char buf[BUFSIZ];
size_t len;
int done;
parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, parser);
XML_SetStartElementHandler(parser, start_elt);
done = 0;
do {
len = fread(buf, sizeof(char), BUFSIZ, fin);
if (ferror(fin))
err(1, "Read error");
done = feof(fin);
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR)
errx(1, "Parse error at line %lu: %s",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
} while (!done);
XML_ParserFree(parser);
return SLIST_FIRST(&feeds);
}