#include #include #include "mem.h" #include "dom.h" #include "tags.h" static void node_print(const struct node *n, const char *prefix, int last) { const char *connector = last ? "└── " : "├── "; const char *extension = last ? " " : "│ "; char new_prefix[256]; struct node *c; printf("%s%s", prefix, connector); if (n->tag == TAG_TEXT) printf("%.*s\n", (int)n->textlen, n->text); else printf("%s\n", tag_to_str(n->tag)); snprintf(new_prefix, sizeof(new_prefix), "%s%s", prefix, extension); for (c = n->first_child; c; c = c->next_sibling) node_print(c, new_prefix, c->next_sibling == NULL); } void dom_print(const struct node *root) { struct node *c; printf("%s\n", tag_to_str(root->tag)); for (c = root->first_child; c; c = c->next_sibling) node_print(c, "", c->next_sibling == NULL); } int main(int argc, char *argv[]) { struct node *root; if (argc < 2) errx(1, "usage: glacier "); unveil(argv[1], "r"); unveil(NULL, NULL); pledge("stdio rpath", NULL); FILE *file; char *html; long len; file = fopen("test.html", "rb"); fseek(file, 0, SEEK_END); len = ftell(file); fseek(file, 0, SEEK_SET); html = MALLOC((size_t)len + 1); fread(html, 1, len, file); html[len] = '\0'; fclose(file); root = dom_init(html); dom_print(root); dom_free(); free(html); return 0; }