aboutsummaryrefslogtreecommitdiff
path: root/html.c
blob: f06b3366dc9ee834a57f14ce463d1553e9a8c32c (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#define _POSIX_C_SOURCE 200809L

#include "libweb/html.h"
#include <dynstr.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct html_node
{
    struct html_attribute
    {
        char *attr, *value;
    } *attrs;

    char *element, *value;
    size_t n;
    struct html_node *child, *sibling;
};

static int serialize_siblings(struct dynstr *, const struct html_node *,
    unsigned);

char *html_encode(const char *s)
{
    struct dynstr d;

    dynstr_init(&d);

    if (!*s && dynstr_append(&d, ""))
    {
        fprintf(stderr, "%s: dynstr_append empty failed\n", __func__);
        goto failure;
    }

    while (*s)
    {
        static const struct esc
        {
            char c;
            const char *str;
        } esc[] =
        {
            {.c = '>', .str = "&gt;"},
            {.c = '<', .str = "&lt;"},
            {.c = '&', .str = "&amp;"},
            {.c = '\"', .str = "&quot;"},
            {.c = '\'', .str = "&apos;"},
            {.c = '\n', .str = "<br>"}
        };

        char buf[sizeof "a"] = {0};
        const char *str = NULL;

        for (size_t i = 0; i < sizeof esc / sizeof *esc; i++)
        {
            const struct esc *const e = &esc[i];

            if (*s == e->c)
            {
                str = e->str;
                break;
            }
        }

        if (!str)
        {
            *buf = *s;
            str = buf;
        }

        if (dynstr_append(&d, "%s", str))
        {
            fprintf(stderr, "%s: dynstr_append failed\n", __func__);
            goto failure;
        }

        s++;
    }

    return d.str;

failure:
    dynstr_free(&d);
    return NULL;
}

int html_node_set_value(struct html_node *const n, const char *const val)
{
    if (!(n->value = html_encode(val)))
    {
        fprintf(stderr, "%s: html_encode failed\n", __func__);
        return -1;
    }

    return 0;
}

int html_node_set_value_unescaped(struct html_node *const n,
    const char *const val)
{
    if (!(n->value = strdup(val)))
    {
        fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
        return -1;
    }

    return 0;
}

int html_node_add_attr(struct html_node *const n, const char *const attr,
    const char *const val)
{
    const size_t el = n->n + 1;
    char *const attrdup = strdup(attr), *valdup = NULL;
    struct html_attribute *attrs = NULL;

    if (!attrdup)
    {
        fprintf(stderr, "%s: strdup(3) attr: %s\n", __func__, strerror(errno));
        goto failure;
    }
    else if (val && !(valdup = strdup(val)))
    {
        fprintf(stderr, "%s: strdup(3) val: %s\n", __func__, strerror(errno));
        goto failure;
    }
    else if (!(attrs = realloc(n->attrs, el * sizeof *attrs)))
    {
        fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
        return -1;
    }

    struct html_attribute *const a = &attrs[n->n];

    *a = (const struct html_attribute)
    {
        .attr = attrdup,
        .value = valdup
    };

    n->attrs = attrs;
    n->n = el;
    return 0;

failure:
    free(attrdup);
    free(valdup);
    return -1;
}

void html_node_add_sibling(struct html_node *const n,
    struct html_node *const sibling)
{
    for (struct html_node *c = n; c; c = c->sibling)
        if (!c->sibling)
        {
            c->sibling = sibling;
            break;
        }
}

struct html_node *html_node_add_child(struct html_node *const n,
    const char *const element)
{
    struct html_node *const child = html_node_alloc(element);

    if (!child)
        return NULL;
    else if (n->child)
        html_node_add_sibling(n->child, child);
    else
        n->child = child;

    return child;
}

static int serialize_node(struct dynstr *const d,
    const struct html_node *const n, const unsigned level)
{
    for (unsigned i = 0; i < level; i++)
        dynstr_append(d, "\t");

    dynstr_append_or_ret_nonzero(d, "<%s", n->element);

    if (n->n)
        dynstr_append_or_ret_nonzero(d, " ");

    for (size_t i = 0; i < n->n; i++)
    {
        const struct html_attribute *const a = &n->attrs[i];

        if (a->value)
            dynstr_append_or_ret_nonzero(d, "%s=\"%s\"", a->attr, a->value);
        else
            dynstr_append_or_ret_nonzero(d, "%s", a->attr);

        if (i + 1 < n->n)
            dynstr_append_or_ret_nonzero(d, " ");
    }

    if (!n->value && !n->child)
        dynstr_append_or_ret_nonzero(d, "/>");
    else
    {
        dynstr_append_or_ret_nonzero(d, ">");

        if (n->value)
            dynstr_append_or_ret_nonzero(d, "%s", n->value);

        if (n->child)
        {
            dynstr_append_or_ret_nonzero(d, "\n");

            if (serialize_siblings(d, n->child, level + 1))
            {
                fprintf(stderr, "%s: serialize_siblings failed\n", __func__);
                return -1;
            }

            for (unsigned i = 0; i < level; i++)
                dynstr_append(d, "\t");
        }

        dynstr_append_or_ret_nonzero(d, "</%s>", n->element);
    }

    dynstr_append_or_ret_nonzero(d, "\n");
    return 0;
}

static int serialize_siblings(struct dynstr *const d,
    const struct html_node *n, const unsigned level)
{
    struct html_node *sibling;

    do
    {
        sibling = n->sibling;

        if (serialize_node(d, n, level))
            return -1;

    } while ((n = sibling));

    return 0;
}

int html_serialize(const struct html_node *const n, struct dynstr *const d)
{
    return serialize_siblings(d, n, 0);
}

static void html_attribute_free(struct html_attribute *const a)
{
    if (a)
    {
        free(a->attr);
        free(a->value);
    }
}

void html_node_free(struct html_node *const n)
{
    if (n)
    {
        struct html_node *s = n->sibling;

        html_node_free(n->child);

        while (s)
        {
            struct html_node *const next = s->sibling;

            html_node_free(s->child);
            free(s->element);
            free(s->value);

            for (size_t i = 0 ; i < s->n; i++)
                html_attribute_free(&s->attrs[i]);

            free(s->attrs);
            free(s);
            s = next;
        }

        free(n->element);
        free(n->value);

        for (size_t i = 0 ; i < n->n; i++)
            html_attribute_free(&n->attrs[i]);

        free(n->attrs);
    }

    free(n);
}

struct html_node *html_node_alloc(const char *const element)
{
    struct html_node *const n = malloc(sizeof *n);

    if (!n)
    {
        fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
        goto failure;
    }

    *n = (const struct html_node)
    {
        .element = strdup(element)
    };

    if (!n->element)
    {
        fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
        goto failure;
    }

    return n;

failure:
    html_node_free(n);
    return NULL;
}