commit 8c46a0669466da45a19e109b1d89b92353aa5ecd
parent d4e112960bb2e26548e1d1c93c3a21c2e917c2f6
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sat, 23 Mar 2024 18:46:45 -0700
Add basic menu making function/data structure
This can be expanded on later (for instance by adding recursion),
but for now it provides a basic interface to building menu's using
a simple data structure.
Diffstat:
M | Makefile | | | 2 | +- |
A | menu.c | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++++ |
A | menu.h | | | 12 | ++++++++++++ |
3 files changed, 60 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -6,7 +6,7 @@ prefix = /var/www/htdocs/${name}.primus.lan
pages = pages/index.c pages/login.c pages/logout.c \
pages/account.c pages/main.c pages/export.c pages/archive.c
-hdrsrc = page.c html.c user.c stmt.c key.c times.c pages/util.c pages/common.c
+hdrsrc = page.c html.c user.c stmt.c key.c times.c menu.c pages/util.c pages/common.c
src = ${hdrsrc} ${pages}
hdr = ${hdrsrc:.c=.h} util.h pages/util.h pages/pages.h
obj = ${src:.c=.o}
diff --git a/menu.c b/menu.c
@@ -0,0 +1,47 @@
+#include <sys/types.h>
+#include <err.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <time.h>
+
+#include <kcgi.h>
+#include <kcgihtml.h>
+
+#include "page.h"
+#include "html.h"
+
+#include "menu.h"
+
+enum kcgi_err
+htmlmenuitem(struct pagedata *pd, struct menuitem *item)
+{
+ switch (item->type) {
+ case MenuURL:
+ return htmllink(pd, item->u.url, item->u.url);
+ case MenuFunc:
+ return item->u.func(pd);
+ default:
+ err(1, "Invalid menu item type");
+ }
+}
+
+enum kcgi_err
+htmlmenu(struct pagedata *pd, struct menuitem *items,
+ unsigned int len)
+{
+ enum kcgi_err status;
+ unsigned int i;
+
+ if ((status = khtml_attr(&pd->html, KELEM_UL,
+ KATTR_CLASS, "menu", KATTR__MAX)) != KCGI_OK)
+ return KCGI_OK;
+
+ for (i = 0; i < len; ++i) {
+ if ((status = khtml_elem(&pd->html, KELEM_LI)) != KCGI_OK ||
+ (status = htmlmenuitem(pd, &items[i])) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
+ return status;
+ }
+
+ return khtml_closeelem(&pd->html, 1);
+}
diff --git a/menu.h b/menu.h
@@ -0,0 +1,12 @@
+struct menuitem {
+ enum {
+ MenuURL,
+ MenuFunc
+ } type;
+ union {
+ char *url;
+ enum kcgi_err (*func)(struct pagedata *);
+ } u;
+};
+
+enum kcgi_err htmlmenu(struct pagedata *pd, struct menuitem *items, unsigned int len);