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
|
#include <stdio.h>
#include <unistd.h>
#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 <file>");
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;
}
|