#include "form.h" #include "defs.h" #include #include #include 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; }