webdump

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/webdump.git
Log | Files | Refs | README | LICENSE

commit 6dae546b7c15b859321849c8b7b7294e6d916adc
parent db8bb6f1a0e1ca29e8cdfd8a6c098fc1076fea80
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat, 22 Jul 2017 15:46:15 +0200

simplify cdata handler, show src attribute (disabled for now)

Diffstat:
main.c | 48++++++++++++++++++------------------------------
1 file changed, 18 insertions(+), 30 deletions(-)

diff --git a/main.c b/main.c @@ -14,7 +14,6 @@ static XMLParser parser; static int isdatastart; -static int iscdatastart; struct node { char tag[256]; @@ -23,6 +22,8 @@ struct node { int isblock; }; +static char src[4096]; /* src or href attribute */ + #define MAX_DEPTH 256 static struct node nodes[MAX_DEPTH]; static int curnode; @@ -63,23 +64,9 @@ static char *blocktags[] = { }; static void -xmlcdatastart(XMLParser *p) -{ - iscdatastart = 1; -} - -static void xmlcdata(XMLParser *p, const char *data, size_t datalen) { - /* TODO */ -/* printf("%s", data);*/ - iscdatastart = 0; -} - -static void -xmlcdataend(XMLParser *p) -{ - iscdatastart = 0; + fputs(data, stdout); } static void @@ -112,9 +99,8 @@ xmldata(XMLParser *p, const char *data, size_t datalen) } if (cur->ispre) { - for (; *s; s++) { + for (; *s; s++) putchar(*s); - } } else { for (; *s; s++) { if (isspace(*s)) @@ -150,6 +136,7 @@ xmltagstart(XMLParser *p, const char *tag, size_t taglen) int i; memset(cur, 0, sizeof(*cur)); + src[0] = '\0'; /* src, href */ strlcpy(cur->tag, tag, sizeof(cur->tag)); for (i = 0; i < sizeof(pretags) / sizeof(*pretags); i++) { @@ -180,9 +167,14 @@ xmltagend(XMLParser *p, const char *tag, size_t taglen, int isshort) if (curnode) curnode--; - cur = &nodes[curnode]; +#if 0 + if (src[0]) + printf(" [%s]", src); + src[0] = '\0'; +#endif + if (cur->isblock) fputs("\n", stdout); @@ -205,7 +197,8 @@ xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) cur = &nodes[curnode]; if (cur->isblock) - putchar('\n'); + fputs("\n", stdout); + if (!strcmp(cur->tag, "li")) { /* indent nested list items */ for (i = curnode; i; i--) { @@ -224,7 +217,7 @@ xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) } if (curnode >= MAX_DEPTH - 2) - errx(1, "max depth reached: %d\n", curnode); + errx(1, "max tag depth reached: %d\n", curnode); curnode++; } @@ -232,15 +225,12 @@ static void xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, size_t namelen, const char *value, size_t valuelen) { -/* if (!strcmp(tag, "a") && !strcmp(name, "href") && valuelen) - printf(" [%s]", value);*/ + if (!strcmp(tag, "a") && !strcmp(name, "href") && valuelen) + strlcpy(src, value, sizeof(src)); /* TODO: check alt and title attr also? */ -/* if (!strcmp(tag, "img") && !strcmp(name, "src") && valuelen) { - printf(" [%s]", value); - }*/ - -/* printf(" %s=\"%s\"", name, value);*/ + if (!strcmp(tag, "img") && !strcmp(name, "src") && valuelen) + strlcpy(src, value, sizeof(src)); } int @@ -250,9 +240,7 @@ main(void) err(1, "pledge"); parser.xmlattr = xmlattr; - parser.xmlcdatastart = xmlcdatastart; parser.xmlcdata = xmlcdata; - parser.xmlcdataend = xmlcdataend; parser.xmldatastart = xmldatastart; parser.xmldata = xmldata; parser.xmldataend = xmldataend;