commit 48fb9b560260955e691c0454346d4c665355e88d
parent 99e94ddb600ca1650830249c4dfceac7b495c1bc
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Tue, 5 Mar 2024 20:53:24 -0800
Start splitting page functions into page.c
This is the start of the process of organizing the program into a
couple different parts.
Diffstat:
4 files changed, 47 insertions(+), 21 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,9 +4,9 @@ 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 =
-#src =
-#obj = ${src:.c=.o}
+hdr = page.h
+src = page.c
+obj = ${src:.c=.o}
all: ${name}
diff --git a/page.c b/page.c
@@ -0,0 +1,29 @@
+#include <sys/types.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <kcgi.h>
+#include <kcgihtml.h>
+
+#include "page.h"
+
+enum kcgi_err
+showpage(struct pagedata *pd, char **names,
+ enum kcgi_err (*functions[])(struct pagedata *), size_t len)
+{
+ enum kcgi_err status;
+
+ if (pd->req.page == len)
+ status = functions[len - 1](pd);
+ else
+ status = functions[pd->req.page](pd);
+
+ if (status != KCGI_OK) {
+ kutil_warn(&pd->req, NULL, "showpage: page %s returned %d",
+ names[(pd->req.page == len ? (len - 1) : pd->req.page)],
+ status);
+ }
+ khttp_free(&pd->req);
+
+ return status;
+}
diff --git a/page.h b/page.h
@@ -0,0 +1,10 @@
+struct pagedata {
+ struct kreq req;
+ struct khtmlreq html;
+ struct sqlbox *db;
+ size_t dbid;
+ struct kvalid *keys;
+};
+
+enum kcgi_err showpage(struct pagedata *pd, char **names,
+ enum kcgi_err (*functions[])(struct pagedata *), size_t len);
diff --git a/timekeeper.c b/timekeeper.c
@@ -20,6 +20,8 @@
#include <sqlbox.h>
+#include "page.h"
+
#define Promises "stdio rpath wpath cpath proc recvfd unix sendfd"
#define Len(X) (sizeof(X) / sizeof(X[0]))
#define Max(A,B) ((A) > (B) ? (A) : (B))
@@ -73,14 +75,6 @@ enum TimeField {
EndTime
};
-struct pagedata {
- struct kreq req;
- struct khtmlreq html;
- struct sqlbox *db;
- size_t dbid;
- struct kvalid *keys;
-};
-
struct user {
char *name;
char *hash;
@@ -894,7 +888,7 @@ pagemain(struct pagedata *pd)
int
main(void)
{
- enum kcgi_err status, pagestatus;
+ enum kcgi_err status;
struct kfcgi *fcgi;
struct kvalid keys[] = {
[KeyUsername] = { kvalid_name, "username" },
@@ -968,15 +962,8 @@ main(void)
status = KCGI_OK;
while ((status = khttp_fcgi_parse(fcgi, &pd.req)) == KCGI_OK) {
- if (pd.req.page == PageMax)
- pagestatus = pagefunctions[Page404](&pd);
- else
- pagestatus = pagefunctions[pd.req.page](&pd);
- if (pagestatus)
- kutil_warn(&pd.req, NULL, "%s: Returned %d",
- pages[(pd.req.page == PageMax ? Page404 : pd.req.page)],
- pagestatus);
- khttp_free(&pd.req);
+ /* Ignore return value */
+ showpage(&pd, pages, pagefunctions, Len(pages));
}
sqlbox_free(pd.db);