timekeeper

[Abandoned unfinished] CGI web application in C for time tracking. (My first, just a learning project)
Log | Files | Refs | README

commit 3e3f2431e1b27841db2b36dd68c630323ff7c36c
parent 48fb9b560260955e691c0454346d4c665355e88d
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Tue,  5 Mar 2024 21:38:16 -0800

Move html/cgi helper functions out of timekeeper.c

- General-purpose HTML related functions moved to html.c
- More specific HTML/HTTP related functions moved to pages/util.c

Diffstat:
MMakefile | 6++++--
Ahtml.c | 32++++++++++++++++++++++++++++++++
Ahtml.h | 2++
Apages/util.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apages/util.h | 3+++
Mtimekeeper.c | 86++-----------------------------------------------------------------------------
6 files changed, 109 insertions(+), 86 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,8 +4,8 @@ cflags = ${CFLAGS} -O0 -Wall -Wextra -I/usr/local/include lddflags = ${LDDFLAGS} -static -L/usr/local/lib -lkcgi -lkcgihtml -lz -lsqlbox -lsqlite3 -lm -lpthread prefix = /var/www/htdocs/${name}.primus.lan -hdr = page.h -src = page.c +src = page.c html.c pages/util.c +hdr = ${src:.c=.h} obj = ${src:.c=.o} all: ${name} @@ -13,6 +13,8 @@ all: ${name} .c.o: ${cc} ${cflags} -c -o $@ $< +${name}.o ${obj}: ${hdr} + ${name}: ${name}.o ${obj} ${cc} -o $@ $@.o ${obj} ${lddflags} diff --git a/html.c b/html.c @@ -0,0 +1,32 @@ +#include <sys/types.h> +#include <stdarg.h> +#include <stdint.h> +#include <kcgi.h> +#include <kcgihtml.h> + +#include "page.h" + +enum kcgi_err +htmlwithin(struct pagedata *pd, enum kelem e, char *text) +{ + enum kcgi_err status; + + if ((status = khtml_elem(&pd->html, e)) != KCGI_OK || + (status = khtml_puts(&pd->html, text)) != KCGI_OK || + (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) + return status; + return KCGI_OK; +} + +enum kcgi_err +htmllink(struct pagedata *pd, char *link, char *text) +{ + enum kcgi_err status; + + if ((status = khtml_attr(&pd->html, KELEM_A, + KATTR_HREF, link, KATTR__MAX)) != KCGI_OK || + (status = khtml_puts(&pd->html, text)) != KCGI_OK || + (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) + return status; + return KCGI_OK; +} diff --git a/html.h b/html.h @@ -0,0 +1,2 @@ +enum kcgi_err htmlwithin(struct pagedata *pd, enum kelem e, char *text); +enum kcgi_err htmllink(struct pagedata *pd, char *link, char *text); diff --git a/pages/util.c b/pages/util.c @@ -0,0 +1,66 @@ +#include <sys/types.h> +#include <stdarg.h> +#include <stdint.h> +#include <kcgi.h> +#include <kcgihtml.h> + +#include "../page.h" + +enum kcgi_err +errorpage(struct pagedata *pd, enum khttp code) +{ + enum kcgi_err status; + + if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[code])) != KCGI_OK || + (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || + (status = khttp_body(&pd->req))) + return status; + return khttp_printf(&pd->req, + "<!doctype HTML><html><head><title>%1$s</title></head><h1>%1$s</h1></html>", + khttps[code]); +} + +enum kcgi_err +starthtmldoc(struct pagedata *pd, enum khttp code) +{ + enum kcgi_err status; + + if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[code])) != KCGI_OK || + (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || + (status = khttp_body(&pd->req)) != KCGI_OK || + (status = khtml_open(&pd->html, &pd->req, KHTML_PRETTY)) != KCGI_OK || + (status = khtml_elem(&pd->html, KELEM_HTML)) != KCGI_OK || + (status = khtml_elem(&pd->html, KELEM_HEAD)) != KCGI_OK || + (status = khtml_attr(&pd->html, KELEM_LINK, + KATTR_HREF, "css/main.css", KATTR_REL, "stylesheet", KATTR__MAX)) != KCGI_OK || + (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || + (status = khtml_elem(&pd->html, KELEM_BODY) != KCGI_OK)) + return status; + kcgi_writer_disable(&pd->req); + + return KCGI_OK; +} + +enum kcgi_err +redirect(struct pagedata *pd, char *to, char *msg) +{ + enum kcgi_err status; + + if ((status = khttp_head(&pd->req, kresps[KRESP_LOCATION], "%s", to)) != KCGI_OK) + return status; + if ((status = starthtmldoc(pd, KHTTP_303)) != KCGI_OK) + return status; + if ((status = khtml_elem(&pd->html, KELEM_H1)) != KCGI_OK || + (status = khtml_puts(&pd->html, msg)) != KCGI_OK || + (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || + (status = khtml_elem(&pd->html, KELEM_P)) != KCGI_OK || + (status = khtml_puts(&pd->html, "You are being redirected to ")) != KCGI_OK || + (status = khtml_attr(&pd->html, KELEM_A, + KATTR_HREF, to, + KATTR__MAX)) != KCGI_OK || + (status = khtml_puts(&pd->html, to)) != KCGI_OK || + (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || + (status = khtml_putc(&pd->html, '.')) != KCGI_OK) + return status; + return khtml_close(&pd->html); +} diff --git a/pages/util.h b/pages/util.h @@ -0,0 +1,3 @@ +enum kcgi_err errorpage(struct pagedata *pd, enum khttp code); +enum kcgi_err starthtmldoc(struct pagedata *pd, enum khttp code); +enum kcgi_err redirect(struct pagedata *pd, char *to, char *msg); diff --git a/timekeeper.c b/timekeeper.c @@ -21,6 +21,8 @@ #include <sqlbox.h> #include "page.h" +#include "html.h" +#include "pages/util.h" #define Promises "stdio rpath wpath cpath proc recvfd unix sendfd" #define Len(X) (sizeof(X) / sizeof(X[0])) @@ -262,60 +264,12 @@ sitegetlogin(struct pagedata *pd) } enum kcgi_err -starthtmldoc(struct pagedata *pd, enum khttp code) -{ - enum kcgi_err status; - - if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[code])) != KCGI_OK || - (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || - (status = khttp_body(&pd->req)) != KCGI_OK || - (status = khtml_open(&pd->html, &pd->req, KHTML_PRETTY)) != KCGI_OK || - (status = khtml_elem(&pd->html, KELEM_HTML)) != KCGI_OK || - (status = khtml_elem(&pd->html, KELEM_HEAD)) != KCGI_OK || - (status = khtml_attr(&pd->html, KELEM_LINK, - KATTR_HREF, "css/main.css", KATTR_REL, "stylesheet", KATTR__MAX)) != KCGI_OK || - (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || - (status = khtml_elem(&pd->html, KELEM_BODY) != KCGI_OK)) - return status; - kcgi_writer_disable(&pd->req); - - return KCGI_OK; -} - -enum kcgi_err -errorpage(struct pagedata *pd, enum khttp code) -{ - enum kcgi_err status; - - if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[code])) != KCGI_OK || - (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK || - (status = khttp_body(&pd->req))) - return status; - return khttp_printf(&pd->req, - "<!doctype HTML><html><head><title>%1$s</title></head><h1>%1$s</h1></html>", - khttps[code]); -} - -enum kcgi_err page404(struct pagedata *pd) { return errorpage(pd, KHTTP_404); } enum kcgi_err -htmllink(struct pagedata *pd, char *link, char *text) -{ - enum kcgi_err status; - - if ((status = khtml_attr(&pd->html, KELEM_A, - KATTR_HREF, link, KATTR__MAX)) != KCGI_OK || - (status = khtml_puts(&pd->html, text)) != KCGI_OK || - (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) - return status; - return KCGI_OK; -} - -enum kcgi_err pageindex(struct pagedata *pd) { enum kcgi_err status; @@ -347,30 +301,6 @@ pageindex(struct pagedata *pd) } enum kcgi_err -redirect(struct pagedata *pd, char *to, char *msg) -{ - enum kcgi_err status; - - if ((status = khttp_head(&pd->req, kresps[KRESP_LOCATION], "%s", to)) != KCGI_OK) - return status; - if ((status = starthtmldoc(pd, KHTTP_303)) != KCGI_OK) - return status; - if ((status = khtml_elem(&pd->html, KELEM_H1)) != KCGI_OK || - (status = khtml_puts(&pd->html, msg)) != KCGI_OK || - (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || - (status = khtml_elem(&pd->html, KELEM_P)) != KCGI_OK || - (status = khtml_puts(&pd->html, "You are being redirected to ")) != KCGI_OK || - (status = khtml_attr(&pd->html, KELEM_A, - KATTR_HREF, to, - KATTR__MAX)) != KCGI_OK || - (status = khtml_puts(&pd->html, to)) != KCGI_OK || - (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK || - (status = khtml_putc(&pd->html, '.')) != KCGI_OK) - return status; - return khtml_close(&pd->html); -} - -enum kcgi_err htmlinput(struct pagedata *pd, enum Field field, char *type, int64_t minl, int64_t maxl) { enum kcgi_err status; @@ -393,18 +323,6 @@ htmlinput(struct pagedata *pd, enum Field field, char *type, int64_t minl, int64 } enum kcgi_err -htmlwithin(struct pagedata *pd, enum kelem e, char *text) -{ - enum kcgi_err status; - - if ((status = khtml_elem(&pd->html, e)) != KCGI_OK || - (status = khtml_puts(&pd->html, text)) != KCGI_OK || - (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) - return status; - return KCGI_OK; -} - -enum kcgi_err pagelogin(struct pagedata *pd) { enum kcgi_err status;