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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
|
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include "util.h"
#include "xml.h"
#define ISINCONTENT(ctx) ((ctx).iscontent && !((ctx).iscontenttag))
#define ISCONTENTTAG(ctx) (!((ctx).iscontent) && (ctx).iscontenttag)
/* string and byte-length */
#define STRP(s) s,sizeof(s)-1
enum FeedType {
FeedTypeNone = 0,
FeedTypeRSS = 1,
FeedTypeAtom = 2
};
enum ContentType {
ContentTypeNone = 0,
ContentTypePlain = 1,
ContentTypeHTML = 2
};
static const char *contenttypes[] = { "", "plain", "html" };
/* String data / memory pool */
typedef struct string {
char *data; /* data */
size_t len; /* string length */
size_t bufsiz; /* allocated size */
} String;
/* NOTE: the order of these fields (content, date, author) indicate the
* priority to use them, from least important to high. */
enum TagId {
TagUnknown = 0,
/* RSS */
RSSTagDcdate, RSSTagPubdate,
RSSTagTitle,
RSSTagMediaDescription, RSSTagDescription, RSSTagContentEncoded,
RSSTagGuid,
/* must be defined after GUID, because it can be a link (isPermaLink) */
RSSTagLink,
RSSTagAuthor, RSSTagDccreator,
/* Atom */
AtomTagUpdated, AtomTagPublished,
AtomTagTitle,
AtomTagMediaDescription, AtomTagSummary, AtomTagContent,
AtomTagId,
AtomTagLink,
AtomTagLinkAlternate,
AtomTagAuthor,
TagLast
};
typedef struct feedtag {
char *name; /* name of tag to match */
size_t len; /* len of `name` */
enum TagId id; /* unique ID */
} FeedTag;
typedef struct field {
String str;
enum TagId tagid; /* tagid set previously, used for tag priority */
} FeedField;
enum {
FeedFieldTime = 0, FeedFieldTitle, FeedFieldLink, FeedFieldContent,
FeedFieldId, FeedFieldAuthor, FeedFieldLast
};
typedef struct feedcontext {
String *field; /* current FeedItem field String */
FeedField fields[FeedFieldLast]; /* data for current item */
enum TagId tagid; /* unique number for parsed tag */
int iscontent; /* in content data */
int iscontenttag; /* in content tag */
enum ContentType contenttype; /* content-type for item */
enum FeedType feedtype;
int attrcount; /* count item HTML element attributes */
} FeedContext;
static long long datetounix(long long, int, int, int, int, int);
static enum TagId gettag(enum FeedType, const char *, size_t);
static long long gettzoffset(const char *);
static int isattr(const char *, size_t, const char *, size_t);
static int istag(const char *, size_t, const char *, size_t);
static int parsetime(const char *, time_t *);
static void printfields(void);
static void string_append(String *, const char *, size_t);
static void string_buffer_realloc(String *, size_t);
static void string_clear(String *);
static void string_print_encoded(String *);
static void string_print_timestamp(String *);
static void string_print_trimmed(String *);
static void string_print_uri(String *);
static void xmlattr(XMLParser *, const char *, size_t, const char *, size_t,
const char *, size_t);
static void xmlattrend(XMLParser *, const char *, size_t, const char *,
size_t);
static void xmlattrstart(XMLParser *, const char *, size_t, const char *,
size_t);
static void xmlcdata(XMLParser *, const char *, size_t);
static void xmldata(XMLParser *, const char *, size_t);
static void xmldataentity(XMLParser *, const char *, size_t);
static void xmltagend(XMLParser *, const char *, size_t, int);
static void xmltagstart(XMLParser *, const char *, size_t);
static void xmltagstartparsed(XMLParser *, const char *, size_t, int);
/* map tag name to tagid */
/* RSS, alphabetical order */
static FeedTag rsstags[] = {
{ STRP("author"), RSSTagAuthor },
{ STRP("content:encoded"), RSSTagContentEncoded },
{ STRP("dc:creator"), RSSTagDccreator },
{ STRP("dc:date"), RSSTagDcdate },
{ STRP("description"), RSSTagDescription },
{ STRP("guid"), RSSTagGuid },
{ STRP("link"), RSSTagLink },
{ STRP("media:description"), RSSTagMediaDescription },
{ STRP("pubdate"), RSSTagPubdate },
{ STRP("title"), RSSTagTitle }
};
/* Atom, alphabetical order */
static FeedTag atomtags[] = {
/* <author><name></name></author> */
{ STRP("author"), AtomTagAuthor },
{ STRP("content"), AtomTagContent },
{ STRP("id"), AtomTagId },
/* Atom: <link href="" />, RSS has <link></link> */
{ STRP("link"), AtomTagLink },
{ STRP("media:description"), AtomTagMediaDescription },
{ STRP("published"), AtomTagPublished },
{ STRP("summary"), AtomTagSummary },
{ STRP("title"), AtomTagTitle },
{ STRP("updated"), AtomTagUpdated }
};
/* map tagid type to RSS/Atom field
NOTE: all tags must be defined */
static int fieldmap[TagLast] = {
[TagUnknown] = -1,
/* RSS */
[RSSTagDcdate] = FeedFieldTime,
[RSSTagPubdate] = FeedFieldTime,
[RSSTagTitle] = FeedFieldTitle,
[RSSTagMediaDescription] = FeedFieldContent,
[RSSTagDescription] = FeedFieldContent,
[RSSTagContentEncoded] = FeedFieldContent,
[RSSTagGuid] = FeedFieldId,
[RSSTagLink] = FeedFieldLink,
[RSSTagAuthor] = FeedFieldAuthor,
[RSSTagDccreator] = FeedFieldAuthor,
/* Atom */
[AtomTagUpdated] = FeedFieldTime,
[AtomTagPublished] = FeedFieldTime,
[AtomTagTitle] = FeedFieldTitle,
[AtomTagMediaDescription] = FeedFieldContent,
[AtomTagSummary] = FeedFieldContent,
[AtomTagContent] = FeedFieldContent,
[AtomTagId] = FeedFieldId,
[AtomTagLink] = -1,
[AtomTagLinkAlternate] = FeedFieldLink,
[AtomTagAuthor] = FeedFieldAuthor
};
static const int FieldSeparator = '\t';
static const char *baseurl = "";
static FeedContext ctx;
static XMLParser parser; /* XML parser state */
static String atomlink;
static enum TagId atomlinktype;
static int rssidpermalink;
/* Unique tagid for parsed tag name. */
static enum TagId
gettag(enum FeedType feedtype, const char *name, size_t namelen)
{
size_t i;
switch (feedtype) {
case FeedTypeRSS:
/* optimization: these are always non-matching */
if (namelen < 4 || namelen > 17)
return TagUnknown;
for (i = 0; i < sizeof(rsstags) / sizeof(rsstags[0]); i++)
if (istag(rsstags[i].name, rsstags[i].len, name, namelen))
return rsstags[i].id;
break;
case FeedTypeAtom:
/* optimization: these are always non-matching */
if (namelen < 2 || namelen > 17)
return TagUnknown;
for (i = 0; i < sizeof(atomtags) / sizeof(atomtags[0]); i++)
if (istag(atomtags[i].name, atomtags[i].len, name, namelen))
return atomtags[i].id;
break;
default:
break;
}
return TagUnknown;
}
static char *
ltrim(const char *s)
{
for (; *s && isspace((unsigned char)*s); s++)
;
return (char *)s;
}
static char *
rtrim(const char *s)
{
const char *e;
for (e = s + strlen(s); e > s && isspace((unsigned char)*(e - 1)); e--)
;
return (char *)e;
}
/* Clear string only; don't free, prevents unnecessary reallocation. */
static void
string_clear(String *s)
{
if (s->data)
s->data[0] = '\0';
s->len = 0;
}
static void
string_buffer_realloc(String *s, size_t newlen)
{
size_t alloclen;
for (alloclen = 64; alloclen <= newlen; alloclen *= 2)
;
if (!(s->data = realloc(s->data, alloclen)))
err(1, "realloc");
s->bufsiz = alloclen;
}
static void
string_append(String *s, const char *data, size_t len)
{
if (!len)
return;
/* check if allocation is necesary, don't shrink buffer,
* should be more than bufsiz ofcourse. */
if (s->len + len >= s->bufsiz)
string_buffer_realloc(s, s->len + len + 1);
memcpy(s->data + s->len, data, len);
s->len += len;
s->data[s->len] = '\0';
}
/* Print text, encode TABs, newlines and '\', remove other whitespace.
* Remove leading and trailing whitespace. */
static void
string_print_encoded(String *s)
{
const char *p, *e;
if (!s->data || !s->len)
return;
p = ltrim(s->data);
e = rtrim(p);
for (; *p && p != e; p++) {
switch (*p) {
case '\n': fputs("\\n", stdout); break;
case '\\': fputs("\\\\", stdout); break;
case '\t': fputs("\\t", stdout); break;
default:
/* ignore control chars */
if (!iscntrl((unsigned char)*p))
putchar(*p);
break;
}
}
}
/* Print text, replace TABs, carriage return and other whitespace with ' '.
* Other control chars are removed. Remove leading and trailing whitespace. */
static void
string_print_trimmed(String *s)
{
char *p, *e;
if (!s->data || !s->len)
return;
p = ltrim(s->data);
e = rtrim(p);
for (; *p && p != e; p++) {
if (isspace((unsigned char)*p))
putchar(' '); /* any whitespace to space */
else if (!iscntrl((unsigned char)*p))
/* ignore other control chars */
putchar(*p);
}
}
/* always print absolute urls (using global baseurl) */
void
string_print_uri(String *s)
{
char link[4096], *p, *e;
int c;
if (!s->data || !s->len)
return;
p = ltrim(s->data);
e = rtrim(p);
c = *e;
*e = '\0';
if (absuri(link, sizeof(link), p, baseurl) != -1)
fputs(link, stdout);
*e = c; /* restore */
}
/* print as UNIX timestamp, print nothing if the parsed time is invalid */
void
string_print_timestamp(String *s)
{
time_t t;
if (!s->data || !s->len)
return;
if (parsetime(s->data, &t) != -1)
printf("%lld", (long long)t);
}
long long
datetounix(long long year, int mon, int day, int hour, int min, int sec)
{
static const int secs_through_month[] = {
0, 31 * 86400, 59 * 86400, 90 * 86400,
120 * 86400, 151 * 86400, 181 * 86400, 212 * 86400,
243 * 86400, 273 * 86400, 304 * 86400, 334 * 86400 };
int is_leap = 0, cycles, centuries = 0, leaps = 0, rem;
long long t;
if (year - 2ULL <= 136) {
leaps = (year - 68) >> 2;
if (!((year - 68) & 3)) {
leaps--;
is_leap = 1;
} else {
is_leap = 0;
}
t = 31536000 * (year - 70) + 86400 * leaps;
} else {
cycles = (year - 100) / 400;
rem = (year - 100) % 400;
if (rem < 0) {
cycles--;
rem += 400;
}
if (!rem) {
is_leap = 1;
} else {
if (rem >= 300)
centuries = 3, rem -= 300;
else if (rem >= 200)
centuries = 2, rem -= 200;
else if (rem >= 100)
centuries = 1, rem -= 100;
if (rem) {
leaps = rem / 4U;
rem %= 4U;
is_leap = !rem;
}
}
leaps += 97 * cycles + 24 * centuries - is_leap;
t = (year - 100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
}
t += secs_through_month[mon];
if (is_leap && mon >= 2)
t += 86400;
t += 86400LL * (day - 1);
t += 3600LL * hour;
t += 60LL * min;
t += sec;
return t;
}
/* Get timezone from string, return time offset in seconds from UTC.
* NOTE: only parses timezones in RFC-822, other timezones are ambiguous
* anyway. If needed you can add some yourself, like "cest", "cet" etc. */
static long long
gettzoffset(const char *s)
{
static struct {
char *name;
size_t len;
const int offhour;
} tzones[] = {
{ STRP("CDT"), -5 * 3600 },
{ STRP("CST"), -6 * 3600 },
{ STRP("EDT"), -4 * 3600 },
{ STRP("EST"), -5 * 3600 },
{ STRP("MDT"), -6 * 3600 },
{ STRP("MST"), -7 * 3600 },
{ STRP("PDT"), -7 * 3600 },
{ STRP("PST"), -8 * 3600 },
};
const char *p;
int tzhour = 0, tzmin = 0, c;
size_t i, namelen;
for (; *s && isspace((unsigned char)*s); s++)
;
switch (*s) {
case '-': /* offset */
case '+':
for (i = 0, p = s + 1; i < 2 && *p && isdigit((unsigned char)*p); i++, p++)
tzhour = (tzhour * 10) + (*p - '0');
if (*p == ':')
p++;
for (i = 0; i < 2 && *p && isdigit((unsigned char)*p); i++, p++)
tzmin = (tzmin * 10) + (*p - '0');
return ((tzhour * 3600) + (tzmin * 60)) * (s[0] == '-' ? -1 : 1);
default: /* timezone name */
for (i = 0; *s && isalpha((unsigned char)s[i]); i++)
;
namelen = i; /* end of name */
/* optimization: these are always non-matching */
if (namelen < 1 || namelen > 3)
return 0;
/* ANSI and military zones, defined wrong in RFC822,
see RFC2822 4.3 page 32.
NOTE: zone J is ambiguous (unused/local observed time) and
handled as UTC+0. */
if (namelen == 1) {
c = (int)*s;
/* NOTE: J is not handled: unused / observer local time */
if (c >= 'A' && c <= 'I')
return c - 'A' + 1; /* +1 through +9 */
else if (c >= 'K' && c <= 'M')
return c - 'K' + 10; /* 10 through 12 */
else if (c >= 'N' && c <= 'Y')
return ('Y' - c) - 12; /* -1 through -12 */
else
return 0;
}
/* compare tz and adjust offset relative to UTC */
for (i = 0; i < sizeof(tzones) / sizeof(*tzones); i++) {
if (tzones[i].len == namelen &&
!strncmp(s, tzones[i].name, namelen))
return tzones[i].offhour;
}
}
return 0;
}
static int
parsetime(const char *s, time_t *tp)
{
static struct {
char *name;
int len;
} mons[] = {
{ STRP("January"), },
{ STRP("February"), },
{ STRP("March"), },
{ STRP("April"), },
{ STRP("May"), },
{ STRP("June"), },
{ STRP("July"), },
{ STRP("August"), },
{ STRP("September"), },
{ STRP("October"), },
{ STRP("November"), },
{ STRP("December"), },
};
int va[6] = { 0 }, i, j, v, vi;
size_t m;
for (; *s && isspace((unsigned char)*s); s++)
;
if (!isdigit((unsigned char)*s) && !isalpha((unsigned char)*s))
return -1;
if (strspn(s, "0123456789") == 4) {
/* format "%Y-%m-%d %H:%M:%S" or "%Y-%m-%dT%H:%M:%S" */
vi = 0;
} else {
/* format: "[%a, ]%d %b %Y %H:%M:%S" */
/* parse "[%a, ]%d %b %Y " part, then use time parsing as above */
for (; *s && isalpha((unsigned char)*s); s++)
;
for (; *s && isspace((unsigned char)*s); s++)
;
if (*s == ',')
s++;
for (; *s && isspace((unsigned char)*s); s++)
;
for (v = 0, i = 0; *s && i < 4 && isdigit((unsigned char)*s); s++, i++)
v = (v * 10) + (*s - '0');
va[2] = v; /* day */
for (; *s && isspace((unsigned char)*s); s++)
;
/* end of word month */
for (j = 0; *s && isalpha((unsigned char)s[j]); j++)
;
/* check month name */
if (j < 3 || j > 9)
return -1; /* month cannot match */
for (m = 0; m < sizeof(mons) / sizeof(*mons); m++) {
/* abbreviation (3 length) or long name */
if ((j == 3 || j == mons[m].len) &&
!strncasecmp(mons[m].name, s, j)) {
va[1] = m + 1;
s += j;
break;
}
}
if (m >= 12)
return -1; /* no month found */
for (; *s && isspace((unsigned char)*s); s++)
;
for (v = 0, i = 0; *s && i < 4 && isdigit((unsigned char)*s); s++, i++)
v = (v * 10) + (*s - '0');
va[0] = v; /* year */
for (; *s && isspace((unsigned char)*s); s++)
;
/* parse only regular time part, see below */
vi = 3;
}
/* parse time part */
for (; *s && vi < 6; vi++) {
for (i = 0, v = 0; *s && i < 4 && isdigit((unsigned char)*s); s++, i++)
v = (v * 10) + (*s - '0');
va[vi] = v;
if ((vi < 2 && *s == '-') ||
(vi == 2 && (*s == 'T' || isspace((unsigned char)*s))) ||
(vi > 2 && *s == ':'))
s++;
}
/* skip milliseconds in for example: "%Y-%m-%dT%H:%M:%S.000Z" */
if (*s == '.') {
for (s++; *s && isdigit((unsigned char)*s); s++)
;
}
/* invalid range */
if (va[0] < 0 || va[0] > 9999 ||
va[1] < 1 || va[1] > 12 ||
va[2] < 1 || va[2] > 31 ||
va[3] < 0 || va[3] > 23 ||
va[4] < 0 || va[4] > 59 ||
va[5] < 0 || va[5] > 59)
return -1;
if (tp)
*tp = datetounix(va[0] - 1900, va[1] - 1, va[2], va[3], va[4], va[5]) -
gettzoffset(s);
return 0;
}
static void
printfields(void)
{
string_print_timestamp(&ctx.fields[FeedFieldTime].str);
putchar(FieldSeparator);
string_print_trimmed(&ctx.fields[FeedFieldTitle].str);
putchar(FieldSeparator);
string_print_uri(&ctx.fields[FeedFieldLink].str);
putchar(FieldSeparator);
string_print_encoded(&ctx.fields[FeedFieldContent].str);
putchar(FieldSeparator);
fputs(contenttypes[ctx.contenttype], stdout);
putchar(FieldSeparator);
string_print_trimmed(&ctx.fields[FeedFieldId].str);
putchar(FieldSeparator);
string_print_trimmed(&ctx.fields[FeedFieldAuthor].str);
putchar('\n');
}
static int
istag(const char *name, size_t len, const char *name2, size_t len2)
{
return (len == len2 && !strcasecmp(name, name2));
}
static int
isattr(const char *name, size_t len, const char *name2, size_t len2)
{
return (len == len2 && !strcasecmp(name, name2));
}
static void
xmlattr(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl,
const char *v, size_t vl)
{
/* handles transforming inline XML to data */
if (ISINCONTENT(ctx)) {
if (ctx.contenttype == ContentTypeHTML)
xmldata(p, v, vl);
return;
}
if (ctx.feedtype == FeedTypeRSS) {
if (ctx.tagid == RSSTagGuid) {
if (isattr(n, nl, STRP("ispermalink")) &&
!isattr(v, vl, STRP("true")))
rssidpermalink = 0;
}
} else if (ctx.feedtype == FeedTypeAtom) {
if (ISCONTENTTAG(ctx)) {
if (isattr(n, nl, STRP("type")) &&
(isattr(v, vl, STRP("xhtml")) ||
isattr(v, vl, STRP("text/xhtml")) ||
isattr(v, vl, STRP("html")) ||
isattr(v, vl, STRP("text/html"))))
{
ctx.contenttype = ContentTypeHTML;
}
} else if (ctx.tagid == AtomTagLink &&
isattr(n, nl, STRP("rel"))) {
/* empty or "alternate": other types could be
"enclosure", "related", "self" or "via" */
if (!vl || isattr(v, vl, STRP("alternate")))
atomlinktype = AtomTagLinkAlternate;
else
atomlinktype = TagUnknown;
} else if (ctx.tagid == AtomTagLink &&
isattr(n, nl, STRP("href"))) {
string_append(&atomlink, v, vl);
}
}
}
static void
xmlattrend(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl)
{
if (!ISINCONTENT(ctx) || ctx.contenttype != ContentTypeHTML)
return;
/* handles transforming inline XML to data */
xmldata(p, "\"", 1);
ctx.attrcount = 0;
}
static void
xmlattrstart(XMLParser *p, const char *t, size_t tl, const char *n, size_t nl)
{
if (!ISINCONTENT(ctx) || ctx.contenttype != ContentTypeHTML)
return;
/* handles transforming inline XML to data */
if (!ctx.attrcount)
xmldata(p, " ", 1);
ctx.attrcount++;
xmldata(p, n, nl);
xmldata(p, "=\"", 2);
}
static void
xmlcdata(XMLParser *p, const char *s, size_t len)
{
if (!ctx.field)
return;
string_append(ctx.field, s, len);
}
/* NOTE: this handler can be called multiple times if the data in this
* block is bigger than the buffer. */
static void
xmldata(XMLParser *p, const char *s, size_t len)
{
if (!ctx.field)
return;
/* add only data from <name> inside <author> tag
* or any other non-<author> tag */
if (ctx.tagid != AtomTagAuthor || istag(p->tag, p->taglen, "name", 4))
string_append(ctx.field, s, len);
}
static void
xmldataentity(XMLParser *p, const char *data, size_t datalen)
{
char buf[16];
ssize_t len;
if (!ctx.field)
return;
/* try to translate entity, else just pass as data to
* xml_data_handler. */
if ((len = xml_entitytostr(data, buf, sizeof(buf))) > 0)
xmldata(p, buf, (size_t)len);
else
xmldata(p, data, datalen);
}
static void
xmltagstart(XMLParser *p, const char *t, size_t tl)
{
enum TagId tagid;
if (ISINCONTENT(ctx)) {
ctx.attrcount = 0;
if (ctx.contenttype == ContentTypeHTML) {
xmldata(p, "<", 1);
xmldata(p, t, tl);
}
return;
}
/* start of RSS or Atom item / entry */
if (ctx.feedtype == FeedTypeNone) {
if (istag(t, tl, STRP("entry"))) {
/* Atom */
ctx.feedtype = FeedTypeAtom;
/* default content type for Atom */
ctx.contenttype = ContentTypePlain;
} else if (istag(t, tl, STRP("item"))) {
/* RSS */
ctx.feedtype = FeedTypeRSS;
/* default content type for RSS */
ctx.contenttype = ContentTypeHTML;
}
return;
}
/* field tagid already set, nested tags are not allowed: return */
if (ctx.tagid)
return;
/* in item */
tagid = gettag(ctx.feedtype, t, tl);
ctx.tagid = tagid;
/* without a rel attribute the default link type is "alternate" */
if (tagid == AtomTagLink) {
atomlinktype = AtomTagLinkAlternate;
string_clear(&atomlink); /* reuse and clear temporary link */
} else if (tagid == RSSTagGuid) {
/* without a ispermalink attribute the default value is "true" */
rssidpermalink = 1;
}
/* map tag type to field: unknown or lesser priority is ignored,
when tags of the same type are repeated only the first is used. */
if (fieldmap[tagid] == -1 || tagid <= ctx.fields[fieldmap[tagid]].tagid) {
ctx.field = NULL;
return;
}
ctx.iscontenttag = (fieldmap[ctx.tagid] == FeedFieldContent);
ctx.field = &(ctx.fields[fieldmap[ctx.tagid]].str);
ctx.fields[fieldmap[ctx.tagid]].tagid = tagid;
/* clear field */
string_clear(ctx.field);
}
static void
xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort)
{
if (ctx.iscontenttag) {
ctx.iscontent = 1;
ctx.iscontenttag = 0;
return;
}
/* don't read field value in Atom <link> tag */
if (ctx.tagid == AtomTagLink)
ctx.field = NULL;
if (!ISINCONTENT(ctx) || ctx.contenttype != ContentTypeHTML)
return;
if (isshort)
xmldata(p, "/>", 2);
else
xmldata(p, ">", 1);
}
static void
xmltagend(XMLParser *p, const char *t, size_t tl, int isshort)
{
size_t i;
if (ctx.feedtype == FeedTypeNone)
return;
if (ISINCONTENT(ctx)) {
/* not close content field */
if (gettag(ctx.feedtype, t, tl) != ctx.tagid) {
if (!isshort && ctx.contenttype == ContentTypeHTML) {
xmldata(p, "</", 2);
xmldata(p, t, tl);
xmldata(p, ">", 1);
}
return;
}
} else if (ctx.tagid == AtomTagLink) {
/* map tag type to field: unknown or lesser priority is ignored,
when tags of the same type are repeated only the first is used. */
if (atomlinktype && atomlinktype > ctx.fields[fieldmap[atomlinktype]].tagid) {
string_append(&ctx.fields[fieldmap[atomlinktype]].str,
atomlink.data, atomlink.len);
ctx.fields[fieldmap[atomlinktype]].tagid = atomlinktype;
}
} else if (ctx.tagid == RSSTagGuid && rssidpermalink) {
if (ctx.tagid > ctx.fields[FeedFieldLink].tagid) {
string_clear(&ctx.fields[FeedFieldLink].str);
string_append(&ctx.fields[FeedFieldLink].str,
ctx.fields[FeedFieldId].str.data,
ctx.fields[FeedFieldId].str.len);
ctx.fields[FeedFieldLink].tagid = ctx.tagid;
}
} else if (!ctx.tagid && ((ctx.feedtype == FeedTypeAtom &&
istag(t, tl, STRP("entry"))) || /* Atom */
(ctx.feedtype == FeedTypeRSS &&
istag(t, tl, STRP("item"))))) /* RSS */
{
/* end of RSS or Atom entry / item */
printfields();
/* clear strings */
for (i = 0; i < FeedFieldLast; i++) {
string_clear(&ctx.fields[i].str);
ctx.fields[i].tagid = TagUnknown;
}
ctx.contenttype = ContentTypeNone;
/* allow parsing of Atom and RSS concatenated in one XML stream. */
ctx.feedtype = FeedTypeNone;
} else if (!ctx.tagid ||
gettag(ctx.feedtype, t, tl) != ctx.tagid) {
/* not end of field */
return;
}
/* close field */
ctx.iscontent = 0;
ctx.tagid = TagUnknown;
ctx.field = NULL;
}
int
main(int argc, char *argv[])
{
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
if (argc > 1)
baseurl = argv[1];
parser.xmlattr = xmlattr;
parser.xmlattrend = xmlattrend;
parser.xmlattrstart = xmlattrstart;
parser.xmlcdata = xmlcdata;
parser.xmldata = xmldata;
parser.xmldataentity = xmldataentity;
parser.xmltagend = xmltagend;
parser.xmltagstart = xmltagstart;
parser.xmltagstartparsed = xmltagstartparsed;
parser.getnext = getchar;
xml_parse(&parser);
return 0;
}
|