From 8016f537ca5a4bbde6fee2566ff82cd222122915 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 9 Mar 2023 01:20:18 +0100 Subject: page.c: Implement function for common nodes This will be used by future commits. --- page.c | 89 +++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/page.c b/page.c index 1c6023d..03671d6 100644 --- a/page.c +++ b/page.c @@ -14,16 +14,17 @@ #include #include -#define PROJECT_TITLE "slcl, a suckless cloud" -#define DOCTYPE_TAG "\n" #define PROJECT_NAME "slcl" +#define PROJECT_TITLE PROJECT_NAME ", a suckless cloud" +#define PROJECT_TAG "" PROJECT_TITLE "" +#define DOCTYPE_TAG "\n" #define PROJECT_URL "https://gitea.privatedns.org/Xavi92/" PROJECT_NAME #define COMMON_HEAD \ " \n" \ " \n" \ - PROJECT_TITLE "\n" + PROJECT_TAG "\n" #define STYLE_A "" #define LOGIN_BODY \ "
\n" \ @@ -590,37 +591,15 @@ end: return ret; } -static struct html_node *resource_layout(const char *const dir, - const struct page_quota *const q, struct html_node **const table) +static int common_head(struct html_node *const head, const char *const tl) { - const char *const fdir = dir + strlen("/user"); - struct html_node *const html = html_node_alloc("html"), - *ret = NULL, *head, *title, *body, *charset, *viewport; + int ret = -1; + struct html_node *charset, *title, *viewport; struct dynstr t; dynstr_init(&t); - if (!html) - { - fprintf(stderr, "%s: html_node_alloc_failed\n", __func__); - goto end; - } - else if (!(head = html_node_add_child(html, "head"))) - { - fprintf(stderr, "%s: html_node_add_child head failed\n", __func__); - goto end; - } - else if (!(body = html_node_add_child(html, "body"))) - { - fprintf(stderr, "%s: html_node_add_child body failed\n", __func__); - goto end; - } - else if (!(*table = html_node_add_child(body, "table"))) - { - fprintf(stderr, "%s: html_node_add_child table failed\n", __func__); - goto end; - } - else if (!(title = html_node_add_child(head, "title"))) + if (!(title = html_node_add_child(head, "title"))) { fprintf(stderr, "%s: html_node_add_child title failed\n", __func__); goto end; @@ -635,12 +614,15 @@ static struct html_node *resource_layout(const char *const dir, fprintf(stderr, "%s: html_node_add_attr charset failed\n", __func__); goto end; } - else if (dynstr_append(&t, PROJECT_NAME " - %s", fdir)) + else if (tl && dynstr_append(&t, PROJECT_NAME " - %s", tl)) { fprintf(stderr, "%s: dynstr_append title failed\n", __func__); goto end; } - else if (html_node_set_value(title, t.str)) + + const char *const value = tl ? t.str : PROJECT_TITLE; + + if (html_node_set_value(title, value)) { fprintf(stderr, "%s: html_node_set_value title failed\n", __func__); goto end; @@ -663,6 +645,46 @@ static struct html_node *resource_layout(const char *const dir, __func__); goto end; } + + ret = 0; + +end: + dynstr_free(&t); + return ret; +} + +static struct html_node *resource_layout(const char *const dir, + const struct page_quota *const q, struct html_node **const table) +{ + const char *const fdir = dir + strlen("/user"); + struct html_node *const html = html_node_alloc("html"), + *ret = NULL, *head, *body; + + if (!html) + { + fprintf(stderr, "%s: html_node_alloc_failed\n", __func__); + goto end; + } + else if (!(head = html_node_add_child(html, "head"))) + { + fprintf(stderr, "%s: html_node_add_child head failed\n", __func__); + goto end; + } + else if (!(body = html_node_add_child(html, "body"))) + { + fprintf(stderr, "%s: html_node_add_child body failed\n", __func__); + goto end; + } + else if (!(*table = html_node_add_child(body, "table"))) + { + fprintf(stderr, "%s: html_node_add_child table failed\n", __func__); + goto end; + } + else if (common_head(head, fdir)) + { + fprintf(stderr, "%s: common_head failed\n", __func__); + goto end; + } else if (prepare_upload_form(body, fdir)) { fprintf(stderr, "%s: prepare_upload_form failed\n", __func__); @@ -692,7 +714,6 @@ static struct html_node *resource_layout(const char *const dir, ret = html; end: - dynstr_free(&t); if (!ret) html_node_free(html); @@ -914,7 +935,7 @@ int page_forbidden(struct http_response *const r) DOCTYPE_TAG "\n" " \n" - " " PROJECT_TITLE "\n" + " " PROJECT_TAG "\n" " " COMMON_HEAD "\n" " \n" "Forbidden\n" @@ -942,7 +963,7 @@ int page_bad_request(struct http_response *const r) DOCTYPE_TAG "\n" " " - " " PROJECT_TITLE "\n" + " " PROJECT_TAG "\n" " " "Invalid request\n" ""; -- cgit v1.2.3