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 ce36531a689dad978e803008ff0600aa7984e380
parent b089e00f076e857bbd8f9f1c316ab55891916aad
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun, 22 Sep 2019 19:45:06 +0200

absolute link references, add base href argument to program

make link references an #ifdef LINKREFS for now

Diffstat:
webdump.c | 53++++++++++++++++++++++++++---------------------------
1 file changed, 26 insertions(+), 27 deletions(-)

diff --git a/webdump.c b/webdump.c @@ -30,7 +30,7 @@ struct uri { static int termwidth = 72; -#if 0 +#ifdef LINKREFS /* linked-list of link references */ struct linkref { char *type; @@ -75,7 +75,7 @@ typedef struct string { int absuri(char *, size_t, const char *, const char *); int parseuri(const char *, struct uri *, int); -static char *basehref = "https://codemadness.org"; +static char *basehref = ""; static char src[4096]; /* src or href attribute */ @@ -413,14 +413,6 @@ xmlcdata(XMLParser *p, const char *data, size_t datalen) printsafe(data); } -#if 0 -static void -xmldatastart(XMLParser *p) -{ -// printf("DEBUG: %s\n", __func__); -} -#endif - static void xmldataend(XMLParser *p) { @@ -589,21 +581,26 @@ xmltagstartparsed(XMLParser *p, const char *t, size_t tl, int isshort) cur = &nodes[curnode]; -#ifdef maybe +#ifdef LINKREFS /* show links as reference at the bottom */ if (src[0]) { - printf(" [%d]", ++linkcount); - if (!strcasecmp(t, "img") || !strcasecmp(t, "video") || - !strcasecmp(t, "audio")) - printf("[%s]", t); - /* TODO: check allocation */ - if (!links_head) - links_cur = links_head = ecalloc(1, sizeof(*links_head)); - else - links_cur = links_cur->next = ecalloc(1, sizeof(*links_head)); - links_cur->type = estrdup(t); - /* TODO: absuri */ - links_cur->url = estrdup(src); + char absurl[1024]; + if (absuri(absurl, sizeof(absurl), src, basehref) != -1) { + if (!links_head) + links_cur = links_head = ecalloc(1, sizeof(*links_head)); + else + links_cur = links_cur->next = ecalloc(1, sizeof(*links_head)); + links_cur->url = estrdup(absurl); + + printf(" [%d]", ++linkcount); + if (!strcasecmp(t, "img") || !strcasecmp(t, "video") || + !strcasecmp(t, "audio")) { + links_cur->type = estrdup(t); + } else { + links_cur->type = estrdup("link"); + } + + } } src[0] = '\0'; #endif @@ -664,7 +661,7 @@ xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, strlcpy(src, value, sizeof(src)); } -#ifdef maybe +#ifdef LINKREFS void printlinkrefs(void) { @@ -679,15 +676,17 @@ printlinkrefs(void) #endif int -main(void) +main(int argc, char **argv) { if (pledge("stdio", NULL) < 0) err(1, "pledge"); + if (argc > 1) + basehref = argv[1]; + parser.xmlattr = xmlattr; parser.xmlcdata = xmlcdata; parser.xmldata = xmldata; -// parser.xmldatastart = xmldatastart; parser.xmldataend = xmldataend; parser.xmldataentity = xmldataentity; parser.xmltagstart = xmltagstart; @@ -697,7 +696,7 @@ main(void) parser.getnext = getchar; xml_parse(&parser); -#ifdef maybe +#ifdef LINKREFS printlinkrefs(); #endif putchar('\n');