From ac48e49ecc3375b32be4c01ce79aa59f5b1f1a74 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Mon, 9 Jun 2025 20:10:57 +0200 Subject: [PATCH] Embedded gpp --- .gitmodules | 3 ++ baked.c | 7 +++++ baked.h | 10 ++++++ build.c | 20 +++++++++--- gebs | 2 +- gpp.c | 43 ++++++++++++++++++++++++++ gpp.h | 12 ++++++++ incbin | 1 + main.c | 87 +++++++++-------------------------------------------- routes.c | 53 ++++++++++++++++++++++++++++++++ routes.h | 11 +++++++ 11 files changed, 171 insertions(+), 78 deletions(-) create mode 100644 baked.c create mode 100644 baked.h create mode 100644 gpp.c create mode 100644 gpp.h create mode 160000 incbin create mode 100644 routes.c create mode 100644 routes.h diff --git a/.gitmodules b/.gitmodules index 3736444..45c4fa0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "gebs"] path = gebs url = https://gitlab.com/kamkow1/gebs.git +[submodule "incbin"] + path = incbin + url = https://github.com/graphitemaster/incbin.git diff --git a/baked.c b/baked.c new file mode 100644 index 0000000..ce2d6d8 --- /dev/null +++ b/baked.c @@ -0,0 +1,7 @@ +#define INCBIN_PREFIX +#define INCBIN_STYLE INCBIN_STYLE_SNAKE +#include "incbin/incbin.h" + +#include "baked.h" + +INCBIN(gpp1, "gpp1"); diff --git a/baked.h b/baked.h new file mode 100644 index 0000000..d43ad54 --- /dev/null +++ b/baked.h @@ -0,0 +1,10 @@ +#ifndef BAKED_H_ +#define BAKED_H_ + +#define INCBIN_PREFIX +#define INCBIN_STYLE INCBIN_STYLE_SNAKE +#include "incbin/incbin.h" + +INCBIN_EXTERN(gpp1); + +#endif // BAKED_H_ diff --git a/build.c b/build.c index 7c21d4a..8aac01f 100644 --- a/build.c +++ b/build.c @@ -13,12 +13,20 @@ int main(int argc, char ** argv) prog = SHIFT(&argc, &argv); char *cmd = SHIFT(&argc, &argv); if (strcmp(cmd, "make") == 0) { - RULE("./aboba", "./main.c", "./mongoose.o", "./gpp1") { + RULE("./aboba", + "./main.c", + "./routes.c", + "./baked.c", + "./gpp.c", + "./mongoose.o", + "./gpp1" + ) { + RULE("./mongoose.o", "./mongoose/mongoose.c") { #if DEBUG - CMD("cc", "-ggdb", "-c", "-o", "./mongoose.o", "./mongoose/mongoose.c"); + CMD("cc", "-ggdb", "-c", "-D_GNU_SOURCE", "-o", "./mongoose.o", "./mongoose/mongoose.c"); #else - CMD("cc", "-c", "-o", "./mongoose.o", "./mongoose/mongoose.c"); + CMD("cc", "-c", "-D_GNU_SOURCE", "-o", "./mongoose.o", "./mongoose/mongoose.c"); #endif } @@ -27,9 +35,11 @@ int main(int argc, char ** argv) } #if DEBUG - CMD("cc", "-ggdb", "-o", "./aboba", "./main.c", "./mongoose.o"); + CMD("cc", "-ggdb", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba", + "./main.c", "./routes.c", "./baked.c", "./gpp.c", "./mongoose.o"); #else - CMD("cc", "-o", "./aboba", "./main.c", "./mongoose.o"); + CMD("cc", "-I.", "-D_GNU_SOURCE", "-DGEBS_NO_PREFIX", "-o", "./aboba", + "./main.c", "./routes.c", "./baked.c", "./gpp.c", "./mongoose.o"); #endif } } else if (strcmp(cmd, "clean") == 0) { diff --git a/gebs b/gebs index 9582176..61e59e6 160000 --- a/gebs +++ b/gebs @@ -1 +1 @@ -Subproject commit 9582176638123bcdb955311c3bbd3243a6388595 +Subproject commit 61e59e655098b29951c3c7eaf5b18a2968be0b38 diff --git a/gpp.c b/gpp.c new file mode 100644 index 0000000..1c8ccb1 --- /dev/null +++ b/gpp.c @@ -0,0 +1,43 @@ +#include + +#include "gebs/gebs.h" + +#include "gpp.h" +#include "baked.h" + +int gpp_fd; + +void gpp_init(void) +{ + gpp_fd = memfd_create("gpp-binary", 0); + if (gpp_fd < 0) { + LOGE("Could not create gpp_fd. Aborting..."); + abort(); + } + + write(gpp_fd, gpp1_data, gpp1_size); +} + +void gpp_deinit(void) +{ + close(gpp_fd); +} + +bool gpp_run(char *path, NString_List *env, String_Builder *out) +{ + Cmd cmd = {0}; + defer { cmd_free(&cmd); } + + cmd_append(&cmd, "gpp1"); + cmd_append(&cmd, "-H"); + cmd_append(&cmd, "-x"); + cmd_append(&cmd, "--nostdinc"); + cmd_append(&cmd, path); + + for (size_t i = 0; i < env->count; i++) { + cmd_append(&cmd, env->items[i]); + } + + return cmd_fd_run_collect(gpp_fd, &cmd, out) == 0; +} + diff --git a/gpp.h b/gpp.h new file mode 100644 index 0000000..98099d3 --- /dev/null +++ b/gpp.h @@ -0,0 +1,12 @@ +#ifndef GPPH_H_ +#define GPPH_H_ + +#include "gebs/gebs.h" + +extern int gpp_fd; + +void gpp_init(void); +void gpp_deinit(void); +bool gpp_run(char *path, NString_List *env, String_Builder *out); + +#endif // GPPH_H_ diff --git a/incbin b/incbin new file mode 160000 index 0000000..22061f5 --- /dev/null +++ b/incbin @@ -0,0 +1 @@ +Subproject commit 22061f51fe9f2f35f061f85c2b217b55dd75310d diff --git a/main.c b/main.c index 313f929..be3bfa5 100644 --- a/main.c +++ b/main.c @@ -1,11 +1,16 @@ #include -#define GEBS_NO_PREFIX + #define GEBS_IMPLEMENTATION #include "gebs/gebs.h" + #include "mongoose/mongoose.h" + #define STB_DS_IMPLEMENTATION #include "stb/stb_ds.h" +#include "routes.h" +#include "gpp.h" + typedef void (*Route_Handler)(struct mg_connection *conn, struct mg_http_message *msg); typedef struct { @@ -27,83 +32,19 @@ void event_handler(struct mg_connection *conn, int ev, void *ev_data) } } -bool gpp1(char *path, NString_List *env, String_Builder *out) -{ - Cmd cmd = {0}; - defer { cmd_free(&cmd); } - - cmd_append(&cmd, "./gpp1"); - cmd_append(&cmd, "-H"); - cmd_append(&cmd, "-x"); - cmd_append(&cmd, "--nostdinc"); - cmd_append(&cmd, path); - - for (size_t i = 0; i < env->count; i++) { - cmd_append(&cmd, env->items[i]); - } - - return cmd_run_collect(&cmd, out) == 0; -} - -void handle_page_not_found(struct mg_connection *conn, struct mg_http_message *msg) -{ - NString_List env = {0}; - defer { list_free(&env); } - - list_append(&env, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf)); - - String_Builder out = {0}; - defer { sb_free(&out); } - bool ok = gpp1("./tmpls/page-missing.t", &env, &out); - sb_finish(&out); - - if (!ok) { - mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;("); - } else { - mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items); - } -} - -void handle_simple_css(struct mg_connection *conn, struct mg_http_message *msg) -{ - struct mg_http_serve_opts opts = { 0 }; - mg_http_serve_file(conn, msg, "./assets/simple.min.css", &opts); -} - -void handle_favicon(struct mg_connection *conn, struct mg_http_message *msg) -{ - struct mg_http_serve_opts opts = { 0 }; - mg_http_serve_file(conn, msg, "./assets/favicon.ico", &opts); -} - -void handle_home(struct mg_connection *conn, struct mg_http_message *msg) -{ - NString_List env = {0}; - defer { list_free(&env); } - - String_Builder out = {0}; - defer { sb_free(&out); } - bool ok = gpp1("./tmpls/index.t", &env, &out); - sb_finish(&out); - - if (!ok) { - mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;("); - } else { - mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items); - } -} - static void init_route_hashtable(void) { - shdefault(route_hashtable, &handle_page_not_found); - shput(route_hashtable, "/page-missing", &handle_page_not_found); - shput(route_hashtable, "/simple.min.css", &handle_simple_css); - shput(route_hashtable, "/favicon.ico", &handle_favicon); - shput(route_hashtable, "/", &handle_home); + shdefault(route_hashtable, &route_page_not_found); + shput(route_hashtable, "/page-missing", &route_page_not_found); + shput(route_hashtable, "/simple.min.css", &route_simple_css); + shput(route_hashtable, "/favicon.ico", &route_favicon); + shput(route_hashtable, "/", &route_home); } int main(int argc, char ** argv) { + gpp_init(); + mg_log_set(MG_LL_DEBUG); struct mg_mgr mgr; mg_mgr_init(&mgr); @@ -118,6 +59,8 @@ int main(int argc, char ** argv) mg_mgr_free(&mgr); shfree(route_hashtable); + + gpp_deinit(); return 0; } diff --git a/routes.c b/routes.c new file mode 100644 index 0000000..a7c2842 --- /dev/null +++ b/routes.c @@ -0,0 +1,53 @@ +#include "gebs/gebs.h" +#include "mongoose/mongoose.h" + +#include "routes.h" +#include "gpp.h" + +void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg) +{ + NString_List env = {0}; + defer { list_free(&env); } + + list_append(&env, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf)); + + String_Builder out = {0}; + defer { sb_free(&out); } + bool ok = gpp_run("./tmpls/page-missing.t", &env, &out); + sb_finish(&out); + + if (!ok) { + mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;("); + } else { + mg_http_reply(conn, 404, "Content-Type: text/html\r\n", out.items); + } +} + +void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg) +{ + struct mg_http_serve_opts opts = { 0 }; + mg_http_serve_file(conn, msg, "./assets/simple.min.css", &opts); +} + +void route_favicon(struct mg_connection *conn, struct mg_http_message *msg) +{ + struct mg_http_serve_opts opts = { 0 }; + mg_http_serve_file(conn, msg, "./assets/favicon.ico", &opts); +} + +void route_home(struct mg_connection *conn, struct mg_http_message *msg) +{ + NString_List env = {0}; + defer { list_free(&env); } + + String_Builder out = {0}; + defer { sb_free(&out); } + bool ok = gpp_run("./tmpls/index.t", &env, &out); + sb_finish(&out); + + if (!ok) { + mg_http_reply(conn, 500, "Content-Type: text/html\r\n", "Internal server error ;("); + } else { + mg_http_reply(conn, 200, "Content-Type: text/html\r\n", out.items); + } +} diff --git a/routes.h b/routes.h new file mode 100644 index 0000000..137c8ea --- /dev/null +++ b/routes.h @@ -0,0 +1,11 @@ +#ifndef ROUTES_H_ +#define ROUTES_H_ + +#include "mongoose/mongoose.h" + +void route_page_not_found(struct mg_connection *conn, struct mg_http_message *msg); +void route_simple_css(struct mg_connection *conn, struct mg_http_message *msg); +void route_favicon(struct mg_connection *conn, struct mg_http_message *msg); +void route_home(struct mg_connection *conn, struct mg_http_message *msg); + +#endif // ROUTES_H_