Embedded gpp

This commit is contained in:
kamkow1
2025-06-09 20:10:57 +02:00
parent 23de19b63e
commit ac48e49ecc
11 changed files with 171 additions and 78 deletions

3
.gitmodules vendored
View File

@ -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

7
baked.c Normal file
View File

@ -0,0 +1,7 @@
#define INCBIN_PREFIX
#define INCBIN_STYLE INCBIN_STYLE_SNAKE
#include "incbin/incbin.h"
#include "baked.h"
INCBIN(gpp1, "gpp1");

10
baked.h Normal file
View File

@ -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_

20
build.c
View File

@ -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) {

2
gebs

Submodule gebs updated: 9582176638...61e59e6550

43
gpp.c Normal file
View File

@ -0,0 +1,43 @@
#include <sys/mman.h>
#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;
}

12
gpp.h Normal file
View File

@ -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_

1
incbin Submodule

Submodule incbin added at 22061f51fe

87
main.c
View File

@ -1,11 +1,16 @@
#include <libgen.h>
#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);
@ -119,5 +60,7 @@ int main(int argc, char ** argv)
mg_mgr_free(&mgr);
shfree(route_hashtable);
gpp_deinit();
return 0;
}

53
routes.c Normal file
View File

@ -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);
}
}

11
routes.h Normal file
View File

@ -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_