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
|
#include "form.h"
#include "defs.h"
#include <libweb/html.h>
#include <stdio.h>
#include <string.h>
static int poweredby(struct html_node *const footer)
{
int ret = -1;
struct html_node *const a = html_node_alloc("a"), *p;
struct dynstr d;
dynstr_init(&d);
if (!a)
{
fprintf(stderr, "%s: html_node_alloc failed\n", __func__);
goto end;
}
else if (!(p = html_node_add_child(footer, "p")))
{
fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
goto end;
}
else if (html_node_add_attr(a, "href", PROJECT_URL))
{
fprintf(stderr, "%s: html_node_add_attr failed\n", __func__);
goto end;
}
else if (html_node_set_value(a, PROJECT_NAME))
{
fprintf(stderr, "%s: html_node_set_value a failed\n", __func__);
goto end;
}
else if (dynstr_append(&d, "Powered by "))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
goto end;
}
else if (html_serialize(a, &d))
{
fprintf(stderr, "%s: html_serialize failed\n", __func__);
goto end;
}
else if (html_node_set_value_unescaped(p, d.str))
{
fprintf(stderr, "%s: html_node_set_value_unescaped failed\n", __func__);
goto end;
}
ret = 0;
end:
dynstr_free(&d);
html_node_free(a);
return ret;
}
static int back(struct html_node *const footer)
{
struct html_node *const a = html_node_add_child(footer, "a");
if (!a)
{
fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
return -1;
}
else if (html_node_add_attr(a, "href", "/"))
{
fprintf(stderr, "%s: html_node_add_attr failed\n", __func__);
return -1;
}
else if (html_node_set_value(a, "Back to index"))
{
fprintf(stderr, "%s: html_node_set_value failed\n", __func__);
return -1;
}
return 0;
}
int form_footer(struct html_node *const n, const char *const resource)
{
struct html_node *const footer = html_node_add_child(n, "footer");
if (!footer)
{
fprintf(stderr, "%s: html_node_add_child failed\n", __func__);
return -1;
}
else if (strcmp(resource, "/")
&& strcmp(resource, "/index.html")
&& back(footer))
{
fprintf(stderr, "%s: back failed\n", __func__);
return -1;
}
else if (poweredby(footer))
{
fprintf(stderr, "%s: poweredby failed\n", __func__);
return -1;
}
return 0;
}
|